forked from launchdarkly/terraform-provider-launchdarkly
/
resource_launchdarkly_audit_log_subscription.go
139 lines (117 loc) · 4.67 KB
/
resource_launchdarkly_audit_log_subscription.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
package launchdarkly
import (
"context"
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
ldapi "github.com/launchdarkly/api-client-go/v7"
)
func resourceAuditLogSubscription() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAuditLogSubscriptionCreate,
UpdateContext: resourceAuditLogSubscriptionUpdate,
DeleteContext: resourceAuditLogSubscriptionDelete,
ReadContext: resourceAuditLogSubscriptionRead,
Exists: resourceAuditLogSubscriptionExists,
Importer: &schema.ResourceImporter{
State: resourceAuditLogSubscriptionImport,
},
Schema: auditLogSubscriptionSchema(false),
}
}
func resourceAuditLogSubscriptionCreate(ctx context.Context, d *schema.ResourceData, metaRaw interface{}) diag.Diagnostics {
client := metaRaw.(*Client)
integrationKey := d.Get(INTEGRATION_KEY).(string)
name := d.Get(NAME).(string)
on := d.Get(ON).(bool)
tags := stringsFromSchemaSet(d.Get(TAGS).(*schema.Set))
config, err := configFromResourceData(d)
if err != nil {
return diag.Errorf("failed to create %s integration with name %s: %v", integrationKey, name, err.Error())
}
statements, err := policyStatementsFromResourceData(d.Get(STATEMENTS).([]interface{}))
if err != nil {
return diag.Errorf("failed to create %s integration with name %s: %v", integrationKey, name, err.Error())
}
subscriptionBody := ldapi.SubscriptionPost{
Name: name,
On: &on,
Tags: &tags,
Config: config,
Statements: &statements,
}
sub, _, err := client.ld.IntegrationAuditLogSubscriptionsApi.CreateSubscription(client.ctx, integrationKey).SubscriptionPost(subscriptionBody).Execute()
if err != nil {
return diag.Errorf("failed to create %s integration with name %s: %v", integrationKey, name, handleLdapiErr(err))
}
d.SetId(*sub.Id)
return resourceAuditLogSubscriptionRead(ctx, d, metaRaw)
}
func resourceAuditLogSubscriptionUpdate(ctx context.Context, d *schema.ResourceData, metaRaw interface{}) diag.Diagnostics {
client := metaRaw.(*Client)
integrationKey := d.Get(INTEGRATION_KEY).(string)
name := d.Get(NAME).(string)
tags := stringsFromResourceData(d, TAGS)
on := d.Get(ON).(bool)
config, err := configFromResourceData(d)
if err != nil {
return diag.FromErr(err)
}
id := d.Id()
statements, err := policyStatementsFromResourceData(d.Get(STATEMENTS).([]interface{}))
if err != nil {
return diag.FromErr(err)
}
patch := []ldapi.PatchOperation{
patchReplace("/name", &name),
patchReplace("/tags", &tags),
patchReplace("/config", &config),
patchReplace("/on", &on),
patchReplace("/statements", &statements),
}
_, _, err = client.ld.IntegrationAuditLogSubscriptionsApi.UpdateSubscription(client.ctx, integrationKey, id).PatchOperation(patch).Execute()
if err != nil {
return diag.Errorf("failed to update %q integration with name %q and ID %q: %s", integrationKey, name, id, handleLdapiErr(err))
}
return resourceAuditLogSubscriptionRead(ctx, d, metaRaw)
}
func resourceAuditLogSubscriptionDelete(ctx context.Context, d *schema.ResourceData, metaRaw interface{}) diag.Diagnostics {
client := metaRaw.(*Client)
id := d.Id()
integrationKey := d.Get(INTEGRATION_KEY).(string)
_, err := client.ld.IntegrationAuditLogSubscriptionsApi.DeleteSubscription(client.ctx, integrationKey, id).Execute()
if err != nil {
return diag.Errorf("failed to delete integration with ID %q: %s", id, handleLdapiErr(err))
}
return diag.Diagnostics{}
}
func resourceAuditLogSubscriptionRead(ctx context.Context, d *schema.ResourceData, metaRaw interface{}) diag.Diagnostics {
return auditLogSubscriptionRead(ctx, d, metaRaw, false)
}
func resourceAuditLogSubscriptionExists(d *schema.ResourceData, metaRaw interface{}) (bool, error) {
client := metaRaw.(*Client)
id := d.Id()
integrationKey := d.Get(INTEGRATION_KEY).(string)
_, res, err := client.ld.IntegrationAuditLogSubscriptionsApi.GetSubscriptionByID(client.ctx, integrationKey, id).Execute()
if isStatusNotFound(res) {
log.Println("got 404 when getting integration. returning false.")
return false, nil
}
if err != nil {
return false, fmt.Errorf("failed to get integration with ID %q: %v", id, handleLdapiErr(err))
}
return true, nil
}
func resourceAuditLogSubscriptionImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
id := d.Id()
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return nil, fmt.Errorf("found unexpected id format for import: %q. expected format: 'integrationKey/integration_id'", id)
}
integrationKey, integrationID := parts[0], parts[1]
_ = d.Set(INTEGRATION_KEY, integrationKey)
d.SetId(integrationID)
return []*schema.ResourceData{d}, nil
}