github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/autoscaling/v1/policies/requests.go (about) 1 package policies 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 ) 6 7 //CreateOptsBuilder is an interface by which can serialize the create parameters 8 type CreateOptsBuilder interface { 9 ToPolicyCreateMap() (map[string]interface{}, error) 10 } 11 12 //CreateOpts is a struct which will be used to create a policy 13 type CreateOpts struct { 14 Name string `json:"scaling_policy_name" required:"true"` 15 ID string `json:"scaling_group_id" required:"true"` 16 Type string `json:"scaling_policy_type" required:"true"` 17 AlarmID string `json:"alarm_id,omitempty"` 18 SchedulePolicy SchedulePolicyOpts `json:"scheduled_policy,omitempty"` 19 Action ActionOpts `json:"scaling_policy_action,omitempty"` 20 CoolDownTime int `json:"cool_down_time,omitempty"` 21 } 22 23 type SchedulePolicyOpts struct { 24 LaunchTime string `json:"launch_time" required:"true"` 25 RecurrenceType string `json:"recurrence_type,omitempty"` 26 RecurrenceValue string `json:"recurrence_value,omitempty"` 27 StartTime string `json:"start_time,omitempty"` 28 EndTime string `json:"end_time,omitempty"` 29 } 30 31 type ActionOpts struct { 32 Operation string `json:"operation,omitempty"` 33 InstanceNum int `json:"instance_number,omitempty"` 34 } 35 36 func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) { 37 return golangsdk.BuildRequestBody(opts, "") 38 } 39 40 //Create is a method which can be able to access to create the policy of autoscaling 41 //service. 42 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 43 b, err := opts.ToPolicyCreateMap() 44 if err != nil { 45 r.Err = err 46 return 47 } 48 49 _, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{ 50 OkCodes: []int{200}, 51 }) 52 return 53 } 54 55 //UpdateOptsBuilder is an interface which can build the map paramter of update function 56 type UpdateOptsBuilder interface { 57 ToPolicyUpdateMap() (map[string]interface{}, error) 58 } 59 60 //UpdateOpts is a struct which represents the parameters of update function 61 type UpdateOpts struct { 62 Name string `json:"scaling_policy_name,omitempty"` 63 Type string `json:"scaling_policy_type,omitempty"` 64 AlarmID string `json:"alarm_id,omitempty"` 65 SchedulePolicy SchedulePolicyOpts `json:"scheduled_policy,omitempty"` 66 Action ActionOpts `json:"scaling_policy_action,omitempty"` 67 CoolDownTime int `json:"cool_down_time,omitempty"` 68 } 69 70 func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) { 71 return golangsdk.BuildRequestBody(opts, "") 72 } 73 74 //Update is a method which can be able to update the policy via accessing to the 75 //autoscaling service with Put method and parameters 76 func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 77 body, err := opts.ToPolicyUpdateMap() 78 if err != nil { 79 r.Err = err 80 return 81 } 82 83 _, r.Err = client.Put(updateURL(client, id), body, &r.Body, &golangsdk.RequestOpts{ 84 OkCodes: []int{200}, 85 }) 86 return 87 } 88 89 //Delete is a method which can be able to access to delete a policy of autoscaling 90 func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { 91 _, r.Err = client.Delete(deleteURL(client, id), nil) 92 return 93 } 94 95 //Get is a method which can be able to access to get a policy detailed information 96 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 97 _, r.Err = client.Get(getURL(client, id), &r.Body, nil) 98 return 99 }