forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
129 lines (98 loc) · 3.47 KB
/
results.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package users
import (
"strconv"
"github.com/rackspace/gophercloud"
os "github.com/rackspace/gophercloud/openstack/identity/v2/users"
"github.com/mitchellh/mapstructure"
)
// User represents a user resource that exists on the API.
type User struct {
// The UUID for this user.
ID string
// The human name for this user.
Name string
// The username for this user.
Username string
// Indicates whether the user is enabled (true) or disabled (false).
Enabled bool
// The email address for this user.
Email string
// The ID of the tenant to which this user belongs.
TenantID string `mapstructure:"tenant_id"`
// Specifies the default region for the user account. This value is inherited
// from the user administrator when the account is created.
DefaultRegion string `mapstructure:"RAX-AUTH:defaultRegion"`
// Identifies the domain that contains the user account. This value is
// inherited from the user administrator when the account is created.
DomainID string `mapstructure:"RAX-AUTH:domainId"`
// The password value that the user needs for authentication. If the Add user
// request included a password value, this attribute is not included in the
// response.
Password string `mapstructure:"OS-KSADM:password"`
// Indicates whether the user has enabled multi-factor authentication.
MultiFactorEnabled bool `mapstructure:"RAX-AUTH:multiFactorEnabled"`
}
// CreateResult represents the result of a Create operation
type CreateResult struct {
os.CreateResult
}
// GetResult represents the result of a Get operation
type GetResult struct {
os.GetResult
}
// UpdateResult represents the result of an Update operation
type UpdateResult struct {
os.UpdateResult
}
func commonExtract(resp interface{}, err error) (*User, error) {
if err != nil {
return nil, err
}
var respStruct struct {
User *User `json:"user"`
}
// Since the API returns a string instead of a bool, we need to hack the JSON
json := resp.(map[string]interface{})
user := json["user"].(map[string]interface{})
if s, ok := user["RAX-AUTH:multiFactorEnabled"].(string); ok && s != "" {
if b, err := strconv.ParseBool(s); err == nil {
user["RAX-AUTH:multiFactorEnabled"] = b
}
}
err = mapstructure.Decode(json, &respStruct)
return respStruct.User, err
}
// Extract will get the Snapshot object out of the GetResult object.
func (r GetResult) Extract() (*User, error) {
return commonExtract(r.Body, r.Err)
}
// Extract will get the Snapshot object out of the CreateResult object.
func (r CreateResult) Extract() (*User, error) {
return commonExtract(r.Body, r.Err)
}
// Extract will get the Snapshot object out of the UpdateResult object.
func (r UpdateResult) Extract() (*User, error) {
return commonExtract(r.Body, r.Err)
}
// ResetAPIKeyResult represents the server response to the ResetAPIKey method.
type ResetAPIKeyResult struct {
gophercloud.Result
}
// ResetAPIKeyValue represents an API Key that has been reset.
type ResetAPIKeyValue struct {
// The Username for this API Key reset.
Username string `mapstructure:"username"`
// The new API Key for this user.
APIKey string `mapstructure:"apiKey"`
}
// Extract will get the Error or ResetAPIKeyValue object out of the ResetAPIKeyResult object.
func (r ResetAPIKeyResult) Extract() (*ResetAPIKeyValue, error) {
if r.Err != nil {
return nil, r.Err
}
var response struct {
ResetAPIKeyValue ResetAPIKeyValue `mapstructure:"RAX-KSKEY:apiKeyCredentials"`
}
err := mapstructure.Decode(r.Body, &response)
return &response.ResetAPIKeyValue, err
}