From 2b8f8ef8edd7bd80eb0365273775a13a3686cd59 Mon Sep 17 00:00:00 2001 From: Kristen Carlson Accardi Date: Fri, 29 Apr 2016 11:07:43 -0700 Subject: [PATCH 1/4] Add ExtractProject() to identity/v3/tokens Add the ability to extract project information out of a scoped v3 token. --- openstack/identity/v3/tokens/results.go | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/openstack/identity/v3/tokens/results.go b/openstack/identity/v3/tokens/results.go index d134f7d4..98a84878 100644 --- a/openstack/identity/v3/tokens/results.go +++ b/openstack/identity/v3/tokens/results.go @@ -46,6 +46,12 @@ 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"` +} + // commonResult is the deferred result of a Create or a Get call. type commonResult struct { gophercloud.Result @@ -118,6 +124,26 @@ func createErr(err error) CreateResult { } } +// 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 +} + // GetResult is the deferred response from a Get call. type GetResult struct { commonResult From db80cca473fb470aa24332866c19b8217b686704 Mon Sep 17 00:00:00 2001 From: Kristen Carlson Accardi Date: Fri, 29 Apr 2016 13:30:45 -0700 Subject: [PATCH 2/4] Add ExtractRoles() to identity/v3/tokens Return a list of the roles associated with a scoped token. --- openstack/identity/v3/tokens/results.go | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/openstack/identity/v3/tokens/results.go b/openstack/identity/v3/tokens/results.go index 98a84878..8bce3faa 100644 --- a/openstack/identity/v3/tokens/results.go +++ b/openstack/identity/v3/tokens/results.go @@ -52,6 +52,16 @@ type Project struct { 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 +} + // commonResult is the deferred result of a Create or a Get call. type commonResult struct { gophercloud.Result @@ -144,6 +154,26 @@ func (result GetResult) ExtractProject() (*Project, error) { 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 From 91c8ca301356b9e69ab1236b188c0950f7cac0b9 Mon Sep 17 00:00:00 2001 From: Kristen Carlson Accardi Date: Fri, 29 Apr 2016 15:53:07 -0700 Subject: [PATCH 3/4] move ExtractServiceCatalog() to commonResult The service catalog object can be retrieved from a GET request when doing token validation as well as the POST request. --- openstack/identity/v3/tokens/results.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openstack/identity/v3/tokens/results.go b/openstack/identity/v3/tokens/results.go index 8bce3faa..021ed962 100644 --- a/openstack/identity/v3/tokens/results.go +++ b/openstack/identity/v3/tokens/results.go @@ -102,7 +102,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 } From 36271d26fbc79fd74690ba1ad9b6c461f4361eaa Mon Sep 17 00:00:00 2001 From: Kristen Carlson Accardi Date: Fri, 29 Apr 2016 16:24:02 -0700 Subject: [PATCH 4/4] Add ExtractUser() to identity/v3/tokens Get user information out of token information from a GET response --- openstack/identity/v3/tokens/results.go | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/openstack/identity/v3/tokens/results.go b/openstack/identity/v3/tokens/results.go index 021ed962..d21d1c6d 100644 --- a/openstack/identity/v3/tokens/results.go +++ b/openstack/identity/v3/tokens/results.go @@ -62,6 +62,17 @@ type Roles struct { Entries []RoleEntry } +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 @@ -135,6 +146,26 @@ 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 {