Skip to content

Commit 81224d9

Browse files
committed
Fixing deploy from URL and comments
LMCROSSITXSADEPLOY-2301
1 parent 8922dd2 commit 81224d9

8 files changed

Lines changed: 198 additions & 66 deletions

clients/cfrestclient/cloud_foundry_operations_extended.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ type CloudFoundryOperationsExtended interface {
1111
GetServiceInstances(mtaId, mtaNamespace, spaceGuid string) ([]models.CloudFoundryServiceInstance, error)
1212
GetServiceBindings(serviceName string) ([]models.ServiceBinding, error)
1313
GetServiceInstanceByName(serviceName, spaceGuid string) (models.CloudFoundryServiceInstance, error)
14+
CreateUserProvidedServiceInstance(serviceName string, spaceGuid string, credentials map[string]string) (models.CloudFoundryServiceInstance, error)
1415
}

clients/cfrestclient/fakes/fake_cloud_foundry_client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ func (f FakeCloudFoundryClient) GetServiceBindings(serviceName string) ([]models
3838
func (f FakeCloudFoundryClient) GetServiceInstanceByName(serviceName, spaceGuid string) (models.CloudFoundryServiceInstance, error) {
3939
return f.Services[0], f.ServiceBindingsErr
4040
}
41+
42+
func (f FakeCloudFoundryClient) CreateUserProvidedServiceInstance(serviceName string, spaceGuid string, credentials map[string]string) (models.CloudFoundryServiceInstance, error) {
43+
return f.Services[0], f.ServicesErr
44+
}

clients/cfrestclient/resilient/resilient_rest_cloud_foundry_client_extended.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ func (c ResilientCloudFoundryRestClient) GetServiceInstanceByName(serviceName, s
5353
}, c.MaxRetriesCount, c.RetryInterval)
5454
}
5555

56+
func (c ResilientCloudFoundryRestClient) CreateUserProvidedServiceInstance(serviceName string, spaceGuid string, credentials map[string]string) (models.CloudFoundryServiceInstance, error) {
57+
return retryOnError(func() (models.CloudFoundryServiceInstance, error) {
58+
return c.CloudFoundryRestClient.CreateUserProvidedServiceInstance(serviceName, spaceGuid, credentials)
59+
}, c.MaxRetriesCount, c.RetryInterval)
60+
}
61+
5662
func retryOnError[T any](operation func() (T, error), retries int, retryInterval time.Duration) (T, error) {
5763
result, err := operation()
5864
for shouldRetry(retries, err) {

clients/cfrestclient/rest_cloud_foundry_client_extended.go

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cfrestclient
22

33
import (
4+
"bytes"
45
"crypto/md5"
56
"crypto/tls"
67
"encoding/hex"
@@ -60,7 +61,7 @@ func (c CloudFoundryRestClient) GetAppProcessStatistics(appGuid string) ([]model
6061
apiEndpoint, _ := c.cliConn.ApiEndpoint()
6162

6263
getAppProcessStatsUrl := fmt.Sprintf("%s/%sapps/%s/processes/web/stats", apiEndpoint, cfBaseUrl, appGuid)
63-
body, err := executeRequest(getAppProcessStatsUrl, token, c.isSslDisabled)
64+
body, err := executeRequest("GET", getAppProcessStatsUrl, token, c.isSslDisabled, nil)
6465
if err != nil {
6566
return nil, err
6667
}
@@ -135,10 +136,61 @@ func (c CloudFoundryRestClient) GetServiceInstanceByName(serviceName, spaceGuid
135136
return resultService, nil
136137
}
137138

139+
func (c CloudFoundryRestClient) CreateUserProvidedServiceInstance(serviceName string, spaceGuid string, credentials map[string]string) (models.CloudFoundryServiceInstance, error) {
140+
token, err := c.cliConn.AccessToken()
141+
if err != nil {
142+
return models.CloudFoundryServiceInstance{}, fmt.Errorf("failed to retrieve access token: %s", err)
143+
}
144+
145+
apiEndpoint, _ := c.cliConn.ApiEndpoint()
146+
147+
createServiceURL := fmt.Sprintf("%s/%sservice_instances", apiEndpoint, cfBaseUrl)
148+
149+
payload := map[string]any{
150+
"type": "user-provided",
151+
"name": serviceName,
152+
"relationships": map[string]any{
153+
"space": map[string]any{
154+
"data": map[string]any{
155+
"guid": spaceGuid,
156+
},
157+
},
158+
},
159+
}
160+
161+
if credentials != nil {
162+
payload["credentials"] = credentials
163+
}
164+
165+
jsonBody, err := json.Marshal(payload)
166+
if err != nil {
167+
return models.CloudFoundryServiceInstance{}, fmt.Errorf("failed to marshal create UPS request: %w", err)
168+
}
169+
170+
body, err := executeRequest(http.MethodPost, createServiceURL, token, c.isSslDisabled, jsonBody)
171+
if err != nil {
172+
return models.CloudFoundryServiceInstance{}, err
173+
}
174+
175+
response, err := parseBody[models.CloudFoundryUserProvidedServiceInstance](body)
176+
if err != nil {
177+
return models.CloudFoundryServiceInstance{}, err
178+
}
179+
180+
return models.CloudFoundryServiceInstance{
181+
Guid: response.Guid,
182+
Name: response.Name,
183+
Type: response.Type,
184+
LastOperation: response.LastOperation,
185+
SpaceGuid: response.SpaceGuid,
186+
Metadata: response.Metadata,
187+
}, nil
188+
}
189+
138190
func getPaginatedResources[T any](url, token string, isSslDisabled bool) ([]T, error) {
139191
var result []T
140192
for url != "" {
141-
body, err := executeRequest(url, token, isSslDisabled)
193+
body, err := executeRequest("GET", url, token, isSslDisabled, nil)
142194
if err != nil {
143195
return nil, err
144196
}
@@ -158,7 +210,7 @@ func getPaginatedResources[T any](url, token string, isSslDisabled bool) ([]T, e
158210
func getPaginatedResourcesWithIncluded[T any, Auxiliary any](url, token string, isSslDisabled bool, auxiliaryContentHandler func(T, Auxiliary) T) ([]T, error) {
159211
var result []T
160212
for url != "" {
161-
body, err := executeRequest(url, token, isSslDisabled)
213+
body, err := executeRequest("GET", url, token, isSslDisabled, nil)
162214
if err != nil {
163215
return nil, err
164216
}
@@ -175,9 +227,22 @@ func getPaginatedResourcesWithIncluded[T any, Auxiliary any](url, token string,
175227
return result, nil
176228
}
177229

178-
func executeRequest(url, token string, isSslDisabled bool) ([]byte, error) {
179-
req, _ := http.NewRequest(http.MethodGet, url, nil)
180-
req.Header.Add("Authorization", token)
230+
func executeRequest(methodType, url, token string, isSslDisabled bool, body []byte) ([]byte, error) {
231+
var reader io.Reader
232+
233+
if body != nil {
234+
reader = bytes.NewReader(body)
235+
}
236+
request, err := http.NewRequest(methodType, url, reader)
237+
if err != nil {
238+
return nil, err
239+
}
240+
241+
request.Header.Add("Authorization", token)
242+
request.Header.Set("Accept", "application/json")
243+
if body != nil {
244+
request.Header.Set("Content-Type", "application/json")
245+
}
181246

182247
// Create transport with TLS configuration
183248
httpTransport := http.DefaultTransport.(*http.Transport).Clone()
@@ -187,7 +252,7 @@ func executeRequest(url, token string, isSslDisabled bool) ([]byte, error) {
187252
userAgentTransport := baseclient.NewUserAgentTransport(httpTransport)
188253

189254
client := &http.Client{Transport: userAgentTransport}
190-
resp, err := client.Do(req)
255+
resp, err := client.Do(request)
191256
if err != nil {
192257
return nil, err
193258
}

clients/models/cf_services_response.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ type CloudFoundryServiceInstance struct {
1414
Offering ServiceOffering `json:"-"`
1515
}
1616

17+
type CloudFoundryUserProvidedServiceInstance struct {
18+
Guid string `json:"guid"`
19+
Name string `json:"name"`
20+
Type string `json:"type"`
21+
LastOperation LastOperation `json:"last_operation,omitempty"`
22+
SpaceGuid string `jsonry:"relationships.space.data.guid"`
23+
Metadata Metadata `json:"metadata"`
24+
}
25+
1726
type LastOperation struct {
1827
Type string `json:"type"`
1928
State string `json:"state"`
@@ -25,7 +34,7 @@ type LastOperation struct {
2534
type ServicePlan struct {
2635
Guid string `json:"guid"`
2736
Name string `json:"name"`
28-
OfferingGuid string `jsonry:"relationships.service_offering.data.guid,omitempty"`
37+
OfferingGuid string `jsonry:"rela tionships.service_offering.data.guid,omitempty"`
2938
}
3039

3140
type ServiceOffering struct {

clients/mtaclient/mta_rest_client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type AsyncUploadJobResult struct {
4141
Error string `json:"error,omitempty"`
4242
File *models.FileMetadata `json:"file,omitempty"`
4343
MtaId string `json:"mta_id,omitempty"`
44+
SchemaVersion string `json:"schema_version,omitempty"`
4445
BytesProcessed int64 `json:"bytes_processed,omitempty"`
4546
ClientActions []string `json:"client_actions,omitempty"`
4647
}

0 commit comments

Comments
 (0)