|
| 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 | +} |
0 commit comments