github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/sfs/v2/shares/requests.go (about)

     1  package shares
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/pagination"
     8  )
     9  
    10  var RequestOpts golangsdk.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  	if err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	allShares, err := ExtractShares(pages)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	return FilterShares(allShares, opts)
    96  }
    97  
    98  func FilterShares(shares []Share, opts ListOpts) ([]Share, error) {
    99  
   100  	var refinedShares []Share
   101  	var matched bool
   102  	m := map[string]interface{}{}
   103  
   104  	if opts.ID != "" {
   105  		m["ID"] = opts.ID
   106  	}
   107  
   108  	if len(m) > 0 && len(shares) > 0 {
   109  		for _, share := range shares {
   110  			matched = true
   111  
   112  			for key, value := range m {
   113  				if sVal := getStructField(&share, key); !(sVal == value) {
   114  					matched = false
   115  				}
   116  			}
   117  
   118  			if matched {
   119  				refinedShares = append(refinedShares, share)
   120  			}
   121  		}
   122  	} else {
   123  		refinedShares = shares
   124  	}
   125  	return refinedShares, nil
   126  }
   127  
   128  func getStructField(v *Share, field string) string {
   129  	r := reflect.ValueOf(v)
   130  	f := reflect.Indirect(r).FieldByName(field)
   131  	return string(f.String())
   132  }
   133  
   134  // CreateOptsBuilder allows extensions to add additional parameters to the
   135  // Create request.
   136  type CreateOptsBuilder interface {
   137  	ToShareCreateMap() (map[string]interface{}, error)
   138  }
   139  
   140  // CreateOpts contains the options for create a Share. This object is
   141  // passed to shares.Create(). For more information about these parameters,
   142  // please refer to the Share object, or the shared file systems API v2
   143  // documentation
   144  type CreateOpts struct {
   145  	// Defines the share protocol to use
   146  	ShareProto string `json:"share_proto" required:"true"`
   147  	// Size in GB
   148  	Size int `json:"size" required:"true"`
   149  	// Defines the share name
   150  	Name string `json:"name,omitempty"`
   151  	// Share description
   152  	Description string `json:"description,omitempty"`
   153  	// ShareType defines the sharetype. If omitted, a default share type is used
   154  	ShareType string `json:"share_type,omitempty"`
   155  	// The UUID from which to create a share
   156  	SnapshotID string `json:"snapshot_id,omitempty"`
   157  	// Determines whether or not the share is public
   158  	IsPublic bool `json:"is_public,omitempty"`
   159  	// Key value pairs of user defined metadata
   160  	Metadata map[string]string `json:"metadata,omitempty"`
   161  	// The UUID of the share network to which the share belongs to
   162  	ShareNetworkID string `json:"share_network_id,omitempty"`
   163  	// The UUID of the consistency group to which the share belongs to
   164  	ConsistencyGroupID string `json:"consistency_group_id,omitempty"`
   165  	// The availability zone of the share
   166  	AvailabilityZone string `json:"availability_zone,omitempty"`
   167  }
   168  
   169  // ToShareCreateMap assembles a request body based on the contents of a
   170  // CreateOpts.
   171  func (opts CreateOpts) ToShareCreateMap() (map[string]interface{}, error) {
   172  	return golangsdk.BuildRequestBody(opts, "share")
   173  }
   174  
   175  // Create will create a new Share based on the values in CreateOpts. To extract
   176  // the Share object from the response, call the Extract method on the
   177  // CreateResult.
   178  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   179  	b, err := opts.ToShareCreateMap()
   180  	if err != nil {
   181  		r.Err = err
   182  		return
   183  	}
   184  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
   185  		OkCodes: []int{200, 201},
   186  	})
   187  	return
   188  }
   189  
   190  // UpdateOptsBuilder allows extensions to add additional parameters to the
   191  // Update request.
   192  type UpdateOptsBuilder interface {
   193  	ToShareUpdateMap() (map[string]interface{}, error)
   194  }
   195  
   196  // UpdateOpts contains the values used when updating a Share.
   197  type UpdateOpts struct {
   198  	// DisplayName is equivalent to Name. The API supports using both
   199  	// This is an inherited attribute from the block storage API
   200  	DisplayName string `json:"display_name" required:"true"`
   201  	// DisplayDescription is equivalent to Description. The API supports using bot
   202  	// This is an inherited attribute from the block storage API
   203  	DisplayDescription string `json:"display_description,omitempty"`
   204  }
   205  
   206  // ToShareUpdateMap builds an update body based on UpdateOpts.
   207  func (opts UpdateOpts) ToShareUpdateMap() (map[string]interface{}, error) {
   208  	return golangsdk.BuildRequestBody(opts, "share")
   209  }
   210  
   211  // Update allows shares to be updated. You can update the DisplayName, DisplayDescription.
   212  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   213  	b, err := opts.ToShareUpdateMap()
   214  	if err != nil {
   215  		r.Err = err
   216  		return
   217  	}
   218  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   219  		OkCodes: []int{200},
   220  	})
   221  	return
   222  }
   223  
   224  // Get will get a single share with given UUID
   225  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
   226  	_, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
   227  
   228  	return
   229  }
   230  
   231  // Delete will delete an existing Share with the given UUID.
   232  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
   233  	_, r.Err = client.Delete(resourceURL(client, id), nil)
   234  	return
   235  }
   236  
   237  // ListAccessRights lists all access rules assigned to a Share based on its id. To extract
   238  // the AccessRight slice from the response, call the Extract method on the AccessRightsResult.
   239  // Client must have Microversion set; minimum supported microversion for ListAccessRights is 2.7.
   240  func ListAccessRights(client *golangsdk.ServiceClient, share_id string) (r AccessRightsResult) {
   241  	requestBody := map[string]interface{}{"os-access_list": nil}
   242  	_, r.Err = client.Post(rootURL(client, share_id), requestBody, &r.Body, &golangsdk.RequestOpts{
   243  		OkCodes: []int{200},
   244  	})
   245  	return
   246  }
   247  
   248  // GrantAccessOptsBuilder allows extensions to add additional parameters to the
   249  // GrantAccess request.
   250  type GrantAccessOptsBuilder interface {
   251  	ToGrantAccessMap() (map[string]interface{}, error)
   252  }
   253  
   254  // GrantAccessOpts contains the options for creation of an GrantAccess request.
   255  // For more information about these parameters, please, refer to the shared file systems API v2,
   256  // Share Actions, Grant Access documentation
   257  type GrantAccessOpts struct {
   258  	// The access rule type that can be "ip", "cert" or "user".
   259  	AccessType string `json:"access_type"`
   260  	// The value that defines the access that can be a valid format of IP, cert or user.
   261  	AccessTo string `json:"access_to"`
   262  	// The access level to the share is either "rw" or "ro".
   263  	AccessLevel string `json:"access_level"`
   264  }
   265  
   266  // ToGrantAccessMap assembles a request body based on the contents of a
   267  // GrantAccessOpts.
   268  func (opts GrantAccessOpts) ToGrantAccessMap() (map[string]interface{}, error) {
   269  	return golangsdk.BuildRequestBody(opts, "os-allow_access")
   270  }
   271  
   272  // GrantAccess will grant access to a Share based on the values in GrantAccessOpts. To extract
   273  // the GrantAccess object from the response, call the Extract method on the GrantAccessResult.
   274  // Client must have Microversion set; minimum supported microversion for GrantAccess is 2.7.
   275  func GrantAccess(client *golangsdk.ServiceClient, share_id string, opts GrantAccessOptsBuilder) (r GrantAccessResult) {
   276  	b, err := opts.ToGrantAccessMap()
   277  	if err != nil {
   278  		r.Err = err
   279  		return
   280  	}
   281  	_, r.Err = client.Post(rootURL(client, share_id), b, &r.Body, &golangsdk.RequestOpts{
   282  		OkCodes: []int{200},
   283  	})
   284  	return
   285  }
   286  
   287  // Delete the Access Rule
   288  type DeleteAccessOptsBuilder interface {
   289  	ToDeleteAccessMap() (map[string]interface{}, error)
   290  }
   291  
   292  type DeleteAccessOpts struct {
   293  	// The access ID to be deleted
   294  	AccessID string `json:"access_id"`
   295  }
   296  
   297  func (opts DeleteAccessOpts) ToDeleteAccessMap() (map[string]interface{}, error) {
   298  	return golangsdk.BuildRequestBody(opts, "os-deny_access")
   299  }
   300  
   301  //Deletes the Access Rule
   302  func DeleteAccess(client *golangsdk.ServiceClient, share_id string, opts DeleteAccessOptsBuilder) (r DeleteAccessResult) {
   303  	b, err := opts.ToDeleteAccessMap()
   304  	if err != nil {
   305  		r.Err = err
   306  		return
   307  	}
   308  	_, r.Err = client.Post(rootURL(client, share_id), b, nil, &golangsdk.RequestOpts{
   309  		OkCodes: []int{202},
   310  	})
   311  	return
   312  }
   313  
   314  //Gets the Mount/Export Locations of the SFS specified
   315  func GetExportLocations(client *golangsdk.ServiceClient, id string) (r GetExportLocationsResult) {
   316  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200},
   317  		MoreHeaders: RequestOpts.MoreHeaders}
   318  	_, r.Err = client.Get(getMountLocationsURL(client, id), &r.Body, reqOpt)
   319  	return
   320  }
   321  
   322  // ExpandOptsBuilder allows extensions to add additional parameters to the
   323  // Expand request.
   324  type ExpandOptsBuilder interface {
   325  	ToShareExpandMap() (map[string]interface{}, error)
   326  }
   327  
   328  // ExpandOpts contains the options for expanding a Share. This object is
   329  // passed to shares.Expand(). For more information about these parameters,
   330  // please refer to the Share object, or the shared file systems API v2
   331  // documentation
   332  type ExpandOpts struct {
   333  	// Specifies the os-extend object.
   334  	OSExtend OSExtendOpts `json:"os-extend" required:"true"`
   335  }
   336  
   337  type OSExtendOpts struct {
   338  	// Specifies the post-expansion capacity (GB) of the shared file system.
   339  	NewSize int `json:"new_size" required:"true"`
   340  }
   341  
   342  // ToShareExpandMap assembles a request body based on the contents of a
   343  // ExpandOpts.
   344  func (opts ExpandOpts) ToShareExpandMap() (map[string]interface{}, error) {
   345  	return golangsdk.BuildRequestBody(opts, "")
   346  }
   347  
   348  // Expand will expand a  Share based on the values in ExpandOpts.
   349  func Expand(client *golangsdk.ServiceClient, share_id string, opts ExpandOptsBuilder) (r ExpandResult) {
   350  	b, err := opts.ToShareExpandMap()
   351  	if err != nil {
   352  		r.Err = err
   353  		return
   354  	}
   355  	_, r.Err = client.Post(grantAccessURL(client, share_id), b, nil, &golangsdk.RequestOpts{
   356  		OkCodes: []int{202},
   357  	})
   358  	return
   359  }
   360  
   361  // ShrinkOptsBuilder allows extensions to add additional parameters to the
   362  // Shrink request.
   363  type ShrinkOptsBuilder interface {
   364  	ToShareShrinkMap() (map[string]interface{}, error)
   365  }
   366  
   367  // ShrinkOpts contains the options for shrinking a Share. This object is
   368  // passed to shares.Shrink(). For more information about these parameters,
   369  // please refer to the Share object, or the shared file systems API v2
   370  // documentation
   371  type ShrinkOpts struct {
   372  	// Specifies the os-shrink object.
   373  	OSShrink OSShrinkOpts `json:"os-shrink" required:"true"`
   374  }
   375  
   376  type OSShrinkOpts struct {
   377  	// Specifies the post-shrinking capacity (GB) of the shared file system.
   378  	NewSize int `json:"new_size" required:"true"`
   379  }
   380  
   381  // ToShareShrinkMap assembles a request body based on the contents of a
   382  // ShrinkOpts.
   383  func (opts ShrinkOpts) ToShareShrinkMap() (map[string]interface{}, error) {
   384  	return golangsdk.BuildRequestBody(opts, "")
   385  }
   386  
   387  // Shrink will shrink a  Share based on the values in ShrinkOpts.
   388  func Shrink(client *golangsdk.ServiceClient, share_id string, opts ShrinkOptsBuilder) (r ShrinkResult) {
   389  	b, err := opts.ToShareShrinkMap()
   390  	if err != nil {
   391  		r.Err = err
   392  		return
   393  	}
   394  	_, r.Err = client.Post(grantAccessURL(client, share_id), b, nil, &golangsdk.RequestOpts{
   395  		OkCodes: []int{202},
   396  	})
   397  	return
   398  }