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

     1  package instances
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  //ListOptsBuilder is an interface by which can be able to build the query string
     9  //of the list function
    10  type ListOptsBuilder interface {
    11  	ToInstancesListQuery() (string, error)
    12  }
    13  
    14  type ListOpts struct {
    15  	LifeCycleStatus string `q:"life_cycle_state"`
    16  	HealthStatus    string `q:"health_status"`
    17  }
    18  
    19  func (opts ListOpts) ToInstancesListQuery() (string, error) {
    20  	q, err := golangsdk.BuildQueryString(opts)
    21  	return q.String(), err
    22  }
    23  
    24  //List is a method by which can be able to access the list function that can get
    25  //instances of a group
    26  func List(client *golangsdk.ServiceClient, groupID string, opts ListOptsBuilder) pagination.Pager {
    27  	url := listURL(client, groupID)
    28  	if opts != nil {
    29  		q, err := opts.ToInstancesListQuery()
    30  		if err != nil {
    31  			return pagination.Pager{Err: err}
    32  		}
    33  		url += q
    34  	}
    35  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    36  		return InstancePage{pagination.SinglePageBase(r)}
    37  	})
    38  }
    39  
    40  //DeleteOptsBuilder is an interface by whick can be able to build the query string
    41  //of instance deletion
    42  type DeleteOptsBuilder interface {
    43  	ToInstanceDeleteQuery() (string, error)
    44  }
    45  
    46  type DeleteOpts struct {
    47  	DeleteInstance bool `q:"instance_delete"`
    48  }
    49  
    50  func (opts DeleteOpts) ToInstanceDeleteQuery() (string, error) {
    51  	q, err := golangsdk.BuildQueryString(opts)
    52  	return q.String(), err
    53  }
    54  
    55  //Delete is a method by which can be able to delete an instance from a group
    56  func Delete(client *golangsdk.ServiceClient, id string, opts DeleteOptsBuilder) (r DeleteResult) {
    57  	url := deleteURL(client, id)
    58  	if opts != nil {
    59  		q, err := opts.ToInstanceDeleteQuery()
    60  		if err != nil {
    61  			r.Err = err
    62  			return
    63  		}
    64  		url += q
    65  	}
    66  	_, r.Err = client.Delete(url, nil)
    67  	return
    68  }
    69  
    70  //BatchOptsBuilder is an interface which can build the query body of batch operation
    71  type BatchOptsBuilder interface {
    72  	ToInstanceBatchMap() (map[string]interface{}, error)
    73  }
    74  
    75  //BatchOpts is a struct which represents parameters of batch operations
    76  type BatchOpts struct {
    77  	Instances   []string `json:"instances_id" required:"true"`
    78  	IsDeleteEcs string   `json:"instance_delete,omitempty"`
    79  	Action      string   `json:"action,omitempty"`
    80  }
    81  
    82  func (opts BatchOpts) ToInstanceBatchMap() (map[string]interface{}, error) {
    83  	return golangsdk.BuildRequestBody(opts, "")
    84  }
    85  
    86  //batch is method which can be able to add/delete numbers instances
    87  func batch(client *golangsdk.ServiceClient, groupID string, opts BatchOptsBuilder) (r BatchResult) {
    88  	b, err := opts.ToInstanceBatchMap()
    89  	if err != nil {
    90  		r.Err = err
    91  		return
    92  	}
    93  	_, r.Err = client.Post(batchURL(client, groupID), b, nil, &golangsdk.RequestOpts{
    94  		OkCodes: []int{204},
    95  	})
    96  	return
    97  }
    98  
    99  //BatchAdd is a method by which can add numbers of instances into a group
   100  func BatchAdd(client *golangsdk.ServiceClient, groupID string, instances []string) (r BatchResult) {
   101  	var opts = BatchOpts{
   102  		Instances: instances,
   103  		Action:    "ADD",
   104  	}
   105  	return batch(client, groupID, opts)
   106  }
   107  
   108  //BatchDelete is a method by which can delete numbers of instances from a group
   109  func BatchDelete(client *golangsdk.ServiceClient, groupID string, instances []string, deleteEcs string) (r BatchResult) {
   110  	var opts = BatchOpts{
   111  		Instances:   instances,
   112  		IsDeleteEcs: deleteEcs,
   113  		Action:      "REMOVE",
   114  	}
   115  	return batch(client, groupID, opts)
   116  }