github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/api/iampap/iampapv1/v1_policies.go (about)

     1  package iampapv1
     2  
     3  import (
     4  	"github.com/IBM-Cloud/bluemix-go/client"
     5  	"github.com/IBM-Cloud/bluemix-go/rest"
     6  )
     7  
     8  type SearchParams struct {
     9  	AccountID     string
    10  	IAMID         string
    11  	AccessGroupID string
    12  	Type          string
    13  	ServiceType   string
    14  	Sort          string
    15  }
    16  
    17  func (p SearchParams) buildRequest(r *rest.Request) {
    18  	if p.AccountID != "" {
    19  		r.Query("account_id", p.AccountID)
    20  	}
    21  	if p.IAMID != "" {
    22  		r.Query("iam_id", p.IAMID)
    23  	}
    24  	if p.AccessGroupID != "" {
    25  		r.Query("access_group_id", p.AccessGroupID)
    26  	}
    27  	if p.Type != "" {
    28  		r.Query("type", p.Type)
    29  	}
    30  	if p.ServiceType != "" {
    31  		r.Query("service_type", p.ServiceType)
    32  	}
    33  	if p.Sort != "" {
    34  		r.Query("sort", p.Sort)
    35  	}
    36  }
    37  
    38  type V1PolicyRepository interface {
    39  	List(params SearchParams) ([]Policy, error)
    40  	Get(policyID string) (Policy, error)
    41  	Create(policy Policy) (Policy, error)
    42  	Update(policyID string, policy Policy, version string) (Policy, error)
    43  	Delete(policyID string) error
    44  }
    45  
    46  type v1PolicyRepository struct {
    47  	client *client.Client
    48  }
    49  
    50  func NewV1PolicyRepository(c *client.Client) V1PolicyRepository {
    51  	return &v1PolicyRepository{
    52  		client: c,
    53  	}
    54  }
    55  
    56  func (r *v1PolicyRepository) List(params SearchParams) ([]Policy, error) {
    57  	request := rest.GetRequest(*r.client.Config.Endpoint + "/v1/policies")
    58  	params.buildRequest(request)
    59  
    60  	response := struct {
    61  		Policies []Policy `json:"policies"`
    62  	}{}
    63  	_, err := r.client.SendRequest(request, &response)
    64  	if err != nil {
    65  		return []Policy{}, err
    66  	}
    67  	return response.Policies, nil
    68  }
    69  
    70  func (r *v1PolicyRepository) Get(policyID string) (Policy, error) {
    71  	var response Policy
    72  	resp, err := r.client.Get("/v1/policies/"+policyID, &response)
    73  	if err != nil {
    74  		return Policy{}, err
    75  	}
    76  	response.Version = resp.Header.Get("ETag")
    77  	return response, nil
    78  }
    79  
    80  func (r *v1PolicyRepository) Create(policy Policy) (Policy, error) {
    81  	var response Policy
    82  	resp, err := r.client.Post("/v1/policies", &policy, &response)
    83  	if err != nil {
    84  		return Policy{}, err
    85  	}
    86  	response.Version = resp.Header.Get("ETag")
    87  	return response, nil
    88  }
    89  
    90  func (r *v1PolicyRepository) Update(policyID string, policy Policy, version string) (Policy, error) {
    91  	var response Policy
    92  	request := rest.PutRequest(*r.client.Config.Endpoint + "/v1/policies/" + policyID)
    93  	request = request.Set("If-Match", version).Body(&policy)
    94  
    95  	resp, err := r.client.SendRequest(request, &response)
    96  	if err != nil {
    97  		return Policy{}, err
    98  	}
    99  	response.Version = resp.Header.Get("Etag")
   100  	return response, nil
   101  }
   102  
   103  func (r *v1PolicyRepository) Delete(policyID string) error {
   104  	_, err := r.client.Delete("/v1/policies/" + policyID)
   105  	if err != nil {
   106  		return err
   107  	}
   108  	return nil
   109  }