github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/opengauss/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 DataStoreOpt struct {
     9  	Type    string `json:"type" required:"true"`
    10  	Version string `json:"version,omitempty"`
    11  }
    12  
    13  type BackupStrategyOpt struct {
    14  	StartTime string `json:"start_time" required:"true"`
    15  	KeepDays  int    `json:"keep_days,omitempty"`
    16  }
    17  
    18  type HaOpt struct {
    19  	Mode            string `json:"mode" required:"true"`
    20  	ReplicationMode string `json:"replication_mode" required:"true"`
    21  	Consistency     string `json:"consistency,omitempty"`
    22  }
    23  
    24  type VolumeOpt struct {
    25  	Type string `json:"type" required:"true"`
    26  	Size int    `json:"size" required:"true"`
    27  }
    28  
    29  type RestorePointOpt struct {
    30  	InstanceId   string            `json:"instance_id" required:"true"`
    31  	Type         string            `json:"type" required:"true"`
    32  	BackupId     string            `json:"backup_id,omitempty"`
    33  	RestoreTime  int               `json:"restore_time,omitempty"`
    34  	DatabaseName map[string]string `json:"database_name,omitempty"`
    35  }
    36  
    37  type CreateGaussDBOpts struct {
    38  	Name                string             `json:"name" required:"true"`
    39  	Region              string             `json:"region,omitempty"`
    40  	Flavor              string             `json:"flavor_ref" required:"true"`
    41  	VpcId               string             `json:"vpc_id,omitempty"`
    42  	SubnetId            string             `json:"subnet_id,omitempty"`
    43  	SecurityGroupId     string             `json:"security_group_id,omitempty"`
    44  	Password            string             `json:"password" required:"true"`
    45  	Port                string             `json:"port,omitempty"`
    46  	DiskEncryptionId    string             `json:"disk_encryption_id,omitempty"`
    47  	TimeZone            string             `json:"time_zone,omitempty"`
    48  	AvailabilityZone    string             `json:"availability_zone" required:"true"`
    49  	ConfigurationId     string             `json:"configuration_id,omitempty"`
    50  	DsspoolId           string             `json:"dsspool_id,omitempty"`
    51  	ReplicaOfId         string             `json:"replica_of_id,omitempty"`
    52  	ShardingNum         int                `json:"sharding_num" required:"true"`
    53  	CoordinatorNum      int                `json:"coordinator_num" required:"true"`
    54  	EnterpriseProjectId string             `json:"enterprise_project_id,omitempty"`
    55  	DataStore           DataStoreOpt       `json:"datastore" required:"true"`
    56  	Volume              VolumeOpt          `json:"volume" required:"true"`
    57  	Ha                  *HaOpt             `json:"ha,omitempty"`
    58  	BackupStrategy      *BackupStrategyOpt `json:"backup_strategy,omitempty"`
    59  	RestorePoint        *RestorePointOpt   `json:"restore_point,omitempty"`
    60  }
    61  
    62  type CreateGaussDBBuilder interface {
    63  	ToInstancesCreateMap() (map[string]interface{}, error)
    64  }
    65  
    66  func (opts CreateGaussDBOpts) ToInstancesCreateMap() (map[string]interface{}, error) {
    67  	b, err := golangsdk.BuildRequestBody(opts, "")
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	return b, nil
    72  }
    73  
    74  func Create(client *golangsdk.ServiceClient, opts CreateGaussDBBuilder) (r CreateResult) {
    75  	b, err := opts.ToInstancesCreateMap()
    76  	if err != nil {
    77  		r.Err = err
    78  		return
    79  	}
    80  
    81  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
    82  		OkCodes:     []int{202},
    83  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    84  	})
    85  	return
    86  }
    87  
    88  type UpdateVolumeOptsBuilder interface {
    89  	ToVolumeUpdateMap() (map[string]interface{}, error)
    90  }
    91  
    92  type UpdateVolumeOpts struct {
    93  	Size int `json:"size" required:"true"`
    94  }
    95  
    96  func (opts UpdateVolumeOpts) ToVolumeUpdateMap() (map[string]interface{}, error) {
    97  	return golangsdk.BuildRequestBody(opts, "enlarge_volume")
    98  }
    99  
   100  func UpdateVolume(client *golangsdk.ServiceClient, opts UpdateVolumeOptsBuilder, id string) (r UpdateResult) {
   101  	b, err := opts.ToVolumeUpdateMap()
   102  	if err != nil {
   103  		r.Err = err
   104  		return
   105  	}
   106  
   107  	_, r.Err = client.Post(updateURL(client, id, "action"), b, &r.Body, &golangsdk.RequestOpts{
   108  		OkCodes:     []int{202},
   109  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   110  	})
   111  	return
   112  }
   113  
   114  type UpdateClusterOptsBuilder interface {
   115  	ToClusterUpdateMap() (map[string]interface{}, error)
   116  }
   117  
   118  type Shard struct {
   119  	Count int `json:"count" required:"true"`
   120  }
   121  
   122  type Coordinator struct {
   123  	AzCode string `json:"az_code" required:"true"`
   124  }
   125  
   126  type UpdateClusterOpts struct {
   127  	Shard        *Shard        `json:"shard,omitempty"`
   128  	Coordinators []Coordinator `json:"coordinators,omitempty"`
   129  }
   130  
   131  func (opts UpdateClusterOpts) ToClusterUpdateMap() (map[string]interface{}, error) {
   132  	return golangsdk.BuildRequestBody(opts, "expand_cluster")
   133  }
   134  
   135  func UpdateCluster(client *golangsdk.ServiceClient, opts UpdateClusterOptsBuilder, id string) (r UpdateResult) {
   136  	b, err := opts.ToClusterUpdateMap()
   137  	if err != nil {
   138  		r.Err = err
   139  		return
   140  	}
   141  
   142  	_, r.Err = client.Post(updateURL(client, id, "action"), b, &r.Body, &golangsdk.RequestOpts{
   143  		OkCodes:     []int{202},
   144  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   145  	})
   146  	return
   147  }
   148  
   149  func Delete(client *golangsdk.ServiceClient, instanceId string) (r DeleteResult) {
   150  	url := deleteURL(client, instanceId)
   151  
   152  	_, r.Err = client.Delete(url, &golangsdk.RequestOpts{
   153  		OkCodes:     []int{202},
   154  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   155  	})
   156  
   157  	return
   158  }
   159  
   160  type ListGaussDBInstanceOpts struct {
   161  	Id            string `q:"id"`
   162  	Name          string `q:"name"`
   163  	Type          string `q:"type"`
   164  	DataStoreType string `q:"datastore_type"`
   165  	VpcId         string `q:"vpc_id"`
   166  	SubnetId      string `q:"subnet_id"`
   167  	Offset        int    `q:"offset"`
   168  	Limit         int    `q:"limit"`
   169  }
   170  
   171  type ListGaussDBBuilder interface {
   172  	ToGaussDBListDetailQuery() (string, error)
   173  }
   174  
   175  func (opts ListGaussDBInstanceOpts) ToGaussDBListDetailQuery() (string, error) {
   176  	q, err := golangsdk.BuildQueryString(opts)
   177  	if err != nil {
   178  		return "", err
   179  	}
   180  	return q.String(), err
   181  }
   182  
   183  func List(client *golangsdk.ServiceClient, opts ListGaussDBBuilder) pagination.Pager {
   184  	url := listURL(client)
   185  	if opts != nil {
   186  		query, err := opts.ToGaussDBListDetailQuery()
   187  
   188  		if err != nil {
   189  			return pagination.Pager{Err: err}
   190  		}
   191  		url += query
   192  	}
   193  
   194  	pageList := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   195  		return GaussDBPage{pagination.SinglePageBase(r)}
   196  	})
   197  	// Headers supplies additional HTTP headers to populate on each paged request
   198  	pageList.Headers = map[string]string{"Content-Type": "application/json"}
   199  
   200  	return pageList
   201  }
   202  
   203  func GetInstanceByID(client *golangsdk.ServiceClient, instanceId string) (GaussDBInstance, error) {
   204  	var instance GaussDBInstance
   205  
   206  	opts := ListGaussDBInstanceOpts{
   207  		Id: instanceId,
   208  	}
   209  
   210  	pages, err := List(client, &opts).AllPages()
   211  	if err != nil {
   212  		return instance, err
   213  	}
   214  
   215  	all, err := ExtractGaussDBInstances(pages)
   216  	if err != nil {
   217  		return instance, err
   218  	}
   219  	if all.TotalCount == 0 {
   220  		return instance, nil
   221  	}
   222  
   223  	instance = all.Instances[0]
   224  	return instance, nil
   225  }
   226  
   227  func GetInstanceByName(client *golangsdk.ServiceClient, name string) (GaussDBInstance, error) {
   228  	var instance GaussDBInstance
   229  
   230  	opts := ListGaussDBInstanceOpts{
   231  		Name: name,
   232  	}
   233  
   234  	pages, err := List(client, &opts).AllPages()
   235  	if err != nil {
   236  		return instance, err
   237  	}
   238  
   239  	all, err := ExtractGaussDBInstances(pages)
   240  	if err != nil {
   241  		return instance, err
   242  	}
   243  	if all.TotalCount == 0 {
   244  		return instance, nil
   245  	}
   246  
   247  	instance = all.Instances[0]
   248  	return instance, nil
   249  }
   250  
   251  type RenameOptsBuilder interface {
   252  	ToRenameMap() (map[string]interface{}, error)
   253  }
   254  
   255  type RenameOpts struct {
   256  	Name string `json:"name" required:"true"`
   257  }
   258  
   259  func (opts RenameOpts) ToRenameMap() (map[string]interface{}, error) {
   260  	return golangsdk.BuildRequestBody(opts, "")
   261  }
   262  
   263  func Rename(client *golangsdk.ServiceClient, opts RenameOptsBuilder, id string) (r RenameResult) {
   264  	b, err := opts.ToRenameMap()
   265  	if err != nil {
   266  		r.Err = err
   267  		return
   268  	}
   269  
   270  	_, r.Err = client.Put(updateURL(client, id, "name"), b, &r.Body, &golangsdk.RequestOpts{
   271  		OkCodes:     []int{200},
   272  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   273  	})
   274  	return
   275  }
   276  
   277  type RestorePasswordOptsBuilder interface {
   278  	ToRestorePasswordMap() (map[string]interface{}, error)
   279  }
   280  
   281  type RestorePasswordOpts struct {
   282  	Password string `json:"password" required:"true"`
   283  }
   284  
   285  func (opts RestorePasswordOpts) ToRestorePasswordMap() (map[string]interface{}, error) {
   286  	return golangsdk.BuildRequestBody(opts, "")
   287  }
   288  
   289  func RestorePassword(client *golangsdk.ServiceClient, opts RestorePasswordOptsBuilder, id string) (r golangsdk.Result) {
   290  	b, err := opts.ToRestorePasswordMap()
   291  	if err != nil {
   292  		r.Err = err
   293  		return
   294  	}
   295  
   296  	_, r.Err = client.Post(updateURL(client, id, "password"), b, nil, &golangsdk.RequestOpts{
   297  		OkCodes:     []int{200},
   298  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   299  	})
   300  	return
   301  }