-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathresource_third_party_integration.go
306 lines (258 loc) · 8.47 KB
/
resource_third_party_integration.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package thirdpartyintegration
import (
"context"
"fmt"
"regexp"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
)
var integrationTypes = []string{
"PAGER_DUTY",
"DATADOG",
"OPS_GENIE",
"VICTOR_OPS",
"WEBHOOK",
"MICROSOFT_TEAMS",
"PROMETHEUS",
}
var requiredPerType = map[string][]string{
"PAGER_DUTY": {"service_key"},
"DATADOG": {"api_key", "region"},
"OPS_GENIE": {"api_key", "region"},
"VICTOR_OPS": {"api_key"},
"WEBHOOK": {"url"},
"MICROSOFT_TEAMS": {"microsoft_teams_webhook_url"},
"PROMETHEUS": {"user_name", "password", "service_discovery", "enabled"},
}
func Resource() *schema.Resource {
return &schema.Resource{
CreateContext: resourceMongoDBAtlasThirdPartyIntegrationCreate,
ReadContext: resourceMongoDBAtlasThirdPartyIntegrationRead,
UpdateContext: resourceMongoDBAtlasThirdPartyIntegrationUpdate,
DeleteContext: resourceMongoDBAtlasThirdPartyIntegrationDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceMongoDBAtlasThirdPartyIntegrationImportState,
},
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"project_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: validateIntegrationType(),
},
"api_key": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Sensitive: true,
},
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"service_key": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Sensitive: true,
},
"team_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"channel_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"routing_key": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Sensitive: true,
},
"url": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"secret": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
},
"microsoft_teams_webhook_url": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Sensitive: true,
},
"user_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Sensitive: true,
},
"password": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Sensitive: true,
},
"service_discovery": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Sensitive: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
Optional: true,
},
"send_collection_latency_metrics": {
Type: schema.TypeBool,
Computed: true,
Optional: true,
},
"send_database_metrics": {
Type: schema.TypeBool,
Computed: true,
Optional: true,
},
},
}
}
func resourceMongoDBAtlasThirdPartyIntegrationCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
connV2 := meta.(*config.MongoDBClient).AtlasV2
projectID := d.Get("project_id").(string)
integrationType := d.Get("type").(string)
// checking per type fields
if requiredSet, ok := requiredPerType[integrationType]; ok {
for _, key := range requiredSet {
_, valid := d.GetOk(key)
if !valid {
return diag.FromErr(fmt.Errorf("error attribute for third party integration %s. please set it", key))
}
}
}
requestBody := schemaToIntegration(d)
_, _, err := connV2.ThirdPartyIntegrationsApi.CreateThirdPartyIntegration(ctx, integrationType, projectID, requestBody).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("error creating third party integration %s", err))
}
return resourceMongoDBAtlasThirdPartyIntegrationRead(ctx, d, meta)
}
func resourceMongoDBAtlasThirdPartyIntegrationRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
connV2 := meta.(*config.MongoDBClient).AtlasV2
projectID := d.Get("project_id").(string)
integrationType := d.Get("type").(string)
integration, resp, err := connV2.ThirdPartyIntegrationsApi.GetThirdPartyIntegration(ctx, projectID, integrationType).Execute()
if err != nil {
if validate.StatusNotFound(resp) {
d.SetId("")
return nil
}
return diag.FromErr(fmt.Errorf("error getting third party integration resource info %s %w", integrationType, err))
}
integrationMap := integrationToSchema(d, integration)
for key, val := range integrationMap {
if err := d.Set(key, val); err != nil {
return diag.FromErr(fmt.Errorf("error setting `%s` for third party integration (%s): %s", key, d.Id(), err))
}
}
d.SetId(integration.GetId())
return nil
}
func resourceMongoDBAtlasThirdPartyIntegrationUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
connV2 := meta.(*config.MongoDBClient).AtlasV2
projectID := d.Get("project_id").(string)
integrationType := d.Get("type").(string)
integration, _, err := connV2.ThirdPartyIntegrationsApi.GetThirdPartyIntegration(ctx, projectID, integrationType).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("error getting third party integration resource info %s %w", integrationType, err))
}
// check for changed attributes per type
updateIntegrationFromSchema(d, integration)
_, _, err = connV2.ThirdPartyIntegrationsApi.UpdateThirdPartyIntegration(ctx, integrationType, projectID, integration).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("error updating third party integration type `%s` (%s): %w", integrationType, d.Id(), err))
}
return resourceMongoDBAtlasThirdPartyIntegrationRead(ctx, d, meta)
}
func resourceMongoDBAtlasThirdPartyIntegrationDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).Atlas
projectID := d.Get("project_id").(string)
integrationType := d.Get("type").(string)
_, err := conn.Integrations.Delete(ctx, projectID, integrationType)
if err != nil {
return diag.FromErr(fmt.Errorf("error deleting third party integration type `%s` (%s): %w", integrationType, d.Id(), err))
}
return nil
}
func resourceMongoDBAtlasThirdPartyIntegrationImportState(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
connV2 := meta.(*config.MongoDBClient).AtlasV2
projectID, integrationType, err := splitIntegrationTypeID(d.Id())
if err != nil {
return nil, err
}
_, _, err = connV2.ThirdPartyIntegrationsApi.GetThirdPartyIntegration(ctx, projectID, integrationType).Execute()
if err != nil {
return nil, fmt.Errorf("couldn't import third party integration (%s) in project(%s), error: %w", integrationType, projectID, err)
}
if err := d.Set("project_id", projectID); err != nil {
return nil, fmt.Errorf("error setting `project_id` for third party integration (%s): %w", d.Id(), err)
}
if err := d.Set("type", integrationType); err != nil {
return nil, fmt.Errorf("error setting `type` for third party integration (%s): %w", d.Id(), err)
}
return []*schema.ResourceData{d}, nil
}
// format {project_id}-{integration_type}
func splitIntegrationTypeID(id string) (projectID, integrationType string, err error) {
var re = regexp.MustCompile(`(?s)^([0-9a-fA-F]{24})-(.*)$`)
parts := re.FindStringSubmatch(id)
if len(parts) != 3 {
err = fmt.Errorf("import format error: to import a third party integration, use the format {project_id}-{integration_type} %s, %+v", id, parts)
return
}
projectID, integrationType = parts[1], parts[2]
return
}
func validateIntegrationType() schema.SchemaValidateDiagFunc {
return func(v any, p cty.Path) diag.Diagnostics {
value := v.(string)
var diags diag.Diagnostics
if !isElementExist(integrationTypes, value) {
diagError := diag.Diagnostic{
Severity: diag.Error,
Summary: "Invalid Third Party Integration type",
Detail: fmt.Sprintf("Third Party integration type %q is not a valid value. Possible values are: %q.", value, integrationTypes),
}
diags = append(diags, diagError)
}
return diags
}
}
func isElementExist(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}