-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add gitops account settings resource and data source for config…
…uring account level gitops settings (Git provider and ISC repo) (#147)
- Loading branch information
1 parent
6eb7bf1
commit 205d0ec
Showing
16 changed files
with
616 additions
and
68 deletions.
There are no files selected for viewing
This file contains 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 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,83 @@ | ||
package cfclient | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type GitopsActiveAccountResponse struct { | ||
Data struct { | ||
Me struct { | ||
ActiveAccount GitopsActiveAccountInfo `json:"activeAccount,omitempty"` | ||
} `json:"me,omitempty"` | ||
} `json:"data,omitempty"` | ||
} | ||
|
||
type GitopsActiveAccountInfo struct { | ||
ID string `json:"id,omitempty"` | ||
AccountName string `json:"name,omitempty"` | ||
GitProvider string `json:"gitProvider,omitempty"` | ||
GitApiUrl string `json:"gitApiUrl,omitempty"` | ||
SharedConfigRepo string `json:"sharedConfigRepo,omitempty"` | ||
Admins []string `json:"admins,omitempty"` | ||
} | ||
|
||
func (client *Client) GetActiveGitopsAccountInfo() (*GitopsActiveAccountInfo, error) { | ||
request := GraphQLRequest{ | ||
Query: ` | ||
query AccountInfo { | ||
me { | ||
activeAccount { | ||
id | ||
name | ||
gitProvider | ||
gitApiUrl | ||
sharedConfigRepo | ||
admins | ||
} | ||
} | ||
} | ||
`, | ||
} | ||
|
||
response, err := client.SendGqlRequest(request) | ||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return nil, err | ||
} | ||
|
||
var gitopsAccountResponse GitopsActiveAccountResponse | ||
|
||
err = DecodeGraphQLResponseInto(response, &gitopsAccountResponse) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
gitopsActiveAccountInfo := gitopsAccountResponse.Data.Me.ActiveAccount | ||
|
||
return &gitopsActiveAccountInfo, nil | ||
} | ||
|
||
func (client *Client) UpdateActiveGitopsAccountSettings(gitProvider string, gitProviderApiUrl string, sharedConfigRepo string) error { | ||
request := GraphQLRequest{ | ||
Query: ` | ||
mutation updateCsdpSettings($gitProvider: GitProviders!, $gitApiUrl: String!, $sharedConfigRepo: String!) { | ||
updateCsdpSettings(gitProvider: $gitProvider, gitApiUrl: $gitApiUrl, sharedConfigRepo: $sharedConfigRepo) | ||
} | ||
`, | ||
Variables: map[string]interface{}{ | ||
"gitProvider": gitProvider, | ||
"gitApiUrl": gitProviderApiUrl, | ||
"sharedConfigRepo": sharedConfigRepo, | ||
}, | ||
} | ||
|
||
_, err := client.SendGqlRequest(request) | ||
|
||
if err != nil { | ||
fmt.Println("Error:", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains 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 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 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,78 @@ | ||
package codefresh | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceAccountGitopsSettings() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "This data source retrieves gitops settings for the active account", | ||
Read: dataSourceAccountGitopsSettingsRead, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Description: "Account Id", | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Account name for active account", | ||
}, | ||
"git_provider": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Git provider name", | ||
}, | ||
"git_provider_api_url": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Git provider API url", | ||
}, | ||
"shared_config_repository": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Shared config repository url", | ||
}, | ||
"admins": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAccountGitopsSettingsRead(d *schema.ResourceData, meta interface{}) error { | ||
|
||
client := meta.(*cfclient.Client) | ||
var accountGitopsInfo *cfclient.GitopsActiveAccountInfo | ||
|
||
accountGitopsInfo, err := client.GetActiveGitopsAccountInfo() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return mapDataAccountGitopsSettingsToResource(accountGitopsInfo, d) | ||
} | ||
|
||
func mapDataAccountGitopsSettingsToResource(account *cfclient.GitopsActiveAccountInfo, d *schema.ResourceData) error { | ||
|
||
if account == nil || account.ID == "" { | ||
return fmt.Errorf("cannot get gitops settings as account wasn't properly retrived") | ||
} | ||
d.SetId(account.ID) | ||
d.Set("name", account.AccountName) | ||
d.Set("admins", account.Admins) | ||
d.Set("git_provider", account.GitProvider) | ||
d.Set("git_provider_api_url", account.GitApiUrl) | ||
d.Set("shared_config_repository", account.SharedConfigRepo) | ||
|
||
return nil | ||
} |
This file contains 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,36 @@ | ||
package gitops | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
const ( | ||
// Git providers enum from https://github.com/codefresh-io/argo-platform/blob/90f86de326422ca3bd1f64ca5dd26aeedf985e3e/libs/ql/schema/entities/common/integration.graphql#L200 | ||
GitProviderGitHub string = "GITHUB" | ||
GitProviderGerrit string = "GERRIT" | ||
GitProviderGitlab string = "GITLAB" | ||
GitProviderBitbucket string = "BITBUCKET" | ||
GitProviderBitbucketServer string = "BITBUCKET_SERVER" | ||
) | ||
|
||
func GetSupportedGitProvidersList() []string { | ||
return []string{GitProviderGitHub, GitProviderGerrit, GitProviderGitlab, GitProviderBitbucket, GitProviderBitbucketServer} | ||
} | ||
|
||
// Matching implementation for https://github.com/codefresh-io/argo-platform/blob/3c6af5b5cbb29aef58ef6617e71159e882987f5c/libs/git/src/helpers.ts#L37. | ||
// Must be updated accordingly | ||
func GetDefaultAPIUrlForProvider(gitProvider string) (*string, error) { | ||
|
||
defaultApiUrlProvider := map[string]string{ | ||
GitProviderGitHub: "https://api.github.com", | ||
GitProviderGitlab: "https://gitlab.com/api/v4", | ||
GitProviderBitbucket: "https://api.bitbucket.org/2.0", | ||
GitProviderGerrit: "https://gerrit-review.googlesource.com/a", | ||
} | ||
|
||
if val, ok := defaultApiUrlProvider[gitProvider]; ok { | ||
return &val, nil | ||
} | ||
|
||
return nil, fmt.Errorf("no default API URL for provider %s can be found. For self hosted git providers URL must be provided explicitly", gitProvider) | ||
} |
This file contains 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,2 @@ | ||
// Shared types, schemas and functions for gitops | ||
package gitops |
This file contains 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
Oops, something went wrong.