github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/autoscaling/v1/policies/requests.go (about) 1 package policies 2 3 import ( 4 "github.com/chnsz/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 InstancePercentage int `json:"instance_percentage,omitempty"` 35 } 36 37 func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) { 38 return golangsdk.BuildRequestBody(opts, "") 39 } 40 41 // Create is a method which can be able to access to create the policy of autoscaling 42 // service. 43 func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 44 b, err := opts.ToPolicyCreateMap() 45 if err != nil { 46 r.Err = err 47 return 48 } 49 50 _, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{ 51 OkCodes: []int{200}, 52 }) 53 return 54 } 55 56 // UpdateOptsBuilder is an interface which can build the map paramter of update function 57 type UpdateOptsBuilder interface { 58 ToPolicyUpdateMap() (map[string]interface{}, error) 59 } 60 61 // UpdateOpts is a struct which represents the parameters of update function 62 type UpdateOpts struct { 63 Name string `json:"scaling_policy_name,omitempty"` 64 Type string `json:"scaling_policy_type,omitempty"` 65 AlarmID string `json:"alarm_id,omitempty"` 66 SchedulePolicy SchedulePolicyOpts `json:"scheduled_policy,omitempty"` 67 Action ActionOpts `json:"scaling_policy_action,omitempty"` 68 CoolDownTime int `json:"cool_down_time,omitempty"` 69 } 70 71 func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) { 72 return golangsdk.BuildRequestBody(opts, "") 73 } 74 75 // Update is a method which can be able to update the policy via accessing to the 76 // autoscaling service with Put method and parameters 77 func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 78 body, err := opts.ToPolicyUpdateMap() 79 if err != nil { 80 r.Err = err 81 return 82 } 83 84 _, r.Err = client.Put(updateURL(client, id), body, &r.Body, &golangsdk.RequestOpts{ 85 OkCodes: []int{200}, 86 }) 87 return 88 } 89 90 // Delete is a method which can be able to access to delete a policy of autoscaling 91 func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { 92 _, r.Err = client.Delete(deleteURL(client, id), nil) 93 return 94 } 95 96 // Get is a method which can be able to access to get a policy detailed information 97 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 98 _, r.Err = client.Get(getURL(client, id), &r.Body, nil) 99 return 100 } 101 102 // ListOpts is the structure that used to query the policies of scaling group. 103 type ListOpts struct { 104 // The scaling group ID. 105 GroupID string `json:"scaling_group_id" required:"true"` 106 // The scaling policy name. 107 Name string `q:"scaling_policy_name"` 108 // The scaling policy type. 109 Type string `q:"scaling_policy_type"` 110 // The scaling policy ID. 111 PolicyID string `q:"scaling_policy_id"` 112 // Start number value. The value must be a positive integer. 113 StartNumber int `q:"start_number"` 114 // Number of records displayed per page. 115 // The value must be a positive integer. 116 Limit int `q:"limit"` 117 } 118 119 // List is a method used to query the policies of scaling group with given parameters. 120 // Due to API limitations, there can be a maximum of 10 policies, so pagination is not considered here, 121 // simply call the API instead. 122 func List(client *golangsdk.ServiceClient, opts ListOpts) (r ListResult) { 123 query, err := golangsdk.BuildQueryString(opts) 124 if err != nil { 125 r.Err = err 126 return 127 } 128 129 url := listURL(client, opts.GroupID) + query.String() 130 _, r.Err = client.Get(url, &r.Body, nil) 131 return 132 }