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

     1  package configurations
     2  
     3  import (
     4  	"encoding/base64"
     5  	"log"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     8  	"github.com/huaweicloud/golangsdk/pagination"
     9  )
    10  
    11  type CreateOptsBuilder interface {
    12  	ToConfigurationCreateMap() (map[string]interface{}, error)
    13  }
    14  
    15  type CreateOpts struct {
    16  	Name           string             `json:"scaling_configuration_name" required:"true"`
    17  	InstanceConfig InstanceConfigOpts `json:"instance_config" required:"true"`
    18  }
    19  
    20  func (opts CreateOpts) ToConfigurationCreateMap() (map[string]interface{}, error) {
    21  	b, err := golangsdk.BuildRequestBody(opts, "")
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	log.Printf("[DEBUG] ToConfigurationCreateMap b is: %#v", b)
    26  	log.Printf("[DEBUG] ToConfigurationCreateMap opts is: %#v", opts)
    27  	publicIp := opts.InstanceConfig.PubicIp
    28  	log.Printf("[DEBUG] ToConfigurationCreateMap publicIp is: %#v", publicIp)
    29  
    30  	if publicIp != (PublicIpOpts{}) {
    31  		public_ip := map[string]interface{}{}
    32  		eip := map[string]interface{}{}
    33  		bandwidth := map[string]interface{}{}
    34  		eip_raw := publicIp.Eip
    35  		log.Printf("[DEBUG] ToConfigurationCreateMap eip_raw is: %#v", eip_raw)
    36  		if eip_raw != (EipOpts{}) {
    37  			if eip_raw.IpType != "" {
    38  				eip["ip_type"] = eip_raw.IpType
    39  			}
    40  			bandWidth := eip_raw.Bandwidth
    41  			if bandWidth != (BandwidthOpts{}) {
    42  				if bandWidth.Size > 0 {
    43  					bandwidth["size"] = bandWidth.Size
    44  				}
    45  				if bandWidth.ChargingMode != "" {
    46  					bandwidth["charging_mode"] = bandWidth.ChargingMode
    47  				}
    48  				if bandWidth.ShareType != "" {
    49  					bandwidth["share_type"] = bandWidth.ShareType
    50  				}
    51  				eip["bandwidth"] = bandwidth
    52  			}
    53  			public_ip["eip"] = eip
    54  		}
    55  		b["instance_config"].(map[string]interface{})["public_ip"] = public_ip
    56  	}
    57  	if opts.InstanceConfig.UserData != nil {
    58  		var userData string
    59  		if _, err := base64.StdEncoding.DecodeString(string(opts.InstanceConfig.UserData)); err != nil {
    60  			userData = base64.StdEncoding.EncodeToString(opts.InstanceConfig.UserData)
    61  		} else {
    62  			userData = string(opts.InstanceConfig.UserData)
    63  		}
    64  		b["instance_config"].(map[string]interface{})["user_data"] = &userData
    65  	}
    66  	log.Printf("[DEBUG] ToConfigurationCreateMap b is: %#v", b)
    67  	return b, nil
    68  }
    69  
    70  //InstanceConfigOpts is an inner struct of CreateOpts
    71  type InstanceConfigOpts struct {
    72  	ID          string            `json:"instance_id,omitempty"`
    73  	FlavorRef   string            `json:"flavorRef,omitempty"`
    74  	ImageRef    string            `json:"imageRef,omitempty"`
    75  	Disk        []DiskOpts        `json:"disk,omitempty"`
    76  	SSHKey      string            `json:"key_name,omitempty"`
    77  	Personality []PersonalityOpts `json:"personality,omitempty"`
    78  	PubicIp     PublicIpOpts      `json:"-"`
    79  	// UserData contains configuration information or scripts to use upon launch.
    80  	// Create will base64-encode it for you, if it isn't already.
    81  	UserData []byte                 `json:"-"`
    82  	Metadata map[string]interface{} `json:"metadata,omitempty"` //TODO not sure the type
    83  }
    84  
    85  //DiskOpts is an inner struct of InstanceConfigOpts
    86  type DiskOpts struct {
    87  	Size       int               `json:"size" required:"true"`
    88  	VolumeType string            `json:"volume_type" required:"true"`
    89  	DiskType   string            `json:"disk_type" required:"true"`
    90  	Metadata   map[string]string `json:"metadata,omitempty"`
    91  }
    92  
    93  type PersonalityOpts struct {
    94  	Path    string `json:"path" required:"true"`
    95  	Content string `json:"content" required:"true"`
    96  }
    97  
    98  type PublicIpOpts struct {
    99  	Eip EipOpts `json:"-"`
   100  }
   101  
   102  type EipOpts struct {
   103  	IpType    string
   104  	Bandwidth BandwidthOpts `json:"-"`
   105  }
   106  
   107  type BandwidthOpts struct {
   108  	Size         int
   109  	ShareType    string
   110  	ChargingMode string
   111  }
   112  
   113  //Create is a method by which can be able to access to create a configuration
   114  //of autoscaling
   115  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   116  	b, err := opts.ToConfigurationCreateMap()
   117  	if err != nil {
   118  		r.Err = err
   119  		return
   120  	}
   121  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
   122  		OkCodes: []int{200},
   123  	})
   124  	return
   125  }
   126  
   127  //Get is a method by which can be able to access to get a configuration of
   128  //autoscaling detailed information
   129  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
   130  	_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
   131  	return
   132  }
   133  
   134  //Delete
   135  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
   136  	_, r.Err = client.Delete(deleteURL(client, id), nil)
   137  	return
   138  }
   139  
   140  type ListOptsBuilder interface {
   141  	ToConfigurationListQuery() (string, error)
   142  }
   143  
   144  type ListOpts struct {
   145  	Name    string `q:"scaling_configuration_name"`
   146  	ImageID string `q:"image_id"`
   147  }
   148  
   149  func (opts ListOpts) ToConfigurationListQuery() (string, error) {
   150  	q, err := golangsdk.BuildQueryString(opts)
   151  	return q.String(), err
   152  }
   153  
   154  //List is method that can be able to list all configurations of autoscaling service
   155  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   156  	url := listURL(client)
   157  	if opts != nil {
   158  		query, err := opts.ToConfigurationListQuery()
   159  		if err != nil {
   160  			return pagination.Pager{Err: err}
   161  		}
   162  		url += query
   163  	}
   164  
   165  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   166  		return ConfigurationPage{pagination.SinglePageBase(r)}
   167  	})
   168  }