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

     1  package groups
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  //CreateGroupBuilder is an interface from which can build the request of creating group
     9  type CreateOptsBuilder interface {
    10  	ToGroupCreateMap() (map[string]interface{}, error)
    11  }
    12  
    13  //CreateGroupOps is a struct contains the parameters of creating group
    14  type CreateOpts struct {
    15  	Name                      string              `json:"scaling_group_name" required:"true"`
    16  	ConfigurationID           string              `json:"scaling_configuration_id,omitempty"`
    17  	DesireInstanceNumber      int                 `json:"desire_instance_number,omitempty"`
    18  	MinInstanceNumber         int                 `json:"min_instance_number,omitempty"`
    19  	MaxInstanceNumber         int                 `json:"max_instance_number,omitempty"`
    20  	CoolDownTime              int                 `json:"cool_down_time,omitempty"`
    21  	LBListenerID              string              `json:"lb_listener_id,omitempty"`
    22  	LBaaSListeners            []LBaaSListenerOpts `json:"lbaas_listeners,omitempty"`
    23  	AvailableZones            []string            `json:"available_zones,omitempty"`
    24  	Networks                  []NetworkOpts       `json:"networks" required:"ture"`
    25  	SecurityGroup             []SecurityGroupOpts `json:"security_groups" required:"ture"`
    26  	VpcID                     string              `json:"vpc_id" required:"ture"`
    27  	HealthPeriodicAuditMethod string              `json:"health_periodic_audit_method,omitempty"`
    28  	HealthPeriodicAuditTime   int                 `json:"health_periodic_audit_time,omitempty"`
    29  	HealthPeriodicAuditGrace  int                 `json:"health_periodic_audit_grace_period,omitempty"`
    30  	InstanceTerminatePolicy   string              `json:"instance_terminate_policy,omitempty"`
    31  	Notifications             []string            `json:"notifications,omitempty"`
    32  	IsDeletePublicip          bool                `json:"delete_publicip,omitempty"`
    33  	EnterpriseProjectID       string              `json:"enterprise_project_id,omitempty"`
    34  }
    35  
    36  type NetworkOpts struct {
    37  	ID string `json:"id,omitempty"`
    38  }
    39  
    40  type SecurityGroupOpts struct {
    41  	ID string `json:"id,omitempty"`
    42  }
    43  
    44  type LBaaSListenerOpts struct {
    45  	PoolID       string `json:"pool_id" required:"true"`
    46  	ProtocolPort int    `json:"protocol_port" required:"true"`
    47  	Weight       int    `json:"weight,omitempty"`
    48  }
    49  
    50  func (opts CreateOpts) ToGroupCreateMap() (map[string]interface{}, error) {
    51  	return golangsdk.BuildRequestBody(opts, "")
    52  }
    53  
    54  //CreateGroup is a method of creating group
    55  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    56  	b, err := opts.ToGroupCreateMap()
    57  	if err != nil {
    58  		r.Err = err
    59  		return
    60  	}
    61  	_, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{
    62  		OkCodes: []int{200},
    63  	})
    64  	return
    65  }
    66  
    67  //DeleteGroup is a method of deleting a group by group id
    68  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
    69  	_, r.Err = client.Delete(deleteURL(client, id), nil)
    70  	return
    71  }
    72  
    73  //GetGroup is a method of getting the detailed information of the group by id
    74  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    75  	_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
    76  	return
    77  }
    78  
    79  type ListOptsBuilder interface {
    80  	ToGroupListQuery() (string, error)
    81  }
    82  
    83  type ListOpts struct {
    84  	Name            string `q:"scaling_group_name"`
    85  	ConfigurationID string `q:"scaling_configuration_id"`
    86  	Status          string `q:"scaling_group_status"`
    87  }
    88  
    89  // ToGroupListQuery formats a ListOpts into a query string.
    90  func (opts ListOpts) ToGroupListQuery() (string, error) {
    91  	q, err := golangsdk.BuildQueryString(opts)
    92  	return q.String(), err
    93  }
    94  
    95  func List(client *golangsdk.ServiceClient, ops ListOptsBuilder) pagination.Pager {
    96  	url := listURL(client)
    97  	if ops != nil {
    98  		q, err := ops.ToGroupListQuery()
    99  		if err != nil {
   100  			return pagination.Pager{Err: err}
   101  		}
   102  		url += q
   103  	}
   104  
   105  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   106  		return GroupPage{pagination.SinglePageBase(r)}
   107  	})
   108  }
   109  
   110  //UpdateOptsBuilder is an interface which can build the map paramter of update function
   111  type UpdateOptsBuilder interface {
   112  	ToGroupUpdateMap() (map[string]interface{}, error)
   113  }
   114  
   115  //UpdateOpts is a struct which represents the parameters of update function
   116  type UpdateOpts struct {
   117  	Name                      string              `json:"scaling_group_name,omitempty"`
   118  	DesireInstanceNumber      int                 `json:"desire_instance_number"`
   119  	MinInstanceNumber         int                 `json:"min_instance_number"`
   120  	MaxInstanceNumber         int                 `json:"max_instance_number"`
   121  	CoolDownTime              int                 `json:"cool_down_time,omitempty"`
   122  	LBListenerID              string              `json:"lb_listener_id,omitempty"`
   123  	LBaaSListeners            []LBaaSListenerOpts `json:"lbaas_listeners,omitempty"`
   124  	AvailableZones            []string            `json:"available_zones,omitempty"`
   125  	Networks                  []NetworkOpts       `json:"networks,omitempty"`
   126  	SecurityGroup             []SecurityGroupOpts `json:"security_groups,omitempty"`
   127  	HealthPeriodicAuditMethod string              `json:"health_periodic_audit_method,omitempty"`
   128  	HealthPeriodicAuditTime   int                 `json:"health_periodic_audit_time,omitempty"`
   129  	HealthPeriodicAuditGrace  int                 `json:"health_periodic_audit_grace_period,omitempty"`
   130  	InstanceTerminatePolicy   string              `json:"instance_terminate_policy,omitempty"`
   131  	Notifications             []string            `json:"notifications,omitempty"`
   132  	IsDeletePublicip          bool                `json:"delete_publicip,omitempty"`
   133  	ConfigurationID           string              `json:"scaling_configuration_id,omitempty"`
   134  	EnterpriseProjectID       string              `json:"enterprise_project_id,omitempty"`
   135  }
   136  
   137  func (opts UpdateOpts) ToGroupUpdateMap() (map[string]interface{}, error) {
   138  	return golangsdk.BuildRequestBody(opts, "")
   139  }
   140  
   141  //Update is a method which can be able to update the group via accessing to the
   142  //autoscaling service with Put method and parameters
   143  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   144  	body, err := opts.ToGroupUpdateMap()
   145  	if err != nil {
   146  		r.Err = err
   147  		return
   148  	}
   149  
   150  	_, r.Err = client.Put(updateURL(client, id), body, &r.Body, &golangsdk.RequestOpts{
   151  		OkCodes: []int{200},
   152  	})
   153  	return
   154  }
   155  
   156  type ActionOptsBuilder interface {
   157  	ToActionMap() (map[string]interface{}, error)
   158  }
   159  
   160  type ActionOpts struct {
   161  	Action string `json:"action" required:"true"`
   162  }
   163  
   164  func (opts ActionOpts) ToActionMap() (map[string]interface{}, error) {
   165  	return golangsdk.BuildRequestBody(opts, "")
   166  }
   167  
   168  func doAction(client *golangsdk.ServiceClient, id string, opts ActionOptsBuilder) (r ActionResult) {
   169  	b, err := opts.ToActionMap()
   170  	if err != nil {
   171  		r.Err = err
   172  		return
   173  	}
   174  	_, r.Err = client.Post(enableURL(client, id), &b, nil, &golangsdk.RequestOpts{
   175  		OkCodes: []int{204},
   176  	})
   177  	return
   178  }
   179  
   180  //Enable is an operation by which can make the group enable service
   181  func Enable(client *golangsdk.ServiceClient, id string) (r ActionResult) {
   182  	opts := ActionOpts{
   183  		Action: "resume",
   184  	}
   185  	return doAction(client, id, opts)
   186  }
   187  
   188  //Disable is an operation by which can be able to pause the group
   189  func Disable(client *golangsdk.ServiceClient, id string) (r ActionResult) {
   190  	opts := ActionOpts{
   191  		Action: "pause",
   192  	}
   193  	return doAction(client, id, opts)
   194  }