Skip to content

Commit 2b2d508

Browse files
feat: Add gitops environments resource (#172)
## What ## Why ## Notes <!-- Add any notes here --> ## Checklist * [ ] _I have read [CONTRIBUTING.md](https://github.com/codefresh-io/terraform-provider-codefresh/blob/master/CONTRIBUTING.md)._ * [ ] _I have [allowed changes to my fork to be made](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork)._ * [ ] _I have added tests, assuming new tests are warranted_. * [ ] _I understand that the `/test` comment will be ignored by the CI trigger [unless it is made by a repo admin or collaborator](https://codefresh.io/docs/docs/pipelines/triggers/git-triggers/#support-for-building-pull-requests-from-forks)._
1 parent f217344 commit 2b2d508

File tree

7 files changed

+652
-0
lines changed

7 files changed

+652
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
package cfclient
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
const (
8+
environmentQueryFields = `
9+
id
10+
name
11+
kind
12+
clusters {
13+
runtimeName
14+
name
15+
server
16+
namespaces
17+
}
18+
labelPairs
19+
`
20+
)
21+
22+
// GitopsEnvironment represents a GitOps environment configuration.
23+
type GitopsEnvironment struct {
24+
ID string `json:"id,omitempty"`
25+
Name string `json:"name"`
26+
Kind string `json:"kind"`
27+
Clusters []GitopsEnvironmentCluster `json:"clusters"`
28+
LabelPairs []string `json:"labelPairs"`
29+
}
30+
31+
// GitopsCluster represents a cluster within a GitOps environment.
32+
type GitopsEnvironmentCluster struct {
33+
Name string `json:"name"`
34+
RuntimeName string `json:"runtimeName"`
35+
Namespaces []string `json:"namespaces"`
36+
}
37+
38+
type GitopsEnvironmentResponse struct {
39+
Errors []GraphQLError `json:"errors,omitempty"`
40+
Data struct {
41+
Environment GitopsEnvironment `json:"environment,omitempty"`
42+
CreateEnvironment GitopsEnvironment `json:"createEnvironment,omitempty"`
43+
UpdateEnvironment GitopsEnvironment `json:"updateEnvironment,omitempty"`
44+
DeleteEnvironment GitopsEnvironment `json:"deleteEnvironment,omitempty"`
45+
} `json:"data"`
46+
}
47+
48+
type GitopsEnvironmentsResponse struct {
49+
Data struct {
50+
Environments []GitopsEnvironment `json:"environments,omitempty"`
51+
} `json:"data"`
52+
}
53+
54+
// At the time of writing Codefresh Graphql API does not support fetching environment by ID, So all environemnts are fetched and filtered within this client
55+
func (client *Client) GetGitopsEnvironments() (*[]GitopsEnvironment, error) {
56+
57+
request := GraphQLRequest{
58+
Query: fmt.Sprintf(`
59+
query ($filters: EnvironmentFilterArgs) {
60+
environments(filters: $filters) {
61+
%s
62+
}
63+
}
64+
`, environmentQueryFields),
65+
Variables: map[string]interface{}{},
66+
}
67+
68+
response, err := client.SendGqlRequest(request)
69+
70+
if err != nil {
71+
return nil, err
72+
}
73+
74+
var gitopsEnvironmentsResponse GitopsEnvironmentsResponse
75+
err = DecodeGraphQLResponseInto(response, &gitopsEnvironmentsResponse)
76+
77+
if err != nil {
78+
return nil, err
79+
}
80+
81+
return &gitopsEnvironmentsResponse.Data.Environments, nil
82+
}
83+
84+
func (client *Client) GetGitopsEnvironmentById(id string) (*GitopsEnvironment, error) {
85+
environments, err := client.GetGitopsEnvironments()
86+
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
for _, env := range *environments {
92+
if env.ID == id {
93+
return &env, nil
94+
}
95+
}
96+
97+
return nil, nil
98+
}
99+
100+
func (client *Client) CreateGitopsEnvironment(environment *GitopsEnvironment) (*GitopsEnvironment, error) {
101+
request := GraphQLRequest{
102+
Query: fmt.Sprintf(`
103+
mutation ($environment: CreateEnvironmentArgs!) {
104+
createEnvironment(environment: $environment) {
105+
%s
106+
}
107+
}
108+
`, environmentQueryFields),
109+
Variables: map[string]interface{}{
110+
"environment": environment,
111+
},
112+
}
113+
114+
response, err := client.SendGqlRequest(request)
115+
116+
if err != nil {
117+
return nil, err
118+
}
119+
120+
var gitopsEnvironmentResponse GitopsEnvironmentResponse
121+
err = DecodeGraphQLResponseInto(response, &gitopsEnvironmentResponse)
122+
123+
if err != nil {
124+
return nil, err
125+
}
126+
127+
if len(gitopsEnvironmentResponse.Errors) > 0 {
128+
return nil, fmt.Errorf("CreateGitopsEnvironment - %s", gitopsEnvironmentResponse.Errors)
129+
}
130+
131+
return &gitopsEnvironmentResponse.Data.CreateEnvironment, nil
132+
}
133+
134+
func (client *Client) DeleteGitopsEnvironment(id string) (*GitopsEnvironment, error) {
135+
136+
type deleteEnvironmentArgs struct {
137+
ID string `json:"id"`
138+
}
139+
140+
request := GraphQLRequest{
141+
Query: fmt.Sprintf(`
142+
mutation ($environment: DeleteEnvironmentArgs!) {
143+
deleteEnvironment(environment: $environment) {
144+
%s
145+
}
146+
}
147+
`, environmentQueryFields),
148+
149+
Variables: map[string]interface{}{
150+
"environment": deleteEnvironmentArgs{
151+
ID: id,
152+
},
153+
},
154+
}
155+
156+
response, err := client.SendGqlRequest(request)
157+
158+
if err != nil {
159+
return nil, err
160+
}
161+
var gitopsEnvironmentResponse GitopsEnvironmentResponse
162+
err = DecodeGraphQLResponseInto(response, &gitopsEnvironmentResponse)
163+
164+
if err != nil {
165+
return nil, err
166+
}
167+
168+
if len(gitopsEnvironmentResponse.Errors) > 0 {
169+
return nil, fmt.Errorf("DeleteGitopsEnvironment - %s", gitopsEnvironmentResponse.Errors)
170+
}
171+
172+
return &gitopsEnvironmentResponse.Data.DeleteEnvironment, nil
173+
}
174+
175+
func (client *Client) UpdateGitopsEnvironment(environment *GitopsEnvironment) (*GitopsEnvironment, error) {
176+
request := GraphQLRequest{
177+
Query: fmt.Sprintf(`
178+
mutation ($environment: UpdateEnvironmentArgs!) {
179+
updateEnvironment(environment: $environment) {
180+
%s
181+
}
182+
}
183+
`, environmentQueryFields),
184+
Variables: map[string]interface{}{
185+
"environment": environment,
186+
},
187+
}
188+
189+
response, err := client.SendGqlRequest(request)
190+
191+
if err != nil {
192+
return nil, err
193+
}
194+
var gitopsEnvironmentResponse GitopsEnvironmentResponse
195+
err = DecodeGraphQLResponseInto(response, &gitopsEnvironmentResponse)
196+
197+
if err != nil {
198+
return nil, err
199+
}
200+
201+
if len(gitopsEnvironmentResponse.Errors) > 0 {
202+
return nil, fmt.Errorf("UpdateGitopsEnvironment - %s", gitopsEnvironmentResponse.Errors)
203+
}
204+
205+
return &gitopsEnvironmentResponse.Data.UpdateEnvironment, nil
206+
}

codefresh/cfclient/gql_client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ type GraphQLRequest struct {
1414
Variables map[string]interface{} `json:"variables,omitempty"`
1515
}
1616

17+
type GraphQLError struct {
18+
Message string `json:"message,omitempty"`
19+
Extensions string `json:"extensions,omitempty"`
20+
}
21+
1722
func (client *Client) SendGqlRequest(request GraphQLRequest) ([]byte, error) {
1823
jsonRequest, err := json.Marshal(request)
1924
if err != nil {

codefresh/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func Provider() *schema.Provider {
7676
"codefresh_account_idp": resourceAccountIdp(),
7777
"codefresh_account_gitops_settings": resourceAccountGitopsSettings(),
7878
"codefresh_service_account": resourceServiceAccount(),
79+
"codefresh_gitops_environment": resourceGitopsEnvironment(),
7980
},
8081
ConfigureFunc: configureProvider,
8182
}

0 commit comments

Comments
 (0)