Skip to content

Commit

Permalink
Merged automatically by CI pipeline
Browse files Browse the repository at this point in the history
SCALRCORE-24801 Integrations > Webhook update provider resource
  • Loading branch information
emocharnik committed Apr 21, 2023
2 parents 7aadd4d + eea0bab commit 3564250
Show file tree
Hide file tree
Showing 15 changed files with 740 additions and 99 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,35 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `data.scalr_endpoint`: optional `id` and `name` arguments can be used together ([#228](https://github.com/Scalr/terraform-provider-scalr/pull/228))
- `data.scalr_service_account`: optional `id` and `email` arguments can be used together ([#228](https://github.com/Scalr/terraform-provider-scalr/pull/228))
- `scalr_workspace_run_schedule`: make `apply-schedule` and `destroy-schedule` attributes nullable ([#231](https://github.com/Scalr/terraform-provider-scalr/pull/231))
- `scalr_webhook`: ([#234](https://github.com/Scalr/terraform-provider-scalr/pull/234))
- endpoint arguments are now included in the webhook resource: `url`, `secret_key`, `timeout` and `max_attempts`
This manifests the new way webhook integration will work further on, deprecating the `endpoint_id` argument
and merging the endpoint information into the webhook. During the deprecation period both old-style and new-style
webhooks are supported. The support for old-style webhooks will be dropped in the next major release.
- added new optional `header` argument (new-style webhooks only) - additional headers to set in the webhook request
- added new optional `environments` argument (new-style webhooks only) - environments that the webhook is shared to
- `data.scalr_webhook`: extended with new attributes from new-style webhook - `url`, `secret_key`, `timeout`,
`max_attempts`, `header`, `environments` ([#234](https://github.com/Scalr/terraform-provider-scalr/pull/234))

### Fixed

- `scalr_account_allowed_ips`: accept /32 suffix ([#224](https://github.com/Scalr/terraform-provider-scalr/pull/224))
- `scalr_vcs_provider`: fix handling resource destroy when resource no longer exists ([#235](https://github.com/Scalr/terraform-provider-scalr/pull/235))
- `scalr_webhook`: fix handling resource destroy when resource no longer exists ([#235](https://github.com/Scalr/terraform-provider-scalr/pull/235))

### Deprecated

- `scalr_endpoint` is deprecated and will be removed in the next major version ([#234](https://github.com/Scalr/terraform-provider-scalr/pull/234))
- `data.scalr_endpoint` is deprecated and will be removed in the next major version ([#234](https://github.com/Scalr/terraform-provider-scalr/pull/234))
- `scalr_webhook`: ([#234](https://github.com/Scalr/terraform-provider-scalr/pull/234))
- attribute `endpoint_id` is deprecated
- attribute `environment_id` is deprecated
- attribute `workspace_id` is deprecated
- `data.scalr_webhook`: ([#234](https://github.com/Scalr/terraform-provider-scalr/pull/234))
- attribute `endpoint_id` is deprecated
- attribute `environment_id` is deprecated
- attribute `workspace_id` is deprecated

## [1.0.4] - 2023-03-13

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions docs/data-sources/scalr_endpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

Retrieves the details of a webhook endpoint.

> **WARNING:** This datasource is deprecated and will be removed in the next major version.
## Example Usage

```hcl
Expand Down
15 changes: 13 additions & 2 deletions docs/data-sources/scalr_webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,18 @@ Arguments `id` and `name` are both optional, specify at least one of them to obt
All arguments plus:

* `enabled` - Boolean indicates if the webhook is enabled.
* `endpoint_id` - ID of the endpoint, in the format `ep-<RANDOM STRING>`.
* `environment_id` - ID of the environment, in the format `env-<RANDOM STRING>`.
* `endpoint_id` - (Deprecated) ID of the endpoint, in the format `ep-<RANDOM STRING>`.
* `environment_id` - (Deprecated) ID of the environment, in the format `env-<RANDOM STRING>`.
* `workspace_id` - (Deprecated) ID of the workspace, in the format `ws-<RANDOM STRING>`.
* `events` - List of event IDs.
* `last_triggered_at` - Date/time when webhook was last triggered.
* `url` - Endpoint URL.
* `secret_key` - Secret key to sign the webhook payload.
* `max_attempts` - Max delivery attempts of the payload.
* `timeout` - Endpoint timeout (in seconds).
* `environments` - The list of environment identifiers that the webhook is shared to,
or `["*"]` if shared with all environments.
* `header` - (Set of header objects) Additional headers to set in the webhook request.
The `header` block item contains:
* `name` - The name of the header.
* `value` - The value of the header.
2 changes: 2 additions & 0 deletions docs/resources/scalr_endpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

Manage the state of endpoints in Scalr. Create, update and destroy

> **WARNING:** This resource is deprecated and will be removed in the next major version.
## Example Usage

Basic usage:
Expand Down
17 changes: 14 additions & 3 deletions docs/resources/scalr_webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ resource "scalr_webhook" "example" {
## Argument Reference

* `name` - (Required) Name of the webhook.
* `account_id` - (Optional) ID of the account, in the format `acc-<RANDOM STRING>`.
* `enabled` - (Optional) Set (true/false) to enable/disable the webhook.
* `endpoint_id` - (Required) ID of the endpoint, in the format `ep-<RANDOM STRING>`.
* `workspace_id` - (Optional) ID of the workspace, in the format `ws-<RANDOM STRING>`.
* `environment_id` - (Required if workspace ID is empty) ID of the environment, in the format `env-<RANDOM STRING>`.
* `endpoint_id` - (Deprecated) ID of the endpoint, in the format `ep-<RANDOM STRING>`.
* `workspace_id` - (Deprecated) ID of the workspace, in the format `ws-<RANDOM STRING>`.
* `environment_id` - (Deprecated) ID of the environment, in the format `env-<RANDOM STRING>`.
* `events` - (Required) List of event IDs.
* `url` - (Optional) Endpoint URL. Required if `endpoint_id` is not set.
* `secret_key` - (Optional) Secret key to sign the webhook payload.
* `max_attempts` - (Optional) Max delivery attempts of the payload.
* `timeout` - (Optional) Endpoint timeout (in seconds).
* `environments` - (Optional) The list of environment identifiers that the webhook is shared to.
Use `["*"]` to share with all environments.
* `header` - (Optional, set of header objects) Additional headers to set in the webhook request.
The `header` block item contains:
* `name` - The name of the header.
* `value` - The value of the header.

## Attributes

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require (
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.1
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734
github.com/scalr/go-scalr v0.0.0-20230420172716-77c82c62f17a
github.com/scalr/go-scalr v0.0.0-20230421073315-7935065e34fd
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/scalr/go-scalr v0.0.0-20230420172716-77c82c62f17a h1:wMX9BnoWxMZWAruqk3anbV7IHG3WBwwmyqx+SSAV9Zs=
github.com/scalr/go-scalr v0.0.0-20230420172716-77c82c62f17a/go.mod h1:p34SHb25YRvbgft7SUjSDYESeoQhWzAlxGXId/BbaSE=
github.com/scalr/go-scalr v0.0.0-20230421073315-7935065e34fd h1:Uu9My2WErwm4sbCK54igTiDfRwuMBKIj2me5e3Pzovw=
github.com/scalr/go-scalr v0.0.0-20230421073315-7935065e34fd/go.mod h1:p34SHb25YRvbgft7SUjSDYESeoQhWzAlxGXId/BbaSE=
github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
Expand Down
3 changes: 3 additions & 0 deletions scalr/data_source_scalr_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (

func dataSourceScalrEndpoint() *schema.Resource {
return &schema.Resource{
DeprecationMessage: "Datasource `scalr_endpoint` is deprecated, the endpoint information" +
" is included in the `scalr_webhook` resource.",

ReadContext: dataSourceScalrEndpointRead,

Schema: map[string]*schema.Schema{
Expand Down
122 changes: 103 additions & 19 deletions scalr/data_source_scalr_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func dataSourceScalrWebhook() *schema.Resource {
"endpoint_id": {
Type: schema.TypeString,
Computed: true,
Deprecated: "Attribute `endpoint_id` is deprecated, the endpoint information" +
" is included in the `scalr_webhook` resource.",
},

"account_id": {
Expand All @@ -62,13 +64,59 @@ func dataSourceScalrWebhook() *schema.Resource {
"environment_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Deprecated: "The attribute `environment_id` is deprecated. The webhook is created on the" +
" account level and the environments to which it is exposed" +
" are controlled by the `environments` attribute.",
},

"workspace_id": {
Type: schema.TypeString,
Computed: true,
Deprecated: "The attribute `workspace_id` is deprecated.",
},

"url": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},

"secret_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},

"timeout": {
Type: schema.TypeInt,
Computed: true,
},

"max_attempts": {
Type: schema.TypeInt,
Computed: true,
},

"header": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"environments": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
Expand All @@ -82,55 +130,91 @@ func dataSourceScalrWebhookRead(ctx context.Context, d *schema.ResourceData, met
webhookName := d.Get("name").(string)
accountID := d.Get("account_id").(string)

var webhook *scalr.Webhook
var newWebhook *scalr.WebhookIntegration
var err error

log.Printf("[DEBUG] Read configuration of webhook with ID '%s' and name '%s'", webhookID, webhookName)
// First read from new API by ID or search by name, as the new API
// works both with old-style and new-style webhooks
if webhookID != "" {
webhook, err = scalrClient.Webhooks.Read(ctx, webhookID)
newWebhook, err = scalrClient.WebhookIntegrations.Read(ctx, webhookID)
if err != nil {
return diag.Errorf("Error retrieving webhook: %v", err)
}
if webhookName != "" && webhookName != webhook.Name {
if webhookName != "" && webhookName != newWebhook.Name {
return diag.Errorf("Could not find webhook with ID '%s' and name '%s'", webhookID, webhookName)
}
} else {
options := GetWebhookByNameOptions{
Name: &webhookName,
Account: &accountID,
}
webhook, err = GetWebhookByName(ctx, options, scalrClient)
newWebhook, err = GetWebhookByName(ctx, options, scalrClient)
if err != nil {
return diag.Errorf("Error retrieving webhook: %v", err)
}
if webhookID != "" && webhookID != webhook.ID {
if webhookID != "" && webhookID != newWebhook.ID {
return diag.Errorf("Could not find webhook with ID '%s' and name '%s'", webhookID, webhookName)
}
}
// Having the webhook found, read from old API then
// to populate deprecated fields available only in old API
oldWebhook, err := scalrClient.Webhooks.Read(ctx, newWebhook.ID)
if err != nil {
return diag.Errorf("Error retrieving webhook: %v", err)
}

// Update the config.
_ = d.Set("name", webhook.Name)
_ = d.Set("enabled", webhook.Enabled)
_ = d.Set("last_triggered_at", webhook.LastTriggeredAt)
_ = d.Set("name", newWebhook.Name)
_ = d.Set("account_id", newWebhook.Account.ID)
_ = d.Set("enabled", newWebhook.Enabled)
_ = d.Set("last_triggered_at", newWebhook.LastTriggeredAt)
_ = d.Set("url", newWebhook.Url)
_ = d.Set("secret_key", newWebhook.SecretKey)
_ = d.Set("timeout", newWebhook.Timeout)
_ = d.Set("max_attempts", newWebhook.MaxAttempts)

events := make([]string, 0)
if webhook.Events != nil {
for _, event := range webhook.Events {
if newWebhook.Events != nil {
for _, event := range newWebhook.Events {
events = append(events, event.ID)
}
}
_ = d.Set("events", events)

if webhook.Workspace != nil {
_ = d.Set("workspace_id", webhook.Workspace.ID)
headers := make([]map[string]interface{}, 0)
if newWebhook.Headers != nil {
for _, header := range newWebhook.Headers {
headers = append(headers, map[string]interface{}{
"name": header.Name,
"value": header.Value,
})
}
}
if webhook.Environment != nil {
_ = d.Set("environment_id", webhook.Environment.ID)
_ = d.Set("header", headers)

if newWebhook.IsShared {
_ = d.Set("environments", []string{"*"})
} else {
environmentIDs := make([]string, 0)
for _, environment := range newWebhook.Environments {
environmentIDs = append(environmentIDs, environment.ID)
}
_ = d.Set("environments", environmentIDs)
}

// Add deprecated attributes from old-style webhook
if oldWebhook.Workspace != nil {
_ = d.Set("workspace_id", oldWebhook.Workspace.ID)
}
if oldWebhook.Environment != nil {
_ = d.Set("environment_id", oldWebhook.Environment.ID)
}
if webhook.Endpoint != nil {
_ = d.Set("endpoint_id", webhook.Endpoint.ID)
if oldWebhook.Endpoint != nil {
_ = d.Set("endpoint_id", oldWebhook.Endpoint.ID)
}
d.SetId(webhook.ID)

d.SetId(newWebhook.ID)

return nil
}
Loading

0 comments on commit 3564250

Please sign in to comment.