github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/account/accountv1/accounts.go (about) 1 package accountv1 2 3 import ( 4 "fmt" 5 6 "github.com/IBM-Cloud/bluemix-go/api/account/accountv2" 7 "github.com/IBM-Cloud/bluemix-go/bmxerror" 8 "github.com/IBM-Cloud/bluemix-go/client" 9 "github.com/IBM-Cloud/bluemix-go/models" 10 "github.com/IBM-Cloud/bluemix-go/rest" 11 ) 12 13 type AccountUser struct { 14 UserId string `json:"userId"` 15 FirstName string `json:"firstname"` 16 LastName string `json:"lastname"` 17 State string `json:"state"` 18 IbmUniqueId string `json:"ibmUniqueId"` 19 Email string `json:"email"` 20 Phonenumber string `json:"phonenumber"` 21 CreatedOn string `json:"createdOn"` 22 VerifiedOn string `json:"verifiedOn"` 23 Id string `json:"id"` 24 UaaGuid string `json:"uaaGuid"` 25 AccountId string `json:"accountId"` 26 Role string `json:"role"` 27 InvitedOn string `json:"invitedOn"` 28 Photo string `json:"photo"` 29 } 30 31 // Accounts ... 32 type Accounts interface { 33 GetAccountUsers(accountGuid string) ([]models.AccountUser, error) 34 InviteAccountUser(accountGuid string, userEmail string) (AccountInviteResponse, error) 35 DeleteAccountUser(accountGuid string, userGuid string) error 36 FindAccountUserByUserId(accountGuid string, userId string) (*models.AccountUser, error) 37 List() ([]models.V2Account, error) 38 Get(accountId string) (models.V1Account, error) 39 } 40 41 type account struct { 42 client *client.Client 43 } 44 45 type Metadata struct { 46 Guid string `json:"guid"` 47 Url string `json:"url"` 48 CreatedAt string `json:"created_at"` 49 UpdatedAt string `json:"updated_at"` 50 VerifiedAt string `json:"verified_at"` 51 Identity Identity `json:"identity"` 52 } 53 54 type AccountUserEntity struct { 55 AccountId string `json:"account_id"` 56 FirstName string `json:"first_name"` 57 LastName string `json:"last_name"` 58 State string `json:"state"` 59 Email string `json:"email"` 60 PhoneNumber string `json:"phonenumber"` 61 Role string `json:"role"` 62 Photo string `json:"photo"` 63 } 64 65 type AccountUserMetadata Metadata 66 67 type Identity struct { 68 Id string `json:"id"` 69 UserName string `json:"username"` 70 Realmid string `json:"realmid"` 71 Identifier string `json:"identifier"` 72 } 73 74 // Account Invites ... 75 type AccountInviteResponse struct { 76 Id string `json:"id"` 77 Email string `json:"email"` 78 State string `json:"state"` 79 } 80 81 type AccountUserQueryResponse struct { 82 Metadata Metadata 83 AccountUsers []AccountUserResource `json:"resources"` 84 } 85 86 type AccountResource struct { 87 Metadata Metadata 88 Entity AccountEntity 89 } 90 91 type AccountEntity struct { 92 Name string `json:"name"` 93 Type string `json:"type"` 94 State string `json:"state"` 95 OwnerIamId string `json:"owner_iam_id"` 96 CountryCode string `json:"country_code"` 97 CurrencyCode string `json:"currency_code"` 98 Organizations []models.AccountOrganization `json:"organizations_region"` 99 } 100 101 func (resource AccountResource) ToModel() models.V2Account { 102 return models.V2Account{ 103 Guid: resource.Metadata.Guid, 104 Name: resource.Entity.Name, 105 Type: resource.Entity.Type, 106 State: resource.Entity.State, 107 OwnerIamId: resource.Entity.OwnerIamId, 108 CountryCode: resource.Entity.CountryCode, 109 Organizations: resource.Entity.Organizations, 110 } 111 } 112 113 // AccountUserResource is the original user information returned by V2 endpoint (for listing) 114 type AccountUserResource struct { 115 ID string `json:"id"` 116 IAMID string `json:"iam_id"` 117 Realm string `json:"realm"` 118 UserID string `json:"user_id"` 119 FirstName string `json:"firstname"` 120 LastName string `json:"lastname"` 121 State string `json:"state"` 122 Email string `json:"email"` 123 PhoneNumber string `json:"phonenumber"` 124 AltPhoneNumber string `json:"altphonenumber"` 125 Photo string `json:"photo"` 126 InvitedOn string `json:"invitedOn"` 127 AddedOn string `json:"added_on"` 128 AccountID string `json:"account_id"` 129 130 Linkages []struct { 131 Origin string `json:"origin"` 132 ID string `json:"id"` 133 } `json:"linkages"` 134 } 135 136 func (resource AccountUserResource) ToModel() models.AccountUser { 137 var uaaGUID string 138 for _, linkage := range resource.Linkages { 139 if linkage.Origin == "UAA" { 140 uaaGUID = linkage.ID 141 break 142 } 143 } 144 user := models.AccountUser{ 145 Id: resource.ID, 146 UserId: resource.UserID, 147 FirstName: resource.FirstName, 148 LastName: resource.LastName, 149 State: resource.State, 150 Email: resource.Email, 151 Phonenumber: resource.PhoneNumber, 152 AccountId: resource.AccountID, 153 Photo: resource.Photo, 154 IbmUniqueId: resource.IAMID, 155 UaaGuid: uaaGUID, 156 AddedOn: resource.AddedOn, 157 InvitedOn: resource.InvitedOn, 158 } 159 160 return user 161 } 162 163 func newAccountAPI(c *client.Client) Accounts { 164 return &account{ 165 client: c, 166 } 167 } 168 169 // GetAccountUser ... 170 func (a *account) GetAccountUsers(accountGuid string) ([]models.AccountUser, error) { 171 var users []models.AccountUser 172 173 resp, err := a.client.GetPaginated(fmt.Sprintf("/v2/accounts/%s/users", accountGuid), 174 accountv2.NewAccountPaginatedResources(AccountUserResource{}), 175 func(resource interface{}) bool { 176 if accountUser, ok := resource.(AccountUserResource); ok { 177 users = append(users, accountUser.ToModel()) 178 return true 179 } 180 return false 181 }) 182 183 if resp.StatusCode == 404 { 184 return []models.AccountUser{}, bmxerror.New(ErrCodeNoAccountExists, 185 fmt.Sprintf("No Account exists with account id:%q", accountGuid)) 186 } 187 188 return users, err 189 } 190 191 // Deprecated: User Invite is deprecated from accounts use UserInvite from userManagement 192 func (a *account) InviteAccountUser(accountGuid string, userEmail string) (AccountInviteResponse, error) { 193 type userEntity struct { 194 Email string `json:"email"` 195 AccountRole string `json:"account_role"` 196 } 197 198 payload := struct { 199 Users []userEntity `json:"users"` 200 }{ 201 Users: []userEntity{ 202 { 203 Email: userEmail, 204 AccountRole: "MEMBER", 205 }, 206 }, 207 } 208 209 resp := AccountInviteResponse{} 210 211 _, err := a.client.Post(fmt.Sprintf("/v2/accounts/%s/users", accountGuid), payload, &resp) 212 return resp, err 213 } 214 215 func (a *account) DeleteAccountUser(accountGuid string, userGuid string) error { 216 _, err := a.client.Delete(fmt.Sprintf("/v2/accounts/%s/users/%s", accountGuid, userGuid)) 217 218 return err 219 } 220 221 func (a *account) FindAccountUserByUserId(accountGuid string, userId string) (*models.AccountUser, error) { 222 queryResp := AccountUserQueryResponse{} 223 224 req := rest.GetRequest(*a.client.Config.Endpoint+fmt.Sprintf("/v2/accounts/%s/users", accountGuid)). 225 Query("user_id", userId) 226 227 response, err := a.client.SendRequest(req, 228 &queryResp) 229 230 if err != nil { 231 switch response.StatusCode { 232 case 404: 233 return nil, nil 234 default: 235 return nil, err 236 } 237 } else if len(queryResp.AccountUsers) == 0 { 238 return nil, nil 239 } else { 240 accountUser := queryResp.AccountUsers[0].ToModel() 241 return &accountUser, nil 242 } 243 } 244 245 func (a *account) List() ([]models.V2Account, error) { 246 var accounts []models.V2Account 247 resp, err := a.client.GetPaginated("/coe/v2/accounts", NewAccountPaginatedResources(AccountResource{}), func(resource interface{}) bool { 248 if accountResource, ok := resource.(AccountResource); ok { 249 accounts = append(accounts, accountResource.ToModel()) 250 return true 251 } 252 return false 253 }) 254 255 if resp.StatusCode == 404 { 256 return []models.V2Account{}, nil 257 } 258 259 return accounts, err 260 } 261 262 func (a *account) Get(accountId string) (models.V1Account, error) { 263 account := models.V1Account{} 264 response, err := a.client.Get(fmt.Sprintf("/coe/v2/accounts/%s", accountId), &account) 265 if err != nil { 266 267 if response.StatusCode == 404 { 268 return models.V1Account{}, bmxerror.New(ErrCodeNoAccountExists, 269 fmt.Sprintf("Account %q does not exists", accountId)) 270 } 271 return models.V1Account{}, err 272 273 } 274 275 return account, nil 276 277 }