Skip to content

Commit

Permalink
Feat: Add gitops account settings resource and data source for config…
Browse files Browse the repository at this point in the history
…uring account level gitops settings (Git provider and ISC repo) (#147)
  • Loading branch information
ilia-medvedev-codefresh authored Jun 3, 2024
1 parent 6eb7bf1 commit 205d0ec
Show file tree
Hide file tree
Showing 16 changed files with 616 additions and 68 deletions.
2 changes: 1 addition & 1 deletion codefresh/cfclient/current_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (client *Client) GetCurrentAccount() (*CurrentAccount, error) {
currentAccount.ID = accX.Get("id").String()
admins := accX.Get("admins").InterSlice()
for _, adminI := range admins {
accountAdminsIDs = append(accountAdminsIDs ,adminI.(string))
accountAdminsIDs = append(accountAdminsIDs, adminI.(string))
}
break
}
Expand Down
83 changes: 83 additions & 0 deletions codefresh/cfclient/gitops_account_settings.go
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
}
16 changes: 8 additions & 8 deletions codefresh/cfclient/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ type RuntimeEnvironment struct {
}

type ExternalResource struct {
ID string `json:"id,omitempty"`
Type string `json:"type"`
Source string `json:"source"`
Context string `json:"context"`
ID string `json:"id,omitempty"`
Type string `json:"type"`
Source string `json:"source"`
Context string `json:"context"`
Destination string `json:"destination"`
IsFolder bool `json:"isFolder"`
Repo string `json:"repo"`
Revision string `json:"revision"`
IsFolder bool `json:"isFolder"`
Repo string `json:"repo"`
Revision string `json:"revision"`
}

func (t *Trigger) SetVariables(variables map[string]interface{}, encrypted bool) {
Expand Down Expand Up @@ -134,7 +134,7 @@ type Spec struct {
Hooks *Hooks `json:"hooks,omitempty"`
Options map[string]bool `json:"options,omitempty"`
PermitRestartFromFailedSteps bool `json:"permitRestartFromFailedSteps,omitempty"`
ExternalResources []ExternalResource `json:"externalResources,omitempty"`
ExternalResources []ExternalResource `json:"externalResources,omitempty"`
}

type Steps struct {
Expand Down
6 changes: 3 additions & 3 deletions codefresh/cfclient/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type User struct {
HasPassword bool `json:"hasPassword,omitempty"`
Notifications []NotificationEvent `json:"notifications,omitempty"`
ShortProfile ShortProfile `json:"shortProfile,omitempty"`
PublicProfile PublicProfile `json:"publicProfile,omitempty"`
PublicProfile PublicProfile `json:"publicProfile,omitempty"`
Logins []Login `json:"logins,omitempty"`
InviteURL string `json:"inviteUrl,omitempty"`
}
Expand Down Expand Up @@ -389,7 +389,7 @@ func (client *Client) UpdateUserDetails(accountId, userId, userName, userEmail s
return &respUser, nil
}

func (client *Client) UpdateLocalUserPassword(userName, password string) (error) {
func (client *Client) UpdateLocalUserPassword(userName, password string) error {

fullPath := "/admin/user/localProvider"

Expand All @@ -410,7 +410,7 @@ func (client *Client) UpdateLocalUserPassword(userName, password string) (error)
return nil
}

func (client *Client) DeleteLocalUserPassword(userName string) (error) {
func (client *Client) DeleteLocalUserPassword(userName string) error {

fullPath := fmt.Sprintf("/admin/user/localProvider?userName=%s", userName)

Expand Down
78 changes: 78 additions & 0 deletions codefresh/data_account_gitops_settings.go
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
}
36 changes: 36 additions & 0 deletions codefresh/internal/gitops/account_settings.go
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)
}
2 changes: 2 additions & 0 deletions codefresh/internal/gitops/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Shared types, schemas and functions for gitops
package gitops
26 changes: 14 additions & 12 deletions codefresh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,19 @@ func Provider() *schema.Provider {
},
},
DataSourcesMap: map[string]*schema.Resource{
"codefresh_account": dataSourceAccount(),
"codefresh_context": dataSourceContext(),
"codefresh_current_account": dataSourceCurrentAccount(),
"codefresh_idps": dataSourceIdps(),
"codefresh_step_types": dataSourceStepTypes(),
"codefresh_team": dataSourceTeam(),
"codefresh_user": dataSourceUser(),
"codefresh_users": dataSourceUsers(),
"codefresh_registry": dataSourceRegistry(),
"codefresh_pipelines": dataSourcePipelines(),
"codefresh_account_idp": dataSourceAccountIdp(),
"codefresh_project": dataSourceProject(),
"codefresh_account": dataSourceAccount(),
"codefresh_context": dataSourceContext(),
"codefresh_current_account": dataSourceCurrentAccount(),
"codefresh_idps": dataSourceIdps(),
"codefresh_step_types": dataSourceStepTypes(),
"codefresh_team": dataSourceTeam(),
"codefresh_user": dataSourceUser(),
"codefresh_users": dataSourceUsers(),
"codefresh_registry": dataSourceRegistry(),
"codefresh_pipelines": dataSourcePipelines(),
"codefresh_account_idp": dataSourceAccountIdp(),
"codefresh_project": dataSourceProject(),
"codefresh_account_gitops_settings": dataSourceAccountGitopsSettings(),
},
ResourcesMap: map[string]*schema.Resource{
"codefresh_account": resourceAccount(),
Expand All @@ -72,6 +73,7 @@ func Provider() *schema.Provider {
"codefresh_abac_rules": resourceGitopsAbacRule(),
"codefresh_idp": resourceIdp(),
"codefresh_account_idp": resourceAccountIdp(),
"codefresh_account_gitops_settings": resourceAccountGitopsSettings(),
},
ConfigureFunc: configureProvider,
}
Expand Down
Loading

0 comments on commit 205d0ec

Please sign in to comment.