generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 41
/
resource_feature_flag_target.go
235 lines (198 loc) · 6.7 KB
/
resource_feature_flag_target.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package feature_flag_target
import (
"context"
"io"
"net/http"
"time"
"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"
)
func ResourceFeatureFlagTarget() *schema.Resource {
resource := &schema.Resource{
Description: "Resource for managing Feature Flag Targets.",
ReadContext: resourceFeatureFlagTargetRead,
DeleteContext: resourceFeatureFlagTargetDelete,
CreateContext: resourceFeatureFlagTargetCreateOrUpdate,
UpdateContext: resourceFeatureFlagTargetUpdate,
Importer: helpers.ProjectResourceImporter,
Schema: map[string]*schema.Schema{
"identifier": {
Description: "Identifier of the Feature Flag Target",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"org_id": {
Description: "Organization Identifier",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"project_id": {
Description: "Project Identifier",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"environment": {
Description: "Environment Identifier",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"account_id": {
Description: "Account Identifier",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Description: "Target Name",
Type: schema.TypeString,
Required: true,
},
"attributes": {
Description: "Attributes",
Type: schema.TypeMap,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
return resource
}
// FFTargetQueryParameters is the query parameters for the feature flag target
type FFTargetQueryParameters struct {
Identifier string
OrganizationID string
ProjectID string
AccountID string
Environment string
}
// FFTargetOpts is the options for the feature flag target
type FFTargetOpts struct {
Name string
Atributes map[string]interface{}
}
// resourceFeatureFlagTargetRead is the read function for the feature flag target
func resourceFeatureFlagTargetRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
id := d.Id()
if id == "" {
d.MarkNewResource()
return nil
}
qp := buildFFTargetQueryParameters(d)
resp, httpResp, err := c.TargetsApi.GetTarget(ctx, id, c.AccountId, qp.OrganizationID, qp.ProjectID, qp.Environment)
if err != nil {
return helpers.HandleReadApiError(err, d, httpResp)
}
readFeatureFlagTarget(d, &resp, *qp)
return nil
}
// resourceFeatureFlagTargetDelete is the delete function for the feature flag target
func resourceFeatureFlagTargetDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
id := d.Id()
if id == "" {
return nil
}
qp := buildFFTargetQueryParameters(d)
httpResp, err := c.TargetsApi.DeleteTarget(ctx, id, c.AccountId, qp.OrganizationID, qp.ProjectID, qp.Environment)
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
return nil
}
// resourceFeatureFlagTargetCreateOrUpdate is the create function for the feature flag target
func resourceFeatureFlagTargetCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
var err error
var httpResp *http.Response
target := buildFFTargetCreate(d)
qp := buildFFTargetQueryParameters(d)
id := d.Id()
if id == "" {
httpResp, err = c.TargetsApi.CreateTarget(ctx, target, c.AccountId, qp.OrganizationID)
} else {
target, httpResp, err = c.TargetsApi.ModifyTarget(ctx, target, c.AccountId, qp.OrganizationID, qp.ProjectID, qp.Environment, id)
}
if err != nil {
return helpers.HandleApiError(err, d, httpResp)
}
readFeatureFlagTarget(d, &target, *qp)
return nil
}
// resourceFeatureFlagTargetUpdate is the update function for the feature flag target
func resourceFeatureFlagTargetUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c, ctx := meta.(*internal.Session).GetPlatformClientWithContext(ctx)
id := d.Id()
if id == "" {
id = d.Get("identifier").(string)
d.MarkNewResource()
}
qp := buildFFTargetQueryParameters(d)
opts := buildFFTargetPatchOpts(d)
var err error
var target nextgen.Target
var httpResp *http.Response
target, httpResp, err = c.TargetsApi.PatchTarget(ctx, c.AccountId, qp.OrganizationID, qp.ProjectID, qp.Environment, id, opts)
if err != nil {
body, _ := io.ReadAll(httpResp.Body)
return diag.Errorf("readstatus: %s, \nBody:%s", httpResp.Status, body)
}
readFeatureFlagTarget(d, &target, *qp)
return nil
}
// readFeatureFlagTarget is the read function for the feature flag target
func readFeatureFlagTarget(d *schema.ResourceData, flag *nextgen.Target, qp FFTargetQueryParameters) {
d.SetId(qp.Identifier)
d.Set("identifier", qp.Identifier)
d.Set("org_id", qp.OrganizationID)
d.Set("project_id", qp.ProjectID)
d.Set("environment", qp.Environment)
d.Set("account_id", qp.AccountID)
d.Set("name", flag.Name)
d.Set("attributes", flag.Attributes)
}
// buildFFTargetQueryParameters is the query parameters for the feature flag target
func buildFFTargetQueryParameters(d *schema.ResourceData) *FFTargetQueryParameters {
return &FFTargetQueryParameters{
Identifier: d.Get("identifier").(string),
OrganizationID: d.Get("org_id").(string),
ProjectID: d.Get("project_id").(string),
AccountID: d.Get("account_id").(string),
Environment: d.Get("environment").(string),
}
}
// buildFFTargetCreateOpts
func buildFFTargetCreate(d *schema.ResourceData) nextgen.Target {
attribute := d.Get("attributes")
return nextgen.Target{
Account: d.Get("account_id").(string),
Attributes: &attribute,
Environment: d.Get("environment").(string),
Identifier: d.Get("identifier").(string),
Org: d.Get("org_id").(string),
Name: d.Get("name").(string),
Project: d.Get("project_id").(string),
CreatedAt: time.Now().Unix(),
}
}
// buildFFTargetPatchOpts is the options for the feature flag target
func buildFFTargetPatchOpts(d *schema.ResourceData) *nextgen.TargetsApiPatchTargetOpts {
opts := &FFTargetOpts{
Name: d.Get("name").(string),
Atributes: d.Get("attributes").(map[string]interface{}),
}
return &nextgen.TargetsApiPatchTargetOpts{
Body: optional.NewInterface(opts),
}
}