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

     1  package instances
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  )
     6  
     7  var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{
     8  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
     9  }
    10  
    11  //CreateOpsBuilder is used for creating instance parameters.
    12  //any struct providing the parameters should implement this interface
    13  type CreateOpsBuilder interface {
    14  	ToInstanceCreateMap() (map[string]interface{}, error)
    15  }
    16  
    17  type UpdateOpsBuilder interface {
    18  	ToInstanceUpdateMap() (map[string]interface{}, error)
    19  }
    20  
    21  type UpdatePolicyOpsBuilder interface {
    22  	ToInstanceUpdatePolicyMap() (map[string]interface{}, error)
    23  }
    24  
    25  type UpdateFlavorOpsBuilder interface {
    26  	ToInstanceFlavorUpdateMap() (map[string]interface{}, error)
    27  }
    28  
    29  type UpdateOps struct {
    30  	Volume map[string]interface{} `json:"volume"`
    31  }
    32  
    33  type UpdatePolicyOps struct {
    34  	StartTime string `json:"starttime"`
    35  	KeepDays  int    `json:"keepday"`
    36  }
    37  
    38  type UpdateFlavorOps struct {
    39  	FlavorRef string `json:"flavorRef"`
    40  }
    41  
    42  //CreateOps is a struct that contains all the parameters.
    43  type CreateOps struct {
    44  	Name string `json:"name" required:"true"`
    45  
    46  	DataStore DataStoreOps `json:"datastore,omitempty"`
    47  
    48  	FlavorRef string `json:"flavorRef" required:"true"`
    49  
    50  	Volume VolumeOps `json:"volume,omitempty"`
    51  
    52  	Region string `json:"region,omitempty"`
    53  
    54  	AvailabilityZone string `json:"availabilityZone,omitempty"`
    55  
    56  	Vpc string `json:"vpc,omitempty"`
    57  
    58  	Nics NicsOps `json:"nics,omitempty"`
    59  
    60  	SecurityGroup SecurityGroupOps `json:"securityGroup,omitempty"`
    61  
    62  	DbPort string `json:"dbPort,omitempty"`
    63  
    64  	BackupStrategy BackupStrategyOps `json:"backupStrategy,omitempty"`
    65  
    66  	DbRtPd string `json:"dbRtPd,omitempty"`
    67  
    68  	Ha HaOps `json:"ha,omitempty"`
    69  
    70  	ReplicaOf string `json:"replicaOf,omitempty"`
    71  }
    72  
    73  type DataStoreOps struct {
    74  	Type    string `json:"type" required:"true"`
    75  	Version string `json:"version" required:"true"`
    76  }
    77  
    78  type VolumeOps struct {
    79  	Type string `json:"type" required:"true"`
    80  	Size int    `json:"size" required:"true"`
    81  }
    82  
    83  type NicsOps struct {
    84  	SubnetId string `json:"subnetId" required:"true"`
    85  }
    86  
    87  type SecurityGroupOps struct {
    88  	Id string `json:"id" required:"true"`
    89  }
    90  
    91  type BackupStrategyOps struct {
    92  	StartTime string `json:"startTime" required:"true"`
    93  	KeepDays  int    `json:"keepDays,omitempty"`
    94  }
    95  
    96  type HaOps struct {
    97  	Enable          bool   `json:"enable" required:"true"`
    98  	ReplicationMode string `json:"replicationMode" required:"true"`
    99  }
   100  
   101  func (ops CreateOps) ToInstanceCreateMap() (map[string]interface{}, error) {
   102  	return golangsdk.BuildRequestBody(ops, "instance")
   103  }
   104  
   105  func (ops UpdateOps) ToInstanceUpdateMap() (map[string]interface{}, error) {
   106  	return golangsdk.BuildRequestBody(ops, "resize")
   107  }
   108  
   109  func (ops UpdatePolicyOps) ToInstanceUpdatePolicyMap() (map[string]interface{}, error) {
   110  	return golangsdk.BuildRequestBody(ops, "policy")
   111  }
   112  
   113  func (ops UpdateFlavorOps) ToInstanceFlavorUpdateMap() (map[string]interface{}, error) {
   114  	return golangsdk.BuildRequestBody(ops, "resize")
   115  }
   116  
   117  //Create a instance with given parameters.
   118  func Create(client *golangsdk.ServiceClient, ops CreateOpsBuilder) (r CreateResult) {
   119  	b, err := ops.ToInstanceCreateMap()
   120  	if err != nil {
   121  		r.Err = err
   122  		return
   123  	}
   124  
   125  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
   126  		OkCodes:     []int{202},
   127  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   128  	})
   129  
   130  	return
   131  }
   132  
   133  func UpdateVolumeSize(client *golangsdk.ServiceClient, ops UpdateOpsBuilder, id string) (r UpdateResult) {
   134  	b, err := ops.ToInstanceUpdateMap()
   135  	if err != nil {
   136  		r.Err = err
   137  		return
   138  	}
   139  
   140  	_, r.Err = client.Post(updateURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
   141  		OkCodes:     []int{202},
   142  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   143  	})
   144  	return
   145  }
   146  
   147  func UpdatePolicy(client *golangsdk.ServiceClient, ops UpdatePolicyOpsBuilder, id string) (r UpdateResult) {
   148  	b, err := ops.ToInstanceUpdatePolicyMap()
   149  	if err != nil {
   150  		r.Err = err
   151  		return
   152  	}
   153  
   154  	_, r.Err = client.Put(updatePolicyURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
   155  		OkCodes:     []int{200},
   156  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   157  	})
   158  	return
   159  }
   160  
   161  func UpdateFlavorRef(client *golangsdk.ServiceClient, ops UpdateFlavorOpsBuilder, id string) (r UpdateResult) {
   162  	b, err := ops.ToInstanceFlavorUpdateMap()
   163  	if err != nil {
   164  		r.Err = err
   165  		return
   166  	}
   167  
   168  	_, r.Err = client.Post(updateURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
   169  		OkCodes:     []int{202},
   170  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   171  	})
   172  	return
   173  }
   174  
   175  //delete a instance via id
   176  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
   177  	RequestOpts.OkCodes = []int{202}
   178  	RequestOpts.JSONBody = nil
   179  	JSONBody := make(map[string]interface{})
   180  	_, r.Err = client.Delete(deleteURL(client, id), &golangsdk.RequestOpts{
   181  		OkCodes:     []int{202},
   182  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: JSONBody,
   183  	})
   184  	return
   185  }
   186  
   187  //get a instance with detailed information by id
   188  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
   189  
   190  	_, r.Err = client.Get(getURL(client, id), &r.Body, &golangsdk.RequestOpts{
   191  		OkCodes:     []int{200},
   192  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   193  	})
   194  	return
   195  }
   196  
   197  //list all the instances
   198  func List(client *golangsdk.ServiceClient) (r ListResult) {
   199  
   200  	_, r.Err = client.Get(listURL(client), &r.Body, &golangsdk.RequestOpts{
   201  		OkCodes:     []int{200},
   202  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
   203  	})
   204  	return
   205  }