no naked returns in go; fix auth v3 unit tests
diff --git a/openstack/identity/v2/tokens/requests.go b/openstack/identity/v2/tokens/requests.go
index 12c0f17..73e0b91 100644
--- a/openstack/identity/v2/tokens/requests.go
+++ b/openstack/identity/v2/tokens/requests.go
@@ -2,6 +2,32 @@
import "github.com/gophercloud/gophercloud"
+type PasswordCredentialsV2 struct {
+ Username string `json:"username" required:"true"`
+ Password string `json:"password" required:"true"`
+}
+
+type TokenCredentialsV2 struct {
+ ID string `json:"id,omitempty" required:"true"`
+}
+
+// AuthOptionsV2 wraps a gophercloud AuthOptions in order to adhere to the AuthOptionsBuilder
+// interface.
+type AuthOptionsV2 struct {
+ PasswordCredentials *PasswordCredentialsV2 `json:"passwordCredentials,omitempty" xor:"TokenCredentials"`
+
+ // The TenantID and TenantName fields are optional for the Identity V2 API.
+ // Some providers allow you to specify a TenantName instead of the TenantId.
+ // Some require both. Your provider's authentication policies will determine
+ // how these fields influence authentication.
+ TenantID string `json:"tenantId,omitempty"`
+ TenantName string `json:"tenantName,omitempty"`
+
+ // TokenCredentials allows users to authenticate (possibly as another user) with an
+ // authentication token ID.
+ TokenCredentials *TokenCredentialsV2 `json:"token,omitempty" xor:"PasswordCredentials"`
+}
+
// AuthOptionsBuilder describes any argument that may be passed to the Create call.
type AuthOptionsBuilder interface {
// ToTokenCreateMap assembles the Create request body, returning an error if parameters are
@@ -9,6 +35,38 @@
ToTokenV2CreateMap() (map[string]interface{}, error)
}
+// AuthOptions wraps a gophercloud AuthOptions in order to adhere to the AuthOptionsBuilder
+// interface.
+type AuthOptions struct {
+ gophercloud.AuthOptions
+}
+
+// ToTokenV2CreateMap allows AuthOptions to satisfy the AuthOptionsBuilder
+// interface in the v2 tokens package
+func (opts AuthOptions) ToTokenV2CreateMap() (map[string]interface{}, error) {
+ v2Opts := AuthOptionsV2{
+ TenantID: opts.TenantID,
+ TenantName: opts.TenantName,
+ }
+
+ if opts.Password != "" {
+ v2Opts.PasswordCredentials = &PasswordCredentialsV2{
+ Username: opts.Username,
+ Password: opts.Password,
+ }
+ } else {
+ v2Opts.TokenCredentials = &TokenCredentialsV2{
+ ID: opts.TokenID,
+ }
+ }
+
+ b, err := gophercloud.BuildRequestBody(v2Opts, "auth")
+ if err != nil {
+ return nil, err
+ }
+ return b, nil
+}
+
// Create authenticates to the identity service and attempts to acquire a Token.
// If successful, the CreateResult
// Generally, rather than interact with this call directly, end users should call openstack.AuthenticatedClient(),
@@ -22,6 +80,7 @@
_, r.Err = client.Post(CreateURL(client), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 203},
})
+ return
}
// Get validates and retrieves information for user's token.
@@ -29,4 +88,5 @@
_, r.Err = client.Get(GetURL(client, token), &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 203},
})
+ return
}
diff --git a/openstack/identity/v2/tokens/requests_test.go b/openstack/identity/v2/tokens/requests_test.go
index d25c2d7..0cbb00c 100644
--- a/openstack/identity/v2/tokens/requests_test.go
+++ b/openstack/identity/v2/tokens/requests_test.go
@@ -1,7 +1,6 @@
package tokens
import (
- "reflect"
"testing"
"github.com/gophercloud/gophercloud"
@@ -13,7 +12,8 @@
th.SetupHTTP()
defer th.TeardownHTTP()
HandleTokenPost(t, requestJSON)
- return Create(client.ServiceClient(), options)
+
+ return Create(client.ServiceClient(), AuthOptions{options})
}
func tokenPostErr(t *testing.T, options gophercloud.AuthOptions, expectedErr error) {
@@ -21,30 +21,15 @@
defer th.TeardownHTTP()
HandleTokenPost(t, "")
- actualErr := Create(client.ServiceClient(), options).Err
- th.CheckDeepEquals(t, reflect.TypeOf(expectedErr), reflect.TypeOf(actualErr))
-}
-
-func TestCreateWithToken(t *testing.T) {
- options := gophercloud.AuthOptions{
- TokenID: "cbc36478b0bd8e67e89469c7749d4127",
- }
-
- IsSuccessful(t, tokenPost(t, options, `
- {
- "auth": {
- "token": {
- "id": "cbc36478b0bd8e67e89469c7749d4127"
- }
- }
- }
- `))
+ actualErr := Create(client.ServiceClient(), AuthOptions{options}).Err
+ th.CheckDeepEquals(t, expectedErr, actualErr)
}
func TestCreateWithPassword(t *testing.T) {
- options := gophercloud.AuthOptions{}
- options.Username = "me"
- options.Password = "swordfish"
+ options := gophercloud.AuthOptions{
+ Username: "me",
+ Password: "swordfish",
+ }
IsSuccessful(t, tokenPost(t, options, `
{
@@ -59,10 +44,11 @@
}
func TestCreateTokenWithTenantID(t *testing.T) {
- options := gophercloud.AuthOptions{}
- options.Username = "me"
- options.Password = "opensesame"
- options.TenantID = "fc394f2ab2df4114bde39905f800dc57"
+ options := gophercloud.AuthOptions{
+ Username: "me",
+ Password: "opensesame",
+ TenantID: "fc394f2ab2df4114bde39905f800dc57",
+ }
IsSuccessful(t, tokenPost(t, options, `
{
@@ -78,10 +64,11 @@
}
func TestCreateTokenWithTenantName(t *testing.T) {
- options := gophercloud.AuthOptions{}
- options.Username = "me"
- options.Password = "opensesame"
- options.TenantName = "demo"
+ options := gophercloud.AuthOptions{
+ Username: "me",
+ Password: "opensesame",
+ TenantName: "demo",
+ }
IsSuccessful(t, tokenPost(t, options, `
{
@@ -97,29 +84,18 @@
}
func TestRequireUsername(t *testing.T) {
- options := gophercloud.AuthOptions{}
- options.Password = "thing"
+ options := gophercloud.AuthOptions{
+ Password: "thing",
+ }
- expected := gophercloud.ErrMissingInput{}
- expected.Argument = "tokens.AuthOptions.Username/tokens.AuthOptions.TokenID"
- expected.Info = "You must provide either username/password or tenantID/token values."
- tokenPostErr(t, options, expected)
+ tokenPostErr(t, options, gophercloud.ErrMissingInput{Argument: "Username"})
}
-func TestRequirePassword(t *testing.T) {
- options := gophercloud.AuthOptions{}
- options.Username = "me"
-
- expected := gophercloud.ErrMissingInput{}
- expected.Argument = "tokens.AuthOptions.Password"
- tokenPostErr(t, options, expected)
-}
-
-func tokenGet(t *testing.T, tokenID string) GetResult {
+func tokenGet(t *testing.T, tokenId string) GetResult {
th.SetupHTTP()
defer th.TeardownHTTP()
- HandleTokenGet(t, tokenID)
- return Get(client.ServiceClient(), tokenID)
+ HandleTokenGet(t, tokenId)
+ return Get(client.ServiceClient(), tokenId)
}
func TestGetWithToken(t *testing.T) {