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