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

     1  package instances
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/pagination"
     8  )
     9  
    10  type CreateOpts struct {
    11  	Name                string         `json:"name"  required:"true"`
    12  	DataStore           DataStore      `json:"datastore" required:"true"`
    13  	Region              string         `json:"region" required:"true"`
    14  	AvailabilityZone    string         `json:"availability_zone" required:"true"`
    15  	VpcId               string         `json:"vpc_id" required:"true"`
    16  	SubnetId            string         `json:"subnet_id" required:"true"`
    17  	SecurityGroupId     string         `json:"security_group_id" required:"true"`
    18  	Password            string         `json:"password" required:"true"`
    19  	DiskEncryptionId    string         `json:"disk_encryption_id,omitempty"`
    20  	Ssl                 string         `json:"ssl_option,omitempty"`
    21  	Mode                string         `json:"mode" required:"true"`
    22  	Flavor              []Flavor       `json:"flavor" required:"true"`
    23  	BackupStrategy      BackupStrategy `json:"backup_strategy,omitempty"`
    24  	EnterpriseProjectID string         `json:"enterprise_project_id,omitempty"`
    25  }
    26  
    27  type DataStore struct {
    28  	Type          string `json:"type" required:"true"`
    29  	Version       string `json:"version" required:"true"`
    30  	StorageEngine string `json:"storage_engine" required:"true"`
    31  }
    32  
    33  type Flavor struct {
    34  	Type     string `json:"type" required:"true"`
    35  	Num      int    `json:"num" required:"true"`
    36  	Storage  string `json:"storage,omitempty"`
    37  	Size     int    `json:"size,omitempty"`
    38  	SpecCode string `json:"spec_code" required:"true"`
    39  }
    40  
    41  type BackupStrategy struct {
    42  	StartTime string `json:"start_time" required:"true"`
    43  	KeepDays  *int   `json:"keep_days,omitempty"`
    44  	Period    string `json:"period,omitempty"`
    45  }
    46  
    47  type CreateInstanceBuilder interface {
    48  	ToInstancesCreateMap() (map[string]interface{}, error)
    49  }
    50  
    51  func (opts CreateOpts) 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 CreateInstanceBuilder) (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  	})
    69  	return
    70  }
    71  
    72  type DeleteInstance struct {
    73  	InstanceId string `json:"instance_id" required:"true"`
    74  }
    75  
    76  type DeleteInstanceBuilder interface {
    77  	ToInstancesDeleteMap() (map[string]interface{}, error)
    78  }
    79  
    80  func (opts DeleteInstance) ToInstancesDeleteMap() (map[string]interface{}, error) {
    81  	b, err := golangsdk.BuildRequestBody(&opts, "")
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	return b, nil
    86  }
    87  
    88  func Delete(client *golangsdk.ServiceClient, instanceId string) (r DeleteInstanceResult) {
    89  
    90  	url := deleteURL(client, instanceId)
    91  
    92  	_, r.Err = client.Delete(url, &golangsdk.RequestOpts{JSONResponse: &r.Body, MoreHeaders: map[string]string{"Content-Type": "application/json"}})
    93  	return
    94  }
    95  
    96  type ListInstanceOpts struct {
    97  	Id            string `q:"id"`
    98  	Name          string `q:"name"`
    99  	Mode          string `q:"mode"`
   100  	DataStoreType string `q:"datastore_type"`
   101  	VpcId         string `q:"vpc_id"`
   102  	SubnetId      string `q:"subnet_id"`
   103  	Offset        int    `q:"offset"`
   104  	Limit         int    `q:"limit"`
   105  }
   106  
   107  type ListInstanceBuilder interface {
   108  	ToInstanceListDetailQuery() (string, error)
   109  }
   110  
   111  func (opts ListInstanceOpts) ToInstanceListDetailQuery() (string, error) {
   112  	q, err := golangsdk.BuildQueryString(opts)
   113  	if err != nil {
   114  		return "", err
   115  	}
   116  	return q.String(), err
   117  }
   118  
   119  func List(client *golangsdk.ServiceClient, opts ListInstanceBuilder) pagination.Pager {
   120  	url := listURL(client)
   121  	if opts != nil {
   122  		query, err := opts.ToInstanceListDetailQuery()
   123  
   124  		if err != nil {
   125  			return pagination.Pager{Err: err}
   126  		}
   127  		url += query
   128  	}
   129  
   130  	pageList := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   131  		return InstancePage{pagination.SinglePageBase(r)}
   132  	})
   133  
   134  	return pageList
   135  }
   136  
   137  // UpdateOpt defines the basic information for update APIs
   138  // URI: <Method> base_url/<Action>
   139  // Request body: {<Param>: <Value>}
   140  // the supported value for Method including: "post" and "put"
   141  type UpdateOpt struct {
   142  	Param  string
   143  	Value  interface{}
   144  	Action string
   145  	Method string
   146  }
   147  
   148  type UpdateVolumeOpts struct {
   149  	GroupID string `json:"group_id,omitempty"`
   150  	Size    *int   `json:"size,omitempty"`
   151  }
   152  
   153  type UpdateNodeNumOpts struct {
   154  	Type     string            `json:"type" required:"true"`
   155  	SpecCode string            `json:"spec_code" required:"true"`
   156  	Num      int               `json:"num" required:"true"`
   157  	Volume   *UpdateVolumeOpts `json:"volume,omitempty"`
   158  }
   159  
   160  type UpdateSpecOpts struct {
   161  	TargetType     string `json:"target_type,omitempty"`
   162  	TargetID       string `json:"target_id" required:"true"`
   163  	TargetSpecCode string `json:"target_spec_code" required:"true"`
   164  }
   165  
   166  func Update(client *golangsdk.ServiceClient, instanceId string, opts []UpdateOpt) (r UpdateInstanceResult) {
   167  	for _, optRaw := range opts {
   168  		url := modifyURL(client, instanceId, optRaw.Action)
   169  		var body interface{}
   170  		if optRaw.Param != "" {
   171  			body = map[string]interface{}{
   172  				optRaw.Param: optRaw.Value,
   173  			}
   174  		} else {
   175  			body = optRaw.Value
   176  		}
   177  
   178  		var httpMethod func(string, interface{}, interface{}, *golangsdk.RequestOpts) (*http.Response, error)
   179  		if optRaw.Method == "post" {
   180  			httpMethod = client.Post
   181  		} else {
   182  			httpMethod = client.Put
   183  		}
   184  
   185  		_, r.Err = httpMethod(url, body, &r.Body, &golangsdk.RequestOpts{
   186  			OkCodes: []int{200, 202},
   187  		})
   188  
   189  		if r.Err != nil {
   190  			break
   191  		}
   192  	}
   193  	return
   194  }