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