From 565b203405ca9b51dd85a5ec6a41d73547381ae5 Mon Sep 17 00:00:00 2001 From: ShiChangkuo Date: Tue, 18 May 2021 14:20:33 +0800 Subject: [PATCH] support IAM access key management --- .../identity/v3.0/credentials/requests.go | 96 +++++++++++++++++++ .../identity/v3.0/credentials/results.go | 76 +++++++++++++++ openstack/identity/v3.0/credentials/urls.go | 16 ++++ 3 files changed, 188 insertions(+) create mode 100644 openstack/identity/v3.0/credentials/requests.go create mode 100644 openstack/identity/v3.0/credentials/results.go create mode 100644 openstack/identity/v3.0/credentials/urls.go diff --git a/openstack/identity/v3.0/credentials/requests.go b/openstack/identity/v3.0/credentials/requests.go new file mode 100644 index 000000000..0f2add765 --- /dev/null +++ b/openstack/identity/v3.0/credentials/requests.go @@ -0,0 +1,96 @@ +package credentials + +import ( + "github.com/huaweicloud/golangsdk" +) + +const parentElement = "credential" + +type ListOptsBuilder interface { + ToCredentialListQuery() (string, error) +} + +type ListOpts struct { + UserID string `json:"user_id,omitempty"` +} + +func (opts ListOpts) ToCredentialListQuery() (string, error) { + q, err := golangsdk.BuildQueryString(opts) + if err != nil { + return "", err + } + return q.String(), err +} + +func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) (l ListResult) { + url := rootURL(client) + if opts != nil { + query, err := opts.ToCredentialListQuery() + if err != nil { + l.Err = err + return + } + url += query + } + + _, l.Err = client.Get(url, &l.Body, nil) + return +} + +type CreateOptsBuilder interface { + ToCredentialCreateMap() (map[string]interface{}, error) +} + +type CreateOpts struct { + UserID string `json:"user_id" required:"true"` + Description string `json:"description,omitempty"` +} + +func (opts CreateOpts) ToCredentialCreateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, parentElement) +} + +func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { + b, err := opts.ToCredentialCreateMap() + if err != nil { + r.Err = err + return + } + _, r.Err = client.Post(rootURL(client), &b, &r.Body, nil) + return +} + +type UpdateOptsBuilder interface { + ToCredentialUpdateMap() (map[string]interface{}, error) +} + +type UpdateOpts struct { + Status string `json:"status,omitempty"` + Description string `json:"description,omitempty"` +} + +func (opts UpdateOpts) ToCredentialUpdateMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, parentElement) +} + +func Update(client *golangsdk.ServiceClient, credentialID string, opts UpdateOptsBuilder) (r CreateResult) { + b, err := opts.ToCredentialUpdateMap() + if err != nil { + r.Err = err + return + } + _, r.Err = client.Put(resourceURL(client, credentialID), &b, &r.Body, &golangsdk.RequestOpts{ + OkCodes: []int{200}, + }) + return +} + +func Get(client *golangsdk.ServiceClient, credentialID string) (r GetResult) { + _, r.Err = client.Get(resourceURL(client, credentialID), &r.Body, nil) + return +} + +func Delete(client *golangsdk.ServiceClient, credentialID string) (r DeleteResult) { + _, r.Err = client.Delete(resourceURL(client, credentialID), nil) + return +} diff --git a/openstack/identity/v3.0/credentials/results.go b/openstack/identity/v3.0/credentials/results.go new file mode 100644 index 000000000..2f2eedb6e --- /dev/null +++ b/openstack/identity/v3.0/credentials/results.go @@ -0,0 +1,76 @@ +package credentials + +import "github.com/huaweicloud/golangsdk" + +type Credential struct { + // IAM user ID + UserID string `json:"user_id"` + + // Description of the access key + Description string `json:"description"` + + // AK + AccessKey string `json:"access"` + + // SK, returned only during creation + SecretKey string `json:"secret,omitempty"` + + // Status of the access key, active/inactive + Status string `json:"status"` + + // Time when the access key was created + CreateTime string `json:"create_time"` + + // Time when the access key was last used + LastUseTime string `json:"last_use_time,omitempty"` +} + +type credentialResult struct { + golangsdk.Result +} + +// CreateResult is the response of a Create operations. Call its Extract method to +// interpret it as a Credential. +type CreateResult struct { + credentialResult +} + +// Extract provides access to the Credential returned by the Get and +// Create functions. +func (r credentialResult) Extract() (*Credential, error) { + var s struct { + Credential *Credential `json:"credential"` + } + err := r.ExtractInto(&s) + return s.Credential, err +} + +// GetResult is the response of a Get operations. Call its Extract method to +// interpret it as a Credential. +type GetResult struct { + credentialResult +} + +// UpdateResult is the response from an Update operation. Call its Extract +// method to interpret it as a Credential. +type UpdateResult struct { + credentialResult +} + +type ListResult struct { + golangsdk.Result +} + +func (lr ListResult) Extract() ([]Credential, error) { + var a struct { + Instances []Credential `json:"credentials"` + } + err := lr.Result.ExtractInto(&a) + return a.Instances, err +} + +// DeleteResult is the response from a Delete operation. Call its ExtractErr to +// determine if the request succeeded or failed. +type DeleteResult struct { + golangsdk.ErrResult +} diff --git a/openstack/identity/v3.0/credentials/urls.go b/openstack/identity/v3.0/credentials/urls.go new file mode 100644 index 000000000..a261f6c78 --- /dev/null +++ b/openstack/identity/v3.0/credentials/urls.go @@ -0,0 +1,16 @@ +package credentials + +import "github.com/huaweicloud/golangsdk" + +const ( + rootPath = "OS-CREDENTIAL" + credentialsPath = "credentials" +) + +func rootURL(client *golangsdk.ServiceClient) string { + return client.ServiceURL(rootPath, credentialsPath) +} + +func resourceURL(client *golangsdk.ServiceClient, credID string) string { + return client.ServiceURL(rootPath, credentialsPath, credID) +}