Skip to content

Commit 205d0ec

Browse files
Feat: Add gitops account settings resource and data source for configuring account level gitops settings (Git provider and ISC repo) (#147)
1 parent 6eb7bf1 commit 205d0ec

16 files changed

+616
-68
lines changed

codefresh/cfclient/current_account.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (client *Client) GetCurrentAccount() (*CurrentAccount, error) {
6060
currentAccount.ID = accX.Get("id").String()
6161
admins := accX.Get("admins").InterSlice()
6262
for _, adminI := range admins {
63-
accountAdminsIDs = append(accountAdminsIDs ,adminI.(string))
63+
accountAdminsIDs = append(accountAdminsIDs, adminI.(string))
6464
}
6565
break
6666
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package cfclient
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type GitopsActiveAccountResponse struct {
8+
Data struct {
9+
Me struct {
10+
ActiveAccount GitopsActiveAccountInfo `json:"activeAccount,omitempty"`
11+
} `json:"me,omitempty"`
12+
} `json:"data,omitempty"`
13+
}
14+
15+
type GitopsActiveAccountInfo struct {
16+
ID string `json:"id,omitempty"`
17+
AccountName string `json:"name,omitempty"`
18+
GitProvider string `json:"gitProvider,omitempty"`
19+
GitApiUrl string `json:"gitApiUrl,omitempty"`
20+
SharedConfigRepo string `json:"sharedConfigRepo,omitempty"`
21+
Admins []string `json:"admins,omitempty"`
22+
}
23+
24+
func (client *Client) GetActiveGitopsAccountInfo() (*GitopsActiveAccountInfo, error) {
25+
request := GraphQLRequest{
26+
Query: `
27+
query AccountInfo {
28+
me {
29+
activeAccount {
30+
id
31+
name
32+
gitProvider
33+
gitApiUrl
34+
sharedConfigRepo
35+
admins
36+
}
37+
}
38+
}
39+
`,
40+
}
41+
42+
response, err := client.SendGqlRequest(request)
43+
if err != nil {
44+
fmt.Println("Error:", err)
45+
return nil, err
46+
}
47+
48+
var gitopsAccountResponse GitopsActiveAccountResponse
49+
50+
err = DecodeGraphQLResponseInto(response, &gitopsAccountResponse)
51+
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
gitopsActiveAccountInfo := gitopsAccountResponse.Data.Me.ActiveAccount
57+
58+
return &gitopsActiveAccountInfo, nil
59+
}
60+
61+
func (client *Client) UpdateActiveGitopsAccountSettings(gitProvider string, gitProviderApiUrl string, sharedConfigRepo string) error {
62+
request := GraphQLRequest{
63+
Query: `
64+
mutation updateCsdpSettings($gitProvider: GitProviders!, $gitApiUrl: String!, $sharedConfigRepo: String!) {
65+
updateCsdpSettings(gitProvider: $gitProvider, gitApiUrl: $gitApiUrl, sharedConfigRepo: $sharedConfigRepo)
66+
}
67+
`,
68+
Variables: map[string]interface{}{
69+
"gitProvider": gitProvider,
70+
"gitApiUrl": gitProviderApiUrl,
71+
"sharedConfigRepo": sharedConfigRepo,
72+
},
73+
}
74+
75+
_, err := client.SendGqlRequest(request)
76+
77+
if err != nil {
78+
fmt.Println("Error:", err)
79+
return err
80+
}
81+
82+
return nil
83+
}

codefresh/cfclient/pipeline.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ type RuntimeEnvironment struct {
9191
}
9292

9393
type ExternalResource struct {
94-
ID string `json:"id,omitempty"`
95-
Type string `json:"type"`
96-
Source string `json:"source"`
97-
Context string `json:"context"`
94+
ID string `json:"id,omitempty"`
95+
Type string `json:"type"`
96+
Source string `json:"source"`
97+
Context string `json:"context"`
9898
Destination string `json:"destination"`
99-
IsFolder bool `json:"isFolder"`
100-
Repo string `json:"repo"`
101-
Revision string `json:"revision"`
99+
IsFolder bool `json:"isFolder"`
100+
Repo string `json:"repo"`
101+
Revision string `json:"revision"`
102102
}
103103

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

140140
type Steps struct {

codefresh/cfclient/user.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type User struct {
4747
HasPassword bool `json:"hasPassword,omitempty"`
4848
Notifications []NotificationEvent `json:"notifications,omitempty"`
4949
ShortProfile ShortProfile `json:"shortProfile,omitempty"`
50-
PublicProfile PublicProfile `json:"publicProfile,omitempty"`
50+
PublicProfile PublicProfile `json:"publicProfile,omitempty"`
5151
Logins []Login `json:"logins,omitempty"`
5252
InviteURL string `json:"inviteUrl,omitempty"`
5353
}
@@ -389,7 +389,7 @@ func (client *Client) UpdateUserDetails(accountId, userId, userName, userEmail s
389389
return &respUser, nil
390390
}
391391

392-
func (client *Client) UpdateLocalUserPassword(userName, password string) (error) {
392+
func (client *Client) UpdateLocalUserPassword(userName, password string) error {
393393

394394
fullPath := "/admin/user/localProvider"
395395

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

413-
func (client *Client) DeleteLocalUserPassword(userName string) (error) {
413+
func (client *Client) DeleteLocalUserPassword(userName string) error {
414414

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

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package codefresh
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceAccountGitopsSettings() *schema.Resource {
11+
return &schema.Resource{
12+
Description: "This data source retrieves gitops settings for the active account",
13+
Read: dataSourceAccountGitopsSettingsRead,
14+
Schema: map[string]*schema.Schema{
15+
"id": {
16+
Type: schema.TypeString,
17+
Description: "Account Id",
18+
Computed: true,
19+
},
20+
"name": {
21+
Type: schema.TypeString,
22+
Computed: true,
23+
Description: "Account name for active account",
24+
},
25+
"git_provider": {
26+
Type: schema.TypeString,
27+
Computed: true,
28+
Description: "Git provider name",
29+
},
30+
"git_provider_api_url": {
31+
Type: schema.TypeString,
32+
Computed: true,
33+
Description: "Git provider API url",
34+
},
35+
"shared_config_repository": {
36+
Type: schema.TypeString,
37+
Computed: true,
38+
Description: "Shared config repository url",
39+
},
40+
"admins": {
41+
Type: schema.TypeList,
42+
Computed: true,
43+
Elem: &schema.Schema{
44+
Type: schema.TypeString,
45+
},
46+
},
47+
},
48+
}
49+
}
50+
51+
func dataSourceAccountGitopsSettingsRead(d *schema.ResourceData, meta interface{}) error {
52+
53+
client := meta.(*cfclient.Client)
54+
var accountGitopsInfo *cfclient.GitopsActiveAccountInfo
55+
56+
accountGitopsInfo, err := client.GetActiveGitopsAccountInfo()
57+
58+
if err != nil {
59+
return err
60+
}
61+
62+
return mapDataAccountGitopsSettingsToResource(accountGitopsInfo, d)
63+
}
64+
65+
func mapDataAccountGitopsSettingsToResource(account *cfclient.GitopsActiveAccountInfo, d *schema.ResourceData) error {
66+
67+
if account == nil || account.ID == "" {
68+
return fmt.Errorf("cannot get gitops settings as account wasn't properly retrived")
69+
}
70+
d.SetId(account.ID)
71+
d.Set("name", account.AccountName)
72+
d.Set("admins", account.Admins)
73+
d.Set("git_provider", account.GitProvider)
74+
d.Set("git_provider_api_url", account.GitApiUrl)
75+
d.Set("shared_config_repository", account.SharedConfigRepo)
76+
77+
return nil
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package gitops
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
const (
8+
// Git providers enum from https://github.com/codefresh-io/argo-platform/blob/90f86de326422ca3bd1f64ca5dd26aeedf985e3e/libs/ql/schema/entities/common/integration.graphql#L200
9+
GitProviderGitHub string = "GITHUB"
10+
GitProviderGerrit string = "GERRIT"
11+
GitProviderGitlab string = "GITLAB"
12+
GitProviderBitbucket string = "BITBUCKET"
13+
GitProviderBitbucketServer string = "BITBUCKET_SERVER"
14+
)
15+
16+
func GetSupportedGitProvidersList() []string {
17+
return []string{GitProviderGitHub, GitProviderGerrit, GitProviderGitlab, GitProviderBitbucket, GitProviderBitbucketServer}
18+
}
19+
20+
// Matching implementation for https://github.com/codefresh-io/argo-platform/blob/3c6af5b5cbb29aef58ef6617e71159e882987f5c/libs/git/src/helpers.ts#L37.
21+
// Must be updated accordingly
22+
func GetDefaultAPIUrlForProvider(gitProvider string) (*string, error) {
23+
24+
defaultApiUrlProvider := map[string]string{
25+
GitProviderGitHub: "https://api.github.com",
26+
GitProviderGitlab: "https://gitlab.com/api/v4",
27+
GitProviderBitbucket: "https://api.bitbucket.org/2.0",
28+
GitProviderGerrit: "https://gerrit-review.googlesource.com/a",
29+
}
30+
31+
if val, ok := defaultApiUrlProvider[gitProvider]; ok {
32+
return &val, nil
33+
}
34+
35+
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)
36+
}

codefresh/internal/gitops/doc.go

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Shared types, schemas and functions for gitops
2+
package gitops

codefresh/provider.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,19 @@ func Provider() *schema.Provider {
4141
},
4242
},
4343
DataSourcesMap: map[string]*schema.Resource{
44-
"codefresh_account": dataSourceAccount(),
45-
"codefresh_context": dataSourceContext(),
46-
"codefresh_current_account": dataSourceCurrentAccount(),
47-
"codefresh_idps": dataSourceIdps(),
48-
"codefresh_step_types": dataSourceStepTypes(),
49-
"codefresh_team": dataSourceTeam(),
50-
"codefresh_user": dataSourceUser(),
51-
"codefresh_users": dataSourceUsers(),
52-
"codefresh_registry": dataSourceRegistry(),
53-
"codefresh_pipelines": dataSourcePipelines(),
54-
"codefresh_account_idp": dataSourceAccountIdp(),
55-
"codefresh_project": dataSourceProject(),
44+
"codefresh_account": dataSourceAccount(),
45+
"codefresh_context": dataSourceContext(),
46+
"codefresh_current_account": dataSourceCurrentAccount(),
47+
"codefresh_idps": dataSourceIdps(),
48+
"codefresh_step_types": dataSourceStepTypes(),
49+
"codefresh_team": dataSourceTeam(),
50+
"codefresh_user": dataSourceUser(),
51+
"codefresh_users": dataSourceUsers(),
52+
"codefresh_registry": dataSourceRegistry(),
53+
"codefresh_pipelines": dataSourcePipelines(),
54+
"codefresh_account_idp": dataSourceAccountIdp(),
55+
"codefresh_project": dataSourceProject(),
56+
"codefresh_account_gitops_settings": dataSourceAccountGitopsSettings(),
5657
},
5758
ResourcesMap: map[string]*schema.Resource{
5859
"codefresh_account": resourceAccount(),
@@ -72,6 +73,7 @@ func Provider() *schema.Provider {
7273
"codefresh_abac_rules": resourceGitopsAbacRule(),
7374
"codefresh_idp": resourceIdp(),
7475
"codefresh_account_idp": resourceAccountIdp(),
76+
"codefresh_account_gitops_settings": resourceAccountGitopsSettings(),
7577
},
7678
ConfigureFunc: configureProvider,
7779
}

0 commit comments

Comments
 (0)