ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 1 | package sharenetworks |
| 2 | |
ehdou | 0794333 | 2016-10-24 21:21:58 +0300 | [diff] [blame] | 3 | import ( |
| 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
| 6 | ) |
ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 7 | |
| 8 | // CreateOptsBuilder allows extensions to add additional parameters to the |
| 9 | // Create request. |
| 10 | type CreateOptsBuilder interface { |
| 11 | ToShareNetworkCreateMap() (map[string]interface{}, error) |
| 12 | } |
| 13 | |
| 14 | // CreateOpts contains options for creating a ShareNetwork. This object is |
| 15 | // passed to the sharenetworks.Create function. For more information about |
| 16 | // these parameters, see the ShareNetwork object. |
| 17 | type CreateOpts struct { |
| 18 | // The UUID of the Neutron network to set up for share servers |
| 19 | NeutronNetID string `json:"neutron_net_id,omitempty"` |
| 20 | // The UUID of the Neutron subnet to set up for share servers |
| 21 | NeutronSubnetID string `json:"neutron_subnet_id,omitempty"` |
| 22 | // The UUID of the nova network to set up for share servers |
| 23 | NovaNetID string `json:"nova_net_id,omitempty"` |
| 24 | // The share network name |
| 25 | Name string `json:"name"` |
| 26 | // The share network description |
| 27 | Description string `json:"description"` |
| 28 | } |
| 29 | |
| 30 | // ToShareNetworkCreateMap assembles a request body based on the contents of a |
| 31 | // CreateOpts. |
| 32 | func (opts CreateOpts) ToShareNetworkCreateMap() (map[string]interface{}, error) { |
| 33 | return gophercloud.BuildRequestBody(opts, "share_network") |
| 34 | } |
| 35 | |
| 36 | // Create will create a new ShareNetwork based on the values in CreateOpts. To |
| 37 | // extract the ShareNetwork object from the response, call the Extract method |
| 38 | // on the CreateResult. |
| 39 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
| 40 | b, err := opts.ToShareNetworkCreateMap() |
| 41 | if err != nil { |
| 42 | r.Err = err |
| 43 | return |
| 44 | } |
| 45 | _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ |
| 46 | OkCodes: []int{200, 202}, |
| 47 | }) |
| 48 | return |
| 49 | } |
ehdou | 5368725 | 2016-10-14 22:10:13 +0300 | [diff] [blame] | 50 | |
| 51 | // Delete will delete the existing ShareNetwork with the provided ID. |
| 52 | func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) { |
| 53 | _, r.Err = client.Delete(deleteURL(client, id), nil) |
| 54 | return |
| 55 | } |
ehdou | 0794333 | 2016-10-24 21:21:58 +0300 | [diff] [blame] | 56 | |
| 57 | // ListOptsBuilder allows extensions to add additional parameters to the List |
| 58 | // request. |
| 59 | type ListOptsBuilder interface { |
| 60 | ToShareNetworkListQuery() (string, error) |
| 61 | } |
| 62 | |
| 63 | // ListOpts holds options for listing ShareNetworks. It is passed to the |
| 64 | // sharenetworks.List function. |
| 65 | type ListOpts struct { |
| 66 | // admin-only option. Set it to true to see all tenant share networks. |
| 67 | AllTenants bool `q:"all_tenants"` |
| 68 | // The UUID of the project where the share network was created |
| 69 | ProjectID string `q:"project_id"` |
| 70 | // The neutron network ID |
| 71 | NeutronNetID string `q:"neutron_net_id"` |
| 72 | // The neutron subnet ID |
| 73 | NeutronSubnetID string `q:"neutron_subnet_id"` |
| 74 | // The nova network ID |
| 75 | NovaNetID string `q:"nova_net_id"` |
| 76 | // The network type. A valid value is VLAN, VXLAN, GRE or flat |
| 77 | NetworkType string `q:"network_type"` |
| 78 | // The Share Network name |
| 79 | Name string `q:"name"` |
| 80 | // The Share Network description |
| 81 | Description string `q:"description"` |
| 82 | // The Share Network IP version |
| 83 | IPVersion gophercloud.IPVersion `q:"ip_version"` |
| 84 | // The Share Network segmentation ID |
| 85 | SegmentationID int `q:"segmentation_id"` |
| 86 | // List all share networks created after the given date |
| 87 | CreatedSince string `q:"created_since"` |
| 88 | // List all share networks created before the given date |
| 89 | CreatedBefore string `q:"created_before"` |
| 90 | // Limit specifies the page size. |
| 91 | Limit int `q:"limit"` |
| 92 | // Limit specifies the page number. |
| 93 | Offset int `q:"offset"` |
| 94 | } |
| 95 | |
| 96 | // ToShareNetworkListQuery formats a ListOpts into a query string. |
| 97 | func (opts ListOpts) ToShareNetworkListQuery() (string, error) { |
| 98 | q, err := gophercloud.BuildQueryString(opts) |
| 99 | return q.String(), err |
| 100 | } |
| 101 | |
| 102 | // ListDetail returns ShareNetworks optionally limited by the conditions provided in ListOpts. |
| 103 | func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 104 | url := listDetailURL(client) |
| 105 | if opts != nil { |
| 106 | query, err := opts.ToShareNetworkListQuery() |
| 107 | if err != nil { |
| 108 | return pagination.Pager{Err: err} |
| 109 | } |
| 110 | url += query |
| 111 | } |
| 112 | |
| 113 | return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { |
| 114 | p := ShareNetworkPage{pagination.MarkerPageBase{PageResult: r}} |
| 115 | p.MarkerPageBase.Owner = p |
| 116 | return p |
| 117 | }) |
| 118 | } |
ehdou | a088cec | 2016-10-25 21:02:05 +0300 | [diff] [blame] | 119 | |
| 120 | // Get retrieves the ShareNetwork with the provided ID. To extract the ShareNetwork |
| 121 | // object from the response, call the Extract method on the GetResult. |
| 122 | func Get(client *gophercloud.ServiceClient, id string) (r GetResult) { |
| 123 | _, r.Err = client.Get(getURL(client, id), &r.Body, nil) |
| 124 | return |
| 125 | } |
ehdou | fd6d86c | 2016-11-03 21:35:45 +0200 | [diff] [blame] | 126 | |
| 127 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 128 | // Update request. |
| 129 | type UpdateOptsBuilder interface { |
| 130 | ToShareNetworkUpdateMap() (map[string]interface{}, error) |
| 131 | } |
| 132 | |
| 133 | // UpdateOpts contain options for updating an existing ShareNetwork. This object is passed |
| 134 | // to the sharenetworks.Update function. For more information about the parameters, see |
| 135 | // the ShareNetwork object. |
| 136 | type UpdateOpts struct { |
| 137 | // The share network name |
| 138 | Name string `json:"name,omitempty"` |
| 139 | // The share network description |
| 140 | Description string `json:"description,omitempty"` |
| 141 | // The UUID of the Neutron network to set up for share servers |
| 142 | NeutronNetID string `json:"neutron_net_id,omitempty"` |
| 143 | // The UUID of the Neutron subnet to set up for share servers |
| 144 | NeutronSubnetID string `json:"neutron_subnet_id,omitempty"` |
| 145 | // The UUID of the nova network to set up for share servers |
| 146 | NovaNetID string `json:"nova_net_id,omitempty"` |
| 147 | } |
| 148 | |
| 149 | // ToShareNetworkUpdateMap assembles a request body based on the contents of an |
| 150 | // UpdateOpts. |
| 151 | func (opts UpdateOpts) ToShareNetworkUpdateMap() (map[string]interface{}, error) { |
| 152 | return gophercloud.BuildRequestBody(opts, "share_network") |
| 153 | } |
| 154 | |
| 155 | // Update will update the ShareNetwork with provided information. To extract the updated |
| 156 | // ShareNetwork from the response, call the Extract method on the UpdateResult. |
| 157 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { |
| 158 | b, err := opts.ToShareNetworkUpdateMap() |
| 159 | if err != nil { |
| 160 | r.Err = err |
| 161 | return |
| 162 | } |
| 163 | _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{ |
| 164 | OkCodes: []int{200}, |
| 165 | }) |
| 166 | return |
| 167 | } |