github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/autoscaling/v1/configurations/create.go (about)

     1  package configurations
     2  
     3  import (
     4  	"encoding/base64"
     5  
     6  	"github.com/opentelekomcloud/gophertelekomcloud"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
     8  )
     9  
    10  type CreateOpts struct {
    11  	// Specifies the AS configuration name.
    12  	// The name contains only letters, digits, underscores (_), and hyphens (-), and cannot exceed 64 characters.
    13  	Name string `json:"scaling_configuration_name" required:"true"`
    14  	// Specifies the ECS configuration.
    15  	InstanceConfig InstanceConfigOpts `json:"instance_config" required:"true"`
    16  }
    17  
    18  type InstanceConfigOpts struct {
    19  	// Specifies the ECS ID. When using the existing ECS specifications as the template to create AS configurations,
    20  	// specify this parameter. In this case, the flavorRef, imageRef, disk, and security_groups fields do not take effect.
    21  	// If the instance_id field is not specified, flavorRef, imageRef, and disk fields are mandatory.
    22  	ID string `json:"instance_id,omitempty"`
    23  	// Specifies the ECS flavor ID. A maximum of 10 flavors can be selected. Use a comma (,) to separate multiple flavor IDs.
    24  	// You can obtain its value from the API for querying details about flavors and extended flavor information.
    25  	FlavorRef string `json:"flavorRef,omitempty"`
    26  	// Specifies the image ID. Its value is the same as that of image_id for specifying the image selected during ECS creation.
    27  	// Obtain the value using the API for querying images.
    28  	ImageRef string `json:"imageRef,omitempty"`
    29  	// Specifies the disk group information. System disks are mandatory and data disks are optional.
    30  	Disk []Disk `json:"disk,omitempty"`
    31  	// Specifies the name of the SSH key pair used to log in to the ECS.
    32  	SSHKey string `json:"key_name" required:"true"`
    33  	// Specifies information about the injected file. Only text files can be injected.
    34  	// A maximum of five files can be injected at a time and the maximum size of each file is 1 KB.
    35  	Personality []Personality `json:"personality,omitempty"`
    36  	// Specifies the EIP of the ECS. The EIP can be configured in two ways.
    37  	// Do not use an EIP. In this case, this parameter is unavailable.
    38  	// Automatically assign an EIP. You need to specify the information about the new EIP.
    39  	PubicIp *PublicIp `json:"public_ip,omitempty"`
    40  	// Specifies the user data to be injected during the ECS creation process. Text, text files, and gzip files can be injected.
    41  	// Constraints:
    42  	// The content to be injected must be encoded with base64. The maximum size of the content to be injected (before encoding) is 32 KB.
    43  	// Examples:
    44  	// Linux
    45  	// #! /bin/bash
    46  	// echo user_test >> /home/user.txt
    47  	// Windows
    48  	// rem cmd
    49  	// echo 111 > c:\aaa.txt
    50  	UserData []byte `json:"-"`
    51  	// Specifies the ECS metadata.
    52  	Metadata AdminPassMetadata `json:"metadata,omitempty"`
    53  	// Specifies security groups.
    54  	// If the security group is specified both in the AS configuration and AS group, scaled ECS instances
    55  	// will be added to the security group specified in the AS configuration.
    56  	// If the security group is not specified in either of them, scaled ECS instances will be added to the default security group.
    57  	// For your convenience, you are advised to specify the security group in the AS configuration.
    58  	SecurityGroups []SecurityGroup `json:"security_groups,omitempty"`
    59  	// This parameter is reserved.
    60  	MarketType string `json:"market_type,omitempty"`
    61  }
    62  
    63  type AdminPassMetadata struct {
    64  	// Specifies the initial login password of the administrator account for logging in to an ECS using password authentication.
    65  	// The Linux administrator is root, and the Windows administrator is Administrator.
    66  	//
    67  	// Password complexity requirements:
    68  	// Consists of 8 to 26 characters.
    69  	// Contains at least three of the following character types: uppercase letters, lowercase letters, digits, and special characters !@$%^-_=+[{}]:,./?
    70  	// The password cannot contain the username or the username in reversed order.
    71  	// The Windows ECS password cannot contain the username, the username in reversed order, or more than two consecutive characters in the username.
    72  	AdminPass string `json:"admin_pass,omitempty"`
    73  }
    74  
    75  func (opts CreateOpts) toConfigurationCreateMap() (map[string]interface{}, error) {
    76  	b, err := golangsdk.BuildRequestBody(opts, "")
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	if opts.InstanceConfig.UserData != nil {
    82  		var userData string
    83  		if _, err := base64.StdEncoding.DecodeString(string(opts.InstanceConfig.UserData)); err != nil {
    84  			userData = base64.StdEncoding.EncodeToString(opts.InstanceConfig.UserData)
    85  		} else {
    86  			userData = string(opts.InstanceConfig.UserData)
    87  		}
    88  		b["instance_config"].(map[string]interface{})["user_data"] = &userData
    89  	}
    90  
    91  	return b, nil
    92  }
    93  
    94  func Create(client *golangsdk.ServiceClient, opts CreateOpts) (string, error) {
    95  	b, err := opts.toConfigurationCreateMap()
    96  	if err != nil {
    97  		return "", err
    98  	}
    99  
   100  	raw, err := client.Post(client.ServiceURL("scaling_configuration"), b, nil, &golangsdk.RequestOpts{
   101  		OkCodes: []int{200},
   102  	})
   103  	if err != nil {
   104  		return "", err
   105  	}
   106  
   107  	var res struct {
   108  		ID string `json:"scaling_configuration_id"`
   109  	}
   110  	err = extract.Into(raw.Body, &res)
   111  	return res.ID, err
   112  }