github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv3/role.go (about) 1 package ccv3 2 3 import ( 4 "bytes" 5 "encoding/json" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal" 11 ) 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 } 110 111 func (client *Client) CreateRole(roleSpec Role) (Role, Warnings, error) { 112 bodyBytes, err := json.Marshal(roleSpec) 113 if err != nil { 114 return Role{}, nil, err 115 } 116 117 request, err := client.newHTTPRequest(requestOptions{ 118 RequestName: internal.PostRoleRequest, 119 Body: bytes.NewReader(bodyBytes), 120 }) 121 if err != nil { 122 return Role{}, nil, err 123 } 124 125 var responseRole Role 126 response := cloudcontroller.Response{ 127 DecodeJSONResponseInto: &responseRole, 128 } 129 err = client.connection.Make(request, &response) 130 131 return responseRole, response.Warnings, err 132 } 133 134 func (client *Client) DeleteRole(roleGUID string) (JobURL, Warnings, error) { 135 request, err := client.newHTTPRequest(requestOptions{ 136 URIParams: map[string]string{ 137 "role_guid": roleGUID, 138 }, 139 RequestName: internal.DeleteRoleRequest, 140 }) 141 if err != nil { 142 return "", nil, err 143 } 144 145 response := cloudcontroller.Response{} 146 err = client.connection.Make(request, &response) 147 148 return JobURL(response.ResourceLocationURL), response.Warnings, err 149 } 150 151 // GetRoles lists roles with optional filters & includes. 152 func (client *Client) GetRoles(query ...Query) ([]Role, IncludedResources, Warnings, error) { 153 request, err := client.newHTTPRequest(requestOptions{ 154 RequestName: internal.GetRolesRequest, 155 Query: query, 156 }) 157 if err != nil { 158 return nil, IncludedResources{}, nil, err 159 } 160 161 var rolesList []Role 162 includes, warnings, err := client.paginateWithIncludes(request, Role{}, func(item interface{}) error { 163 if role, ok := item.(Role); ok { 164 rolesList = append(rolesList, role) 165 } else { 166 return ccerror.UnknownObjectInListError{ 167 Expected: Role{}, 168 Unexpected: item, 169 } 170 } 171 return nil 172 }) 173 174 return rolesList, includes, warnings, err 175 }