-
Notifications
You must be signed in to change notification settings - Fork 43
Adding a new flag to the deploy command and the related new functionality in order to support the collecting of secrets and sending them to the backend #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
9b5f948
2937061
1b06284
8922dd2
81224d9
ad47fc6
e2d70cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package cfrestclient | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "crypto/md5" | ||
| "crypto/tls" | ||
| "encoding/hex" | ||
|
|
@@ -60,7 +61,7 @@ func (c CloudFoundryRestClient) GetAppProcessStatistics(appGuid string) ([]model | |
| apiEndpoint, _ := c.cliConn.ApiEndpoint() | ||
|
|
||
| getAppProcessStatsUrl := fmt.Sprintf("%s/%sapps/%s/processes/web/stats", apiEndpoint, cfBaseUrl, appGuid) | ||
| body, err := executeRequest(getAppProcessStatsUrl, token, c.isSslDisabled) | ||
| body, err := executeRequest("GET", getAppProcessStatsUrl, token, c.isSslDisabled, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -114,10 +115,82 @@ func (c CloudFoundryRestClient) GetServiceBindings(serviceName string) ([]models | |
| return getPaginatedResourcesWithIncluded(getServiceBindingsUrl, token, c.isSslDisabled, buildServiceBinding) | ||
| } | ||
|
|
||
| func (c CloudFoundryRestClient) GetServiceInstanceByName(serviceName, spaceGuid string) (models.CloudFoundryServiceInstance, error) { | ||
| token, err := c.cliConn.AccessToken() | ||
| if err != nil { | ||
| return models.CloudFoundryServiceInstance{}, fmt.Errorf("failed to retrieve access token: %s", err) | ||
| } | ||
| apiEndpoint, _ := c.cliConn.ApiEndpoint() | ||
|
|
||
| getServicesUrl := fmt.Sprintf("%s/%sservice_instances?names=%s&space_guids=%s", | ||
| apiEndpoint, cfBaseUrl, serviceName, spaceGuid) | ||
| services, err := getPaginatedResourcesWithIncluded(getServicesUrl, token, c.isSslDisabled, buildServiceInstance) | ||
| if err != nil { | ||
| return models.CloudFoundryServiceInstance{}, err | ||
| } | ||
| if len(services) == 0 { | ||
| return models.CloudFoundryServiceInstance{}, fmt.Errorf("service instance not found") | ||
| } | ||
|
|
||
| resultService := services[0] | ||
| return resultService, nil | ||
| } | ||
|
|
||
| func (c CloudFoundryRestClient) CreateUserProvidedServiceInstance(serviceName string, spaceGuid string, credentials map[string]string) (models.CloudFoundryServiceInstance, error) { | ||
| token, err := c.cliConn.AccessToken() | ||
| if err != nil { | ||
| return models.CloudFoundryServiceInstance{}, fmt.Errorf("failed to retrieve access token: %s", err) | ||
| } | ||
|
|
||
| apiEndpoint, _ := c.cliConn.ApiEndpoint() | ||
|
|
||
| createServiceURL := fmt.Sprintf("%s/%sservice_instances", apiEndpoint, cfBaseUrl) | ||
|
|
||
| payload := map[string]any{ | ||
| "type": "user-provided", | ||
| "name": serviceName, | ||
| "relationships": map[string]any{ | ||
| "space": map[string]any{ | ||
| "data": map[string]any{ | ||
| "guid": spaceGuid, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| if credentials != nil { | ||
| payload["credentials"] = credentials | ||
| } | ||
|
|
||
| jsonBody, err := json.Marshal(payload) | ||
| if err != nil { | ||
| return models.CloudFoundryServiceInstance{}, fmt.Errorf("failed to marshal create UPS request: %w", err) | ||
| } | ||
|
|
||
| body, err := executeRequest(http.MethodPost, createServiceURL, token, c.isSslDisabled, jsonBody) | ||
| if err != nil { | ||
| return models.CloudFoundryServiceInstance{}, err | ||
| } | ||
|
|
||
| response, err := parseBody[models.CloudFoundryUserProvidedServiceInstance](body) | ||
| if err != nil { | ||
| return models.CloudFoundryServiceInstance{}, err | ||
| } | ||
|
|
||
| return models.CloudFoundryServiceInstance{ | ||
| Guid: response.Guid, | ||
| Name: response.Name, | ||
| Type: response.Type, | ||
| LastOperation: response.LastOperation, | ||
| SpaceGuid: response.SpaceGuid, | ||
| Metadata: response.Metadata, | ||
| }, nil | ||
| } | ||
|
|
||
| func getPaginatedResources[T any](url, token string, isSslDisabled bool) ([]T, error) { | ||
| var result []T | ||
| for url != "" { | ||
| body, err := executeRequest(url, token, isSslDisabled) | ||
| body, err := executeRequest("GET", url, token, isSslDisabled, nil) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -137,7 +210,7 @@ func getPaginatedResources[T any](url, token string, isSslDisabled bool) ([]T, e | |
| func getPaginatedResourcesWithIncluded[T any, Auxiliary any](url, token string, isSslDisabled bool, auxiliaryContentHandler func(T, Auxiliary) T) ([]T, error) { | ||
| var result []T | ||
| for url != "" { | ||
| body, err := executeRequest(url, token, isSslDisabled) | ||
| body, err := executeRequest("GET", url, token, isSslDisabled, nil) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -154,9 +227,22 @@ func getPaginatedResourcesWithIncluded[T any, Auxiliary any](url, token string, | |
| return result, nil | ||
| } | ||
|
|
||
| func executeRequest(url, token string, isSslDisabled bool) ([]byte, error) { | ||
| req, _ := http.NewRequest(http.MethodGet, url, nil) | ||
| req.Header.Add("Authorization", token) | ||
| func executeRequest(methodType, url, token string, isSslDisabled bool, body []byte) ([]byte, error) { | ||
| var reader io.Reader | ||
|
|
||
| if body != nil { | ||
| reader = bytes.NewReader(body) | ||
| } | ||
| request, err := http.NewRequest(methodType, url, reader) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| request.Header.Add("Authorization", token) | ||
| request.Header.Set("Accept", "application/json") | ||
| if body != nil { | ||
| request.Header.Set("Content-Type", "application/json") | ||
| } | ||
|
|
||
| // Create transport with TLS configuration | ||
| httpTransport := http.DefaultTransport.(*http.Transport).Clone() | ||
|
|
@@ -166,7 +252,7 @@ func executeRequest(url, token string, isSslDisabled bool) ([]byte, error) { | |
| userAgentTransport := baseclient.NewUserAgentTransport(httpTransport) | ||
|
|
||
| client := &http.Client{Transport: userAgentTransport} | ||
| resp, err := client.Do(req) | ||
| resp, err := client.Do(request) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,15 @@ type CloudFoundryServiceInstance struct { | |
| Offering ServiceOffering `json:"-"` | ||
| } | ||
|
|
||
| type CloudFoundryUserProvidedServiceInstance struct { | ||
| Guid string `json:"guid"` | ||
| Name string `json:"name"` | ||
| Type string `json:"type"` | ||
| LastOperation LastOperation `json:"last_operation,omitempty"` | ||
| SpaceGuid string `jsonry:"relationships.space.data.guid"` | ||
| Metadata Metadata `json:"metadata"` | ||
| } | ||
|
|
||
| type LastOperation struct { | ||
| Type string `json:"type"` | ||
| State string `json:"state"` | ||
|
|
@@ -25,7 +34,7 @@ type LastOperation struct { | |
| type ServicePlan struct { | ||
| Guid string `json:"guid"` | ||
| Name string `json:"name"` | ||
| OfferingGuid string `jsonry:"relationships.service_offering.data.guid,omitempty"` | ||
| OfferingGuid string `jsonry:"rela tionships.service_offering.data.guid,omitempty"` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this space typo. |
||
| } | ||
|
|
||
| type ServiceOffering struct { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
http.MethodGetinstead of raw strings.