Skip to content

Commit 1255288

Browse files
initial - only create works
1 parent 92fb1c3 commit 1255288

File tree

3 files changed

+416
-0
lines changed

3 files changed

+416
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
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+
Server string `json:"server"`
35+
RuntimeName string `json:"runtimeName"`
36+
Namespaces []string `json:"namespaces"`
37+
}
38+
39+
type GitopsEnvironmentResponse struct {
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+
return &gitopsEnvironmentResponse.Data.CreateEnvironment, nil
128+
}
129+
130+
func (client *Client) DeleteGitopsEnvironment(id string) (*GitopsEnvironment, error) {
131+
132+
type deleteEnvironmentArgs struct {
133+
ID string `json:"id"`
134+
}
135+
136+
request := GraphQLRequest{
137+
Query: fmt.Sprintf(`
138+
mutation ($environment: DeleteEnvironmentArgs!) {
139+
deleteEnvironment(environment: $environment) {
140+
%s
141+
}
142+
}
143+
`, environmentQueryFields),
144+
145+
Variables: map[string]interface{}{
146+
"environment": deleteEnvironmentArgs{
147+
ID: id,
148+
},
149+
},
150+
}
151+
152+
response, err := client.SendGqlRequest(request)
153+
154+
if err != nil {
155+
return nil, err
156+
}
157+
var gitopsEnvironmentResponse GitopsEnvironmentResponse
158+
err = DecodeGraphQLResponseInto(response, &gitopsEnvironmentResponse)
159+
160+
if err != nil {
161+
return nil, err
162+
}
163+
164+
return &gitopsEnvironmentResponse.Data.DeleteEnvironment, nil
165+
}
166+
167+
func (client *Client) UpdateGitopsEnvironment(environment *GitopsEnvironment) (*GitopsEnvironment, error) {
168+
request := GraphQLRequest{
169+
Query: fmt.Sprintf(`
170+
mutation ($environment: UpdateEnvironmentArgs!) {
171+
updateEnvironment(environment: $environment) {
172+
%s
173+
}
174+
}
175+
`, environmentQueryFields),
176+
Variables: map[string]interface{}{
177+
"environment": environment,
178+
},
179+
}
180+
181+
response, err := client.SendGqlRequest(request)
182+
183+
if err != nil {
184+
return nil, err
185+
}
186+
var gitopsEnvironmentResponse GitopsEnvironmentResponse
187+
err = DecodeGraphQLResponseInto(response, &gitopsEnvironmentResponse)
188+
189+
if err != nil {
190+
return nil, err
191+
}
192+
193+
return &gitopsEnvironmentResponse.Data.UpdateEnvironment, nil
194+
}

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)