generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
resource_environment.go
140 lines (116 loc) · 4.38 KB
/
resource_environment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package environment
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/antihax/optional"
"github.com/harness/harness-go-sdk/harness/nextgen"
"github.com/harness/terraform-provider-harness/helpers"
"github.com/harness/terraform-provider-harness/internal"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func ResourceEnvironment() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for creating a Harness environment.",
ReadContext: resourceEnvironmentRead,
UpdateContext: resourceEnvironmentCreateOrUpdate,
DeleteContext: resourceEnvironmentDelete,
CreateContext: resourceEnvironmentCreateOrUpdate,
Importer: helpers.ProjectResourceImporter,
Schema: map[string]*schema.Schema{
"color": {
Description: "Color of the environment.",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"type": {
Description: fmt.Sprintf("The type of environment. Valid values are %s", strings.Join(nextgen.EnvironmentTypeValues, ", ")),
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(nextgen.EnvironmentTypeValues, false),
},
"yaml": {
Description: "Environment YAML",
Type: schema.TypeString,
Optional: true,
},
},
}
helpers.SetProjectLevelResourceSchema(resource.Schema)
return resource
}
func resourceEnvironmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
resp, httpResp, err := c.EnvironmentsApi.GetEnvironmentV2(ctx, d.Id(), c.AccountId, &nextgen.EnvironmentsApiGetEnvironmentV2Opts{
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
ProjectIdentifier: optional.NewString(d.Get("project_id").(string)),
})
if err != nil {
return helpers.HandleReadApiError(err, d, httpResp)
}
readEnvironment(d, resp.Data.Environment)
return nil
}
func resourceEnvironmentCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
var err error
var resp nextgen.ResponseDtoEnvironmentResponse
var httpResp *http.Response
id := d.Id()
env := buildEnvironment(d)
if id == "" {
resp, httpResp, err = c.EnvironmentsApi.UpsertEnvironmentV2(ctx, c.AccountId, &nextgen.EnvironmentsApiUpsertEnvironmentV2Opts{
Body: optional.NewInterface(env),
})
} else {
resp, httpResp, err = c.EnvironmentsApi.UpdateEnvironmentV2(ctx, c.AccountId, &nextgen.EnvironmentsApiUpdateEnvironmentV2Opts{
Body: optional.NewInterface(env),
})
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readEnvironment(d, resp.Data.Environment)
return nil
}
func resourceEnvironmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
_, httpResp, err := c.EnvironmentsApi.DeleteEnvironmentV2(ctx, d.Id(), c.AccountId, &nextgen.EnvironmentsApiDeleteEnvironmentV2Opts{
OrgIdentifier: optional.NewString(d.Get("org_id").(string)),
ProjectIdentifier: optional.NewString(d.Get("project_id").(string)),
})
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
return nil
}
func buildEnvironment(d *schema.ResourceData) *nextgen.EnvironmentRequest {
return &nextgen.EnvironmentRequest{
Identifier: d.Get("identifier").(string),
OrgIdentifier: d.Get("org_id").(string),
ProjectIdentifier: d.Get("project_id").(string),
Name: d.Get("name").(string),
Color: d.Get("color").(string),
Description: d.Get("description").(string),
Tags: helpers.ExpandTags(d.Get("tags").(*schema.Set).List()),
Type_: d.Get("type").(string),
Yaml: d.Get("yaml").(string),
}
}
func readEnvironment(d *schema.ResourceData, env *nextgen.EnvironmentResponseDetails) {
d.SetId(env.Identifier)
d.Set("identifier", env.Identifier)
d.Set("org_id", env.OrgIdentifier)
d.Set("name", env.Name)
d.Set("color", env.Color)
d.Set("description", env.Description)
d.Set("tags", helpers.FlattenTags(env.Tags))
d.Set("type", env.Type_.String())
if d.Get("yaml").(string) != "" {
d.Set("yaml", env.Yaml)
}
}