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