File: delegate.go

package info (click to toggle)
golang-github-rackspace-gophercloud 1.0.0%2Bgit20161013.1012.e00690e8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,148 kB
  • ctags: 6,414
  • sloc: sh: 16; makefile: 6
file content (60 lines) | stat: -rw-r--r-- 1,796 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package tokens

import (
	"errors"

	"github.com/rackspace/gophercloud"
	os "github.com/rackspace/gophercloud/openstack/identity/v2/tokens"
)

var (
	// ErrPasswordProvided is returned if both a password and an API key are provided to Create.
	ErrPasswordProvided = errors.New("Please provide either a password or an API key.")
)

// AuthOptions wraps the OpenStack AuthOptions struct to be able to customize the request body
// when API key authentication is used.
type AuthOptions struct {
	os.AuthOptions
}

// WrapOptions embeds a root AuthOptions struct in a package-specific one.
func WrapOptions(original gophercloud.AuthOptions) AuthOptions {
	return AuthOptions{AuthOptions: os.WrapOptions(original)}
}

// ToTokenCreateMap serializes an AuthOptions into a request body. If an API key is provided, it
// will be used, otherwise
func (auth AuthOptions) ToTokenCreateMap() (map[string]interface{}, error) {
	if auth.APIKey == "" {
		return auth.AuthOptions.ToTokenCreateMap()
	}

	// Verify that other required attributes are present.
	if auth.Username == "" {
		return nil, os.ErrUsernameRequired
	}

	authMap := make(map[string]interface{})

	authMap["RAX-KSKEY:apiKeyCredentials"] = map[string]interface{}{
		"username": auth.Username,
		"apiKey":   auth.APIKey,
	}

	if auth.TenantID != "" {
		authMap["tenantId"] = auth.TenantID
	}
	if auth.TenantName != "" {
		authMap["tenantName"] = auth.TenantName
	}

	return map[string]interface{}{"auth": authMap}, nil
}

// Create authenticates to Rackspace's identity service and attempts to acquire a Token. Rather
// than interact with this service directly, users should generally call
// rackspace.AuthenticatedClient().
func Create(client *gophercloud.ServiceClient, auth AuthOptions) os.CreateResult {
	return os.Create(client, auth)
}