-
Notifications
You must be signed in to change notification settings - Fork 944
/
Copy pathrole_resource.go
109 lines (97 loc) · 3.09 KB
/
role_resource.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
package resources
import (
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
)
// IncludedUsers represent a set of users included on an API response.
type IncludedUsers map[constant.IncludedType]User
type Role struct {
// GUID is the unique identifier for the role.
GUID string `json:"guid"`
// Type is the type of the role.
Type constant.RoleType `json:"type"`
// UserGUID is the unique identifier of the user who has this role.
UserGUID string
// Username is the name of the user who has this role, e.g. "admin", "[email protected]"
Username string
// Origin is the identity server, default "uaa". Active Directory can also be an origin
Origin string
// OrgGUID is the unique identifier of the org where this role applies,
// if it is an org role.
OrgGUID string
// SpaceGUID is the unique identifier of the space where this role applies,
// if it is a space role.
SpaceGUID string
}
// MarshalJSON converts a Role into a Cloud Controller Application.
func (r Role) MarshalJSON() ([]byte, error) {
type data struct {
GUID string `json:"guid"`
}
type orgOrSpaceJSON struct {
Data data `json:"data"`
}
var ccRole struct {
GUID string `json:"guid,omitempty"`
Type string `json:"type"`
Relationships struct {
Organization *orgOrSpaceJSON `json:"organization,omitempty"`
Space *orgOrSpaceJSON `json:"space,omitempty"`
User struct {
Data struct {
GUID string `json:"guid,omitempty"`
Username string `json:"username,omitempty"`
Origin string `json:"origin,omitempty"`
} `json:"data"`
} `json:"user"`
} `json:"relationships"`
}
ccRole.GUID = r.GUID
ccRole.Type = string(r.Type)
if r.OrgGUID != "" {
ccRole.Relationships.Organization = &orgOrSpaceJSON{
Data: data{GUID: r.OrgGUID},
}
}
if r.SpaceGUID != "" {
ccRole.Relationships.Space = &orgOrSpaceJSON{
Data: data{GUID: r.SpaceGUID},
}
}
if r.Username != "" {
ccRole.Relationships.User.Data.Username = r.Username
ccRole.Relationships.User.Data.Origin = r.Origin
} else {
ccRole.Relationships.User.Data.GUID = r.UserGUID
}
return json.Marshal(ccRole)
}
// UnmarshalJSON helps unmarshal a Cloud Controller Role response.
func (r *Role) UnmarshalJSON(data []byte) error {
var ccRole struct {
GUID string `json:"guid"`
Type string `json:"type"`
Relationships Relationships
IncludedUsers IncludedUsers
}
err := cloudcontroller.DecodeJSON(data, &ccRole)
if err != nil {
return err
}
r.GUID = ccRole.GUID
r.Type = constant.RoleType(ccRole.Type)
if userRelationship, ok := ccRole.Relationships[constant.RelationshipTypeUser]; ok {
r.UserGUID = userRelationship.GUID
}
if spaceRelationship, ok := ccRole.Relationships[constant.RelationshipTypeSpace]; ok {
r.SpaceGUID = spaceRelationship.GUID
}
if orgRelationship, ok := ccRole.Relationships[constant.RelationshipTypeOrganization]; ok {
r.OrgGUID = orgRelationship.GUID
}
if includedUsers, ok := ccRole.IncludedUsers[constant.IncludedTypeUsers]; ok {
r.Username = includedUsers.Username
}
return nil
}