forked from launchdarkly/terraform-provider-launchdarkly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhooks_helper.go
69 lines (62 loc) · 1.89 KB
/
webhooks_helper.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
package launchdarkly
import (
"context"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func baseWebhookSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
SECRET: {
Type: schema.TypeString,
Optional: true,
Description: "If sign is true, and the secret attribute is omitted, LaunchDarkly will automatically generate a secret for you",
Sensitive: true,
},
NAME: {
Type: schema.TypeString,
Optional: true,
Description: "A human-readable name for your webhook",
},
STATEMENTS: policyStatementsSchema(policyStatementSchemaOptions{}),
TAGS: tagsSchema(),
}
}
func webhookRead(ctx context.Context, d *schema.ResourceData, meta interface{}, isDataSource bool) diag.Diagnostics {
var diags diag.Diagnostics
client := meta.(*Client)
var webhookID string
if isDataSource {
webhookID = d.Get(ID).(string)
} else {
webhookID = d.Id()
}
webhook, res, err := client.ld.WebhooksApi.GetWebhook(client.ctx, webhookID).Execute()
if isStatusNotFound(res) && !isDataSource {
log.Printf("[WARN] failed to find webhook with id %q, removing from state", webhookID)
d.SetId("")
return nil
}
if err != nil {
return diag.Errorf("failed to get webhook with id %q: %s", webhookID, handleLdapiErr(err))
}
if webhook.Statements != nil {
statements := policyStatementsToResourceData(*webhook.Statements)
err = d.Set(STATEMENTS, statements)
if err != nil {
return diag.Errorf("failed to set statements on webhook with id %q: %v", webhookID, err)
}
}
if isDataSource {
d.SetId(webhook.Id)
}
_ = d.Set(URL, webhook.Url)
_ = d.Set(SECRET, webhook.Secret)
_ = d.Set(ON, webhook.On)
_ = d.Set(NAME, webhook.Name)
err = d.Set(TAGS, webhook.Tags)
if err != nil {
return diag.Errorf("failed to set tags on webhook with id %q: %v", webhookID, err)
}
return diags
}