forked from launchdarkly/terraform-provider-launchdarkly
/
resource_launchdarkly_destination.go
229 lines (196 loc) · 8.02 KB
/
resource_launchdarkly_destination.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
package launchdarkly
import (
"context"
"fmt"
"log"
"strings"
"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"
ldapi "github.com/launchdarkly/api-client-go/v7"
)
func resourceDestination() *schema.Resource {
return &schema.Resource{
CreateContext: resourceDestinationCreate,
ReadContext: resourceDestinationRead,
UpdateContext: resourceDestinationUpdate,
DeleteContext: resourceDestinationDelete,
Exists: resourceDestinationExists,
Importer: &schema.ResourceImporter{
State: resourceDestinationImport,
},
Schema: map[string]*schema.Schema{
PROJECT_KEY: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The LaunchDarkly project key",
ValidateDiagFunc: validateKey(),
},
ENV_KEY: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The LaunchDarkly environment key",
},
NAME: {
Type: schema.TypeString,
Description: "A human-readable name for your data export destination",
Required: true,
},
// kind can only be one of five types (kinesis, google-pubsub, mparticle, azure-event-hubs, or segment)
KIND: {
Type: schema.TypeString,
Required: true,
Description: "The data export destination type. Available choices are 'kinesis', 'google-pubsub', 'segment', 'azure-event-hubs', and 'mparticle'",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"kinesis", "google-pubsub", "mparticle", "azure-event-hubs", "segment"}, false)),
ForceNew: true,
},
CONFIG: {
Type: schema.TypeMap,
Required: true,
Description: "The destination-specific configuration object corresponding to your data export kind - see documentation for required fields for each kind",
Elem: &schema.Schema{Type: schema.TypeString},
},
ON: {
Type: schema.TypeBool,
Description: "Whether the data export destination is on or not",
Optional: true,
},
TAGS: tagsSchema(),
},
}
}
func resourceDestinationCreate(ctx context.Context, d *schema.ResourceData, metaRaw interface{}) diag.Diagnostics {
client := metaRaw.(*Client)
destinationProjKey := d.Get(PROJECT_KEY).(string)
destinationEnvKey := d.Get(ENV_KEY).(string)
destinationName := d.Get(NAME).(string)
destinationKind := d.Get(KIND).(string)
destinationOn := d.Get(ON).(bool)
destinationConfig, err := destinationConfigFromResourceData(d)
if err != nil {
return diag.FromErr(err)
}
destinationBody := ldapi.DestinationPost{
Name: &destinationName,
Kind: &destinationKind,
Config: &destinationConfig,
On: &destinationOn,
}
destination, _, err := client.ld.DataExportDestinationsApi.PostDestination(client.ctx, destinationProjKey, destinationEnvKey).DestinationPost(destinationBody).Execute()
if err != nil {
d.SetId("")
return diag.Errorf("failed to create destination with project key %q and env key %q: %s", destinationProjKey, destinationEnvKey, handleLdapiErr(err))
}
// destination defined in api-client-go/model_destination.go
d.SetId(strings.Join([]string{destinationProjKey, destinationEnvKey, *destination.Id}, "/"))
return resourceDestinationRead(ctx, d, metaRaw)
}
func resourceDestinationRead(ctx context.Context, d *schema.ResourceData, metaRaw interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := metaRaw.(*Client)
_, _, destinationID, err := destinationImportIDtoKeys(d.Id())
if err != nil {
return diag.FromErr(err)
}
destinationProjKey := d.Get(PROJECT_KEY).(string)
destinationEnvKey := d.Get(ENV_KEY).(string)
destination, res, err := client.ld.DataExportDestinationsApi.GetDestination(client.ctx, destinationProjKey, destinationEnvKey, destinationID).Execute()
if isStatusNotFound(res) {
log.Printf("[WARN] failed to find destination with id: %q in project %q, environment: %q, removing from state", destinationID, destinationProjKey, destinationEnvKey)
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("[WARN] failed to find destination with id: %q in project %q, environment: %q, removing from state", destinationID, destinationProjKey, destinationEnvKey),
})
d.SetId("")
return diags
}
if err != nil {
return diag.Errorf("failed to get destination with id %q: %s", destinationID, handleLdapiErr(err))
}
cfg := destinationConfigToResourceData(*destination.Kind, destination.Config)
preservedCfg := preserveObfuscatedConfigAttributes(d.Get(CONFIG).(map[string]interface{}), cfg)
_ = d.Set(NAME, destination.Name)
_ = d.Set(KIND, destination.Kind)
_ = d.Set(CONFIG, preservedCfg)
_ = d.Set(ON, destination.On)
d.SetId(strings.Join([]string{destinationProjKey, destinationEnvKey, *destination.Id}, "/"))
return diags
}
func resourceDestinationUpdate(ctx context.Context, d *schema.ResourceData, metaRaw interface{}) diag.Diagnostics {
client := metaRaw.(*Client)
_, _, destinationID, err := destinationImportIDtoKeys(d.Id())
if err != nil {
return diag.FromErr(err)
}
destinationProjKey := d.Get(PROJECT_KEY).(string)
destinationEnvKey := d.Get(ENV_KEY).(string)
destinationName := d.Get(NAME).(string)
destinationKind := d.Get(KIND).(string)
destinationConfig, err := destinationConfigFromResourceData(d)
if err != nil {
return diag.FromErr(err)
}
destinationOn := d.Get(ON).(bool)
patch := []ldapi.PatchOperation{
patchReplace("/name", &destinationName),
patchReplace("/kind", &destinationKind),
patchReplace("/on", &destinationOn),
patchReplace("/config", &destinationConfig),
}
_, _, err = client.ld.DataExportDestinationsApi.PatchDestination(client.ctx, destinationProjKey, destinationEnvKey, destinationID).PatchOperation(patch).Execute()
if err != nil {
return diag.Errorf("failed to update destination with id %q: %s", destinationID, handleLdapiErr(err))
}
return resourceDestinationRead(ctx, d, metaRaw)
}
func resourceDestinationDelete(ctx context.Context, d *schema.ResourceData, metaRaw interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := metaRaw.(*Client)
_, _, destinationID, err := destinationImportIDtoKeys(d.Id())
if err != nil {
return diag.FromErr(err)
}
destinationProjKey := d.Get(PROJECT_KEY).(string)
destinationEnvKey := d.Get(ENV_KEY).(string)
_, err = client.ld.DataExportDestinationsApi.DeleteDestination(client.ctx, destinationProjKey, destinationEnvKey, destinationID).Execute()
if err != nil {
return diag.Errorf("failed to delete destination with id %q: %s", destinationID, handleLdapiErr(err))
}
return diags
}
func resourceDestinationExists(d *schema.ResourceData, metaRaw interface{}) (bool, error) {
client := metaRaw.(*Client)
_, _, destinationID, err := destinationImportIDtoKeys(d.Id())
if err != nil {
return false, err
}
destinationProjKey := d.Get(PROJECT_KEY).(string)
destinationEnvKey := d.Get(ENV_KEY).(string)
_, res, err := client.ld.DataExportDestinationsApi.GetDestination(client.ctx, destinationProjKey, destinationEnvKey, destinationID).Execute()
if isStatusNotFound(res) {
return false, nil
}
if err != nil {
return false, fmt.Errorf("failed to get destination with id %q: %s", destinationID, handleLdapiErr(err))
}
return true, nil
}
func resourceDestinationImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
projKey, envKey, _, err := destinationImportIDtoKeys(d.Id())
if err != nil {
return nil, err
}
_ = d.Set(PROJECT_KEY, projKey)
_ = d.Set(ENV_KEY, envKey)
return []*schema.ResourceData{d}, nil
}
func destinationImportIDtoKeys(importID string) (projKey, envKey, destinationID string, err error) {
if strings.Count(importID, "/") != 2 {
return "", "", "", fmt.Errorf("found unexpected destination import id format: %q expected format: 'project_key/env_key/destination_id'", importID)
}
parts := strings.SplitN(importID, "/", 3)
projKey, envKey, destinationID = parts[0], parts[1], parts[2]
return projKey, envKey, destinationID, nil
}