Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

[rfr] Extract additional info from GET #564

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
90 changes: 89 additions & 1 deletion openstack/identity/v3/tokens/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ type ServiceCatalog struct {
Entries []CatalogEntry
}

// Project contains project information extracted from a scoped token.
type Project struct {
ID string `mapstructure:"id"`
Name string `mapstructure:"name"`
}

// RoleEntry contains the name of a user's role in a project.
type RoleEntry struct {
Name string `mapstructure:"name"`
}

// Roles contains a list of role names extracted from a token.
type Roles struct {
Entries []RoleEntry
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a tag here?

}

type Domain struct {
ID string `mapstructure:"id"`
Name string `mapstructure:"name"`
}

type User struct {
ValidDomain Domain `mapstructure:"domain"`
ID string `mapstructure:"id"`
Name string `mapstructure:"name"`
}

// commonResult is the deferred result of a Create or a Get call.
type commonResult struct {
gophercloud.Result
Expand Down Expand Up @@ -86,7 +113,8 @@ func (r commonResult) ExtractToken() (*Token, error) {
}

// ExtractServiceCatalog returns the ServiceCatalog that was generated along with the user's Token.
func (result CreateResult) ExtractServiceCatalog() (*ServiceCatalog, error) {
// It can also be retrieved as the result of a GET response to a token validation
func (result commonResult) ExtractServiceCatalog() (*ServiceCatalog, error) {
if result.Err != nil {
return nil, result.Err
}
Expand Down Expand Up @@ -118,6 +146,66 @@ func createErr(err error) CreateResult {
}
}

// ExtractUser returns the User object from a token
func (r GetResult) ExtractUser() (*User, error) {
if r.Err != nil {
return nil, r.Err
}

var response struct {
Token struct {
ValidUser User `mapstructure:"user"`
} `mapstructure:"token"`
}

err := mapstructure.Decode(r.Body, &response)
if err != nil {
return nil, err
}

return &response.Token.ValidUser, nil
}

// ExtractProject returns project information from a GET token request.
func (result GetResult) ExtractProject() (*Project, error) {
if result.Err != nil {
return nil, result.Err
}

var response struct {
Token struct {
ValidProject Project `mapstructure:"project"`
} `mapstructure:"token"`
}

err := mapstructure.Decode(result.Body, &response)
if err != nil {
return nil, err
}

return &response.Token.ValidProject, nil
}

// ExtractRoles gets a list of project role names from a GET token request.
func (result GetResult) ExtractRoles() (*Roles, error) {
if result.Err != nil {
return nil, result.Err
}

var response struct {
Token struct {
ValidRoles []RoleEntry `mapstructure:"roles"`
} `mapstructure:"token"`
}

err := mapstructure.Decode(result.Body, &response)
if err != nil {
return nil, err
}

return &Roles{Entries: response.Token.ValidRoles}, nil
}

// GetResult is the deferred response from a Get call.
type GetResult struct {
commonResult
Expand Down