github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/sfs_turbo/v1/shares/requests.go (about)

     1  package shares
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  var requestOpts = golangsdk.RequestOpts{
     9  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    10  }
    11  
    12  // CreateOptsBuilder allows extensions to add additional parameters to the
    13  // Create request.
    14  type CreateOptsBuilder interface {
    15  	ToShareCreateMap() (map[string]interface{}, error)
    16  }
    17  
    18  // CreateOpts is the structure used to create a new SFS Turbo resource.
    19  type CreateOpts struct {
    20  	// Turbo configuration details.
    21  	Share Share `json:"share" required:"share"`
    22  	// The configuration of pre-paid billing mode.
    23  	BssParam *BssParam `json:"bss_param,omitempty"`
    24  }
    25  
    26  // CreateOpts contains the options for create an SFS Turbo. This object is
    27  // passed to shares.Create().
    28  type Share struct {
    29  	// Defines the SFS Turbo file system name
    30  	Name string `json:"name" required:"true"`
    31  	// Defines the SFS Turbo file system protocol to use, the vaild value is NFS.
    32  	ShareProto string `json:"share_proto,omitempty"`
    33  	// ShareType defines the file system type. the vaild values are STANDARD and PERFORMANCE.
    34  	ShareType string `json:"share_type" required:"true"`
    35  	// Size in GB, range from 500 to 32768.
    36  	Size int `json:"size" required:"true"`
    37  	// The availability zone of the SFS Turbo file system
    38  	AvailabilityZone string `json:"availability_zone" required:"true"`
    39  	// The VPC ID
    40  	VpcID string `json:"vpc_id" required:"true"`
    41  	// The subnet ID
    42  	SubnetID string `json:"subnet_id" required:"true"`
    43  	// The security group ID
    44  	SecurityGroupID string `json:"security_group_id" required:"true"`
    45  	// The enterprise project ID
    46  	EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
    47  	// The backup ID
    48  	BackupID string `json:"backup_id,omitempty"`
    49  	// Share description
    50  	Description string `json:"description,omitempty"`
    51  	// The metadata information
    52  	Metadata Metadata `json:"metadata,omitempty"`
    53  }
    54  
    55  // Metadata specifies the metadata information
    56  type Metadata struct {
    57  	ExpandType            string `json:"expand_type,omitempty"`
    58  	CryptKeyID            string `json:"crypt_key_id,omitempty"`
    59  	DedicatedFlavor       string `json:"dedicated_flavor,omitempty"`
    60  	MasterDedicatedHostID string `json:"master_dedicated_host_id,omitempty"`
    61  	SlaveDedicatedHostID  string `json:"slave_dedicated_host_id,omitempty"`
    62  	DedicatedStorageID    string `json:"dedicated_storage_id,omitempty"`
    63  	HpcBw                 string `json:"hpc_bw,omitempty"`
    64  }
    65  
    66  // BssParam is an object that represents the prepaid configuration.
    67  type BssParam struct {
    68  	// The number of cycles for prepaid.
    69  	// + minimum: 1
    70  	// + maximum: 11
    71  	PeriodNum int `json:"period_num" required:"true"`
    72  	// The prepaid type.
    73  	// + 2: month
    74  	// + 3: year
    75  	PeriodType int `json:"period_type" require:"true"`
    76  	// Whether to automatically renew.
    77  	// + 0: manual renew.
    78  	// + 1: automatic renew.
    79  	IsAutoRenew *int `json:"is_auto_renew,omitempty"`
    80  	// Whether to pay automatically.
    81  	// + 0: manual payment.
    82  	// + 1: automatic payment.
    83  	IsAutoPay *int `json:"is_auto_pay,omitempty"`
    84  }
    85  
    86  // ToShareCreateMap assembles a request body based on the contents of a
    87  // CreateOpts.
    88  func (opts CreateOpts) ToShareCreateMap() (map[string]interface{}, error) {
    89  	return golangsdk.BuildRequestBody(opts, "")
    90  }
    91  
    92  // Create will create a new SFS Turbo file system based on the values in CreateOpts. To extract
    93  // the Share object from the response, call the Extract method on the
    94  // CreateResult.
    95  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    96  	b, err := opts.ToShareCreateMap()
    97  	if err != nil {
    98  		r.Err = err
    99  		return
   100  	}
   101  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
   102  		OkCodes: []int{200, 202},
   103  	})
   104  	return
   105  }
   106  
   107  // List returns a Pager which allows you to iterate over a collection of
   108  // SFS Turbo resources.
   109  func List(c *golangsdk.ServiceClient) ([]Turbo, error) {
   110  	pages, err := pagination.NewPager(c, listURL(c), func(r pagination.PageResult) pagination.Page {
   111  		return TurboPage{pagination.LinkedPageBase{PageResult: r}}
   112  	}).AllPages()
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	return ExtractTurbos(pages)
   118  }
   119  
   120  // ListOptsBuilder allows extensions to add additional parameters to the List
   121  // request.
   122  type ListOptsBuilder interface {
   123  	ToVolumeListQuery() (string, error)
   124  }
   125  
   126  // ListOpts holds options for listing Volumes. It is passed to the volumes.List
   127  // function.
   128  type ListOpts struct {
   129  	// Requests a page size of items.
   130  	Limit int `q:"limit"`
   131  	// Used in conjunction with limit to return a slice of items.
   132  	Offset int `q:"offset"`
   133  }
   134  
   135  // ToVolumeListQuery formats a ListOpts into a query string.
   136  func (opts ListOpts) ToVolumeListQuery() (string, error) {
   137  	q, err := golangsdk.BuildQueryString(opts)
   138  	return q.String(), err
   139  }
   140  
   141  // ListPage returns one page limited by the conditions provided in Opts.
   142  func ListPage(client *golangsdk.ServiceClient, opts ListOptsBuilder) (*PagedList, error) {
   143  	url := listURL(client)
   144  	if opts != nil {
   145  		query, err := opts.ToVolumeListQuery()
   146  		if err != nil {
   147  			return nil, err
   148  		}
   149  		url += query
   150  	}
   151  
   152  	var rst golangsdk.Result
   153  	_, err := client.Get(url, &rst.Body, &golangsdk.RequestOpts{
   154  		MoreHeaders: requestOpts.MoreHeaders,
   155  	})
   156  
   157  	if err != nil {
   158  		return nil, err
   159  	}
   160  
   161  	var r PagedList
   162  	err = rst.ExtractInto(&r)
   163  	return &r, err
   164  }
   165  
   166  // Get will get a single SFS Trubo file system with given UUID
   167  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
   168  	_, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
   169  
   170  	return
   171  }
   172  
   173  // Delete will delete an existing SFS Trubo file system with the given UUID.
   174  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
   175  	_, r.Err = client.Delete(resourceURL(client, id), nil)
   176  	return
   177  }
   178  
   179  // ExpandOptsBuilder allows extensions to add additional parameters to the
   180  // Expand request.
   181  type ExpandOptsBuilder interface {
   182  	ToShareExpandMap() (map[string]interface{}, error)
   183  }
   184  
   185  type UpdateNameOptsBuilder interface {
   186  	ToShareUpdateNameMap() (map[string]interface{}, error)
   187  }
   188  
   189  type UpdateSecurityGroupIdOptsBuilder interface {
   190  	ToShareUpdateSecurityGroupIdMap() (map[string]interface{}, error)
   191  }
   192  
   193  // ExpandOpts contains the options for expanding a SFS Turbo. This object is
   194  // passed to shares.Expand().
   195  type ExpandOpts struct {
   196  	// Specifies the extend object.
   197  	Extend ExtendOpts `json:"extend" required:"true"`
   198  }
   199  
   200  type UpdateNameOpts struct {
   201  	Name string `json:"name" required:"true"`
   202  }
   203  
   204  type UpdateSecurityGroupIdOpts struct {
   205  	SecurityGroupId string `json:"security_group_id" required:"true"`
   206  }
   207  
   208  // BssParamExtend is an object that represents the payment detail.
   209  type BssParamExtend struct {
   210  	// Whether to pay automatically.
   211  	// + 0: manual payment.
   212  	// + 1: automatic payment.
   213  	IsAutoPay *int `json:"is_auto_pay,omitempty"`
   214  }
   215  
   216  type ExtendOpts struct {
   217  	// Specifies the post-expansion capacity (GB) of the shared file system.
   218  	NewSize int `json:"new_size" required:"true"`
   219  	// New bandwidth of the file system, in GB/s. This parameter is only supported for HPC Cache file systems.
   220  	NewBandwidth int `json:"new_bandwidth,omitempty"`
   221  	// The configuration of pre-paid billing mode.
   222  	BssParam *BssParamExtend `json:"bss_param,omitempty"`
   223  }
   224  
   225  // ToShareExpandMap assembles a request body based on the contents of a
   226  // ExpandOpts.
   227  func (opts ExpandOpts) ToShareExpandMap() (map[string]interface{}, error) {
   228  	return golangsdk.BuildRequestBody(opts, "")
   229  }
   230  
   231  func (opts UpdateNameOpts) ToShareUpdateNameMap() (map[string]interface{}, error) {
   232  	return golangsdk.BuildRequestBody(opts, "change_name")
   233  }
   234  
   235  func (opts UpdateSecurityGroupIdOpts) ToShareUpdateSecurityGroupIdMap() (map[string]interface{}, error) {
   236  	return golangsdk.BuildRequestBody(opts, "change_security_group")
   237  }
   238  
   239  // Expand will expand a SFS Turbo based on the values in ExpandOpts.
   240  func Expand(client *golangsdk.ServiceClient, shareId string, opts ExpandOptsBuilder) (r ExpandResult) {
   241  	b, err := opts.ToShareExpandMap()
   242  	if err != nil {
   243  		r.Err = err
   244  		return
   245  	}
   246  	_, r.Err = client.Post(actionURL(client, shareId), b, &r.Body, &golangsdk.RequestOpts{
   247  		OkCodes: []int{202},
   248  	})
   249  	return
   250  }
   251  
   252  func UpdateName(client *golangsdk.ServiceClient, shareId string, opts UpdateNameOptsBuilder) (r UpdateNameResult) {
   253  	b, err := opts.ToShareUpdateNameMap()
   254  	if err != nil {
   255  		r.Err = err
   256  		return
   257  	}
   258  	_, r.Err = client.Post(actionURL(client, shareId), b, &r.Body, &golangsdk.RequestOpts{
   259  		OkCodes: []int{204},
   260  	})
   261  	return
   262  }
   263  
   264  func UpdateSecurityGroupId(client *golangsdk.ServiceClient, shareId string, opts UpdateSecurityGroupIdOptsBuilder) (r UpdateSecurityGroupIdResult) {
   265  	b, err := opts.ToShareUpdateSecurityGroupIdMap()
   266  	if err != nil {
   267  		r.Err = err
   268  		return
   269  	}
   270  	_, r.Err = client.Post(actionURL(client, shareId), b, &r.Body, &golangsdk.RequestOpts{})
   271  	return
   272  }