github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/autoscaling/v1/instances/requests.go (about) 1 package instances 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 "github.com/chnsz/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 Action string `json:"action" required:"true"` 78 Instances []string `json:"instances_id" required:"true"` 79 AppendEcs string `json:"instance_append,omitempty"` 80 IsDeleteEcs string `json:"instance_delete,omitempty"` 81 } 82 83 func (opts BatchOpts) ToInstanceBatchMap() (map[string]interface{}, error) { 84 return golangsdk.BuildRequestBody(opts, "") 85 } 86 87 // BatchAction is method which can be able to add/delete/protect/un-protect/standby/un-standby numbers instances 88 func BatchAction(client *golangsdk.ServiceClient, groupID string, opts BatchOptsBuilder) (r BatchResult) { 89 b, err := opts.ToInstanceBatchMap() 90 if err != nil { 91 r.Err = err 92 return 93 } 94 _, r.Err = client.Post(batchURL(client, groupID), b, nil, &golangsdk.RequestOpts{ 95 OkCodes: []int{204}, 96 }) 97 return 98 } 99 100 // BatchAdd is a method by which can add numbers of instances into a group 101 func BatchAdd(client *golangsdk.ServiceClient, groupID string, instances []string) (r BatchResult) { 102 var opts = BatchOpts{ 103 Instances: instances, 104 Action: "ADD", 105 } 106 return BatchAction(client, groupID, opts) 107 } 108 109 // BatchDelete is a method by which can delete numbers of instances from a group 110 func BatchDelete(client *golangsdk.ServiceClient, groupID string, instances []string, deleteEcs string) (r BatchResult) { 111 var opts = BatchOpts{ 112 Instances: instances, 113 IsDeleteEcs: deleteEcs, 114 Action: "REMOVE", 115 } 116 return BatchAction(client, groupID, opts) 117 }