github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/iamuum/iamuumv1/access_group.go (about)

     1  package iamuumv1
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  
     7  	"github.com/IBM-Cloud/bluemix-go/client"
     8  	"github.com/IBM-Cloud/bluemix-go/helpers"
     9  	"github.com/IBM-Cloud/bluemix-go/models"
    10  	"github.com/IBM-Cloud/bluemix-go/rest"
    11  )
    12  
    13  type Groups struct {
    14  	PaginationFields
    15  	Groups []models.AccessGroup `json:"groups"`
    16  }
    17  
    18  func (g *Groups) Resources() []interface{} {
    19  	r := make([]interface{}, len(g.Groups))
    20  	for i := range g.Groups {
    21  		r[i] = g.Groups[i]
    22  	}
    23  	return r
    24  }
    25  
    26  type AccessGroupRepository interface {
    27  	List(accountID string) ([]models.AccessGroup, error)
    28  	Create(group models.AccessGroup, accountID string) (*models.AccessGroup, error)
    29  	FindByName(name string, accountID string) ([]models.AccessGroup, error)
    30  	Delete(accessGroupID string, recursive bool) error
    31  	Update(accessGroupID string, group AccessGroupUpdateRequest, revision string) (models.AccessGroup, error)
    32  	Get(accessGroupID string) (group *models.AccessGroup, revision string, err error)
    33  }
    34  
    35  type accessGroupRepository struct {
    36  	client *client.Client
    37  }
    38  
    39  type AccessGroupUpdateRequest struct {
    40  	Name        string `json:"name,omitempty"`
    41  	Description string `json:"description,omitempty"`
    42  }
    43  
    44  func NewAccessGroupRepository(c *client.Client) AccessGroupRepository {
    45  	return &accessGroupRepository{
    46  		client: c,
    47  	}
    48  }
    49  
    50  func (r *accessGroupRepository) List(accountID string) ([]models.AccessGroup, error) {
    51  	var groups []models.AccessGroup
    52  	_, err := r.client.GetPaginated(fmt.Sprintf("/v1/groups?account=%s", url.QueryEscape(accountID)), NewPaginatedResourcesHandler(&Groups{}), func(v interface{}) bool {
    53  		groups = append(groups, v.(models.AccessGroup))
    54  		return true
    55  	})
    56  	if err != nil {
    57  		return []models.AccessGroup{}, err
    58  	}
    59  	return groups, err
    60  }
    61  
    62  func (r *accessGroupRepository) Create(accessGroup models.AccessGroup, accountID string) (*models.AccessGroup, error) {
    63  	req := rest.PostRequest(helpers.GetFullURL(*r.client.Config.Endpoint, "/v1/groups")).Query("account", accountID).Body(accessGroup)
    64  
    65  	newAccessGroup := models.AccessGroup{}
    66  	_, err := r.client.SendRequest(req, &newAccessGroup)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	return &newAccessGroup, nil
    71  }
    72  
    73  func (r *accessGroupRepository) FindByName(name string, accountID string) ([]models.AccessGroup, error) {
    74  	var groups []models.AccessGroup
    75  	_, err := r.client.GetPaginated(fmt.Sprintf("/v1/groups?account=%s", url.QueryEscape(accountID)), NewPaginatedResourcesHandler(&Groups{}), func(v interface{}) bool {
    76  		if v.(models.AccessGroup).Name == name {
    77  			groups = append(groups, v.(models.AccessGroup))
    78  		}
    79  		return true
    80  	})
    81  	if err != nil {
    82  		return []models.AccessGroup{}, err
    83  	}
    84  	return groups, err
    85  }
    86  
    87  func (r *accessGroupRepository) Delete(accessGroupID string, recursive bool) error {
    88  	req := rest.DeleteRequest((helpers.GetFullURL(*r.client.Config.Endpoint, "/v1/groups/"+accessGroupID)))
    89  
    90  	if recursive {
    91  		req = req.Query("force", "true")
    92  	}
    93  	_, err := r.client.SendRequest(req, nil)
    94  	return err
    95  }
    96  
    97  func (r *accessGroupRepository) Update(accessGroupID string, group AccessGroupUpdateRequest, revision string) (models.AccessGroup, error) {
    98  	req := rest.PatchRequest((helpers.GetFullURL(*r.client.Config.Endpoint, "/v1/groups/"+accessGroupID))).Body(group).Add("If-Match", revision)
    99  	resp := models.AccessGroup{}
   100  	_, err := r.client.SendRequest(req, &resp)
   101  	if err != nil {
   102  		return resp, err
   103  	}
   104  	return resp, nil
   105  }
   106  
   107  func (r *accessGroupRepository) Get(accessGroupID string) (*models.AccessGroup, string, error) {
   108  	group := models.AccessGroup{}
   109  	response, err := r.client.Get("/v1/groups/"+url.PathEscape(accessGroupID), &group)
   110  	if err != nil {
   111  		return &group, "", err
   112  	}
   113  	return &group, response.Header.Get("Etag"), nil
   114  }