github.com/gophercloud/gophercloud@v1.14.1/openstack/networking/v2/subnets/requests.go (about) 1 package subnets 2 3 import ( 4 "fmt" 5 6 "github.com/gophercloud/gophercloud" 7 "github.com/gophercloud/gophercloud/pagination" 8 ) 9 10 // ListOptsBuilder allows extensions to add additional parameters to the 11 // List request. 12 type ListOptsBuilder interface { 13 ToSubnetListQuery() (string, error) 14 } 15 16 // ListOpts allows the filtering and sorting of paginated collections through 17 // the API. Filtering is achieved by passing in struct field values that map to 18 // the subnet attributes you want to see returned. SortKey allows you to sort 19 // by a particular subnet attribute. SortDir sets the direction, and is either 20 // `asc' or `desc'. Marker and Limit are used for pagination. 21 type ListOpts struct { 22 Name string `q:"name"` 23 Description string `q:"description"` 24 DNSPublishFixedIP *bool `q:"dns_publish_fixed_ip"` 25 EnableDHCP *bool `q:"enable_dhcp"` 26 NetworkID string `q:"network_id"` 27 TenantID string `q:"tenant_id"` 28 ProjectID string `q:"project_id"` 29 IPVersion int `q:"ip_version"` 30 GatewayIP string `q:"gateway_ip"` 31 CIDR string `q:"cidr"` 32 IPv6AddressMode string `q:"ipv6_address_mode"` 33 IPv6RAMode string `q:"ipv6_ra_mode"` 34 ID string `q:"id"` 35 SubnetPoolID string `q:"subnetpool_id"` 36 Limit int `q:"limit"` 37 Marker string `q:"marker"` 38 SortKey string `q:"sort_key"` 39 SortDir string `q:"sort_dir"` 40 Tags string `q:"tags"` 41 TagsAny string `q:"tags-any"` 42 NotTags string `q:"not-tags"` 43 NotTagsAny string `q:"not-tags-any"` 44 } 45 46 // ToSubnetListQuery formats a ListOpts into a query string. 47 func (opts ListOpts) ToSubnetListQuery() (string, error) { 48 q, err := gophercloud.BuildQueryString(opts) 49 return q.String(), err 50 } 51 52 // List returns a Pager which allows you to iterate over a collection of 53 // subnets. It accepts a ListOpts struct, which allows you to filter and sort 54 // the returned collection for greater efficiency. 55 // 56 // Default policy settings return only those subnets that are owned by the tenant 57 // who submits the request, unless the request is submitted by a user with 58 // administrative rights. 59 func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 60 url := listURL(c) 61 if opts != nil { 62 query, err := opts.ToSubnetListQuery() 63 if err != nil { 64 return pagination.Pager{Err: err} 65 } 66 url += query 67 } 68 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 69 return SubnetPage{pagination.LinkedPageBase{PageResult: r}} 70 }) 71 } 72 73 // Get retrieves a specific subnet based on its unique ID. 74 func Get(c *gophercloud.ServiceClient, id string) (r GetResult) { 75 resp, err := c.Get(getURL(c, id), &r.Body, nil) 76 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 77 return 78 } 79 80 // CreateOptsBuilder allows extensions to add additional parameters to the 81 // List request. 82 type CreateOptsBuilder interface { 83 ToSubnetCreateMap() (map[string]interface{}, error) 84 } 85 86 // CreateOpts represents the attributes used when creating a new subnet. 87 type CreateOpts struct { 88 // NetworkID is the UUID of the network the subnet will be associated with. 89 NetworkID string `json:"network_id" required:"true"` 90 91 // CIDR is the address CIDR of the subnet. 92 CIDR string `json:"cidr,omitempty"` 93 94 // Name is a human-readable name of the subnet. 95 Name string `json:"name,omitempty"` 96 97 // Description of the subnet. 98 Description string `json:"description,omitempty"` 99 100 // The UUID of the project who owns the Subnet. Only administrative users 101 // can specify a project UUID other than their own. 102 TenantID string `json:"tenant_id,omitempty"` 103 104 // The UUID of the project who owns the Subnet. Only administrative users 105 // can specify a project UUID other than their own. 106 ProjectID string `json:"project_id,omitempty"` 107 108 // AllocationPools are IP Address pools that will be available for DHCP. 109 AllocationPools []AllocationPool `json:"allocation_pools,omitempty"` 110 111 // GatewayIP sets gateway information for the subnet. Setting to nil will 112 // cause a default gateway to automatically be created. Setting to an empty 113 // string will cause the subnet to be created with no gateway. Setting to 114 // an explicit address will set that address as the gateway. 115 GatewayIP *string `json:"gateway_ip,omitempty"` 116 117 // IPVersion is the IP version for the subnet. 118 IPVersion gophercloud.IPVersion `json:"ip_version,omitempty"` 119 120 // EnableDHCP will either enable to disable the DHCP service. 121 EnableDHCP *bool `json:"enable_dhcp,omitempty"` 122 123 // DNSNameservers are the nameservers to be set via DHCP. 124 DNSNameservers []string `json:"dns_nameservers,omitempty"` 125 126 // DNSPublishFixedIP will either enable or disable the publication of fixed IPs to the DNS 127 DNSPublishFixedIP *bool `json:"dns_publish_fixed_ip,omitempty"` 128 129 // ServiceTypes are the service types associated with the subnet. 130 ServiceTypes []string `json:"service_types,omitempty"` 131 132 // HostRoutes are any static host routes to be set via DHCP. 133 HostRoutes []HostRoute `json:"host_routes,omitempty"` 134 135 // The IPv6 address modes specifies mechanisms for assigning IPv6 IP addresses. 136 IPv6AddressMode string `json:"ipv6_address_mode,omitempty"` 137 138 // The IPv6 router advertisement specifies whether the networking service 139 // should transmit ICMPv6 packets. 140 IPv6RAMode string `json:"ipv6_ra_mode,omitempty"` 141 142 // SubnetPoolID is the id of the subnet pool that subnet should be associated to. 143 SubnetPoolID string `json:"subnetpool_id,omitempty"` 144 145 // Prefixlen is used when user creates a subnet from the subnetpool. It will 146 // overwrite the "default_prefixlen" value of the referenced subnetpool. 147 Prefixlen int `json:"prefixlen,omitempty"` 148 } 149 150 // ToSubnetCreateMap builds a request body from CreateOpts. 151 func (opts CreateOpts) ToSubnetCreateMap() (map[string]interface{}, error) { 152 b, err := gophercloud.BuildRequestBody(opts, "subnet") 153 if err != nil { 154 return nil, err 155 } 156 157 if m := b["subnet"].(map[string]interface{}); m["gateway_ip"] == "" { 158 m["gateway_ip"] = nil 159 } 160 161 return b, nil 162 } 163 164 // Create accepts a CreateOpts struct and creates a new subnet using the values 165 // provided. You must remember to provide a valid NetworkID, CIDR and IP 166 // version. 167 func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 168 b, err := opts.ToSubnetCreateMap() 169 if err != nil { 170 r.Err = err 171 return 172 } 173 resp, err := c.Post(createURL(c), b, &r.Body, nil) 174 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 175 return 176 } 177 178 // UpdateOptsBuilder allows extensions to add additional parameters to the 179 // Update request. 180 type UpdateOptsBuilder interface { 181 ToSubnetUpdateMap() (map[string]interface{}, error) 182 } 183 184 // UpdateOpts represents the attributes used when updating an existing subnet. 185 type UpdateOpts struct { 186 // Name is a human-readable name of the subnet. 187 Name *string `json:"name,omitempty"` 188 189 // Description of the subnet. 190 Description *string `json:"description,omitempty"` 191 192 // AllocationPools are IP Address pools that will be available for DHCP. 193 AllocationPools []AllocationPool `json:"allocation_pools,omitempty"` 194 195 // GatewayIP sets gateway information for the subnet. Setting to nil will 196 // cause a default gateway to automatically be created. Setting to an empty 197 // string will cause the subnet to be created with no gateway. Setting to 198 // an explicit address will set that address as the gateway. 199 GatewayIP *string `json:"gateway_ip,omitempty"` 200 201 // DNSNameservers are the nameservers to be set via DHCP. 202 DNSNameservers *[]string `json:"dns_nameservers,omitempty"` 203 204 // DNSPublishFixedIP will either enable or disable the publication of fixed IPs to the DNS 205 DNSPublishFixedIP *bool `json:"dns_publish_fixed_ip,omitempty"` 206 207 // ServiceTypes are the service types associated with the subnet. 208 ServiceTypes *[]string `json:"service_types,omitempty"` 209 210 // HostRoutes are any static host routes to be set via DHCP. 211 HostRoutes *[]HostRoute `json:"host_routes,omitempty"` 212 213 // EnableDHCP will either enable to disable the DHCP service. 214 EnableDHCP *bool `json:"enable_dhcp,omitempty"` 215 216 // RevisionNumber implements extension:standard-attr-revisions. If != "" it 217 // will set revision_number=%s. If the revision number does not match, the 218 // update will fail. 219 RevisionNumber *int `json:"-" h:"If-Match"` 220 } 221 222 // ToSubnetUpdateMap builds a request body from UpdateOpts. 223 func (opts UpdateOpts) ToSubnetUpdateMap() (map[string]interface{}, error) { 224 b, err := gophercloud.BuildRequestBody(opts, "subnet") 225 if err != nil { 226 return nil, err 227 } 228 229 if m := b["subnet"].(map[string]interface{}); m["gateway_ip"] == "" { 230 m["gateway_ip"] = nil 231 } 232 233 return b, nil 234 } 235 236 // Update accepts a UpdateOpts struct and updates an existing subnet using the 237 // values provided. 238 func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 239 b, err := opts.ToSubnetUpdateMap() 240 if err != nil { 241 r.Err = err 242 return 243 } 244 h, err := gophercloud.BuildHeaders(opts) 245 if err != nil { 246 r.Err = err 247 return 248 } 249 for k := range h { 250 if k == "If-Match" { 251 h[k] = fmt.Sprintf("revision_number=%s", h[k]) 252 } 253 } 254 255 resp, err := c.Put(updateURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ 256 MoreHeaders: h, 257 OkCodes: []int{200, 201}, 258 }) 259 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 260 return 261 } 262 263 // Delete accepts a unique ID and deletes the subnet associated with it. 264 func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) { 265 resp, err := c.Delete(deleteURL(c, id), nil) 266 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 267 return 268 }