github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/geminidb/v3/instances/requests.go (about)

     1  package instances
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  type FlavorOpt struct {
     9  	Num      string `json:"num" required:"true"`
    10  	Size     int    `json:"size" required:"true"`
    11  	Storage  string `json:"storage" required:"true"`
    12  	SpecCode string `json:"spec_code" required:"true"`
    13  }
    14  
    15  type BackupStrategyOpt struct {
    16  	StartTime string `json:"start_time,omitempty"`
    17  	KeepDays  string `json:"keep_days,omitempty"`
    18  }
    19  
    20  type ChargeInfoOpt struct {
    21  	ChargingMode string `json:"charge_mode,omitempty"`
    22  	PeriodType   string `json:"period_type,omitempty"`
    23  	PeriodNum    int    `json:"period_num,omitempty"`
    24  	IsAutoRenew  string `json:"is_auto_renew,omitempty"`
    25  	IsAutoPay    string `json:"is_auto_pay,omitempty"`
    26  }
    27  
    28  type CreateGeminiDBOpts struct {
    29  	Name                string             `json:"name" required:"true"`
    30  	Region              string             `json:"region" required:"true"`
    31  	AvailabilityZone    string             `json:"availability_zone" required:"true"`
    32  	VpcId               string             `json:"vpc_id" required:"true"`
    33  	SubnetId            string             `json:"subnet_id" required:"true"`
    34  	SecurityGroupId     string             `json:"security_group_id,omitempty"`
    35  	Password            string             `json:"password" required:"true"`
    36  	Mode                string             `json:"mode" required:"true"`
    37  	ConfigurationId     string             `json:"configuration_id,omitempty"`
    38  	EnterpriseProjectId string             `json:"enterprise_project_id,omitempty"`
    39  	DedicatedResourceId string             `json:"dedicated_resource_id,omitempty"`
    40  	Ssl                 string             `json:"ssl_option,omitempty"`
    41  	DataStore           DataStore          `json:"datastore" required:"true"`
    42  	Flavor              []FlavorOpt        `json:"flavor" required:"true"`
    43  	BackupStrategy      *BackupStrategyOpt `json:"backup_strategy,omitempty"`
    44  	ChargeInfo          *ChargeInfoOpt     `json:"charge_info,omitempty"`
    45  }
    46  
    47  type CreateGeminiDBBuilder interface {
    48  	ToInstancesCreateMap() (map[string]interface{}, error)
    49  }
    50  
    51  func (opts CreateGeminiDBOpts) ToInstancesCreateMap() (map[string]interface{}, error) {
    52  	b, err := golangsdk.BuildRequestBody(opts, "")
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	return b, nil
    57  }
    58  
    59  func Create(client *golangsdk.ServiceClient, opts CreateGeminiDBBuilder) (r CreateResult) {
    60  	b, err := opts.ToInstancesCreateMap()
    61  	if err != nil {
    62  		r.Err = err
    63  		return
    64  	}
    65  
    66  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
    67  		OkCodes:     []int{202},
    68  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    69  	})
    70  	return
    71  }
    72  
    73  type ExtendVolumeOpts struct {
    74  	Size      int    `json:"size" required:"true"`
    75  	IsAutoPay string `json:"is_auto_pay,omitempty"`
    76  }
    77  
    78  type ExtendVolumeBuilder interface {
    79  	ToVolumeExtendMap() (map[string]interface{}, error)
    80  }
    81  
    82  func (opts ExtendVolumeOpts) ToVolumeExtendMap() (map[string]interface{}, error) {
    83  	b, err := golangsdk.BuildRequestBody(opts, "")
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	return b, nil
    88  }
    89  
    90  func ExtendVolume(client *golangsdk.ServiceClient, instanceId string, opts ExtendVolumeBuilder) (r ExtendResult) {
    91  	b, err := opts.ToVolumeExtendMap()
    92  	if err != nil {
    93  		r.Err = err
    94  		return
    95  	}
    96  
    97  	_, r.Err = client.Post(extendURL(client, instanceId), b, &r.Body, &golangsdk.RequestOpts{
    98  		OkCodes:     []int{202},
    99  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   100  	})
   101  	return
   102  }
   103  
   104  type EnlargeNodeOpts struct {
   105  	Num       int    `json:"num" required:"true"`
   106  	IsAutoPay string `json:"is_auto_pay,omitempty"`
   107  }
   108  
   109  type EnlargeNodeBuilder interface {
   110  	ToNodeEnlargeMap() (map[string]interface{}, error)
   111  }
   112  
   113  func (opts EnlargeNodeOpts) ToNodeEnlargeMap() (map[string]interface{}, error) {
   114  	b, err := golangsdk.BuildRequestBody(opts, "")
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	return b, nil
   119  }
   120  
   121  func EnlargeNode(client *golangsdk.ServiceClient, instanceId string, opts EnlargeNodeBuilder) (r ExtendResult) {
   122  	b, err := opts.ToNodeEnlargeMap()
   123  	if err != nil {
   124  		r.Err = err
   125  		return
   126  	}
   127  
   128  	_, r.Err = client.Post(enlargeNodeURL(client, instanceId), b, &r.Body, &golangsdk.RequestOpts{
   129  		OkCodes:     []int{202},
   130  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   131  	})
   132  	return
   133  }
   134  
   135  type ReduceNodeOpts struct {
   136  	Num int `json:"num" required:"true"`
   137  }
   138  
   139  type ReduceNodeBuilder interface {
   140  	ToNodeReduceMap() (map[string]interface{}, error)
   141  }
   142  
   143  func (opts ReduceNodeOpts) ToNodeReduceMap() (map[string]interface{}, error) {
   144  	b, err := golangsdk.BuildRequestBody(opts, "")
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  	return b, nil
   149  }
   150  
   151  func ReduceNode(client *golangsdk.ServiceClient, instanceId string, opts ReduceNodeBuilder) (r ExtendResult) {
   152  	b, err := opts.ToNodeReduceMap()
   153  	if err != nil {
   154  		r.Err = err
   155  		return
   156  	}
   157  
   158  	_, r.Err = client.Post(reduceNodeURL(client, instanceId), b, &r.Body, &golangsdk.RequestOpts{
   159  		OkCodes:     []int{202},
   160  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   161  	})
   162  	return
   163  }
   164  
   165  func Delete(client *golangsdk.ServiceClient, instanceId string) (r DeleteResult) {
   166  	url := deleteURL(client, instanceId)
   167  
   168  	_, r.Err = client.Delete(url, &golangsdk.RequestOpts{
   169  		OkCodes:     []int{202},
   170  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   171  	})
   172  
   173  	return
   174  }
   175  
   176  type ListGeminiDBInstanceOpts struct {
   177  	Id            string `q:"id"`
   178  	Name          string `q:"name"`
   179  	Mode          string `q:"mode"`
   180  	DataStoreType string `q:"datastore_type"`
   181  	VpcId         string `q:"vpc_id"`
   182  	SubnetId      string `q:"subnet_id"`
   183  	Offset        int    `q:"offset"`
   184  	Limit         int    `q:"limit"`
   185  }
   186  
   187  type ListGeminiDBBuilder interface {
   188  	ToGeminiDBListDetailQuery() (string, error)
   189  }
   190  
   191  func (opts ListGeminiDBInstanceOpts) ToGeminiDBListDetailQuery() (string, error) {
   192  	q, err := golangsdk.BuildQueryString(opts)
   193  	if err != nil {
   194  		return "", err
   195  	}
   196  	return q.String(), err
   197  }
   198  
   199  func List(client *golangsdk.ServiceClient, opts ListGeminiDBBuilder) pagination.Pager {
   200  	url := listURL(client)
   201  	if opts != nil {
   202  		query, err := opts.ToGeminiDBListDetailQuery()
   203  
   204  		if err != nil {
   205  			return pagination.Pager{Err: err}
   206  		}
   207  		url += query
   208  	}
   209  
   210  	pageList := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   211  		return GeminiDBPage{pagination.SinglePageBase(r)}
   212  	})
   213  	// Headers supplies additional HTTP headers to populate on each paged request
   214  	pageList.Headers = map[string]string{"Content-Type": "application/json"}
   215  
   216  	return pageList
   217  }
   218  
   219  func GetInstanceByID(client *golangsdk.ServiceClient, instanceId string) (GeminiDBInstance, error) {
   220  	var instance GeminiDBInstance
   221  
   222  	opts := ListGeminiDBInstanceOpts{
   223  		Id: instanceId,
   224  	}
   225  
   226  	pages, err := List(client, &opts).AllPages()
   227  	if err != nil {
   228  		return instance, err
   229  	}
   230  
   231  	all, err := ExtractGeminiDBInstances(pages)
   232  	if err != nil {
   233  		return instance, err
   234  	}
   235  	if all.TotalCount == 0 {
   236  		return instance, nil
   237  	}
   238  
   239  	instance = all.Instances[0]
   240  	return instance, nil
   241  }
   242  
   243  func GetInstanceByName(client *golangsdk.ServiceClient, name string) (GeminiDBInstance, error) {
   244  	var instance GeminiDBInstance
   245  
   246  	opts := ListGeminiDBInstanceOpts{
   247  		Name: name,
   248  	}
   249  
   250  	pages, err := List(client, &opts).AllPages()
   251  	if err != nil {
   252  		return instance, err
   253  	}
   254  
   255  	all, err := ExtractGeminiDBInstances(pages)
   256  	if err != nil {
   257  		return instance, err
   258  	}
   259  	if all.TotalCount == 0 {
   260  		return instance, nil
   261  	}
   262  
   263  	instance = all.Instances[0]
   264  	return instance, nil
   265  }
   266  
   267  type UpdateNameOpts struct {
   268  	Name string `json:"name" required:"true"`
   269  }
   270  
   271  type UpdateNameBuilder interface {
   272  	ToNameUpdateMap() (map[string]interface{}, error)
   273  }
   274  
   275  func (opts UpdateNameOpts) ToNameUpdateMap() (map[string]interface{}, error) {
   276  	b, err := golangsdk.BuildRequestBody(opts, "")
   277  	if err != nil {
   278  		return nil, err
   279  	}
   280  	return b, nil
   281  }
   282  
   283  func UpdateName(client *golangsdk.ServiceClient, instanceId string, opts UpdateNameBuilder) (r UpdateResult) {
   284  	b, err := opts.ToNameUpdateMap()
   285  	if err != nil {
   286  		r.Err = err
   287  		return
   288  	}
   289  
   290  	_, r.Err = client.Put(updateNameURL(client, instanceId), b, nil, &golangsdk.RequestOpts{
   291  		OkCodes:     []int{204},
   292  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   293  	})
   294  	return
   295  }
   296  
   297  type UpdatePassOpts struct {
   298  	Password string `json:"password" required:"true"`
   299  }
   300  
   301  type UpdatePassBuilder interface {
   302  	ToPassUpdateMap() (map[string]interface{}, error)
   303  }
   304  
   305  func (opts UpdatePassOpts) ToPassUpdateMap() (map[string]interface{}, error) {
   306  	b, err := golangsdk.BuildRequestBody(opts, "")
   307  	if err != nil {
   308  		return nil, err
   309  	}
   310  	return b, nil
   311  }
   312  
   313  func UpdatePass(client *golangsdk.ServiceClient, instanceId string, opts UpdatePassBuilder) (r UpdateResult) {
   314  	b, err := opts.ToPassUpdateMap()
   315  	if err != nil {
   316  		r.Err = err
   317  		return
   318  	}
   319  
   320  	_, r.Err = client.Put(updatePassURL(client, instanceId), b, nil, &golangsdk.RequestOpts{
   321  		OkCodes:     []int{204},
   322  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   323  	})
   324  	return
   325  }
   326  
   327  type ResizeOpt struct {
   328  	InstanceID string `json:"target_id" required:"true"`
   329  	SpecCode   string `json:"target_spec_code" required:"true"`
   330  }
   331  
   332  type ResizeOpts struct {
   333  	Resize    ResizeOpt `json:"resize" required:"true"`
   334  	IsAutoPay string    `json:"is_auto_pay,omitempty"`
   335  }
   336  
   337  type ResizeBuilder interface {
   338  	ToResizeMap() (map[string]interface{}, error)
   339  }
   340  
   341  func (opts ResizeOpts) ToResizeMap() (map[string]interface{}, error) {
   342  	b, err := golangsdk.BuildRequestBody(opts, "")
   343  	if err != nil {
   344  		return nil, err
   345  	}
   346  	return b, nil
   347  }
   348  
   349  func Resize(client *golangsdk.ServiceClient, instanceId string, opts ResizeBuilder) (r ExtendResult) {
   350  	b, err := opts.ToResizeMap()
   351  	if err != nil {
   352  		r.Err = err
   353  		return
   354  	}
   355  
   356  	_, r.Err = client.Put(resizeURL(client, instanceId), b, &r.Body, &golangsdk.RequestOpts{
   357  		OkCodes:     []int{202},
   358  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   359  	})
   360  	return
   361  }
   362  
   363  type UpdateSgOpts struct {
   364  	SecurityGroupID string `json:"security_group_id" required:"true"`
   365  }
   366  
   367  type UpdateSgBuilder interface {
   368  	ToSgUpdateMap() (map[string]interface{}, error)
   369  }
   370  
   371  func (opts UpdateSgOpts) ToSgUpdateMap() (map[string]interface{}, error) {
   372  	b, err := golangsdk.BuildRequestBody(opts, "")
   373  	if err != nil {
   374  		return nil, err
   375  	}
   376  	return b, nil
   377  }
   378  
   379  func UpdateSg(client *golangsdk.ServiceClient, instanceId string, opts UpdateSgBuilder) (r ExtendResult) {
   380  	b, err := opts.ToSgUpdateMap()
   381  	if err != nil {
   382  		r.Err = err
   383  		return
   384  	}
   385  
   386  	_, r.Err = client.Put(updateSgURL(client, instanceId), b, &r.Body, &golangsdk.RequestOpts{
   387  		OkCodes:     []int{202},
   388  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   389  	})
   390  	return
   391  }
   392  
   393  func ListDeh(client *golangsdk.ServiceClient) pagination.Pager {
   394  	pageList := pagination.NewPager(client, listDehURL(client), func(r pagination.PageResult) pagination.Page {
   395  		return DehResourcePage{pagination.SinglePageBase(r)}
   396  	})
   397  	// Headers supplies additional HTTP headers to populate on each paged request
   398  	pageList.Headers = map[string]string{"Content-Type": "application/json"}
   399  
   400  	return pageList
   401  }