github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/resources/role_resource.go (about) 1 package resources 2 3 import ( 4 "encoding/json" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller" 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 8 ) 9 10 // IncludedUsers represent a set of users included on an API response. 11 type IncludedUsers map[constant.IncludedType]User 12 13 type Role struct { 14 // GUID is the unique identifier for the role. 15 GUID string `json:"guid"` 16 // Type is the type of the role. 17 Type constant.RoleType `json:"type"` 18 // UserGUID is the unique identifier of the user who has this role. 19 UserGUID string 20 // Username is the name of the user who has this role, e.g. "admin", "user@example.com" 21 Username string 22 // Origin is the identity server, default "uaa". Active Directory can also be an origin 23 Origin string 24 // OrgGUID is the unique identifier of the org where this role applies, 25 // if it is an org role. 26 OrgGUID string 27 // SpaceGUID is the unique identifier of the space where this role applies, 28 // if it is a space role. 29 SpaceGUID string 30 } 31 32 // MarshalJSON converts a Role into a Cloud Controller Application. 33 func (r Role) MarshalJSON() ([]byte, error) { 34 type data struct { 35 GUID string `json:"guid"` 36 } 37 38 type orgOrSpaceJSON struct { 39 Data data `json:"data"` 40 } 41 var ccRole struct { 42 GUID string `json:"guid,omitempty"` 43 Type string `json:"type"` 44 Relationships struct { 45 Organization *orgOrSpaceJSON `json:"organization,omitempty"` 46 Space *orgOrSpaceJSON `json:"space,omitempty"` 47 User struct { 48 Data struct { 49 GUID string `json:"guid,omitempty"` 50 Username string `json:"username,omitempty"` 51 Origin string `json:"origin,omitempty"` 52 } `json:"data"` 53 } `json:"user"` 54 } `json:"relationships"` 55 } 56 57 ccRole.GUID = r.GUID 58 ccRole.Type = string(r.Type) 59 if r.OrgGUID != "" { 60 ccRole.Relationships.Organization = &orgOrSpaceJSON{ 61 Data: data{GUID: r.OrgGUID}, 62 } 63 } 64 if r.SpaceGUID != "" { 65 ccRole.Relationships.Space = &orgOrSpaceJSON{ 66 Data: data{GUID: r.SpaceGUID}, 67 } 68 } 69 if r.Username != "" { 70 ccRole.Relationships.User.Data.Username = r.Username 71 ccRole.Relationships.User.Data.Origin = r.Origin 72 } else { 73 ccRole.Relationships.User.Data.GUID = r.UserGUID 74 } 75 76 return json.Marshal(ccRole) 77 } 78 79 // UnmarshalJSON helps unmarshal a Cloud Controller Role response. 80 func (r *Role) UnmarshalJSON(data []byte) error { 81 var ccRole struct { 82 GUID string `json:"guid"` 83 Type string `json:"type"` 84 Relationships Relationships 85 IncludedUsers IncludedUsers 86 } 87 88 err := cloudcontroller.DecodeJSON(data, &ccRole) 89 if err != nil { 90 return err 91 } 92 93 r.GUID = ccRole.GUID 94 r.Type = constant.RoleType(ccRole.Type) 95 if userRelationship, ok := ccRole.Relationships[constant.RelationshipTypeUser]; ok { 96 r.UserGUID = userRelationship.GUID 97 } 98 if spaceRelationship, ok := ccRole.Relationships[constant.RelationshipTypeSpace]; ok { 99 r.SpaceGUID = spaceRelationship.GUID 100 } 101 if orgRelationship, ok := ccRole.Relationships[constant.RelationshipTypeOrganization]; ok { 102 r.OrgGUID = orgRelationship.GUID 103 } 104 105 if includedUsers, ok := ccRole.IncludedUsers[constant.IncludedTypeUsers]; ok { 106 r.Username = includedUsers.Username 107 } 108 return nil 109 }