github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/rds/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 CreateOpts struct {
     9  	Name                string          `json:"name"  required:"true"`
    10  	Datastore           *Datastore      `json:"datastore" required:"true"`
    11  	Ha                  *Ha             `json:"ha,omitempty"`
    12  	ConfigurationId     string          `json:"configuration_id,omitempty"`
    13  	Port                string          `json:"port,omitempty"`
    14  	Password            string          `json:"password" required:"true"`
    15  	BackupStrategy      *BackupStrategy `json:"backup_strategy,omitempty"`
    16  	EnterpriseProjectId string          `json:"enterprise_project_id,omitempty"`
    17  	DiskEncryptionId    string          `json:"disk_encryption_id,omitempty"`
    18  	FlavorRef           string          `json:"flavor_ref" required:"true"`
    19  	Volume              *Volume         `json:"volume" required:"true"`
    20  	Region              string          `json:"region" required:"true"`
    21  	AvailabilityZone    string          `json:"availability_zone" required:"true"`
    22  	VpcId               string          `json:"vpc_id" required:"true"`
    23  	SubnetId            string          `json:"subnet_id" required:"true"`
    24  	SecurityGroupId     string          `json:"security_group_id" required:"true"`
    25  	ChargeInfo          *ChargeInfo     `json:"charge_info,omitempty"`
    26  	TimeZone            string          `json:"time_zone,omitempty"`
    27  	FixedIp             string          `json:"data_vip,omitempty"`
    28  }
    29  
    30  type CreateReplicaOpts struct {
    31  	Name                string      `json:"name"  required:"true"`
    32  	ReplicaOfId         string      `json:"replica_of_id" required:"true"`
    33  	EnterpriseProjectId string      `json:"enterprise_project_id,omitempty"`
    34  	DiskEncryptionId    string      `json:"disk_encryption_id,omitempty"`
    35  	FlavorRef           string      `json:"flavor_ref" required:"true"`
    36  	Volume              *Volume     `json:"volume" required:"true"`
    37  	Region              string      `json:"region,omitempty"`
    38  	AvailabilityZone    string      `json:"availability_zone" required:"true"`
    39  	ChargeInfo          *ChargeInfo `json:"charge_info,omitempty"`
    40  }
    41  
    42  type Datastore struct {
    43  	Type    string `json:"type" required:"true"`
    44  	Version string `json:"version" required:"true"`
    45  }
    46  
    47  type Ha struct {
    48  	Mode            string `json:"mode" required:"true"`
    49  	ReplicationMode string `json:"replication_mode,omitempty"`
    50  }
    51  
    52  type BackupStrategy struct {
    53  	StartTime string `json:"start_time" required:"true"`
    54  	KeepDays  int    `json:"keep_days,omitempty"`
    55  }
    56  
    57  type Volume struct {
    58  	Type string `json:"type" required:"true"`
    59  	Size int    `json:"size" required:"true"`
    60  }
    61  
    62  type ChargeInfo struct {
    63  	ChargeMode  string `json:"charge_mode" required:"true"`
    64  	PeriodType  string `json:"period_type,omitempty"`
    65  	PeriodNum   int    `json:"period_num,omitempty"`
    66  	IsAutoRenew string `json:"is_auto_renew,omitempty"`
    67  	IsAutoPay   string `json:"is_auto_pay,omitempty"`
    68  }
    69  
    70  type CreateRdsBuilder interface {
    71  	ToInstancesCreateMap() (map[string]interface{}, error)
    72  }
    73  
    74  func (opts CreateOpts) ToInstancesCreateMap() (map[string]interface{}, error) {
    75  	b, err := golangsdk.BuildRequestBody(opts, "")
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	return b, nil
    80  }
    81  
    82  func Create(client *golangsdk.ServiceClient, opts CreateRdsBuilder) (r CreateResult) {
    83  	b, err := opts.ToInstancesCreateMap()
    84  	if err != nil {
    85  		r.Err = err
    86  		return
    87  	}
    88  
    89  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
    90  		OkCodes: []int{200, 202},
    91  	})
    92  	return
    93  }
    94  
    95  func (opts CreateReplicaOpts) ToInstancesCreateMap() (map[string]interface{}, error) {
    96  	b, err := golangsdk.BuildRequestBody(opts, "")
    97  	if err != nil {
    98  		return nil, err
    99  	}
   100  	return b, nil
   101  }
   102  
   103  func CreateReplica(client *golangsdk.ServiceClient, opts CreateRdsBuilder) (r CreateResult) {
   104  	b, err := opts.ToInstancesCreateMap()
   105  	if err != nil {
   106  		r.Err = err
   107  		return
   108  	}
   109  
   110  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
   111  		OkCodes: []int{202},
   112  	})
   113  
   114  	return
   115  }
   116  
   117  type DeleteOpts struct {
   118  	InstanceId string `json:"instance_id" required:"true"`
   119  }
   120  
   121  type DeleteInstanceBuilder interface {
   122  	ToInstancesDeleteMap() (map[string]interface{}, error)
   123  }
   124  
   125  func (opts DeleteOpts) ToInstancesDeleteMap() (map[string]interface{}, error) {
   126  	b, err := golangsdk.BuildRequestBody(&opts, "")
   127  	if err != nil {
   128  		return nil, err
   129  	}
   130  	return b, nil
   131  }
   132  
   133  func Delete(client *golangsdk.ServiceClient, instanceId string) (r DeleteResult) {
   134  
   135  	url := deleteURL(client, instanceId)
   136  
   137  	_, r.Err = client.Delete(url, &golangsdk.RequestOpts{
   138  		JSONResponse: &r.Body,
   139  		MoreHeaders:  map[string]string{"Content-Type": "application/json"},
   140  	})
   141  	return
   142  }
   143  
   144  type ListOpts struct {
   145  	Id            string `q:"id"`
   146  	Name          string `q:"name"`
   147  	Type          string `q:"type"`
   148  	DataStoreType string `q:"datastore_type"`
   149  	VpcId         string `q:"vpc_id"`
   150  	SubnetId      string `q:"subnet_id"`
   151  	Offset        int    `q:"offset"`
   152  	Limit         int    `q:"limit"`
   153  }
   154  
   155  type ListRdsBuilder interface {
   156  	ToRdsListDetailQuery() (string, error)
   157  }
   158  
   159  func (opts ListOpts) ToRdsListDetailQuery() (string, error) {
   160  	q, err := golangsdk.BuildQueryString(opts)
   161  	if err != nil {
   162  		return "", err
   163  	}
   164  	return q.String(), err
   165  }
   166  
   167  func List(client *golangsdk.ServiceClient, opts ListRdsBuilder) pagination.Pager {
   168  	url := listURL(client)
   169  	if opts != nil {
   170  		query, err := opts.ToRdsListDetailQuery()
   171  
   172  		if err != nil {
   173  			return pagination.Pager{Err: err}
   174  		}
   175  		url += query
   176  	}
   177  
   178  	pageRdsList := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   179  		return RdsPage{pagination.SinglePageBase(r)}
   180  	})
   181  
   182  	rdsheader := map[string]string{"Content-Type": "application/json"}
   183  	pageRdsList.Headers = rdsheader
   184  	return pageRdsList
   185  }
   186  
   187  type ActionInstanceBuilder interface {
   188  	ToActionInstanceMap() (map[string]interface{}, error)
   189  }
   190  
   191  func toActionInstanceMap(opts ActionInstanceBuilder) (map[string]interface{}, error) {
   192  	b, err := golangsdk.BuildRequestBody(opts, "")
   193  	if err != nil {
   194  		return nil, err
   195  	}
   196  	return b, nil
   197  }
   198  
   199  type RestartInstanceOpts struct {
   200  	Restart string `json:"restart" required:"true"`
   201  }
   202  
   203  func (opts RestartInstanceOpts) ToActionInstanceMap() (map[string]interface{}, error) {
   204  	return toActionInstanceMap(opts)
   205  }
   206  
   207  func Restart(client *golangsdk.ServiceClient, opts ActionInstanceBuilder, instanceId string) (r RestartResult) {
   208  	b, err := opts.ToActionInstanceMap()
   209  	if err != nil {
   210  		r.Err = err
   211  		return
   212  	}
   213  
   214  	_, r.Err = client.Post(updateURL(client, instanceId, "action"), b, &r.Body, &golangsdk.RequestOpts{
   215  		OkCodes: []int{202},
   216  	})
   217  	return
   218  }
   219  
   220  type RenameInstanceOpts struct {
   221  	Name string `json:"name" required:"true"`
   222  }
   223  
   224  func (opts RenameInstanceOpts) ToActionInstanceMap() (map[string]interface{}, error) {
   225  	return toActionInstanceMap(opts)
   226  }
   227  
   228  func Rename(client *golangsdk.ServiceClient, opts ActionInstanceBuilder, instanceId string) (r RenameResult) {
   229  	b, err := opts.ToActionInstanceMap()
   230  	if err != nil {
   231  		r.Err = err
   232  		return
   233  	}
   234  	_, r.Err = client.Put(updateURL(client, instanceId, "name"), b, nil, &golangsdk.RequestOpts{
   235  		OkCodes: []int{200, 202},
   236  	})
   237  	return
   238  }
   239  
   240  type SingleToHaRdsOpts struct {
   241  	SingleToHa *SingleToHaRds `json:"single_to_ha" required:"true"`
   242  }
   243  
   244  type SingleToHaRds struct {
   245  	AzCodeNewNode string `json:"az_code_new_node" required:"true"`
   246  	Password      string `json:"password,omitempty"`
   247  }
   248  
   249  func (opts SingleToHaRdsOpts) ToActionInstanceMap() (map[string]interface{}, error) {
   250  	return toActionInstanceMap(opts)
   251  }
   252  
   253  func SingleToHa(client *golangsdk.ServiceClient, opts ActionInstanceBuilder, instanceId string) (r SingleToHaResult) {
   254  	b, err := opts.ToActionInstanceMap()
   255  	if err != nil {
   256  		r.Err = err
   257  		return
   258  	}
   259  	_, r.Err = client.Post(updateURL(client, instanceId, "action"), b, &r.Body, &golangsdk.RequestOpts{
   260  		OkCodes: []int{202},
   261  	})
   262  
   263  	return
   264  }
   265  
   266  type SpecCode struct {
   267  	Speccode string `json:"spec_code" required:"true"`
   268  }
   269  
   270  type ResizeFlavorOpts struct {
   271  	ResizeFlavor *SpecCode `json:"resize_flavor" required:"true"`
   272  }
   273  
   274  func (opts ResizeFlavorOpts) ToActionInstanceMap() (map[string]interface{}, error) {
   275  	return toActionInstanceMap(opts)
   276  }
   277  
   278  func Resize(client *golangsdk.ServiceClient, opts ActionInstanceBuilder, instanceId string) (r ResizeFlavorResult) {
   279  	b, err := opts.ToActionInstanceMap()
   280  	if err != nil {
   281  		r.Err = err
   282  		return
   283  	}
   284  	_, r.Err = client.Post(updateURL(client, instanceId, "action"), b, &r.Body, &golangsdk.RequestOpts{
   285  		OkCodes: []int{202},
   286  	})
   287  
   288  	return
   289  }
   290  
   291  type EnlargeVolumeOpts struct {
   292  	EnlargeVolume *EnlargeVolumeSize `json:"enlarge_volume" required:"true"`
   293  }
   294  
   295  type EnlargeVolumeSize struct {
   296  	Size int `json:"size" required:"true"`
   297  }
   298  
   299  func (opts EnlargeVolumeOpts) ToActionInstanceMap() (map[string]interface{}, error) {
   300  	return toActionInstanceMap(opts)
   301  }
   302  
   303  func EnlargeVolume(client *golangsdk.ServiceClient, opts ActionInstanceBuilder, instanceId string) (r EnlargeVolumeResult) {
   304  	b, err := opts.ToActionInstanceMap()
   305  	if err != nil {
   306  		r.Err = err
   307  		return
   308  	}
   309  	_, r.Err = client.Post(updateURL(client, instanceId, "action"), b, &r.Body, &golangsdk.RequestOpts{
   310  		OkCodes: []int{202},
   311  	})
   312  
   313  	return
   314  }
   315  
   316  type DbErrorlogOpts struct {
   317  	StartDate string `q:"start_date"`
   318  	EndDate   string `q:"end_date"`
   319  	Offset    string `q:"offset"`
   320  	Limit     string `q:"limit"`
   321  	Level     string `q:"level"`
   322  }
   323  
   324  type DbErrorlogBuilder interface {
   325  	DbErrorlogQuery() (string, error)
   326  }
   327  
   328  func (opts DbErrorlogOpts) DbErrorlogQuery() (string, error) {
   329  	q, err := golangsdk.BuildQueryString(opts)
   330  	if err != nil {
   331  		return "", err
   332  	}
   333  	return q.String(), err
   334  }
   335  
   336  func ListErrorLog(client *golangsdk.ServiceClient, opts DbErrorlogBuilder, instanceID string) pagination.Pager {
   337  	url := updateURL(client, instanceID, "errorlog")
   338  	if opts != nil {
   339  		query, err := opts.DbErrorlogQuery()
   340  
   341  		if err != nil {
   342  			return pagination.Pager{Err: err}
   343  		}
   344  		url += query
   345  	}
   346  
   347  	pageRdsList := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   348  		return ErrorLogPage{pagination.SinglePageBase(r)}
   349  	})
   350  
   351  	rdsheader := map[string]string{"Content-Type": "application/json"}
   352  	pageRdsList.Headers = rdsheader
   353  	return pageRdsList
   354  }
   355  
   356  type DbSlowLogOpts struct {
   357  	StartDate string `q:"start_date"`
   358  	EndDate   string `q:"end_date"`
   359  	Offset    string `q:"offset"`
   360  	Limit     string `q:"limit"`
   361  	Level     string `q:"level"`
   362  }
   363  
   364  type DbSlowLogBuilder interface {
   365  	ToDbSlowLogListQuery() (string, error)
   366  }
   367  
   368  func (opts DbSlowLogOpts) ToDbSlowLogListQuery() (string, error) {
   369  	q, err := golangsdk.BuildQueryString(opts)
   370  	if err != nil {
   371  		return "", err
   372  	}
   373  	return q.String(), err
   374  }
   375  
   376  func ListSlowLog(client *golangsdk.ServiceClient, opts DbSlowLogBuilder, instanceID string) pagination.Pager {
   377  	url := updateURL(client, instanceID, "slowlog")
   378  	if opts != nil {
   379  		query, err := opts.ToDbSlowLogListQuery()
   380  
   381  		if err != nil {
   382  			return pagination.Pager{Err: err}
   383  		}
   384  		url += query
   385  	}
   386  
   387  	pageRdsList := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   388  		return ErrorLogPage{pagination.SinglePageBase(r)}
   389  	})
   390  
   391  	rdsheader := map[string]string{"Content-Type": "application/json"}
   392  	pageRdsList.Headers = rdsheader
   393  	return pageRdsList
   394  }
   395  
   396  type RDSJobOpts struct {
   397  	JobID string `q:"id"`
   398  }
   399  
   400  type RDSJobBuilder interface {
   401  	ToRDSJobQuery() (string, error)
   402  }
   403  
   404  func (opts RDSJobOpts) ToRDSJobQuery() (string, error) {
   405  	q, err := golangsdk.BuildQueryString(opts)
   406  	if err != nil {
   407  		return "", err
   408  	}
   409  	return q.String(), err
   410  }
   411  
   412  func GetRDSJob(client *golangsdk.ServiceClient, opts RDSJobBuilder) (r RDSJobResult) {
   413  	url := jobURL(client)
   414  	if opts != nil {
   415  		query, err := opts.ToRDSJobQuery()
   416  		if err != nil {
   417  			r.Err = err
   418  			return
   419  		}
   420  		url += query
   421  	}
   422  	_, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{
   423  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   424  	})
   425  	return
   426  }