github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/subnetpools/requests.go (about) 1 package subnetpools 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 ToSubnetPoolListQuery() (string, error) 12 } 13 14 // ListOpts allows the filtering and sorting of paginated collections through 15 // the Neutron API. Filtering is achieved by passing in struct field values 16 // that map to the subnetpool attributes you want to see returned. 17 // SortKey allows you to sort by a particular subnetpool attribute. 18 // SortDir sets the direction, and is either `asc' or `desc'. 19 // Marker and Limit are used for the pagination. 20 type ListOpts struct { 21 ID string `q:"id"` 22 Name string `q:"name"` 23 DefaultQuota int `q:"default_quota"` 24 TenantID string `q:"tenant_id"` 25 ProjectID string `q:"project_id"` 26 DefaultPrefixLen int `q:"default_prefixlen"` 27 MinPrefixLen int `q:"min_prefixlen"` 28 MaxPrefixLen int `q:"max_prefixlen"` 29 AddressScopeID string `q:"address_scope_id"` 30 IPVersion int `q:"ip_version"` 31 Shared *bool `q:"shared"` 32 Description string `q:"description"` 33 IsDefault *bool `q:"is_default"` 34 RevisionNumber int `q:"revision_number"` 35 Limit int `q:"limit"` 36 Marker string `q:"marker"` 37 SortKey string `q:"sort_key"` 38 SortDir string `q:"sort_dir"` 39 } 40 41 // ToSubnetPoolListQuery formats a ListOpts into a query string. 42 func (opts ListOpts) ToSubnetPoolListQuery() (string, error) { 43 q, err := golangsdk.BuildQueryString(opts) 44 return q.String(), err 45 } 46 47 // List returns a Pager which allows you to iterate over a collection of 48 // subnetpools. 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 the subnetpools owned by the project 52 // of the user submitting the request, unless the user has the administrative role. 53 func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 54 url := listURL(c) 55 if opts != nil { 56 query, err := opts.ToSubnetPoolListQuery() 57 if err != nil { 58 return pagination.Pager{Err: err} 59 } 60 url += query 61 } 62 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 63 return SubnetPoolPage{pagination.LinkedPageBase{PageResult: r}} 64 }) 65 } 66 67 // Get retrieves a specific subnetpool based on its ID. 68 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 69 _, r.Err = c.Get(getURL(c, id), &r.Body, nil) 70 return 71 } 72 73 // CreateOptsBuilder allows to add additional parameters to the 74 // Create request. 75 type CreateOptsBuilder interface { 76 ToSubnetPoolCreateMap() (map[string]interface{}, error) 77 } 78 79 // CreateOpts specifies parameters of a new subnetpool. 80 type CreateOpts struct { 81 // Name is the human-readable name of the subnetpool. 82 Name string `json:"name"` 83 84 // DefaultQuota is the per-project quota on the prefix space 85 // that can be allocated from the subnetpool for project subnets. 86 DefaultQuota int `json:"default_quota,omitempty"` 87 88 // TenantID is the id of the Identity project. 89 TenantID string `json:"tenant_id,omitempty"` 90 91 // ProjectID is the id of the Identity project. 92 ProjectID string `json:"project_id,omitempty"` 93 94 // Prefixes is the list of subnet prefixes to assign to the subnetpool. 95 // Neutron API merges adjacent prefixes and treats them as a single prefix. 96 // Each subnet prefix must be unique among all subnet prefixes in all subnetpools 97 // that are associated with the address scope. 98 Prefixes []string `json:"prefixes"` 99 100 // DefaultPrefixLen is the size of the prefix to allocate when the cidr 101 // or prefixlen attributes are omitted when you create the subnet. 102 // Defaults to the MinPrefixLen. 103 DefaultPrefixLen int `json:"default_prefixlen,omitempty"` 104 105 // MinPrefixLen is the smallest prefix that can be allocated from a subnetpool. 106 // For IPv4 subnetpools, default is 8. 107 // For IPv6 subnetpools, default is 64. 108 MinPrefixLen int `json:"min_prefixlen,omitempty"` 109 110 // MaxPrefixLen is the maximum prefix size that can be allocated from the subnetpool. 111 // For IPv4 subnetpools, default is 32. 112 // For IPv6 subnetpools, default is 128. 113 MaxPrefixLen int `json:"max_prefixlen,omitempty"` 114 115 // AddressScopeID is the Neutron address scope to assign to the subnetpool. 116 AddressScopeID string `json:"address_scope_id,omitempty"` 117 118 // Shared indicates whether this network is shared across all projects. 119 Shared bool `json:"shared,omitempty"` 120 121 // Description is the human-readable description for the resource. 122 Description string `json:"description,omitempty"` 123 124 // IsDefault indicates if the subnetpool is default pool or not. 125 IsDefault bool `json:"is_default,omitempty"` 126 } 127 128 // ToSubnetPoolCreateMap constructs a request body from CreateOpts. 129 func (opts CreateOpts) ToSubnetPoolCreateMap() (map[string]interface{}, error) { 130 return golangsdk.BuildRequestBody(opts, "subnetpool") 131 } 132 133 // Create requests the creation of a new subnetpool on the server. 134 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 135 b, err := opts.ToSubnetPoolCreateMap() 136 if err != nil { 137 r.Err = err 138 return 139 } 140 _, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{ 141 OkCodes: []int{201}, 142 }) 143 return 144 } 145 146 // UpdateOptsBuilder allows extensions to add additional parameters to the 147 // Update request. 148 type UpdateOptsBuilder interface { 149 ToSubnetPoolUpdateMap() (map[string]interface{}, error) 150 } 151 152 // UpdateOpts represents options used to update a network. 153 type UpdateOpts struct { 154 // Name is the human-readable name of the subnetpool. 155 Name string `json:"name,omitempty"` 156 157 // DefaultQuota is the per-project quota on the prefix space 158 // that can be allocated from the subnetpool for project subnets. 159 DefaultQuota *int `json:"default_quota,omitempty"` 160 161 // TenantID is the id of the Identity project. 162 TenantID string `json:"tenant_id,omitempty"` 163 164 // ProjectID is the id of the Identity project. 165 ProjectID string `json:"project_id,omitempty"` 166 167 // Prefixes is the list of subnet prefixes to assign to the subnetpool. 168 // Neutron API merges adjacent prefixes and treats them as a single prefix. 169 // Each subnet prefix must be unique among all subnet prefixes in all subnetpools 170 // that are associated with the address scope. 171 Prefixes []string `json:"prefixes,omitempty"` 172 173 // DefaultPrefixLen is yhe size of the prefix to allocate when the cidr 174 // or prefixlen attributes are omitted when you create the subnet. 175 // Defaults to the MinPrefixLen. 176 DefaultPrefixLen int `json:"default_prefixlen,omitempty"` 177 178 // MinPrefixLen is the smallest prefix that can be allocated from a subnetpool. 179 // For IPv4 subnetpools, default is 8. 180 // For IPv6 subnetpools, default is 64. 181 MinPrefixLen int `json:"min_prefixlen,omitempty"` 182 183 // MaxPrefixLen is the maximum prefix size that can be allocated from the subnetpool. 184 // For IPv4 subnetpools, default is 32. 185 // For IPv6 subnetpools, default is 128. 186 MaxPrefixLen int `json:"max_prefixlen,omitempty"` 187 188 // AddressScopeID is the Neutron address scope to assign to the subnetpool. 189 AddressScopeID *string `json:"address_scope_id,omitempty"` 190 191 // Description is thehuman-readable description for the resource. 192 Description *string `json:"description,omitempty"` 193 194 // IsDefault indicates if the subnetpool is default pool or not. 195 IsDefault *bool `json:"is_default,omitempty"` 196 } 197 198 // ToSubnetPoolUpdateMap builds a request body from UpdateOpts. 199 func (opts UpdateOpts) ToSubnetPoolUpdateMap() (map[string]interface{}, error) { 200 return golangsdk.BuildRequestBody(opts, "subnetpool") 201 } 202 203 // Update accepts a UpdateOpts struct and updates an existing subnetpool using the 204 // values provided. 205 func Update(c *golangsdk.ServiceClient, subnetPoolID string, opts UpdateOptsBuilder) (r UpdateResult) { 206 b, err := opts.ToSubnetPoolUpdateMap() 207 if err != nil { 208 r.Err = err 209 return 210 } 211 _, r.Err = c.Put(updateURL(c, subnetPoolID), b, &r.Body, &golangsdk.RequestOpts{ 212 OkCodes: []int{200}, 213 }) 214 return 215 } 216 217 // Delete accepts a unique ID and deletes the subnetpool associated with it. 218 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 219 _, r.Err = c.Delete(deleteURL(c, id), nil) 220 return 221 }