github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/ecs/v1/cloudservers/requests.go (about)

     1  package cloudservers
     2  
     3  import (
     4  	"encoding/base64"
     5  
     6  	"github.com/opentelekomcloud/gophertelekomcloud"
     7  )
     8  
     9  type CreateOpts struct {
    10  	// ImageRef ID  the ID of the system image used for creating ECSs.
    11  	ImageRef string `json:"imageRef" required:"true"`
    12  
    13  	// FlavorRef ID of the ECS to be created.
    14  	FlavorRef string `json:"flavorRef" required:"true"`
    15  
    16  	// Name of the ECS instance.
    17  	Name string `json:"name" required:"true"`
    18  
    19  	// UserData to be injected during the ECS creation process.
    20  	UserData []byte `json:"-"`
    21  
    22  	// AdminPass sets the root user password. If not set, a randomly-generated
    23  	// password will be created and returned in the response.
    24  	AdminPass string `json:"adminPass,omitempty"`
    25  
    26  	// KeyName of the SSH key used for logging in to the ECS.
    27  	KeyName string `json:"key_name,omitempty"`
    28  
    29  	// VpcId of the VPC to which the ECS belongs.
    30  	VpcId string `json:"vpcid" required:"true"`
    31  
    32  	// Nics information of the ECS.
    33  	Nics []Nic `json:"nics" required:"true"`
    34  
    35  	// PublicIp of the ECS.
    36  	PublicIp *PublicIp `json:"publicip,omitempty"`
    37  
    38  	// Count of ECSs to be created.
    39  	// If this parameter is not specified, the default value is 1.
    40  	Count int `json:"count,omitempty"`
    41  
    42  	// ECS RootVolume configurations.
    43  	RootVolume RootVolume `json:"root_volume" required:"true"`
    44  
    45  	// ECS DataVolumes configurations.
    46  	DataVolumes []DataVolume `json:"data_volumes,omitempty"`
    47  
    48  	// SecurityGroups of the ECS.
    49  	SecurityGroups []SecurityGroup `json:"security_groups,omitempty"`
    50  
    51  	// AvailabilityZone specifies name of the AZ where the ECS is located.
    52  	AvailabilityZone string `json:"availability_zone" required:"true"`
    53  
    54  	// ExtendParam provides the supplementary information about the ECS to be created.
    55  	ExtendParam *ServerExtendParam `json:"extendparam,omitempty"`
    56  
    57  	// MetaData specifies the metadata of the ECS to be created.
    58  	MetaData *MetaData `json:"metadata,omitempty"`
    59  
    60  	// SchedulerHints schedules ECSs, for example, by configuring an ECS group.
    61  	SchedulerHints *SchedulerHints `json:"os:scheduler_hints,omitempty"`
    62  
    63  	// ECS Tags.
    64  	Tags []string `json:"tags,omitempty"`
    65  
    66  	ServerTags []ServerTags `json:"server_tags,omitempty"`
    67  }
    68  
    69  // CreateOptsBuilder allows extensions to add additional parameters to the
    70  // Create request.
    71  type CreateOptsBuilder interface {
    72  	ToServerCreateMap() (map[string]interface{}, error)
    73  }
    74  
    75  // ToServerCreateMap assembles a request body based on the contents of a
    76  // CreateOpts.
    77  func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
    78  	b, err := golangsdk.BuildRequestBody(opts, "")
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	if opts.UserData != nil {
    84  		var userData string
    85  		if _, err := base64.StdEncoding.DecodeString(string(opts.UserData)); err != nil {
    86  			userData = base64.StdEncoding.EncodeToString(opts.UserData)
    87  		} else {
    88  			userData = string(opts.UserData)
    89  		}
    90  		b["user_data"] = &userData
    91  	}
    92  
    93  	return map[string]interface{}{"server": b}, nil
    94  }
    95  
    96  type Nic struct {
    97  	// SubnetId of the ECS.
    98  	SubnetId string `json:"subnet_id" required:"true"`
    99  
   100  	// IpAddress of the NIC used by the ECS.
   101  	IpAddress string `json:"ip_address,omitempty"`
   102  
   103  	// BindingProfile allows you to customize data.
   104  	// Configure this parameter when creating a HANA ECS.
   105  	BindingProfile BindingProfile `json:"binding:profile,omitempty"`
   106  
   107  	// ExtraDhcpOpts indicates extended DHCP options.
   108  	ExtraDhcpOpts []ExtraDhcpOpts `json:"extra_dhcp_opts,omitempty"`
   109  }
   110  
   111  type BindingProfile struct {
   112  	// DisableSecurityGroups indicates that a HANA ECS NIC is not added to a security group.
   113  	DisableSecurityGroups *bool `json:"disable_security_groups,omitempty"`
   114  }
   115  
   116  type ExtraDhcpOpts struct {
   117  	// Set the parameter value to 26.
   118  	OptName string `json:"opt_name" required:"true"`
   119  
   120  	// OptValue specifies the NIC MTU, which ranges from 1280 to 8888.
   121  	OptValue int `json:"opt_value" required:"true"`
   122  }
   123  
   124  type PublicIp struct {
   125  	// Id of the existing EIP assigned to the ECS to be created.
   126  	Id string `json:"id,omitempty"`
   127  
   128  	// Eip that will be automatically assigned to an ECS.
   129  	Eip *Eip `json:"eip,omitempty"`
   130  }
   131  
   132  type Eip struct {
   133  	// Specifies the EIP type
   134  	IpType string `json:"iptype" required:"true"`
   135  
   136  	// Specifies the EIP bandwidth.
   137  	BandWidth *BandWidth `json:"bandwidth" required:"true"`
   138  }
   139  
   140  type BandWidth struct {
   141  	// Specifies the bandwidth size.
   142  	Size int `json:"size" required:"true"`
   143  
   144  	// Specifies the bandwidth sharing type
   145  	ShareType string `json:"sharetype" required:"true"`
   146  
   147  	// Specifies the bandwidth billing mode.
   148  	ChargeMode string `json:"chargemode" required:"true"`
   149  }
   150  
   151  type RootVolume struct {
   152  	// VolumeType of the ECS system disk.
   153  	VolumeType string `json:"volumetype" required:"true"`
   154  
   155  	// System disk Size, in GB.
   156  	Size int `json:"size,omitempty"`
   157  
   158  	ExtendParam *VolumeExtendParam `json:"extendparam,omitempty"`
   159  
   160  	// Pay attention to this parameter if your ECS is SDI-compliant.
   161  	// If the value of this parameter is true, the created disk is of SCSI type.
   162  	PassThrough *bool `json:"hw:passthrough,omitempty"`
   163  
   164  	Metadata map[string]interface{} `json:"metadata,omitempty"`
   165  }
   166  
   167  type DataVolume struct {
   168  	// VolumeType of the ECS data disk.
   169  	VolumeType string `json:"volumetype" required:"true"`
   170  
   171  	// The data disk Size, in GB.
   172  	Size int `json:"size" required:"true"`
   173  
   174  	// MultiAttach is the shared disk information.
   175  	MultiAttach *bool `json:"multiattach,omitempty"`
   176  
   177  	// PassThrough indicates whether the data volume uses a SCSI lock.
   178  	PassThrough *bool `json:"hw:passthrough,omitempty"`
   179  
   180  	Extendparam *VolumeExtendParam `json:"extendparam,omitempty"`
   181  
   182  	// DataImageID If data disks are created using a data disk
   183  	// image, this parameter is mandatory and it does not support metadata.
   184  	DataImageID string `json:"data_image_id,omitempty"`
   185  
   186  	// EVS disk Metadata.
   187  	Metadata map[string]interface{} `json:"metadata,omitempty"`
   188  }
   189  
   190  type VolumeExtendParam struct {
   191  	SnapshotId string `json:"snapshotId,omitempty"`
   192  }
   193  
   194  type ServerExtendParam struct {
   195  	// RegionID is the ID of the region where the ECS resides.
   196  	RegionID string `json:"regionID,omitempty"`
   197  
   198  	// SupportAutoRecovery specifies whether automatic recovery is enabled on the ECS.
   199  	SupportAutoRecovery string `json:"support_auto_recovery,omitempty"`
   200  }
   201  
   202  type MetaData struct {
   203  	// AdminPass specifies the password of user Administrator for logging in to a Windows ECS.
   204  	AdminPass string `json:"admin_pass,omitempty"`
   205  
   206  	// OpSvcUserId specifies the user ID.
   207  	OpSvcUserId string `json:"op_svc_userid,omitempty"`
   208  
   209  	// AgencyName specifies the IAM agency name.
   210  	AgencyName string `json:"agency_name,omitempty"`
   211  
   212  	// If you have an OS or a software license, you can migrate your services to the cloud
   213  	// platform in BYOL mode to continue using your existing licenses.
   214  	BYOL string `json:"BYOL,omitempty"`
   215  }
   216  
   217  type SecurityGroup struct {
   218  	// ID of the security group to which an ECS is to be added
   219  	ID string `json:"id,omitempty"`
   220  }
   221  
   222  type SchedulerHints struct {
   223  	// ECS Group ID, which is in UUID format.
   224  	Group string `json:"group,omitempty"`
   225  
   226  	// Specifies whether the ECS is created on a Dedicated Host (DeH) or in a shared pool.
   227  	Tenancy string `json:"tenancy,omitempty"`
   228  
   229  	// DedicatedHostID specifies a DeH ID.
   230  	DedicatedHostID string `json:"dedicated_host_id,omitempty"`
   231  }
   232  
   233  type ServerTags struct {
   234  	Key   string `json:"key" required:"true"`
   235  	Value string `json:"value,omitempty"`
   236  }
   237  
   238  // Create requests a server to be provisioned to the user in the current tenant.
   239  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) {
   240  	b, err := opts.ToServerCreateMap()
   241  	if err != nil {
   242  		r.Err = err
   243  		return
   244  	}
   245  
   246  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
   247  		OkCodes: []int{200}},
   248  	)
   249  	return
   250  }
   251  
   252  // DryRun requests a server to be provisioned to the user in the current tenant.
   253  func DryRun(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r DryRunResult) {
   254  	b, err := opts.ToServerCreateMap()
   255  	if err != nil {
   256  		r.Err = err
   257  		return
   258  	}
   259  	b["dry_run"] = true
   260  
   261  	_, r.Err = client.Post(createURL(client), b, nil, &golangsdk.RequestOpts{
   262  		OkCodes: []int{202}},
   263  	)
   264  	return
   265  }
   266  
   267  // Get retrieves a particular Server based on its unique ID.
   268  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   269  	_, r.Err = c.Get(getURL(c, id), &r.Body, &golangsdk.RequestOpts{
   270  		OkCodes: []int{200, 203},
   271  	})
   272  	return
   273  }
   274  
   275  type DeleteOpts struct {
   276  	// Servers to be deleted
   277  	Servers []Server `json:"servers" required:"true"`
   278  
   279  	// DeletePublicIP specifies whether to delete the EIP bound to the ECS when deleting the ECS.
   280  	DeletePublicIP bool `json:"delete_publicip,omitempty"`
   281  
   282  	// DeleteVolume specifies whether to delete the data disks of the ECS.
   283  	DeleteVolume bool `json:"delete_volume,omitempty"`
   284  }
   285  
   286  type Server struct {
   287  	// ID of the ECS to be deleted.
   288  	Id string `json:"id" required:"true"`
   289  }
   290  
   291  // ToServerDeleteMap assembles a request body based on the contents of a
   292  // DeleteOpts.
   293  func (opts DeleteOpts) ToServerDeleteMap() (map[string]interface{}, error) {
   294  	return golangsdk.BuildRequestBody(opts, "")
   295  }
   296  
   297  // Delete requests a server to be deleted to the user in the current tenant.
   298  func Delete(client *golangsdk.ServiceClient, opts DeleteOpts) (r JobResult) {
   299  	b, err := opts.ToServerDeleteMap()
   300  	if err != nil {
   301  		r.Err = err
   302  		return
   303  	}
   304  	_, r.Err = client.Post(deleteURL(client), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
   305  	return
   306  }