github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/sfs/v2/shares/requests.go (about) 1 package shares 2 3 import ( 4 "reflect" 5 6 "github.com/opentelekomcloud/gophertelekomcloud" 7 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 8 ) 9 10 var RequestOpts = golangsdk.RequestOpts{ 11 MoreHeaders: map[string]string{"Content-Type": "application/json", 12 "X-Openstack-Manila-Api-Version": "2.9"}, 13 } 14 15 // SortDir is a type for specifying in which direction to sort a list of Shares. 16 type SortDir string 17 18 // SortKey is a type for specifying by which key to sort a list of Shares. 19 type SortKey string 20 21 var ( 22 // SortAsc is used to sort a list of Shares in ascending order. 23 SortAsc SortDir = "asc" 24 // SortDesc is used to sort a list of Shares in descending order. 25 SortDesc SortDir = "desc" 26 // SortId is used to sort a list of Shares by id. 27 SortId SortKey = "id" 28 // SortName is used to sort a list of Shares by name. 29 SortName SortKey = "name" 30 // SortSize is used to sort a list of Shares by size. 31 SortSize SortKey = "size" 32 // SortHost is used to sort a list of Shares by host. 33 SortHost SortKey = "host" 34 // SortShareProto is used to sort a list of Shares by share_proto. 35 SortShareProto SortKey = "share_proto" 36 // SortStatus is used to sort a list of Shares by status. 37 SortStatus SortKey = "status" 38 // SortProjectId is used to sort a list of Shares by project_id. 39 SortProjectId SortKey = "project_id" 40 // SortShareTypeId is used to sort a list of Shares by share_type_id. 41 SortShareTypeId SortKey = "share_type_id" 42 // SortShareNetworkId is used to sort a list of Shares by share_network_id. 43 SortShareNetworkId SortKey = "share_network_id" 44 // SortSnapshotId is used to sort a list of Shares by snapshot_id. 45 SortSnapshotId SortKey = "snapshot_id" 46 // SortCreatedAt is used to sort a list of Shares by date created. 47 SortCreatedAt SortKey = "created_at" 48 // SortUpdatedAt is used to sort a list of Shares by date updated. 49 SortUpdatedAt SortKey = "updated_at" 50 ) 51 52 // ListOptsBuilder allows extensions to add additional parameters to the 53 // List request. 54 type ListOptsBuilder interface { 55 ToShareListQuery() (string, error) 56 } 57 58 // ListOpts allows the filtering and sorting of paginated collections through 59 // the API. Filtering is achieved by passing in struct field values that map to 60 // the share attributes you want to see returned. SortKey allows you to sort 61 // by a particular share attribute. SortDir sets the direction, and is either 62 // `asc' or `desc'. Marker and Limit are used for pagination. 63 type ListOpts struct { 64 ID string 65 Status string `q:"status"` 66 Name string `q:"name"` 67 Limit int `q:"limit"` 68 Offset int `q:"offset"` 69 SortKey SortKey `q:"sort_key"` 70 SortDir SortDir `q:"sort_dir"` 71 IsPublic bool `q:"is_public"` 72 } 73 74 // List returns a Pager which allows you to iterate over a collection of 75 // share resources. It accepts a ListOpts struct, which allows you to 76 // filter the returned collection for greater efficiency. 77 func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Share, error) { 78 q, err := golangsdk.BuildQueryString(&opts) 79 if err != nil { 80 return nil, err 81 } 82 u := listURL(c) + q.String() 83 pages, err := pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page { 84 return SharePage{pagination.LinkedPageBase{PageResult: r}} 85 }).AllPages() 86 87 if err != nil { 88 return nil, err 89 } 90 91 allShares, err := ExtractShares(pages) 92 if err != nil { 93 return nil, err 94 } 95 96 return FilterShares(allShares, opts) 97 } 98 99 func FilterShares(shares []Share, opts ListOpts) ([]Share, error) { 100 101 var refinedShares []Share 102 var matched bool 103 m := map[string]interface{}{} 104 105 if opts.ID != "" { 106 m["ID"] = opts.ID 107 } 108 109 if len(m) > 0 && len(shares) > 0 { 110 for _, share := range shares { 111 matched = true 112 113 for key, value := range m { 114 if sVal := getStructField(&share, key); !(sVal == value) { 115 matched = false 116 } 117 } 118 119 if matched { 120 refinedShares = append(refinedShares, share) 121 } 122 } 123 } else { 124 refinedShares = shares 125 } 126 return refinedShares, nil 127 } 128 129 func getStructField(v *Share, field string) string { 130 r := reflect.ValueOf(v) 131 f := reflect.Indirect(r).FieldByName(field) 132 return f.String() 133 } 134 135 // CreateOptsBuilder allows extensions to add additional parameters to the 136 // Create request. 137 type CreateOptsBuilder interface { 138 ToShareCreateMap() (map[string]interface{}, error) 139 } 140 141 // CreateOpts contains the options for create a Share. This object is 142 // passed to shares.Create(). For more information about these parameters, 143 // please refer to the Share object, or the shared file systems API v2 144 // documentation 145 type CreateOpts struct { 146 // Defines the share protocol to use 147 ShareProto string `json:"share_proto" required:"true"` 148 // Size in GB 149 Size int `json:"size" required:"true"` 150 // Defines the share name 151 Name string `json:"name,omitempty"` 152 // Share description 153 Description string `json:"description,omitempty"` 154 // ShareType defines the sharetype. If omitted, a default share type is used 155 ShareType string `json:"share_type,omitempty"` 156 // The UUID from which to create a share 157 SnapshotID string `json:"snapshot_id,omitempty"` 158 // Determines whether or not the share is public 159 IsPublic bool `json:"is_public,omitempty"` 160 // Key value pairs of user defined metadata 161 Metadata map[string]string `json:"metadata,omitempty"` 162 // The UUID of the share network to which the share belongs to 163 ShareNetworkID string `json:"share_network_id,omitempty"` 164 // The UUID of the consistency group to which the share belongs to 165 ConsistencyGroupID string `json:"consistency_group_id,omitempty"` 166 // The availability zone of the share 167 AvailabilityZone string `json:"availability_zone,omitempty"` 168 } 169 170 // ToShareCreateMap assembles a request body based on the contents of a 171 // CreateOpts. 172 func (opts CreateOpts) ToShareCreateMap() (map[string]interface{}, error) { 173 return golangsdk.BuildRequestBody(opts, "share") 174 } 175 176 // Create will create a new Share based on the values in CreateOpts. To extract 177 // the Share object from the response, call the Extract method on the 178 // CreateResult. 179 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 180 b, err := opts.ToShareCreateMap() 181 if err != nil { 182 r.Err = err 183 return 184 } 185 _, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{ 186 OkCodes: []int{200, 201}, 187 }) 188 return 189 } 190 191 // UpdateOptsBuilder allows extensions to add additional parameters to the 192 // Update request. 193 type UpdateOptsBuilder interface { 194 ToShareUpdateMap() (map[string]interface{}, error) 195 } 196 197 // UpdateOpts contains the values used when updating a Share. 198 type UpdateOpts struct { 199 // DisplayName is equivalent to Name. The API supports using both 200 // This is an inherited attribute from the block storage API 201 DisplayName string `json:"display_name" required:"true"` 202 // DisplayDescription is equivalent to Description. The API supports using bot 203 // This is an inherited attribute from the block storage API 204 DisplayDescription string `json:"display_description,omitempty"` 205 } 206 207 // ToShareUpdateMap builds an update body based on UpdateOpts. 208 func (opts UpdateOpts) ToShareUpdateMap() (map[string]interface{}, error) { 209 return golangsdk.BuildRequestBody(opts, "share") 210 } 211 212 // Update allows shares to be updated. You can update the DisplayName, DisplayDescription. 213 func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 214 b, err := opts.ToShareUpdateMap() 215 if err != nil { 216 r.Err = err 217 return 218 } 219 _, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ 220 OkCodes: []int{200}, 221 }) 222 return 223 } 224 225 // Get will get a single share with given UUID 226 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 227 _, r.Err = client.Get(resourceURL(client, id), &r.Body, nil) 228 229 return 230 } 231 232 // Delete will delete an existing Share with the given UUID. 233 func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { 234 _, r.Err = client.Delete(resourceURL(client, id), nil) 235 return 236 } 237 238 // ListAccessRights lists all access rules assigned to a Share based on its id. To extract 239 // the AccessRight slice from the response, call the Extract method on the AccessRightsResult. 240 // Client must have Microversion set; minimum supported microversion for ListAccessRights is 2.7. 241 func ListAccessRights(client *golangsdk.ServiceClient, share_id string) (r AccessRightsResult) { 242 requestBody := map[string]interface{}{"os-access_list": nil} 243 _, r.Err = client.Post(rootURL(client, share_id), requestBody, &r.Body, &golangsdk.RequestOpts{ 244 OkCodes: []int{200}, 245 }) 246 return 247 } 248 249 // GrantAccessOptsBuilder allows extensions to add additional parameters to the 250 // GrantAccess request. 251 type GrantAccessOptsBuilder interface { 252 ToGrantAccessMap() (map[string]interface{}, error) 253 } 254 255 // GrantAccessOpts contains the options for creation of an GrantAccess request. 256 // For more information about these parameters, please, refer to the shared file systems API v2, 257 // Share Actions, Grant Access documentation 258 type GrantAccessOpts struct { 259 // The access rule type that can be "ip", "cert" or "user". 260 AccessType string `json:"access_type"` 261 // The value that defines the access that can be a valid format of IP, cert or user. 262 AccessTo string `json:"access_to"` 263 // The access level to the share is either "rw" or "ro". 264 AccessLevel string `json:"access_level"` 265 } 266 267 // ToGrantAccessMap assembles a request body based on the contents of a 268 // GrantAccessOpts. 269 func (opts GrantAccessOpts) ToGrantAccessMap() (map[string]interface{}, error) { 270 return golangsdk.BuildRequestBody(opts, "os-allow_access") 271 } 272 273 // GrantAccess will grant access to a Share based on the values in GrantAccessOpts. To extract 274 // the GrantAccess object from the response, call the Extract method on the GrantAccessResult. 275 // Client must have Microversion set; minimum supported microversion for GrantAccess is 2.7. 276 func GrantAccess(client *golangsdk.ServiceClient, share_id string, opts GrantAccessOptsBuilder) (r GrantAccessResult) { 277 b, err := opts.ToGrantAccessMap() 278 if err != nil { 279 r.Err = err 280 return 281 } 282 _, r.Err = client.Post(rootURL(client, share_id), b, &r.Body, &golangsdk.RequestOpts{ 283 OkCodes: []int{200}, 284 }) 285 return 286 } 287 288 // Delete the Access Rule 289 type DeleteAccessOptsBuilder interface { 290 ToDeleteAccessMap() (map[string]interface{}, error) 291 } 292 293 type DeleteAccessOpts struct { 294 // The access ID to be deleted 295 AccessID string `json:"access_id"` 296 } 297 298 func (opts DeleteAccessOpts) ToDeleteAccessMap() (map[string]interface{}, error) { 299 return golangsdk.BuildRequestBody(opts, "os-deny_access") 300 } 301 302 // Deletes the Access Rule 303 func DeleteAccess(client *golangsdk.ServiceClient, share_id string, opts DeleteAccessOptsBuilder) (r DeleteAccessResult) { 304 b, err := opts.ToDeleteAccessMap() 305 if err != nil { 306 r.Err = err 307 return 308 } 309 _, r.Err = client.Post(rootURL(client, share_id), b, nil, &golangsdk.RequestOpts{ 310 OkCodes: []int{202}, 311 }) 312 return 313 } 314 315 // Gets the Mount/Export Locations of the SFS specified 316 func GetExportLocations(client *golangsdk.ServiceClient, id string) (r GetExportLocationsResult) { 317 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}, 318 MoreHeaders: RequestOpts.MoreHeaders} 319 _, r.Err = client.Get(getMountLocationsURL(client, id), &r.Body, reqOpt) 320 return 321 } 322 323 // ExpandOptsBuilder allows extensions to add additional parameters to the 324 // Expand request. 325 type ExpandOptsBuilder interface { 326 ToShareExpandMap() (map[string]interface{}, error) 327 } 328 329 // ExpandOpts contains the options for expanding a Share. This object is 330 // passed to shares.Expand(). For more information about these parameters, 331 // please refer to the Share object, or the shared file systems API v2 332 // documentation 333 type ExpandOpts struct { 334 // Specifies the os-extend object. 335 OSExtend OSExtendOpts `json:"os-extend" required:"true"` 336 } 337 338 type OSExtendOpts struct { 339 // Specifies the post-expansion capacity (GB) of the shared file system. 340 NewSize int `json:"new_size" required:"true"` 341 } 342 343 // ToShareExpandMap assembles a request body based on the contents of a 344 // ExpandOpts. 345 func (opts ExpandOpts) ToShareExpandMap() (map[string]interface{}, error) { 346 return golangsdk.BuildRequestBody(opts, "") 347 } 348 349 // Expand will expand a Share based on the values in ExpandOpts. 350 func Expand(client *golangsdk.ServiceClient, share_id string, opts ExpandOptsBuilder) (r ExpandResult) { 351 b, err := opts.ToShareExpandMap() 352 if err != nil { 353 r.Err = err 354 return 355 } 356 _, r.Err = client.Post(grantAccessURL(client, share_id), b, nil, &golangsdk.RequestOpts{ 357 OkCodes: []int{202}, 358 }) 359 return 360 } 361 362 // ShrinkOptsBuilder allows extensions to add additional parameters to the 363 // Shrink request. 364 type ShrinkOptsBuilder interface { 365 ToShareShrinkMap() (map[string]interface{}, error) 366 } 367 368 // ShrinkOpts contains the options for shrinking a Share. This object is 369 // passed to shares.Shrink(). For more information about these parameters, 370 // please refer to the Share object, or the shared file systems API v2 371 // documentation 372 type ShrinkOpts struct { 373 // Specifies the os-shrink object. 374 OSShrink OSShrinkOpts `json:"os-shrink" required:"true"` 375 } 376 377 type OSShrinkOpts struct { 378 // Specifies the post-shrinking capacity (GB) of the shared file system. 379 NewSize int `json:"new_size" required:"true"` 380 } 381 382 // ToShareShrinkMap assembles a request body based on the contents of a 383 // ShrinkOpts. 384 func (opts ShrinkOpts) ToShareShrinkMap() (map[string]interface{}, error) { 385 return golangsdk.BuildRequestBody(opts, "") 386 } 387 388 // Shrink will shrink a Share based on the values in ShrinkOpts. 389 func Shrink(client *golangsdk.ServiceClient, share_id string, opts ShrinkOptsBuilder) (r ShrinkResult) { 390 b, err := opts.ToShareShrinkMap() 391 if err != nil { 392 r.Err = err 393 return 394 } 395 _, r.Err = client.Post(grantAccessURL(client, share_id), b, nil, &golangsdk.RequestOpts{ 396 OkCodes: []int{202}, 397 }) 398 return 399 }