Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions openstack/identity/v3.0/credentials/requests.go
Original file line number Diff line number Diff line change
@@ -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
}
76 changes: 76 additions & 0 deletions openstack/identity/v3.0/credentials/results.go
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions openstack/identity/v3.0/credentials/urls.go
Original file line number Diff line number Diff line change
@@ -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)
}