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