github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+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 func (client *Client) CreateServiceKey(serviceInstanceGUID string, keyName string, parameters map[string]interface{}) (ServiceKey, Warnings, error) { 55 requestBody := serviceKeyRequestBody{ 56 ServiceInstanceGUID: serviceInstanceGUID, 57 Name: keyName, 58 Parameters: parameters, 59 } 60 61 bodyBytes, err := json.Marshal(requestBody) 62 if err != nil { 63 return ServiceKey{}, nil, err 64 } 65 66 request, err := client.newHTTPRequest(requestOptions{ 67 RequestName: internal.PostServiceKeyRequest, 68 Body: bytes.NewReader(bodyBytes), 69 }) 70 if err != nil { 71 return ServiceKey{}, nil, err 72 } 73 74 var serviceKey ServiceKey 75 response := cloudcontroller.Response{ 76 Result: &serviceKey, 77 } 78 err = client.connection.Make(request, &response) 79 80 return serviceKey, response.Warnings, err 81 }