github.com/sleungcy/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/service_key.go (about) 1 package ccv2 2 3 import ( 4 "bytes" 5 "encoding/json" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal" 9 ) 10 11 // ServiceKey represents a Cloud Controller Service Key. 12 type ServiceKey struct { 13 // GUID is the unique Service Key identifier. 14 GUID string 15 // Name is the name of the service key. 16 Name string 17 // ServiceInstanceGUID is the associated service instance GUID. 18 ServiceInstanceGUID string 19 // Credentials are the credentials returned by the service broker for the service key. 20 Credentials map[string]interface{} 21 } 22 23 func (serviceKey *ServiceKey) UnmarshalJSON(data []byte) error { 24 var ccServiceKey struct { 25 Metadata internal.Metadata 26 Entity struct { 27 ServiceInstanceGUID string `json:"service_instance_guid"` 28 Name string `json:"name"` 29 Credentials map[string]interface{} `json:"credentials"` 30 } 31 } 32 err := cloudcontroller.DecodeJSON(data, &ccServiceKey) 33 34 if err != nil { 35 return err 36 } 37 38 serviceKey.GUID = ccServiceKey.Metadata.GUID 39 serviceKey.Name = ccServiceKey.Entity.Name 40 serviceKey.ServiceInstanceGUID = ccServiceKey.Entity.ServiceInstanceGUID 41 serviceKey.Credentials = ccServiceKey.Entity.Credentials 42 43 return nil 44 } 45 46 // serviceKeyRequestBody represents the body of the service key create 47 // request. 48 type serviceKeyRequestBody struct { 49 ServiceInstanceGUID string `json:"service_instance_guid"` 50 Name string `json:"name"` 51 Parameters map[string]interface{} `json:"parameters,omitempty"` 52 } 53 54 // CreateServiceKey creates a new service key using the provided name and 55 // parameters for the requested service instance. 56 func (client *Client) CreateServiceKey(serviceInstanceGUID string, keyName string, parameters map[string]interface{}) (ServiceKey, Warnings, error) { 57 requestBody := serviceKeyRequestBody{ 58 ServiceInstanceGUID: serviceInstanceGUID, 59 Name: keyName, 60 Parameters: parameters, 61 } 62 63 bodyBytes, err := json.Marshal(requestBody) 64 if err != nil { 65 return ServiceKey{}, nil, err 66 } 67 68 request, err := client.newHTTPRequest(requestOptions{ 69 RequestName: internal.PostServiceKeyRequest, 70 Body: bytes.NewReader(bodyBytes), 71 }) 72 if err != nil { 73 return ServiceKey{}, nil, err 74 } 75 76 var serviceKey ServiceKey 77 response := cloudcontroller.Response{ 78 DecodeJSONResponseInto: &serviceKey, 79 } 80 err = client.connection.Make(request, &response) 81 82 return serviceKey, response.Warnings, err 83 }