Skip to content

Commit

Permalink
Updates the terraform provider to enable setting webhook name on crea…
Browse files Browse the repository at this point in the history
…te/update.

Closes FEA-233.
  • Loading branch information
ryanrhanson committed Dec 3, 2024
1 parent 0bb4976 commit 123dd18
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/resources/webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Manage a Doppler Webhook.
resource "doppler_webhook" "ci" {
project = doppler_project.test_proj.name
url = "https://localhost/webhook"
name = "My Webhook"
secret = "my signing secret-2"
enabled = true
enabled_configs = [doppler_config.ci_github.name]
Expand All @@ -41,6 +42,7 @@ resource "doppler_webhook" "ci" {
- `authentication` (Block List, Max: 1) Authentication method used by the webhook (see [below for nested schema](#nestedblock--authentication))
- `enabled` (Boolean) Whether the webhook is enabled or disabled. Default to true.
- `enabled_configs` (Set of String) Configs this webhook will trigger for
- `name` (String) Name of the webhook
- `payload` (String, Sensitive) The webhook's payload as a JSON string. Leave empty to use the default webhook payload
- `secret` (String, Sensitive) Secret used for request signing

Expand Down
7 changes: 6 additions & 1 deletion doppler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ type CreateWebhookOptionalParameters struct {
Auth *WebhookAuth
WebhookPayload string
EnabledConfigs []string
Name string
}

func (client APIClient) CreateWebhook(ctx context.Context, project string, url string, enabled bool, options *CreateWebhookOptionalParameters) (*Webhook, error) {
Expand All @@ -741,6 +742,9 @@ func (client APIClient) CreateWebhook(ctx context.Context, project string, url s
if options.EnabledConfigs != nil {
payload["enableConfigs"] = options.EnabledConfigs
}
if options.Name != "" {
payload["name"] = options.Name
}
}

body, err := json.Marshal(payload)
Expand Down Expand Up @@ -792,7 +796,7 @@ func (client APIClient) DisableWebhook(ctx context.Context, project string, slug
return &result.Webhook, nil
}

func (client APIClient) UpdateWebhook(ctx context.Context, project string, slug string, webhookUrl string, secret string, webhookPayload string, enabledConfigs []string, disabledConfigs []string, auth WebhookAuth) (*Webhook, error) {
func (client APIClient) UpdateWebhook(ctx context.Context, project string, slug string, webhookUrl string, secret string, webhookPayload string, webhookName string, enabledConfigs []string, disabledConfigs []string, auth WebhookAuth) (*Webhook, error) {
params := []QueryParam{
{Key: "project", Value: project},
}
Expand All @@ -801,6 +805,7 @@ func (client APIClient) UpdateWebhook(ctx context.Context, project string, slug
payload["url"] = webhookUrl
payload["secret"] = secret
payload["payload"] = webhookPayload
payload["name"] = webhookName
payload["enableConfigs"] = enabledConfigs
payload["disableConfigs"] = disabledConfigs
payload["authentication"] = auth
Expand Down
11 changes: 9 additions & 2 deletions doppler/resource_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func resourceWebhook() *schema.Resource {
Optional: true,
Sensitive: true,
},
"name": {
Description: "Name of the webhook",
Type: schema.TypeString,
Optional: true,
},
"authentication": {
Description: "Authentication method used by the webhook",
Type: schema.TypeList,
Expand Down Expand Up @@ -98,14 +103,15 @@ func resourceWebhookCreate(ctx context.Context, d *schema.ResourceData, m interf
enabled := d.Get("enabled").(bool)
secret := d.Get("secret").(string)
payload := d.Get("payload").(string)
name := d.Get("name").(string)

rawEnabledConfigs := d.Get("enabled_configs").(*schema.Set).List()
enabledConfigs := make([]string, len(rawEnabledConfigs))
for i, v := range rawEnabledConfigs {
enabledConfigs[i] = v.(string)
}

options := CreateWebhookOptionalParameters{Secret: secret, WebhookPayload: payload, EnabledConfigs: enabledConfigs}
options := CreateWebhookOptionalParameters{Secret: secret, WebhookPayload: payload, EnabledConfigs: enabledConfigs, Name: name}

authConfigList := d.Get("authentication").([]interface{})

Expand Down Expand Up @@ -141,6 +147,7 @@ func resourceWebhookUpdate(ctx context.Context, d *schema.ResourceData, m interf
project := d.Get("project").(string)
secret := d.Get("secret").(string)
payload := d.Get("payload").(string)
name := d.Get("name").(string)

if d.HasChange("enabled") {
if d.Get("enabled").(bool) {
Expand Down Expand Up @@ -204,7 +211,7 @@ func resourceWebhookUpdate(ctx context.Context, d *schema.ResourceData, m interf
auth = WebhookAuth{Type: "None"}
}

webhook, err := client.UpdateWebhook(ctx, project, slug, url, secret, payload, enabledConfigs, disabledConfigs, auth)
webhook, err := client.UpdateWebhook(ctx, project, slug, url, secret, payload, name, enabledConfigs, disabledConfigs, auth)

if err != nil {
return diag.FromErr(err)
Expand Down
1 change: 1 addition & 0 deletions examples/resources/webhook.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
resource "doppler_webhook" "ci" {
project = doppler_project.test_proj.name
url = "https://localhost/webhook"
name = "My Webhook"
secret = "my signing secret-2"
enabled = true
enabled_configs = [doppler_config.ci_github.name]
Expand Down

0 comments on commit 123dd18

Please sign in to comment.