github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/service_keys.go (about)

     1  package mccpv2
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/IBM-Cloud/bluemix-go/bmxerror"
     8  	"github.com/IBM-Cloud/bluemix-go/client"
     9  	"github.com/IBM-Cloud/bluemix-go/rest"
    10  )
    11  
    12  //ErrCodeServiceKeyDoesNotExist ...
    13  const ErrCodeServiceKeyDoesNotExist = "erviceKeyDoesNotExist"
    14  
    15  //ServiceKeyRequest ...
    16  type ServiceKeyRequest struct {
    17  	Name                string                 `json:"name"`
    18  	ServiceInstanceGUID string                 `json:"service_instance_guid"`
    19  	Params              map[string]interface{} `json:"parameters,omitempty"`
    20  }
    21  
    22  //ServiceKey  model...
    23  type ServiceKey struct {
    24  	GUID                string
    25  	Name                string                 `json:"name"`
    26  	ServiceInstanceGUID string                 `json:"service_instance_guid"`
    27  	ServiceInstanceURL  string                 `json:"service_instance_url"`
    28  	Credentials         map[string]interface{} `json:"credentials"`
    29  }
    30  
    31  //ServiceKeyFields ...
    32  type ServiceKeyFields struct {
    33  	Metadata ServiceKeyMetadata
    34  	Entity   ServiceKey
    35  }
    36  
    37  //ServiceKeyMetadata ...
    38  type ServiceKeyMetadata struct {
    39  	GUID      string `json:"guid"`
    40  	URL       string `json:"url"`
    41  	CreatedAt string `json:"created_at"`
    42  	UpdatedAt string `json:"updated_at"`
    43  }
    44  
    45  //ServiceKeyResource ...
    46  type ServiceKeyResource struct {
    47  	Resource
    48  	Entity ServiceKeyEntity
    49  }
    50  
    51  //ServiceKeyEntity ...
    52  type ServiceKeyEntity struct {
    53  	Name                string                 `json:"name"`
    54  	ServiceInstanceGUID string                 `json:"service_instance_guid"`
    55  	ServiceInstanceURL  string                 `json:"service_instance_url"`
    56  	Credentials         map[string]interface{} `json:"credentials"`
    57  }
    58  
    59  //ToModel ...
    60  func (resource ServiceKeyResource) ToModel() ServiceKey {
    61  
    62  	entity := resource.Entity
    63  
    64  	return ServiceKey{
    65  		GUID:                resource.Metadata.GUID,
    66  		Name:                entity.Name,
    67  		ServiceInstanceGUID: entity.ServiceInstanceGUID,
    68  		ServiceInstanceURL:  entity.ServiceInstanceURL,
    69  		Credentials:         entity.Credentials,
    70  	}
    71  }
    72  
    73  //ServiceKeys ...
    74  type ServiceKeys interface {
    75  	Create(serviceInstanceGUID string, keyName string, params map[string]interface{}) (*ServiceKeyFields, error)
    76  	FindByName(serviceInstanceGUID string, keyName string) (*ServiceKey, error)
    77  	Get(serviceKeyGUID string) (*ServiceKeyFields, error)
    78  	Delete(serviceKeyGUID string) error
    79  	List(filters ...string) ([]ServiceKey, error)
    80  }
    81  
    82  type serviceKey struct {
    83  	client *client.Client
    84  }
    85  
    86  func newServiceKeyAPI(c *client.Client) ServiceKeys {
    87  	return &serviceKey{
    88  		client: c,
    89  	}
    90  }
    91  
    92  func (r *serviceKey) Create(serviceInstanceGUID string, keyName string, params map[string]interface{}) (*ServiceKeyFields, error) {
    93  	serviceKeyFields := ServiceKeyFields{}
    94  	reqParam := ServiceKeyRequest{
    95  		ServiceInstanceGUID: serviceInstanceGUID,
    96  		Name:                keyName,
    97  		Params:              params,
    98  	}
    99  	_, err := r.client.Post("/v2/service_keys", reqParam, &serviceKeyFields)
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  	return &serviceKeyFields, nil
   104  }
   105  
   106  func (r *serviceKey) Delete(serviceKeyGUID string) error {
   107  	rawURL := fmt.Sprintf("/v2/service_keys/%s", serviceKeyGUID)
   108  	_, err := r.client.Delete(rawURL)
   109  	return err
   110  }
   111  
   112  func (r *serviceKey) Get(guid string) (*ServiceKeyFields, error) {
   113  	rawURL := fmt.Sprintf("/v2/service_keys/%s", guid)
   114  	serviceKeyFields := ServiceKeyFields{}
   115  	_, err := r.client.Get(rawURL, &serviceKeyFields)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	return &serviceKeyFields, err
   121  }
   122  
   123  func (r *serviceKey) FindByName(serviceInstanceGUID string, keyName string) (*ServiceKey, error) {
   124  	rawURL := fmt.Sprintf("/v2/service_instances/%s/service_keys", serviceInstanceGUID)
   125  	req := rest.GetRequest(rawURL)
   126  	if keyName != "" {
   127  		req.Query("q", "name:"+keyName)
   128  	}
   129  	httpReq, err := req.Build()
   130  	if err != nil {
   131  		return nil, err
   132  	}
   133  	path := httpReq.URL.String()
   134  	serviceKeys, err := r.listServiceKeysWithPath(path)
   135  	if err != nil {
   136  		return nil, err
   137  	}
   138  	if len(serviceKeys) == 0 {
   139  		return nil, bmxerror.New(ErrCodeServiceKeyDoesNotExist,
   140  			fmt.Sprintf("Given service key %q doesn't exist for the given service instance  %q", keyName, serviceInstanceGUID))
   141  	}
   142  	return &serviceKeys[0], nil
   143  }
   144  
   145  func (r *serviceKey) List(filters ...string) ([]ServiceKey, error) {
   146  	rawURL := "/v2/service_keys"
   147  	req := rest.GetRequest(rawURL)
   148  	if len(filters) > 0 {
   149  		req.Query("q", strings.Join(filters, ""))
   150  	}
   151  	httpReq, err := req.Build()
   152  	if err != nil {
   153  		return nil, err
   154  	}
   155  	path := httpReq.URL.String()
   156  	keys, err := r.listServiceKeysWithPath(path)
   157  	if err != nil {
   158  		return nil, err
   159  	}
   160  	return keys, nil
   161  }
   162  
   163  func (r *serviceKey) listServiceKeysWithPath(path string) ([]ServiceKey, error) {
   164  	var serviceKeys []ServiceKey
   165  	_, err := r.client.GetPaginated(path, NewCCPaginatedResources(ServiceKeyResource{}), func(resource interface{}) bool {
   166  		if serviceKeyResource, ok := resource.(ServiceKeyResource); ok {
   167  			serviceKeys = append(serviceKeys, serviceKeyResource.ToModel())
   168  			return true
   169  		}
   170  		return false
   171  	})
   172  	return serviceKeys, err
   173  }