generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/mesh integrations #79
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| ) | ||
|
|
||
| const CONTENT_TYPE_INTEGRATION = "application/vnd.meshcloud.api.meshintegration.v1-preview.hal+json" | ||
|
|
||
| type MeshIntegration struct { | ||
| ApiVersion string `json:"apiVersion" tfsdk:"api_version"` | ||
| Kind string `json:"kind" tfsdk:"kind"` | ||
| Metadata MeshIntegrationMetadata `json:"metadata" tfsdk:"metadata"` | ||
| Spec MeshIntegrationSpec `json:"spec" tfsdk:"spec"` | ||
| Status *MeshIntegrationStatus `json:"status,omitempty" tfsdk:"status"` | ||
| } | ||
|
|
||
| type MeshIntegrationMetadata struct { | ||
| Uuid *string `json:"uuid,omitempty" tfsdk:"uuid"` | ||
| OwnedByWorkspace string `json:"ownedByWorkspace" tfsdk:"owned_by_workspace"` | ||
| CreatedOn *string `json:"createdOn,omitempty" tfsdk:"created_on"` | ||
| } | ||
|
|
||
| type MeshIntegrationSpec struct { | ||
| DisplayName string `json:"displayName" tfsdk:"display_name"` | ||
| Config MeshIntegrationConfig `json:"config" tfsdk:"config"` | ||
| } | ||
|
|
||
| type MeshIntegrationStatus struct { | ||
| IsBuiltIn bool `json:"isBuiltIn" tfsdk:"is_built_in"` | ||
| WorkloadIdentityFederation *MeshWorkloadIdentityFederation `json:"workloadIdentityFederation,omitempty" tfsdk:"workload_identity_federation"` | ||
| } | ||
|
|
||
| // Integration Config wrapper with type discrimination | ||
| type MeshIntegrationConfig struct { | ||
| Type string `json:"type" tfsdk:"type"` | ||
| Github *MeshIntegrationGithubConfig `json:"github,omitempty" tfsdk:"github"` | ||
| Gitlab *MeshIntegrationGitlabConfig `json:"gitlab,omitempty" tfsdk:"gitlab"` | ||
| AzureDevops *MeshIntegrationAzureDevopsConfig `json:"azuredevops,omitempty" tfsdk:"azuredevops"` | ||
| } | ||
|
|
||
| // GitHub Integration | ||
| type MeshIntegrationGithubConfig struct { | ||
| Owner string `json:"owner" tfsdk:"owner"` | ||
| BaseUrl string `json:"baseUrl" tfsdk:"base_url"` | ||
| AppId string `json:"appId" tfsdk:"app_id"` | ||
| AppPrivateKey string `json:"appPrivateKey" tfsdk:"app_private_key"` | ||
| RunnerRef BuildingBlockRunnerRef `json:"runnerRef" tfsdk:"runner_ref"` | ||
| } | ||
|
|
||
| // GitLab Integration | ||
| type MeshIntegrationGitlabConfig struct { | ||
| BaseUrl string `json:"baseUrl" tfsdk:"base_url"` | ||
| RunnerRef BuildingBlockRunnerRef `json:"runnerRef" tfsdk:"runner_ref"` | ||
| } | ||
|
|
||
| // Azure DevOps Integration | ||
| type MeshIntegrationAzureDevopsConfig struct { | ||
| BaseUrl string `json:"baseUrl" tfsdk:"base_url"` | ||
| Organization string `json:"organization" tfsdk:"organization"` | ||
| PersonalAccessToken string `json:"personalAccessToken" tfsdk:"personal_access_token"` | ||
| RunnerRef BuildingBlockRunnerRef `json:"runnerRef" tfsdk:"runner_ref"` | ||
| } | ||
|
|
||
| // Building Block Runner Reference | ||
| type BuildingBlockRunnerRef struct { | ||
| Uuid string `json:"uuid" tfsdk:"uuid"` | ||
| Kind string `json:"kind" tfsdk:"kind"` | ||
| } | ||
|
|
||
| // Workload Identity Federation | ||
| type MeshWorkloadIdentityFederation struct { | ||
| Issuer string `json:"issuer" tfsdk:"issuer"` | ||
| Subject string `json:"subject" tfsdk:"subject"` | ||
| Gcp *MeshWifProvider `json:"gcp,omitempty" tfsdk:"gcp"` | ||
| Aws *MeshAwsWifProvider `json:"aws,omitempty" tfsdk:"aws"` | ||
| Azure *MeshWifProvider `json:"azure,omitempty" tfsdk:"azure"` | ||
| } | ||
|
|
||
| type MeshWifProvider struct { | ||
| Audience string `json:"audience" tfsdk:"audience"` | ||
| } | ||
|
|
||
| type MeshAwsWifProvider struct { | ||
| Audience string `json:"audience" tfsdk:"audience"` | ||
| Thumbprint string `json:"thumbprint" tfsdk:"thumbprint"` | ||
| } | ||
|
|
||
| func (c *MeshStackProviderClient) urlForIntegration(workspace string, uuid string) *url.URL { | ||
| return c.endpoints.Integrations.JoinPath(workspace, uuid) | ||
| } | ||
|
|
||
| func (c *MeshStackProviderClient) ReadIntegration(workspace string, uuid string) (*MeshIntegration, error) { | ||
| targetUrl := c.urlForIntegration(workspace, uuid) | ||
| req, err := http.NewRequest("GET", targetUrl.String(), nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| req.Header.Set("Accept", CONTENT_TYPE_INTEGRATION) | ||
|
|
||
| res, err := c.doAuthenticatedRequest(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| defer res.Body.Close() | ||
|
|
||
| data, err := io.ReadAll(res.Body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if res.StatusCode == http.StatusNotFound { | ||
| return nil, nil | ||
| } | ||
|
|
||
| if !isSuccessHTTPStatus(res) { | ||
| return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) | ||
| } | ||
|
|
||
| var integration MeshIntegration | ||
| err = json.Unmarshal(data, &integration) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &integration, nil | ||
| } | ||
|
|
||
| func (c *MeshStackProviderClient) ReadIntegrations() (*[]MeshIntegration, error) { | ||
| var allIntegrations []MeshIntegration | ||
|
|
||
| pageNumber := 0 | ||
| targetUrl := c.endpoints.Integrations | ||
| query := targetUrl.Query() | ||
|
|
||
| for { | ||
| query.Set("page", fmt.Sprintf("%d", pageNumber)) | ||
| targetUrl.RawQuery = query.Encode() | ||
|
|
||
| req, err := http.NewRequest("GET", targetUrl.String(), nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| req.Header.Set("Accept", CONTENT_TYPE_INTEGRATION) | ||
|
|
||
| res, err := c.doAuthenticatedRequest(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| defer res.Body.Close() | ||
|
|
||
| data, err := io.ReadAll(res.Body) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read response body: %w", err) | ||
| } | ||
|
|
||
| if !isSuccessHTTPStatus(res) { | ||
| return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data) | ||
| } | ||
|
|
||
| var response struct { | ||
| Embedded struct { | ||
| MeshIntegrations []MeshIntegration `json:"meshIntegrations"` | ||
| } `json:"_embedded"` | ||
| Page struct { | ||
| Size int `json:"size"` | ||
| TotalElements int `json:"totalElements"` | ||
| TotalPages int `json:"totalPages"` | ||
| Number int `json:"number"` | ||
| } `json:"page"` | ||
| } | ||
|
|
||
| err = json.Unmarshal(data, &response) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| allIntegrations = append(allIntegrations, response.Embedded.MeshIntegrations...) | ||
|
|
||
| // Check if there are more pages | ||
| if response.Page.Number >= response.Page.TotalPages-1 { | ||
| break | ||
| } | ||
|
|
||
| pageNumber++ | ||
| } | ||
|
|
||
| return &allIntegrations, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.