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