github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/api/cloudcontroller/ccv3/service_broker.go (about) 1 package ccv3 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "errors" 7 8 "code.cloudfoundry.org/cli/api/cloudcontroller" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 11 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal" 12 ) 13 14 // ServiceBroker represents Service Broker data 15 type ServiceBroker struct { 16 // GUID is a unique service broker identifier. 17 GUID string 18 // Name is the name of the service broker. 19 Name string 20 // URL is the url of the service broker. 21 URL string 22 // Status is the state of the service broker. 23 Status string 24 25 Metadata *Metadata 26 } 27 28 type ServiceBrokerModel struct { 29 // Name is the name of the service broker. 30 Name string 31 // URL is the url of the service broker. 32 URL string 33 // Username is the Basic Auth username for the service broker. 34 Username string 35 // Password is the Basic Auth password for the service broker. 36 Password string 37 // Space GUID for the space that the broker is in. Empty when not a space-scoped service broker. 38 SpaceGUID string 39 } 40 41 // serviceBrokerRequest represents a Cloud Controller V3 Service Broker (when creating and updating). 42 type serviceBrokerRequest struct { 43 // GUID is a unique service broker identifier. 44 GUID string `json:"guid,omitempty"` 45 // Name is the name of the service broker. 46 Name string `json:"name,omitempty"` 47 // URL is the url of the service broker. 48 URL string `json:"url,omitempty"` 49 // Authentication contains the authentication for authenticating with the service broker. 50 Authentication *serviceBrokerAuthentication `json:"authentication,omitempty"` 51 // This is the relationship for the space GUID 52 Relationships *serviceBrokerRelationships `json:"relationships,omitempty"` 53 } 54 55 // serviceBrokerResponse represents a Cloud Controller V3 Service Broker (when reading). 56 type serviceBrokerResponse struct { 57 // GUID is a unique service broker identifier. 58 GUID string `json:"guid,omitempty"` 59 // Name is the name of the service broker. 60 Name string `json:"name"` 61 // URL is the url of the service broker. 62 URL string `json:"url"` 63 // Status is the state of the service broker. 64 Status string `json:"status,omitempty"` 65 // Authentication contains the authentication for authenticating with the service broker. 66 Authentication serviceBrokerAuthentication `json:"authentication"` 67 // Metadata is used for custom tagging of API resources 68 Metadata *Metadata 69 // This is the relationship for the space GUID 70 Relationships *serviceBrokerRelationships `json:"relationships,omitempty"` 71 } 72 73 // serviceBrokerAuthentication represents a data structure for the Credentials 74 // of V3 Service Broker. 75 type serviceBrokerAuthentication struct { 76 // Type is the type of authentication for the service broker, e.g. "basic" 77 Type constant.ServiceBrokerCredentialsType `json:"type"` 78 // Data is the authentication data of the service broker of a particular type. 79 Credentials serviceBrokerBasicAuthCredentials `json:"credentials"` 80 } 81 82 // serviceBrokerBasicAuthCredentials represents a data structure for the Credentials Data 83 // of V3 Service Broker Credentials. 84 type serviceBrokerBasicAuthCredentials struct { 85 // Username is the Basic Auth username for the service broker. 86 Username string `json:"username"` 87 // Password is the Basic Auth password for the service broker. 88 Password string `json:"password"` 89 } 90 91 // serviceBrokerRelationships represents a data structure for the relationships data 92 // of V3 Service Broker Relationships. 93 type serviceBrokerRelationships struct { 94 // Space represents the space that a space-scoped broker is in 95 Space serviceBrokerRelationshipsSpace `json:"space"` 96 } 97 98 // serviceBrokerRelationshipsSpace represents a data structure for the relationships space data 99 // of V3 Service Broker Relationships. 100 type serviceBrokerRelationshipsSpace struct { 101 // Data holds the space GUID object 102 Data serviceBrokerRelationshipsSpaceData `json:"data"` 103 } 104 105 // serviceBrokerRelationshipsSpaceData represents a data structure for the relationships space GUID data 106 // of V3 Service Broker Relationships. 107 type serviceBrokerRelationshipsSpaceData struct { 108 // GUID is the space guid associated with a space-scoped broker 109 GUID string `json:"guid"` 110 } 111 112 // CreateServiceBroker registers a new service broker. 113 func (client *Client) CreateServiceBroker(serviceBroker ServiceBrokerModel) (JobURL, Warnings, error) { 114 bodyBytes, err := json.Marshal(newServiceBroker(serviceBroker)) 115 if err != nil { 116 return "", nil, err 117 } 118 119 request, err := client.newHTTPRequest(requestOptions{ 120 RequestName: internal.PostServiceBrokerRequest, 121 Body: bytes.NewReader(bodyBytes), 122 }) 123 if err != nil { 124 return "", nil, err 125 } 126 127 response := cloudcontroller.Response{} 128 err = client.connection.Make(request, &response) 129 return JobURL(response.ResourceLocationURL), response.Warnings, err 130 } 131 132 // DeleteServiceBroker deletes a named service broker 133 func (client *Client) DeleteServiceBroker(serviceBrokerGUID string) (JobURL, Warnings, error) { 134 request, err := client.newHTTPRequest(requestOptions{ 135 RequestName: internal.DeleteServiceBrokerRequest, 136 URIParams: map[string]string{ 137 "service_broker_guid": serviceBrokerGUID, 138 }, 139 }) 140 141 if err != nil { 142 return "", nil, err 143 } 144 145 response := cloudcontroller.Response{} 146 err = client.connection.Make(request, &response) 147 return JobURL(response.ResourceLocationURL), response.Warnings, err 148 } 149 150 // GetServiceBrokers lists service brokers. 151 func (client *Client) GetServiceBrokers(query ...Query) ([]ServiceBroker, Warnings, error) { 152 request, err := client.newHTTPRequest(requestOptions{ 153 RequestName: internal.GetServiceBrokersRequest, 154 Query: query, 155 }) 156 if err != nil { 157 return nil, nil, err 158 } 159 160 var fullList []ServiceBroker 161 warnings, err := client.paginate(request, serviceBrokerResponse{}, func(item interface{}) error { 162 if serviceBroker, ok := item.(serviceBrokerResponse); ok { 163 fullList = append(fullList, extractServiceBrokerData(serviceBroker)) 164 } else { 165 return ccerror.UnknownObjectInListError{ 166 Expected: serviceBrokerResponse{}, 167 Unexpected: item, 168 } 169 } 170 return nil 171 }) 172 173 return fullList, warnings, err 174 } 175 176 // UpdateServiceBroker updates an existing service broker. 177 func (client *Client) UpdateServiceBroker(serviceBrokerGUID string, serviceBroker ServiceBrokerModel) (JobURL, Warnings, error) { 178 brokerUpdateRequest, err := newUpdateServiceBroker(serviceBroker) 179 if err != nil { 180 return "", nil, err 181 } 182 183 bodyBytes, err := json.Marshal(brokerUpdateRequest) 184 if err != nil { 185 return "", nil, err 186 } 187 188 request, err := client.newHTTPRequest(requestOptions{ 189 RequestName: internal.PatchServiceBrokerRequest, 190 URIParams: map[string]string{ 191 "service_broker_guid": serviceBrokerGUID, 192 }, 193 Body: bytes.NewReader(bodyBytes), 194 }) 195 if err != nil { 196 return "", nil, err 197 } 198 199 response := cloudcontroller.Response{} 200 err = client.connection.Make(request, &response) 201 return JobURL(response.ResourceLocationURL), response.Warnings, err 202 } 203 204 func newServiceBroker(serviceBroker ServiceBrokerModel) serviceBrokerRequest { 205 serviceBrokerRequest := serviceBrokerRequest{ 206 Name: serviceBroker.Name, 207 URL: serviceBroker.URL, 208 Authentication: &serviceBrokerAuthentication{ 209 Type: constant.BasicCredentials, 210 Credentials: serviceBrokerBasicAuthCredentials{ 211 Username: serviceBroker.Username, 212 Password: serviceBroker.Password, 213 }, 214 }, 215 } 216 217 if serviceBroker.SpaceGUID != "" { 218 serviceBrokerRequest.Relationships = &serviceBrokerRelationships{ 219 Space: serviceBrokerRelationshipsSpace{ 220 Data: serviceBrokerRelationshipsSpaceData{ 221 GUID: serviceBroker.SpaceGUID, 222 }, 223 }, 224 } 225 } 226 227 return serviceBrokerRequest 228 } 229 230 func newUpdateServiceBroker(serviceBroker ServiceBrokerModel) (serviceBrokerRequest, error) { 231 name := serviceBroker.Name 232 username := serviceBroker.Username 233 password := serviceBroker.Password 234 brokerURL := serviceBroker.URL 235 if (username == "" && password != "") || (username != "" && password == "") { 236 return serviceBrokerRequest{}, errors.New("Incorrect usage: both username and password must be defined in order to do an update") 237 } 238 request := serviceBrokerRequest{ 239 Name: name, 240 URL: brokerURL, 241 } 242 243 if username != "" && password != "" { 244 request.Authentication = &serviceBrokerAuthentication{ 245 Type: constant.BasicCredentials, 246 Credentials: serviceBrokerBasicAuthCredentials{ 247 Username: username, 248 Password: password, 249 }, 250 } 251 } 252 253 return request, nil 254 } 255 256 func extractServiceBrokerData(response serviceBrokerResponse) ServiceBroker { 257 return ServiceBroker{ 258 Name: response.Name, 259 URL: response.URL, 260 GUID: response.GUID, 261 Status: response.Status, 262 Metadata: response.Metadata, 263 } 264 }