github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/api/cloudcontroller/ccv2/service_key.go (about) 1 package ccv2 2 3 import ( 4 "bytes" 5 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 6 "encoding/json" 7 8 "code.cloudfoundry.org/cli/api/cloudcontroller" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal" 10 ) 11 12 // ServiceKey represents a Cloud Controller Service Key. 13 type ServiceKey struct { 14 // GUID is the unique Service Key identifier. 15 GUID string 16 // Name is the name of the service key. 17 Name string 18 // ServiceInstanceGUID is the associated service instance GUID. 19 ServiceInstanceGUID string 20 // Credentials are the credentials returned by the service broker for the service key. 21 Credentials map[string]interface{} 22 } 23 24 func (serviceKey *ServiceKey) UnmarshalJSON(data []byte) error { 25 var ccServiceKey struct { 26 Metadata internal.Metadata 27 Entity struct { 28 ServiceInstanceGUID string `json:"service_instance_guid"` 29 Name string `json:"name"` 30 Credentials map[string]interface{} `json:"credentials"` 31 } 32 } 33 err := cloudcontroller.DecodeJSON(data, &ccServiceKey) 34 35 if err != nil { 36 return err 37 } 38 39 serviceKey.GUID = ccServiceKey.Metadata.GUID 40 serviceKey.Name = ccServiceKey.Entity.Name 41 serviceKey.ServiceInstanceGUID = ccServiceKey.Entity.ServiceInstanceGUID 42 serviceKey.Credentials = ccServiceKey.Entity.Credentials 43 44 return nil 45 } 46 47 // serviceKeyRequestBody represents the body of the service key create 48 // request. 49 type serviceKeyRequestBody struct { 50 ServiceInstanceGUID string `json:"service_instance_guid"` 51 Name string `json:"name"` 52 Parameters map[string]interface{} `json:"parameters,omitempty"` 53 } 54 55 // CreateServiceKey creates a new service key using the provided name and 56 // parameters for the requested service instance. 57 func (client *Client) CreateServiceKey(serviceInstanceGUID string, keyName string, parameters map[string]interface{}) (ServiceKey, Warnings, error) { 58 requestBody := serviceKeyRequestBody{ 59 ServiceInstanceGUID: serviceInstanceGUID, 60 Name: keyName, 61 Parameters: parameters, 62 } 63 64 bodyBytes, err := json.Marshal(requestBody) 65 if err != nil { 66 return ServiceKey{}, nil, err 67 } 68 69 request, err := client.newHTTPRequest(requestOptions{ 70 RequestName: internal.PostServiceKeyRequest, 71 Body: bytes.NewReader(bodyBytes), 72 }) 73 if err != nil { 74 return ServiceKey{}, nil, err 75 } 76 77 var serviceKey ServiceKey 78 response := cloudcontroller.Response{ 79 DecodeJSONResponseInto: &serviceKey, 80 } 81 err = client.connection.Make(request, &response) 82 83 return serviceKey, response.Warnings, err 84 } 85 86 // GetServiceKey returns back a service key. 87 func (client *Client) GetServiceKey(guid string) (ServiceKey, Warnings, error) { 88 request, err := client.newHTTPRequest(requestOptions{ 89 RequestName: internal.GetServiceKeyRequest, 90 URIParams: Params{ 91 "service_key_guid": guid, 92 }, 93 }) 94 if err != nil { 95 return ServiceKey{}, nil, err 96 } 97 98 var obj ServiceKey 99 response := cloudcontroller.Response{ 100 DecodeJSONResponseInto: &obj, 101 } 102 103 err = client.connection.Make(request, &response) 104 return obj, response.Warnings, err 105 } 106 107 // GetServiceKeys returns a list of Service Key based off of the provided filters. 108 func (client *Client) GetServiceKeys(filters ...Filter) ([]ServiceKey, Warnings, error) { 109 params := ConvertFilterParameters(filters) 110 request, err := client.newHTTPRequest(requestOptions{ 111 RequestName: internal.GetServiceKeysRequest, 112 Query: params, 113 }) 114 if err != nil { 115 return nil, nil, err 116 } 117 118 var fullServiceKeysList []ServiceKey 119 warnings, err := client.paginate(request, ServiceKey{}, func(item interface{}) error { 120 if i, ok := item.(ServiceKey); ok { 121 fullServiceKeysList = append(fullServiceKeysList, i) 122 } else { 123 return ccerror.UnknownObjectInListError{ 124 Expected: ServiceKey{}, 125 Unexpected: item, 126 } 127 } 128 return nil 129 }) 130 131 return fullServiceKeysList, warnings, err 132 } 133 134 // DeleteServiceKey delete a service key. 135 func (client *Client) DeleteServiceKey(guid string) (Warnings, error) { 136 request, err := client.newHTTPRequest(requestOptions{ 137 RequestName: internal.DeleteServiceKeyRequest, 138 URIParams: Params{ 139 "service_key_guid": guid, 140 }, 141 }) 142 if err != nil { 143 return nil, err 144 } 145 response := cloudcontroller.Response{} 146 147 err = client.connection.Make(request, &response) 148 return response.Warnings, err 149 }