github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/taurusdb/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  var requestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{
     9  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    10  }
    11  
    12  type ChargeInfoOpt struct {
    13  	ChargingMode string `json:"charge_mode,omitempty"`
    14  	PeriodType   string `json:"period_type,omitempty"`
    15  	PeriodNum    int    `json:"period_num,omitempty"`
    16  	IsAutoRenew  string `json:"is_auto_renew,omitempty"`
    17  	IsAutoPay    string `json:"is_auto_pay,omitempty"`
    18  }
    19  
    20  type VolumeOpt struct {
    21  	Size int `json:"size" required:"true"`
    22  }
    23  
    24  type DataStoreOpt struct {
    25  	Type    string `json:"type" required:"true"`
    26  	Version string `json:"version" required:"true"`
    27  }
    28  
    29  type BackupStrategyOpt struct {
    30  	StartTime string `json:"start_time" required:"true"`
    31  	KeepDays  string `json:"keep_days" required:"true"`
    32  }
    33  
    34  type CreateTaurusDBOpts struct {
    35  	Name                string             `json:"name" required:"true"`
    36  	Region              string             `json:"region" required:"true"`
    37  	Mode                string             `json:"mode" required:"true"`
    38  	Flavor              string             `json:"flavor_ref" required:"true"`
    39  	VpcId               string             `json:"vpc_id" required:"true"`
    40  	SubnetId            string             `json:"subnet_id" required:"true"`
    41  	SecurityGroupId     string             `json:"security_group_id,omitempty"`
    42  	Password            string             `json:"password" required:"true"`
    43  	TimeZone            string             `json:"time_zone" required:"true"`
    44  	AZMode              string             `json:"availability_zone_mode" required:"true"`
    45  	SlaveCount          int                `json:"slave_count" required:"true"`
    46  	MasterAZ            string             `json:"master_availability_zone,omitempty"`
    47  	ConfigurationId     string             `json:"configuration_id,omitempty"`
    48  	EnterpriseProjectId string             `json:"enterprise_project_id,omitempty"`
    49  	DedicatedResourceId string             `json:"dedicated_resource_id,omitempty"`
    50  	LowerCaseTableNames *int               `json:"lower_case_table_names,omitempty"`
    51  	DataStore           DataStoreOpt       `json:"datastore" required:"true"`
    52  	BackupStrategy      *BackupStrategyOpt `json:"backup_strategy,omitempty"`
    53  	ChargeInfo          *ChargeInfoOpt     `json:"charge_info,omitempty"`
    54  	Volume              *VolumeOpt         `json:"volume,omitempty"`
    55  }
    56  
    57  type CreateTaurusDBBuilder interface {
    58  	ToInstancesCreateMap() (map[string]interface{}, error)
    59  }
    60  
    61  func (opts CreateTaurusDBOpts) ToInstancesCreateMap() (map[string]interface{}, error) {
    62  	b, err := golangsdk.BuildRequestBody(opts, "")
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	return b, nil
    67  }
    68  
    69  func Create(client *golangsdk.ServiceClient, opts CreateTaurusDBBuilder) (r CreateResult) {
    70  	b, err := opts.ToInstancesCreateMap()
    71  	if err != nil {
    72  		r.Err = err
    73  		return
    74  	}
    75  
    76  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
    77  		OkCodes:     []int{201, 202},
    78  		MoreHeaders: requestOpts.MoreHeaders,
    79  	})
    80  
    81  	return
    82  }
    83  
    84  type CreateReplicaOpts struct {
    85  	Priorities []int  `json:"priorities" required:"true"`
    86  	IsAutoPay  string `json:"is_auto_pay,omitempty"`
    87  }
    88  
    89  type CreateReplicaBuilder interface {
    90  	ToReplicaCreateMap() (map[string]interface{}, error)
    91  }
    92  
    93  func (opts CreateReplicaOpts) ToReplicaCreateMap() (map[string]interface{}, error) {
    94  	b, err := golangsdk.BuildRequestBody(opts, "")
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	return b, nil
    99  }
   100  
   101  func CreateReplica(client *golangsdk.ServiceClient, instanceId string, opts CreateReplicaBuilder) (r JobResult) {
   102  	b, err := opts.ToReplicaCreateMap()
   103  	if err != nil {
   104  		r.Err = err
   105  		return
   106  	}
   107  
   108  	_, r.Err = client.Post(enlargeURL(client, instanceId), b, &r.Body, &golangsdk.RequestOpts{
   109  		OkCodes:     []int{201, 202},
   110  		MoreHeaders: requestOpts.MoreHeaders,
   111  	})
   112  
   113  	return
   114  }
   115  
   116  func DeleteReplica(client *golangsdk.ServiceClient, instanceId, nodeId string) (r JobResult) {
   117  	url := deleteReplicaURL(client, instanceId, nodeId)
   118  
   119  	_, r.Err = client.DeleteWithResponse(url, &r.Body, &golangsdk.RequestOpts{
   120  		OkCodes:     []int{200, 202},
   121  		MoreHeaders: requestOpts.MoreHeaders,
   122  	})
   123  
   124  	return
   125  }
   126  
   127  func Delete(client *golangsdk.ServiceClient, instanceId string) (r DeleteResult) {
   128  	url := deleteURL(client, instanceId)
   129  
   130  	_, r.Err = client.Delete(url, &golangsdk.RequestOpts{
   131  		OkCodes:     []int{200, 202},
   132  		MoreHeaders: requestOpts.MoreHeaders,
   133  	})
   134  
   135  	return
   136  }
   137  
   138  func Get(client *golangsdk.ServiceClient, instanceId string) (r GetResult) {
   139  	url := getURL(client, instanceId)
   140  
   141  	_, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{
   142  		MoreHeaders: requestOpts.MoreHeaders,
   143  	})
   144  
   145  	return
   146  }
   147  
   148  type ListTaurusDBInstanceOpts struct {
   149  	Id            string `q:"id"`
   150  	Name          string `q:"name"`
   151  	Type          string `q:"type"`
   152  	DataStoreType string `q:"datastore_type"`
   153  	VpcId         string `q:"vpc_id"`
   154  	SubnetId      string `q:"subnet_id"`
   155  	Offset        int    `q:"offset"`
   156  	Limit         int    `q:"limit"`
   157  }
   158  
   159  type ListTaurusDBBuilder interface {
   160  	ToTaurusDBListDetailQuery() (string, error)
   161  }
   162  
   163  func (opts ListTaurusDBInstanceOpts) ToTaurusDBListDetailQuery() (string, error) {
   164  	q, err := golangsdk.BuildQueryString(opts)
   165  	if err != nil {
   166  		return "", err
   167  	}
   168  	return q.String(), err
   169  }
   170  
   171  func List(client *golangsdk.ServiceClient, opts ListTaurusDBBuilder) pagination.Pager {
   172  	url := listURL(client)
   173  	if opts != nil {
   174  		query, err := opts.ToTaurusDBListDetailQuery()
   175  
   176  		if err != nil {
   177  			return pagination.Pager{Err: err}
   178  		}
   179  		url += query
   180  	}
   181  
   182  	pageList := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   183  		return TaurusDBPage{pagination.SinglePageBase(r)}
   184  	})
   185  	// Headers supplies additional HTTP headers to populate on each paged request
   186  	pageList.Headers = requestOpts.MoreHeaders
   187  
   188  	return pageList
   189  }
   190  
   191  func GetInstanceByName(client *golangsdk.ServiceClient, name string) (TaurusDBInstance, error) {
   192  	var instance TaurusDBInstance
   193  
   194  	opts := ListTaurusDBInstanceOpts{
   195  		Name: name,
   196  	}
   197  
   198  	pages, err := List(client, &opts).AllPages()
   199  	if err != nil {
   200  		return instance, err
   201  	}
   202  
   203  	all, err := ExtractTaurusDBInstances(pages)
   204  	if err != nil {
   205  		return instance, err
   206  	}
   207  	if all.TotalCount == 0 {
   208  		return instance, nil
   209  	}
   210  
   211  	instance = all.Instances[0]
   212  	return instance, nil
   213  }
   214  
   215  type UpdateNameOpts struct {
   216  	Name string `json:"name" required:"true"`
   217  }
   218  
   219  type UpdateNameBuilder interface {
   220  	ToNameUpdateMap() (map[string]interface{}, error)
   221  }
   222  
   223  func (opts UpdateNameOpts) ToNameUpdateMap() (map[string]interface{}, error) {
   224  	b, err := golangsdk.BuildRequestBody(opts, "")
   225  	if err != nil {
   226  		return nil, err
   227  	}
   228  	return b, nil
   229  }
   230  
   231  func UpdateName(client *golangsdk.ServiceClient, instanceId string, opts UpdateNameBuilder) (r JobResult) {
   232  	b, err := opts.ToNameUpdateMap()
   233  	if err != nil {
   234  		r.Err = err
   235  		return
   236  	}
   237  
   238  	_, r.Err = client.Put(nameURL(client, instanceId), b, &r.Body, &golangsdk.RequestOpts{
   239  		OkCodes:     []int{200},
   240  		MoreHeaders: requestOpts.MoreHeaders,
   241  	})
   242  
   243  	return
   244  }
   245  
   246  type UpdatePassOpts struct {
   247  	Password string `json:"password" required:"true"`
   248  }
   249  
   250  type UpdatePassBuilder interface {
   251  	ToPassUpdateMap() (map[string]interface{}, error)
   252  }
   253  
   254  func (opts UpdatePassOpts) ToPassUpdateMap() (map[string]interface{}, error) {
   255  	b, err := golangsdk.BuildRequestBody(opts, "")
   256  	if err != nil {
   257  		return nil, err
   258  	}
   259  	return b, nil
   260  }
   261  
   262  func UpdatePass(client *golangsdk.ServiceClient, instanceId string, opts UpdatePassBuilder) (r JobResult) {
   263  	b, err := opts.ToPassUpdateMap()
   264  	if err != nil {
   265  		r.Err = err
   266  		return
   267  	}
   268  
   269  	_, r.Err = client.Post(passwordURL(client, instanceId), b, &r.Body, &golangsdk.RequestOpts{
   270  		OkCodes:     []int{200},
   271  		MoreHeaders: requestOpts.MoreHeaders,
   272  	})
   273  
   274  	return
   275  }
   276  
   277  type ExtendVolumeOpts struct {
   278  	Size      int    `json:"size" required:"true"`
   279  	IsAutoPay string `json:"is_auto_pay,omitempty"`
   280  }
   281  
   282  type ExtendVolumeBuilder interface {
   283  	ToVolumeExtendMap() (map[string]interface{}, error)
   284  }
   285  
   286  func (opts ExtendVolumeOpts) ToVolumeExtendMap() (map[string]interface{}, error) {
   287  	b, err := golangsdk.BuildRequestBody(opts, "")
   288  	if err != nil {
   289  		return nil, err
   290  	}
   291  	return b, nil
   292  }
   293  
   294  func ExtendVolume(client *golangsdk.ServiceClient, instanceId string, opts ExtendVolumeBuilder) (r JobResult) {
   295  	b, err := opts.ToVolumeExtendMap()
   296  	if err != nil {
   297  		r.Err = err
   298  		return
   299  	}
   300  
   301  	_, r.Err = client.Post(volumeURL(client, instanceId), b, &r.Body, &golangsdk.RequestOpts{
   302  		OkCodes:     []int{201},
   303  		MoreHeaders: requestOpts.MoreHeaders,
   304  	})
   305  
   306  	return
   307  }
   308  
   309  type ResizeOpt struct {
   310  	Spec string `json:"spec_code" required:"true"`
   311  }
   312  
   313  type ResizeOpts struct {
   314  	Resize    ResizeOpt `json:"resize_flavor" required:"true"`
   315  	IsAutoPay string    `json:"is_auto_pay,omitempty"`
   316  }
   317  
   318  type ResizeBuilder interface {
   319  	ToResizeMap() (map[string]interface{}, error)
   320  }
   321  
   322  func (opts ResizeOpts) ToResizeMap() (map[string]interface{}, error) {
   323  	b, err := golangsdk.BuildRequestBody(opts, "")
   324  	if err != nil {
   325  		return nil, err
   326  	}
   327  	return b, nil
   328  }
   329  
   330  func Resize(client *golangsdk.ServiceClient, instanceId string, opts ResizeBuilder) (r JobResult) {
   331  	b, err := opts.ToResizeMap()
   332  	if err != nil {
   333  		r.Err = err
   334  		return
   335  	}
   336  
   337  	_, r.Err = client.Post(actionURL(client, instanceId), b, &r.Body, &golangsdk.RequestOpts{
   338  		OkCodes:     []int{200},
   339  		MoreHeaders: requestOpts.MoreHeaders,
   340  	})
   341  
   342  	return
   343  }
   344  
   345  type ProxyOpts struct {
   346  	Flavor  string `json:"flavor_ref" required:"true"`
   347  	NodeNum int    `json:"node_num" required:"true"`
   348  }
   349  
   350  type ProxyBuilder interface {
   351  	ToProxyMap() (map[string]interface{}, error)
   352  }
   353  
   354  func (opts ProxyOpts) ToProxyMap() (map[string]interface{}, error) {
   355  	b, err := golangsdk.BuildRequestBody(opts, "")
   356  	if err != nil {
   357  		return nil, err
   358  	}
   359  	return b, nil
   360  }
   361  
   362  func EnableProxy(client *golangsdk.ServiceClient, instanceId string, opts ProxyBuilder) (r JobResult) {
   363  	b, err := opts.ToProxyMap()
   364  	if err != nil {
   365  		r.Err = err
   366  		return
   367  	}
   368  
   369  	_, r.Err = client.Post(proxyURL(client, instanceId), b, &r.Body, &golangsdk.RequestOpts{
   370  		OkCodes:     []int{201},
   371  		MoreHeaders: requestOpts.MoreHeaders,
   372  	})
   373  
   374  	return
   375  }
   376  
   377  type EnlargeProxyOpts struct {
   378  	NodeNum int `json:"node_num" required:"true"`
   379  }
   380  
   381  type EnlargeProxyBuilder interface {
   382  	ToEnlargeProxyMap() (map[string]interface{}, error)
   383  }
   384  
   385  func (opts EnlargeProxyOpts) ToEnlargeProxyMap() (map[string]interface{}, error) {
   386  	b, err := golangsdk.BuildRequestBody(opts, "")
   387  	if err != nil {
   388  		return nil, err
   389  	}
   390  	return b, nil
   391  }
   392  
   393  func EnlargeProxy(client *golangsdk.ServiceClient, instanceId string, opts EnlargeProxyBuilder) (r JobResult) {
   394  	b, err := opts.ToEnlargeProxyMap()
   395  	if err != nil {
   396  		r.Err = err
   397  		return
   398  	}
   399  
   400  	_, r.Err = client.Post(proxyEnlargeURL(client, instanceId), b, &r.Body, &golangsdk.RequestOpts{
   401  		OkCodes:     []int{201},
   402  		MoreHeaders: requestOpts.MoreHeaders,
   403  	})
   404  
   405  	return
   406  }
   407  
   408  func DeleteProxy(client *golangsdk.ServiceClient, instanceId string) (r JobResult) {
   409  	url := proxyURL(client, instanceId)
   410  
   411  	_, r.Err = client.DeleteWithResponse(url, &r.Body, &golangsdk.RequestOpts{
   412  		OkCodes:     []int{200, 202},
   413  		MoreHeaders: requestOpts.MoreHeaders,
   414  	})
   415  
   416  	return
   417  }
   418  
   419  func GetProxy(client *golangsdk.ServiceClient, instanceId string) (r GetProxyResult) {
   420  	url := proxyURL(client, instanceId)
   421  
   422  	_, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{
   423  		MoreHeaders: requestOpts.MoreHeaders,
   424  	})
   425  
   426  	return
   427  }
   428  
   429  func ListDeh(client *golangsdk.ServiceClient) pagination.Pager {
   430  	pageList := pagination.NewPager(client, listDehURL(client), func(r pagination.PageResult) pagination.Page {
   431  		return DehResourcePage{pagination.SinglePageBase(r)}
   432  	})
   433  	// Headers supplies additional HTTP headers to populate on each paged request
   434  	pageList.Headers = map[string]string{"Content-Type": "application/json"}
   435  
   436  	return pageList
   437  }