Skip to content

Commit 5117fb6

Browse files
working and documented resource
1 parent 1255288 commit 5117fb6

File tree

5 files changed

+270
-33
lines changed

5 files changed

+270
-33
lines changed

codefresh/cfclient/gitops_environments.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ const (
2121

2222
// GitopsEnvironment represents a GitOps environment configuration.
2323
type GitopsEnvironment struct {
24-
ID string `json:"id,omitempty"`
25-
Name string `json:"name"`
26-
Kind string `json:"kind"`
24+
ID string `json:"id,omitempty"`
25+
Name string `json:"name"`
26+
Kind string `json:"kind"`
2727
Clusters []GitopsEnvironmentCluster `json:"clusters"`
28-
LabelPairs []string `json:"labelPairs"`
28+
LabelPairs []string `json:"labelPairs"`
2929
}
3030

3131
// GitopsCluster represents a cluster within a GitOps environment.

codefresh/resource_gitops_environment.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ func resourceGitopsEnvironment() *schema.Resource {
2525
Computed: true,
2626
},
2727
"name": {
28-
Type: schema.TypeString,
29-
Required: true,
28+
Type: schema.TypeString,
29+
Required: true,
3030
Description: "The name of the environment. Must be unique per account",
3131
},
3232
"kind": {
33-
Type: schema.TypeString,
34-
Required: true,
35-
Description: "The type of environment. Possible values: NON_PROD, PROD",
33+
Type: schema.TypeString,
34+
Required: true,
35+
Description: "The type of environment. Possible values: NON_PROD, PROD",
3636
ValidateFunc: validation.StringInSlice([]string{"NON_PROD", "PROD"}, false),
3737
},
3838
"cluster": {
@@ -41,65 +41,67 @@ func resourceGitopsEnvironment() *schema.Resource {
4141
Elem: &schema.Resource{
4242
Schema: map[string]*schema.Schema{
4343
"name": {
44-
Type: schema.TypeString,
45-
Required: true,
44+
Type: schema.TypeString,
45+
Required: true,
4646
Description: "Target cluster name",
4747
},
4848
"server": {
49-
Type: schema.TypeString,
50-
Optional: true,
49+
Type: schema.TypeString,
50+
Optional: true,
5151
Description: "Target cluster server url. Defaults to https://kubernetes.default.svc which is the default in-cluster url",
52-
Default: "https://kubernetes.default.svc",
52+
Default: "https://kubernetes.default.svc",
5353
},
5454
"runtime_name": {
55-
Type: schema.TypeString,
56-
Required: true,
55+
Type: schema.TypeString,
56+
Required: true,
5757
Description: "Runtime name where the target cluster is registered",
5858
},
5959
"namespaces": {
60-
Type: schema.TypeList,
61-
Elem: &schema.Schema{Type: schema.TypeString},
62-
Required: true,
60+
Type: schema.TypeList,
61+
Elem: &schema.Schema{Type: schema.TypeString},
62+
Required: true,
6363
Description: "List of namespaces in the target cluster",
6464
},
6565
},
6666
},
6767
},
6868
"label_pairs": {
69-
Type: schema.TypeList,
70-
Elem: &schema.Schema{Type: schema.TypeString},
71-
Optional: true,
69+
Type: schema.TypeList,
70+
Elem: &schema.Schema{Type: schema.TypeString},
71+
Optional: true,
7272
Description: "List of labels and values in the format label=value that can be used to assign applications to the environment. Example: ['codefresh.io/environment=prod']",
7373
},
7474
},
7575
}
7676
}
7777

78-
//func resourceGitopsEnvironmentCreate(d *schema.ResourceData, m interface{}) error {
78+
// func resourceGitopsEnvironmentCreate(d *schema.ResourceData, m interface{}) error {
7979
func resourceGitopsEnvironmentCreate(d *schema.ResourceData, meta interface{}) error {
8080
client := meta.(*cfclient.Client)
8181

8282
environment := mapResourceToGitopsEnvironment(d)
8383
newEnvironment, err := client.CreateGitopsEnvironment(environment)
84+
8485
if err != nil {
8586
return err
8687
}
8788

8889
d.SetId(newEnvironment.ID)
8990

90-
return mapGitopsEnvironmentToResource(d, newEnvironment)
91+
return resourceGitopsEnvironmentRead(d, meta)
9192
}
9293

9394
func resourceGitopsEnvironmentUpdate(d *schema.ResourceData, meta interface{}) error {
9495
client := meta.(*cfclient.Client)
9596

9697
environment := mapResourceToGitopsEnvironment(d)
97-
updatedEnvironment, err := client.UpdateGitopsEnvironment(environment)
98+
_, err := client.UpdateGitopsEnvironment(environment)
99+
98100
if err != nil {
99101
return err
100102
}
101103

102-
return mapGitopsEnvironmentToResource(d, updatedEnvironment)
104+
return resourceGitopsEnvironmentRead(d, meta)
103105
}
104106

105107
func resourceGitopsEnvironmentDelete(d *schema.ResourceData, meta interface{}) error {
@@ -147,7 +149,6 @@ func resourceGitopsEnvironmentRead(d *schema.ResourceData, meta interface{}) err
147149

148150
func mapResourceToGitopsEnvironment(d *schema.ResourceData) *cfclient.GitopsEnvironment {
149151

150-
151152
clusters := expandClusters(d.Get("cluster").([]interface{}))
152153

153154
labelPairs := []string{}
@@ -159,8 +160,8 @@ func mapResourceToGitopsEnvironment(d *schema.ResourceData) *cfclient.GitopsEnvi
159160
return &cfclient.GitopsEnvironment{
160161
ID: d.Get("id").(string),
161162
Name: d.Get("name").(string),
162-
Kind: d.Get("kind").(string),
163-
Clusters: clusters,
163+
Kind: d.Get("kind").(string),
164+
Clusters: clusters,
164165
LabelPairs: labelPairs,
165166
}
166167
}
@@ -190,7 +191,7 @@ func mapGitopsEnvironmentToResource(d *schema.ResourceData, environment *cfclien
190191

191192
func flattenClusters(clusters []cfclient.GitopsEnvironmentCluster) []map[string]interface{} {
192193

193-
var res = make([]map[string]interface{}, len(clusters))
194+
var res = make([]map[string]interface{}, 0)
194195

195196
for _, cluster := range clusters {
196197
m := make(map[string]interface{})
@@ -210,10 +211,10 @@ func expandClusters(list []interface{}) []cfclient.GitopsEnvironmentCluster {
210211
for _, item := range list {
211212
clusterMap := item.(map[string]interface{})
212213
cluster := cfclient.GitopsEnvironmentCluster{
213-
Name: clusterMap["name"].(string),
214-
Server: clusterMap["server"].(string),
214+
Name: clusterMap["name"].(string),
215+
Server: clusterMap["server"].(string),
215216
RuntimeName: clusterMap["runtime_name"].(string),
216-
Namespaces: datautil.ConvertStringArr(clusterMap["namespaces"].([]interface{})),
217+
Namespaces: datautil.ConvertStringArr(clusterMap["namespaces"].([]interface{})),
217218
}
218219
clusters = append(clusters, cluster)
219220
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package codefresh
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
11+
)
12+
13+
func TestAccCodefreshGitopsEnvironmentsResource(t *testing.T) {
14+
resourceName := "codefresh_gitops_environment.test"
15+
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
Providers: testAccProviders,
19+
CheckDestroy: testAccCheckCodefreshGitopsEnvironmentDestroy,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testAccCodefreshGitopsEnvironmentConfig(
23+
"test-for-tf",
24+
"NON_PROD",
25+
[]cfclient.GitopsEnvironmentCluster{{
26+
Name: "in-cluster2",
27+
Server: "https://kubernetes.default.svc",
28+
RuntimeName: "test-runtime",
29+
Namespaces: []string{"test-ns-1", "test-ns2"},
30+
}},
31+
[]string{"codefresh.io/environment=test-for-tf", "codefresh.io/environment-1=test-for-tf1"},
32+
),
33+
Check: resource.ComposeTestCheckFunc(
34+
testAccCheckCodefreshGitopsEnvironmentExists(resourceName),
35+
resource.TestCheckResourceAttr(resourceName, "name", "test-for-tf"),
36+
resource.TestCheckResourceAttr(resourceName, "kind", "NON_PROD"),
37+
resource.TestCheckResourceAttr(resourceName, "cluster.0.name", "in-cluster2"),
38+
resource.TestCheckResourceAttr(resourceName, "cluster.0.server", "https://kubernetes.default.svc"),
39+
resource.TestCheckResourceAttr(resourceName, "cluster.0.runtime_name", "test-runtime"),
40+
resource.TestCheckResourceAttr(resourceName, "cluster.0.namespaces.0", "test-ns-1"),
41+
resource.TestCheckResourceAttr(resourceName, "cluster.0.namespaces.1", "test-ns2"),
42+
resource.TestCheckResourceAttr(resourceName, "label_pairs.0", "codefresh.io/environment=test-for-tf"),
43+
resource.TestCheckResourceAttr(resourceName, "label_pairs.1", "codefresh.io/environment-1=test-for-tf1"),
44+
),
45+
},
46+
{
47+
Config: testAccCodefreshGitopsEnvironmentConfig(
48+
"test-for-tf",
49+
"NON_PROD",
50+
[]cfclient.GitopsEnvironmentCluster{
51+
{
52+
Name: "in-cluster2",
53+
Server: "https://kubernetes.default.svc",
54+
RuntimeName: "test-runtime",
55+
Namespaces: []string{"test-ns-1", "test-ns2"},
56+
},
57+
{
58+
Name: "in-cluster3",
59+
Server: "https://kubernetes2.default.svc",
60+
RuntimeName: "test-runtime-2",
61+
Namespaces: []string{"test-ns-3"},
62+
},
63+
},
64+
[]string{"codefresh.io/environment=test-for-tf", "codefresh.io/environment-1=test-for-tf1"},
65+
),
66+
Check: resource.ComposeTestCheckFunc(
67+
testAccCheckCodefreshGitopsEnvironmentExists(resourceName),
68+
resource.TestCheckResourceAttr(resourceName, "name", "test-for-tf"),
69+
resource.TestCheckResourceAttr(resourceName, "kind", "NON_PROD"),
70+
resource.TestCheckResourceAttr(resourceName, "cluster.0.name", "in-cluster2"),
71+
resource.TestCheckResourceAttr(resourceName, "cluster.1.name", "in-cluster3"),
72+
resource.TestCheckResourceAttr(resourceName, "cluster.1.server", "https://kubernetes2.default.svc"),
73+
resource.TestCheckResourceAttr(resourceName, "cluster.1.runtime_name", "test-runtime-2"),
74+
resource.TestCheckResourceAttr(resourceName, "cluster.1.namespaces.0", "test-ns-3"),
75+
),
76+
},
77+
{
78+
ResourceName: resourceName,
79+
ImportState: true,
80+
ImportStateVerify: true,
81+
},
82+
},
83+
})
84+
}
85+
86+
func testAccCheckCodefreshGitopsEnvironmentExists(resource string) resource.TestCheckFunc {
87+
return func(state *terraform.State) error {
88+
rs, ok := state.RootModule().Resources[resource]
89+
if !ok {
90+
return fmt.Errorf("Not found: %s", resource)
91+
}
92+
if rs.Primary.ID == "" {
93+
return fmt.Errorf("No Record ID is set")
94+
}
95+
96+
envID := rs.Primary.ID
97+
apiClient := testAccProvider.Meta().(*cfclient.Client)
98+
_, err := apiClient.GetGitopsEnvironmentById(envID)
99+
if err != nil {
100+
return fmt.Errorf("error fetching gitops environment with ID %s. %s", envID, err)
101+
}
102+
return nil
103+
}
104+
}
105+
106+
func testAccCheckCodefreshGitopsEnvironmentDestroy(state *terraform.State) error {
107+
// Implement destroy check if needed
108+
return nil
109+
}
110+
111+
// CONFIG
112+
func testAccCodefreshGitopsEnvironmentConfig(name, kind string, clusters []cfclient.GitopsEnvironmentCluster, labelPairs []string) string {
113+
var clusterBlocks []string
114+
for _, c := range clusters {
115+
ns := fmt.Sprintf("[\"%s\"]", strings.Join(c.Namespaces, "\", \""))
116+
block := fmt.Sprintf(` cluster {
117+
name = "%s"
118+
server = "%s"
119+
runtime_name = "%s"
120+
namespaces = %s
121+
}`, c.Name, c.Server, c.RuntimeName, ns)
122+
clusterBlocks = append(clusterBlocks, block)
123+
}
124+
labelsStr := fmt.Sprintf("[\"%s\"]", strings.Join(labelPairs, "\", \""))
125+
return fmt.Sprintf(`
126+
resource "codefresh_gitops_environment" "test" {
127+
name = "%s"
128+
kind = "%s"
129+
%s
130+
label_pairs = %s
131+
}
132+
`, name, kind, strings.Join(clusterBlocks, "\n"), labelsStr)
133+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
page_title: "codefresh_gitops_environment Resource - terraform-provider-codefresh"
3+
subcategory: ""
4+
description: |-
5+
Codefresh GitOps environment resource. See official documentation https://codefresh.io/docs/gitops/environments/environments-overview/.
6+
---
7+
8+
# codefresh_gitops_environment (Resource)
9+
10+
Codefresh GitOps environment resource. See [official documentation](https://codefresh.io/docs/gitops/environments/environments-overview/).
11+
12+
## Example Usage
13+
14+
```hcl
15+
resource "codefresh_gitops_environment" "example" {
16+
name = "test-gitops-env"
17+
kind = "NON_PROD"
18+
19+
cluster {
20+
name = "test-cluster"
21+
server = "https://kubernetes.default.svc"
22+
runtime_name = "test-runtime"
23+
namespaces = ["test-ns-1", "test-ns-2"]
24+
}
25+
26+
label_pairs = [
27+
"codefresh.io/environment=test-gitops-env",
28+
"codefresh.io/environment-1=test-gitops-env1"
29+
]
30+
}
31+
```
32+
33+
<!-- schema generated by tfplugindocs -->
34+
## Schema
35+
36+
### Required
37+
38+
- `cluster` (Block List, Min: 1) (see [below for nested schema](#nestedblock--cluster))
39+
- `kind` (String) The type of environment. Possible values: NON_PROD, PROD
40+
- `name` (String) The name of the environment. Must be unique per account
41+
42+
### Optional
43+
44+
- `id` (String) Environment ID
45+
- `label_pairs` (List of String) List of labels and values in the format label=value that can be used to assign applications to the environment. Example: ['codefresh.io/environment=prod']
46+
47+
<a id="nestedblock--cluster"></a>
48+
### Nested Schema for `cluster`
49+
50+
Required:
51+
52+
- `name` (String) Target cluster name
53+
- `namespaces` (List of String) List of namespaces in the target cluster
54+
- `runtime_name` (String) Runtime name where the target cluster is registered
55+
56+
Optional:
57+
58+
- `server` (String) Target cluster server url. Defaults to https://kubernetes.default.svc which is the default in-cluster url
59+
60+
## Import
61+
62+
```sh
63+
terraform import codefresh_gitops_environment.example <environment_id>
64+
```

0 commit comments

Comments
 (0)