github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/kms/v1/rotation/requests.go (about) 1 package rotation 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 ) 6 7 type RotationOptsBuilder interface { 8 ToKeyRotationMap() (map[string]interface{}, error) 9 } 10 11 type UpdateOptsBuilder interface { 12 ToKeyRotationIntervalMap() (map[string]interface{}, error) 13 } 14 15 type RotationOpts struct { 16 // ID of a CMK 17 KeyID string `json:"key_id" required:"true"` 18 // 36-byte sequence number of a request message 19 Sequence string `json:"sequence,omitempty"` 20 } 21 22 type IntervalOpts struct { 23 // ID of a CMK 24 KeyID string `json:"key_id" required:"true"` 25 // Rotation interval. The value is an integer in the range 30 to 365. 26 // Set the interval based on how often a CMK is used. 27 // If it is frequently used, set a short interval; otherwise, set a long one. 28 Interval int `json:"rotation_interval" required:"true"` 29 // 36-byte sequence number of a request message 30 Sequence string `json:"sequence,omitempty"` 31 } 32 33 // ToKeyRotationMap assembles a request body based on the contents of a 34 // RotationOpts. 35 func (opts RotationOpts) ToKeyRotationMap() (map[string]interface{}, error) { 36 return golangsdk.BuildRequestBody(opts, "") 37 } 38 39 // ToKeyRotationIntervalMap assembles a request body based on the contents of a 40 // IntervalOpts. 41 func (opts IntervalOpts) ToKeyRotationIntervalMap() (map[string]interface{}, error) { 42 return golangsdk.BuildRequestBody(opts, "") 43 } 44 45 // Enable will enable rotation for a CMK based on the values in RotationOpts. 46 // The default rotation interval is 365 days. 47 // CMKs created using imported key materials and Default Master Keys do not support rotation. 48 func Enable(client *golangsdk.ServiceClient, opts RotationOptsBuilder) (r golangsdk.ErrResult) { 49 b, err := opts.ToKeyRotationMap() 50 if err != nil { 51 r.Err = err 52 return 53 } 54 55 _, r.Err = client.Post(enableURL(client), b, &r.Body, nil) 56 return 57 } 58 59 // Disable will disable rotation for a CMK based on the values in RotationOpts. 60 func Disable(client *golangsdk.ServiceClient, opts RotationOptsBuilder) (r golangsdk.ErrResult) { 61 b, err := opts.ToKeyRotationMap() 62 if err != nil { 63 r.Err = err 64 return 65 } 66 67 _, r.Err = client.Post(disableURL(client), b, &r.Body, nil) 68 return 69 } 70 71 // Get retrieves the key with the provided ID. To extract the key object 72 // from the response, call the Extract method on the GetResult. 73 func Get(client *golangsdk.ServiceClient, opts RotationOptsBuilder) (r GetResult) { 74 b, err := opts.ToKeyRotationMap() 75 if err != nil { 76 r.Err = err 77 return 78 } 79 80 _, r.Err = client.Post(getURL(client), &b, &r.Body, nil) 81 return 82 } 83 84 // Update will change the rotation interval for a CMK 85 func Update(client *golangsdk.ServiceClient, opts UpdateOptsBuilder) (r golangsdk.ErrResult) { 86 b, err := opts.ToKeyRotationIntervalMap() 87 if err != nil { 88 r.Err = err 89 return 90 } 91 92 _, r.Err = client.Post(intervalURL(client), b, &r.Body, nil) 93 return 94 }