github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/account/accountv2/accounts.go (about)

     1  package accountv2
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/IBM-Cloud/bluemix-go/bmxerror"
     7  	"github.com/IBM-Cloud/bluemix-go/client"
     8  )
     9  
    10  //Metadata ...
    11  type Metadata struct {
    12  	GUID string `json:"guid"`
    13  	URL  string `json:"url"`
    14  }
    15  
    16  //Resource ...
    17  type Resource struct {
    18  	Metadata Metadata
    19  }
    20  
    21  //Account Model ...
    22  type Account struct {
    23  	GUID          string
    24  	Name          string
    25  	Type          string
    26  	State         string
    27  	OwnerGUID     string
    28  	OwnerUserID   string
    29  	OwnerUniqueID string
    30  	CustomerID    string
    31  	CountryCode   string
    32  	CurrencyCode  string
    33  	Organizations []AccountOrganization
    34  	Members       []AccountMember `json:"members"`
    35  }
    36  
    37  //AccountOrganization ...
    38  type AccountOrganization struct {
    39  	GUID   string `json:"guid"`
    40  	Region string `json:"region"`
    41  }
    42  
    43  //AccountMember ...
    44  type AccountMember struct {
    45  	GUID     string `json:"guid"`
    46  	UserID   string `json:"user_id"`
    47  	UniqueID string `json:"unique_id"`
    48  }
    49  
    50  //AccountResource ...
    51  type AccountResource struct {
    52  	Resource
    53  	Entity AccountEntity
    54  }
    55  
    56  //AccountEntity ...
    57  type AccountEntity struct {
    58  	Name          string                `json:"name"`
    59  	Type          string                `json:"type"`
    60  	State         string                `json:"state"`
    61  	OwnerGUID     string                `json:"owner"`
    62  	OwnerUserID   string                `json:"owner_userid"`
    63  	OwnerUniqueID string                `json:"owner_unique_id"`
    64  	CustomerID    string                `json:"customer_id"`
    65  	CountryCode   string                `json:"country_code"`
    66  	CurrencyCode  string                `json:"currency_code"`
    67  	Organizations []AccountOrganization `json:"organizations_region"`
    68  	Members       []AccountMember       `json:"members"`
    69  }
    70  
    71  //ToModel ...
    72  func (resource AccountResource) ToModel() Account {
    73  	entity := resource.Entity
    74  
    75  	return Account{
    76  		GUID:          resource.Metadata.GUID,
    77  		Name:          entity.Name,
    78  		Type:          entity.Type,
    79  		State:         entity.State,
    80  		OwnerGUID:     entity.OwnerGUID,
    81  		OwnerUserID:   entity.OwnerUserID,
    82  		OwnerUniqueID: entity.OwnerUniqueID,
    83  		CustomerID:    entity.CustomerID,
    84  		CountryCode:   entity.CountryCode,
    85  		CurrencyCode:  entity.CurrencyCode,
    86  		Organizations: entity.Organizations,
    87  		Members:       entity.Members,
    88  	}
    89  }
    90  
    91  func (nameQueryResponse AccountNameQueryResponse) ToModel() Account {
    92  	entity := nameQueryResponse.Entity
    93  	guid := nameQueryResponse.Metadata.GUID
    94  
    95  	return Account{
    96  		GUID:          guid,
    97  		Name:          entity.Name,
    98  		Type:          entity.Type,
    99  		State:         entity.State,
   100  		OwnerGUID:     entity.OwnerGUID,
   101  		OwnerUserID:   entity.OwnerUserID,
   102  		OwnerUniqueID: entity.OwnerUniqueID,
   103  		CustomerID:    entity.CustomerID,
   104  		CountryCode:   entity.CountryCode,
   105  		CurrencyCode:  entity.CurrencyCode,
   106  		Organizations: entity.Organizations,
   107  		Members:       entity.Members,
   108  	}
   109  }
   110  
   111  //AccountQueryResponse ...
   112  type AccountQueryResponse struct {
   113  	Metadata Metadata
   114  	Accounts []AccountResource `json:"resources"`
   115  }
   116  
   117  //AccountQueryResponse ...
   118  type AccountNameQueryResponse struct {
   119  	Metadata Metadata
   120  	Entity   AccountEntity
   121  }
   122  
   123  //Accounts ...
   124  type Accounts interface {
   125  	List() ([]Account, error)
   126  	FindByOrg(orgGUID string, region string) (*Account, error)
   127  	FindByOwner(userID string) (*Account, error)
   128  	Get(accountId string) (*Account, error)
   129  }
   130  
   131  type account struct {
   132  	client *client.Client
   133  }
   134  
   135  func newAccountAPI(c *client.Client) Accounts {
   136  	return &account{
   137  		client: c,
   138  	}
   139  }
   140  
   141  //FindByOrg ...
   142  func (a *account) FindByOrg(orgGUID, region string) (*Account, error) {
   143  	type organizationRegion struct {
   144  		GUID   string `json:"guid"`
   145  		Region string `json:"region"`
   146  	}
   147  
   148  	payLoad := struct {
   149  		OrganizationsRegion []organizationRegion `json:"organizations_region"`
   150  	}{
   151  		OrganizationsRegion: []organizationRegion{
   152  			{
   153  				GUID:   orgGUID,
   154  				Region: region,
   155  			},
   156  		},
   157  	}
   158  
   159  	queryResp := AccountQueryResponse{}
   160  	response, err := a.client.Post("/coe/v2/getaccounts", payLoad, &queryResp)
   161  	if err != nil {
   162  
   163  		if response.StatusCode == 404 {
   164  			return nil, bmxerror.New(ErrCodeNoAccountExists,
   165  				fmt.Sprintf("No account exists in the given region: %q and the given org: %q", region, orgGUID))
   166  		}
   167  		return nil, err
   168  
   169  	}
   170  
   171  	if len(queryResp.Accounts) > 0 {
   172  		account := queryResp.Accounts[0].ToModel()
   173  		return &account, nil
   174  	}
   175  
   176  	return nil, bmxerror.New(ErrCodeNoAccountExists,
   177  		fmt.Sprintf("No account exists in the given region: %q and the given org: %q", region, orgGUID))
   178  }
   179  
   180  func (a *account) List() ([]Account, error) {
   181  	var accounts []Account
   182  	resp, err := a.client.GetPaginated("/coe/v2/accounts", NewAccountPaginatedResources(AccountResource{}), func(resource interface{}) bool {
   183  		if accountResource, ok := resource.(AccountResource); ok {
   184  			accounts = append(accounts, accountResource.ToModel())
   185  			return true
   186  		}
   187  		return false
   188  	})
   189  
   190  	if resp.StatusCode == 404 || len(accounts) == 0 {
   191  		return nil, bmxerror.New(ErrCodeNoAccountExists,
   192  			fmt.Sprintf("No Account exists"))
   193  	}
   194  
   195  	return accounts, err
   196  }
   197  
   198  //FindByOwner ...
   199  func (a *account) FindByOwner(userID string) (*Account, error) {
   200  	accounts, err := a.List()
   201  	if err != nil {
   202  		return nil, err
   203  	}
   204  
   205  	for _, a := range accounts {
   206  		if a.OwnerUserID == userID {
   207  			return &a, nil
   208  		}
   209  	}
   210  	return nil, bmxerror.New(ErrCodeNoAccountExists,
   211  		fmt.Sprintf("No account exists for the user %q", userID))
   212  }
   213  
   214  //Get ...
   215  func (a *account) Get(accountId string) (*Account, error) {
   216  	queryResp := AccountNameQueryResponse{}
   217  	response, err := a.client.Get(fmt.Sprintf("/coe/v2/accounts/%s", accountId), &queryResp)
   218  	if err != nil {
   219  
   220  		if response.StatusCode == 404 {
   221  			return nil, bmxerror.New(ErrCodeNoAccountExists,
   222  				fmt.Sprintf("Account %q does not exists", accountId))
   223  		}
   224  		return nil, err
   225  
   226  	}
   227  
   228  	account := queryResp.ToModel()
   229  	return &account, nil
   230  
   231  }