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

     1  package cloudservers
     2  
     3  import (
     4  	"encoding/base64"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/pagination"
     8  )
     9  
    10  type CreateOpts struct {
    11  	ImageRef string `json:"imageRef" required:"true"`
    12  
    13  	FlavorRef string `json:"flavorRef" required:"true"`
    14  
    15  	Name string `json:"name" required:"true"`
    16  
    17  	UserData []byte `json:"-"`
    18  
    19  	// AdminPass sets the root user password. If not set, a randomly-generated
    20  	// password will be created and returned in the response.
    21  	AdminPass string `json:"adminPass,omitempty"`
    22  
    23  	KeyName string `json:"key_name,omitempty"`
    24  
    25  	VpcId string `json:"vpcid" required:"true"`
    26  
    27  	Nics []Nic `json:"nics" required:"true"`
    28  
    29  	PublicIp *PublicIp `json:"publicip,omitempty"`
    30  
    31  	Count int `json:"count,omitempty"`
    32  
    33  	IsAutoRename *bool `json:"isAutoRename,omitempty"`
    34  
    35  	RootVolume RootVolume `json:"root_volume" required:"true"`
    36  
    37  	DataVolumes []DataVolume `json:"data_volumes,omitempty"`
    38  
    39  	SecurityGroups []SecurityGroup `json:"security_groups,omitempty"`
    40  
    41  	AvailabilityZone string `json:"availability_zone" required:"true"`
    42  
    43  	ExtendParam *ServerExtendParam `json:"extendparam,omitempty"`
    44  
    45  	MetaData *MetaData `json:"metadata,omitempty"`
    46  
    47  	SchedulerHints *SchedulerHints `json:"os:scheduler_hints,omitempty"`
    48  
    49  	Tags []string `json:"tags,omitempty"`
    50  
    51  	ServerTags []ServerTags `json:"server_tags,omitempty"`
    52  }
    53  
    54  // CreateOptsBuilder allows extensions to add additional parameters to the
    55  // Create request.
    56  type CreateOptsBuilder interface {
    57  	ToServerCreateMap() (map[string]interface{}, error)
    58  }
    59  
    60  // ToServerCreateMap assembles a request body based on the contents of a
    61  // CreateOpts.
    62  func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
    63  	b, err := golangsdk.BuildRequestBody(opts, "")
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	if opts.UserData != nil {
    69  		var userData string
    70  		if _, err := base64.StdEncoding.DecodeString(string(opts.UserData)); err != nil {
    71  			userData = base64.StdEncoding.EncodeToString(opts.UserData)
    72  		} else {
    73  			userData = string(opts.UserData)
    74  		}
    75  		b["user_data"] = &userData
    76  	}
    77  
    78  	return map[string]interface{}{"server": b}, nil
    79  }
    80  
    81  type Nic struct {
    82  	SubnetId string `json:"subnet_id" required:"true"`
    83  
    84  	IpAddress string `json:"ip_address,omitempty"`
    85  }
    86  
    87  type PublicIp struct {
    88  	Id string `json:"id,omitempty"`
    89  
    90  	Eip *Eip `json:"eip,omitempty"`
    91  }
    92  
    93  type Eip struct {
    94  	IpType string `json:"iptype" required:"true"`
    95  
    96  	BandWidth *BandWidth `json:"bandwidth" required:"true"`
    97  
    98  	ExtendParam *EipExtendParam `json:"extendparam,omitempty"`
    99  }
   100  
   101  type BandWidth struct {
   102  	Size int `json:"size,omitempty"`
   103  
   104  	ShareType string `json:"sharetype" required:"true"`
   105  
   106  	ChargeMode string `json:"chargemode,omitempty"`
   107  
   108  	Id string `json:"id,omitempty"`
   109  }
   110  
   111  type EipExtendParam struct {
   112  	ChargingMode string `json:"chargingMode,omitempty"`
   113  }
   114  
   115  type RootVolume struct {
   116  	VolumeType string `json:"volumetype" required:"true"`
   117  
   118  	Size int `json:"size,omitempty"`
   119  
   120  	ExtendParam *VolumeExtendParam `json:"extendparam,omitempty"`
   121  }
   122  
   123  type DataVolume struct {
   124  	VolumeType string `json:"volumetype" required:"true"`
   125  
   126  	Size int `json:"size" required:"true"`
   127  
   128  	MultiAttach *bool `json:"multiattach,omitempty"`
   129  
   130  	PassThrough *bool `json:"hw:passthrough,omitempty"`
   131  
   132  	Extendparam *VolumeExtendParam `json:"extendparam,omitempty"`
   133  }
   134  
   135  type VolumeExtendParam struct {
   136  	SnapshotId string `json:"snapshotId,omitempty"`
   137  }
   138  
   139  type ServerExtendParam struct {
   140  	ChargingMode string `json:"chargingMode,omitempty"`
   141  
   142  	RegionID string `json:"regionID,omitempty"`
   143  
   144  	PeriodType string `json:"periodType,omitempty"`
   145  
   146  	PeriodNum int `json:"periodNum,omitempty"`
   147  
   148  	IsAutoRenew string `json:"isAutoRenew,omitempty"`
   149  
   150  	IsAutoPay string `json:"isAutoPay,omitempty"`
   151  
   152  	EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
   153  
   154  	SupportAutoRecovery string `json:"support_auto_recovery,omitempty"`
   155  }
   156  
   157  type MetaData struct {
   158  	OpSvcUserId string `json:"op_svc_userid,omitempty"`
   159  	AgencyName  string `json:"agency_name,omitempty"`
   160  }
   161  
   162  type SecurityGroup struct {
   163  	ID string `json:"id" required:"true"`
   164  }
   165  
   166  type SchedulerHints struct {
   167  	Group       string `json:"group,omitempty"`
   168  	FaultDomain string `json:"fault_domain,omitempty"`
   169  
   170  	// Specifies whether the ECS is created on a Dedicated Host (DeH) or in a shared pool.
   171  	Tenancy string `json:"tenancy,omitempty"`
   172  
   173  	// DedicatedHostID specifies a DeH ID.
   174  	DedicatedHostID string `json:"dedicated_host_id,omitempty"`
   175  }
   176  
   177  type ServerTags struct {
   178  	Key   string `json:"key" required:"true"`
   179  	Value string `json:"value,omitempty"`
   180  }
   181  
   182  // Create requests a server to be provisioned to the user in the current tenant.
   183  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) {
   184  	reqBody, err := opts.ToServerCreateMap()
   185  	if err != nil {
   186  		r.Err = err
   187  		return
   188  	}
   189  
   190  	_, r.Err = client.Post(createURL(client), reqBody, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
   191  	return
   192  }
   193  
   194  // CreatePrePaid requests a server to be provisioned to the user in the current tenant.
   195  func CreatePrePaid(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r OrderResult) {
   196  	reqBody, err := opts.ToServerCreateMap()
   197  	if err != nil {
   198  		r.Err = err
   199  		return
   200  	}
   201  
   202  	_, r.Err = client.Post(createURL(client), reqBody, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
   203  	return
   204  }
   205  
   206  // Get retrieves a particular Server based on its unique ID.
   207  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   208  	_, r.Err = c.Get(getURL(c, id), &r.Body, &golangsdk.RequestOpts{
   209  		OkCodes: []int{200, 203},
   210  	})
   211  	return
   212  }
   213  
   214  type DeleteOpts struct {
   215  	Servers        []Server `json:"servers" required:"true"`
   216  	DeletePublicIP bool     `json:"delete_publicip,omitempty"`
   217  	DeleteVolume   bool     `json:"delete_volume,omitempty"`
   218  }
   219  
   220  type Server struct {
   221  	Id string `json:"id" required:"true"`
   222  }
   223  
   224  // ToServerDeleteMap assembles a request body based on the contents of a
   225  // DeleteOpts.
   226  func (opts DeleteOpts) ToServerDeleteMap() (map[string]interface{}, error) {
   227  	return golangsdk.BuildRequestBody(opts, "")
   228  }
   229  
   230  // Delete requests a server to be deleted to the user in the current tenant.
   231  func Delete(client *golangsdk.ServiceClient, opts DeleteOpts) (r JobResult) {
   232  	reqBody, err := opts.ToServerDeleteMap()
   233  	if err != nil {
   234  		r.Err = err
   235  		return
   236  	}
   237  	_, r.Err = client.Post(deleteURL(client), reqBody, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
   238  	return
   239  }
   240  
   241  type DeleteOrderOpts struct {
   242  	ResourceIds []string `json:"resourceIds" required:"true"`
   243  	UnSubType   int      `json:"unSubType" required:"true"`
   244  }
   245  
   246  // ToServerDeleteOrderMap assembles a request body based on the contents of a
   247  // DeleteOrderOpts.
   248  func (opts DeleteOrderOpts) ToServerDeleteOrderMap() (map[string]interface{}, error) {
   249  	return golangsdk.BuildRequestBody(opts, "")
   250  }
   251  
   252  // DeleteOrder requests a server to be deleted to the user in the current tenant.
   253  func DeleteOrder(client *golangsdk.ServiceClient, opts DeleteOrderOpts) (r DeleteOrderResult) {
   254  	reqBody, err := opts.ToServerDeleteOrderMap()
   255  	if err != nil {
   256  		r.Err = err
   257  		return
   258  	}
   259  	_, r.Err = client.Post(deleteOrderURL(client), reqBody, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
   260  	return
   261  }
   262  
   263  // ListOptsBuilder allows extensions to add additional parameters to the
   264  // List request.
   265  type ListOptsBuilder interface {
   266  	ToServerListQuery() (string, error)
   267  }
   268  
   269  // ListOpts allows the filtering and sorting of paginated collections through
   270  // the API. Filtering is achieved by passing in struct field values that map to
   271  // the server attributes you want to see returned. Marker and Limit are used
   272  // for pagination.
   273  type ListOpts struct {
   274  	// Name of the server as a string; can be queried with regular expressions.
   275  	// Realize that ?name=bob returns both bob and bobb. If you need to match bob
   276  	// only, you can use a regular expression matching the syntax of the
   277  	// underlying database server implemented for Compute.
   278  	Name string `q:"name"`
   279  
   280  	// Flavor is the name of the flavor in URL format.
   281  	Flavor string `q:"flavor"`
   282  
   283  	// Status is the value of the status of the server so that you can filter on
   284  	// "ACTIVE" for example.
   285  	Status string `q:"status"`
   286  
   287  	// Specifies the ECS that is bound to an enterprise project.
   288  	EnterpriseProjectID string `q:"enterprise_project_id"`
   289  
   290  	// Indicates the filtering result for IPv4 addresses, which are fuzzy matched.
   291  	// These IP addresses are private IP addresses of the ECS.
   292  	IP string `q:"ip"`
   293  
   294  	// Specifies the maximum number of ECSs on one page.
   295  	// Each page contains 25 ECSs by default, and a maximum of 1000 ECSs are returned.
   296  	Limit int `q:"limit"`
   297  
   298  	// Specifies a page number. The default value is 1.
   299  	// The value must be greater than or equal to 0. If the value is 0, the first page is displayed.
   300  	Offset int `q:"offset"`
   301  }
   302  
   303  // ToServerListQuery formats a ListOpts into a query string.
   304  func (opts ListOpts) ToServerListQuery() (string, error) {
   305  	q, err := golangsdk.BuildQueryString(opts)
   306  	return q.String(), err
   307  }
   308  
   309  // List makes a request against the API to list servers accessible to you.
   310  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   311  	url := listDetailURL(client)
   312  	if opts != nil {
   313  		query, err := opts.ToServerListQuery()
   314  		if err != nil {
   315  			return pagination.Pager{Err: err}
   316  		}
   317  		url += query
   318  	}
   319  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   320  		return ServerPage{pagination.LinkedPageBase{PageResult: r}}
   321  	})
   322  }
   323  
   324  type ResizeOpts struct {
   325  	FlavorRef   string             `json:"flavorRef" required:"true"`
   326  	Mode        string             `json:"mode,omitempty"`
   327  	ExtendParam *ResizeExtendParam `json:"extendparam,omitempty"`
   328  }
   329  
   330  type ResizeExtendParam struct {
   331  	AutoPay string `json:"isAutoPay,omitempty"`
   332  }
   333  
   334  // ResizeOptsBuilder allows extensions to add additional parameters to the
   335  // Resize request.
   336  type ResizeOptsBuilder interface {
   337  	ToServerResizeMap() (map[string]interface{}, error)
   338  }
   339  
   340  // ToServerResizeMap assembles a request body based on the contents of a
   341  // ResizeOpts.
   342  func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
   343  	return golangsdk.BuildRequestBody(opts, "resize")
   344  }
   345  
   346  // Resize requests a server to be resizeed.
   347  func Resize(client *golangsdk.ServiceClient, opts ResizeOptsBuilder, serverId string) (r JobResult) {
   348  	reqBody, err := opts.ToServerResizeMap()
   349  	if err != nil {
   350  		r.Err = err
   351  		return
   352  	}
   353  
   354  	_, r.Err = client.Post(resizeURL(client, serverId), reqBody, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
   355  	return
   356  }