@@ -129,3 +129,78 @@ func Get(client *gophercloud.ServiceClient, groupID, policyID string) GetResult
129129
130130 return result
131131}
132+
133+ // UpdateOptsBuilder is the interface responsible for generating the map
134+ // structure for producing JSON for an Update operation.
135+ type UpdateOptsBuilder interface {
136+ ToPolicyUpdateMap () (map [string ]interface {}, error )
137+ }
138+
139+ // UpdateOpts represents the options for updating an existing policy.
140+ //
141+ // Update operations completely replace the configuration being updated. Empty
142+ // values in the update are accepted and overwrite previously specified
143+ // parameters.
144+ type UpdateOpts struct {
145+ // Name [required] is a name for the policy.
146+ Name string
147+
148+ // Type [required] of policy, i.e. either "webhook" or "schedule".
149+ Type Type
150+
151+ // Cooldown [required] period in seconds. If you don't specify a cooldown,
152+ // it will default to zero, and the policy will be configured as such.
153+ Cooldown int
154+
155+ // Adjustment [requried] type and value for the policy.
156+ Adjustment Adjustment
157+
158+ // Additional configuration options for some types of policy.
159+ Args map [string ]interface {}
160+ }
161+
162+ // ToPolicyUpdateMap converts an UpdateOpts struct into a map for use as the
163+ // request body in an Update request.
164+ func (opts UpdateOpts ) ToPolicyUpdateMap () (map [string ]interface {}, error ) {
165+ if opts .Name == "" {
166+ return nil , ErrNoName
167+ }
168+
169+ if opts .Type == Schedule && opts .Args == nil {
170+ return nil , ErrNoArgs
171+ }
172+
173+ policy := make (map [string ]interface {})
174+
175+ policy ["name" ] = opts .Name
176+ policy ["type" ] = opts .Type
177+ policy ["cooldown" ] = opts .Cooldown
178+
179+ // TODO: Function to validate and cast key + value?
180+ policy [string (opts .Adjustment .Type )] = opts .Adjustment .Value
181+
182+ if opts .Args != nil {
183+ policy ["args" ] = opts .Args
184+ }
185+
186+ return policy , nil
187+ }
188+
189+ // Update requests the configuration of the given policy be updated.
190+ func Update (client * gophercloud.ServiceClient , groupID , policyID string , opts UpdateOptsBuilder ) UpdateResult {
191+ var result UpdateResult
192+
193+ url := updateURL (client , groupID , policyID )
194+ reqBody , err := opts .ToPolicyUpdateMap ()
195+
196+ if err != nil {
197+ result .Err = err
198+ return result
199+ }
200+
201+ _ , result .Err = client .Put (url , reqBody , nil , & gophercloud.RequestOpts {
202+ OkCodes : []int {204 },
203+ })
204+
205+ return result
206+ }
0 commit comments