Skip to content

Commit

Permalink
Reintroduce old MS Teams webhook as readonly (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
lootag authored Aug 26, 2024
1 parent 88dc870 commit a2fea89
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 17 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -667,3 +667,10 @@ Fix:
New Features:

* endpoints can now specified in an abbreviated fashion: `[AP1, AP2, EU1, EU2, US1, US2]`.

## Release 1.18.5

Fix:

### resource/coralogix_webhook
* Replaced depracated MS Teams webhook with MS Teams Workflow Webhook.
105 changes: 101 additions & 4 deletions coralogix/resource_coralogix_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ type WebhookResourceModel struct {
PagerDuty *PagerDutyModel `tfsdk:"pager_duty"`
SendLog *SendLogModel `tfsdk:"sendlog"`
EmailGroup *EmailGroupModel `tfsdk:"email_group"`
MsTeamsWorkflow *MsTeamsWorkflowModel `tfsdk:"microsoft_teams"`
MsTeamsWorkflow *MsTeamsWorkflowModel `tfsdk:"microsoft_teams_workflow"`
MsTeams *MsTeamsWorkflowModel `tfsdk:"microsoft_teams"`
Jira *JiraModel `tfsdk:"jira"`
Opsgenie *OpsgenieModel `tfsdk:"opsgenie"`
Demisto *DemistoModel `tfsdk:"demisto"`
Expand Down Expand Up @@ -416,6 +417,7 @@ func (r *WebhookResource) Schema(_ context.Context, _ resource.SchemaRequest, re
path.MatchRelative().AtParent().AtName("sendlog"),
path.MatchRelative().AtParent().AtName("email_group"),
path.MatchRelative().AtParent().AtName("microsoft_teams"),
path.MatchRelative().AtParent().AtName("microsoft_teams_workflow"),
path.MatchRelative().AtParent().AtName("jira"),
path.MatchRelative().AtParent().AtName("opsgenie"),
path.MatchRelative().AtParent().AtName("demisto"),
Expand Down Expand Up @@ -468,6 +470,7 @@ func (r *WebhookResource) Schema(_ context.Context, _ resource.SchemaRequest, re
path.MatchRelative().AtParent().AtName("sendlog"),
path.MatchRelative().AtParent().AtName("email_group"),
path.MatchRelative().AtParent().AtName("microsoft_teams"),
path.MatchRelative().AtParent().AtName("microsoft_teams_workflow"),
path.MatchRelative().AtParent().AtName("jira"),
path.MatchRelative().AtParent().AtName("opsgenie"),
path.MatchRelative().AtParent().AtName("demisto"),
Expand All @@ -491,6 +494,7 @@ func (r *WebhookResource) Schema(_ context.Context, _ resource.SchemaRequest, re
path.MatchRelative().AtParent().AtName("sendlog"),
path.MatchRelative().AtParent().AtName("email_group"),
path.MatchRelative().AtParent().AtName("microsoft_teams"),
path.MatchRelative().AtParent().AtName("microsoft_teams_workflow"),
path.MatchRelative().AtParent().AtName("jira"),
path.MatchRelative().AtParent().AtName("opsgenie"),
path.MatchRelative().AtParent().AtName("demisto"),
Expand Down Expand Up @@ -525,6 +529,7 @@ func (r *WebhookResource) Schema(_ context.Context, _ resource.SchemaRequest, re
path.MatchRelative().AtParent().AtName("pager_duty"),
path.MatchRelative().AtParent().AtName("email_group"),
path.MatchRelative().AtParent().AtName("microsoft_teams"),
path.MatchRelative().AtParent().AtName("microsoft_teams_workflow"),
path.MatchRelative().AtParent().AtName("jira"),
path.MatchRelative().AtParent().AtName("opsgenie"),
path.MatchRelative().AtParent().AtName("demisto"),
Expand All @@ -549,6 +554,7 @@ func (r *WebhookResource) Schema(_ context.Context, _ resource.SchemaRequest, re
path.MatchRelative().AtParent().AtName("pager_duty"),
path.MatchRelative().AtParent().AtName("sendlog"),
path.MatchRelative().AtParent().AtName("microsoft_teams"),
path.MatchRelative().AtParent().AtName("microsoft_teams_workflow"),
path.MatchRelative().AtParent().AtName("jira"),
path.MatchRelative().AtParent().AtName("opsgenie"),
path.MatchRelative().AtParent().AtName("demisto"),
Expand All @@ -558,7 +564,7 @@ func (r *WebhookResource) Schema(_ context.Context, _ resource.SchemaRequest, re
Optional: true,
MarkdownDescription: "Email group webhook.",
},
"microsoft_teams": schema.SingleNestedAttribute{
"microsoft_teams_workflow": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"url": schema.StringAttribute{
Optional: true,
Expand All @@ -572,6 +578,7 @@ func (r *WebhookResource) Schema(_ context.Context, _ resource.SchemaRequest, re
path.MatchRelative().AtParent().AtName("pager_duty"),
path.MatchRelative().AtParent().AtName("sendlog"),
path.MatchRelative().AtParent().AtName("email_group"),
path.MatchRelative().AtParent().AtName("microsoft_teams"),
path.MatchRelative().AtParent().AtName("jira"),
path.MatchRelative().AtParent().AtName("opsgenie"),
path.MatchRelative().AtParent().AtName("demisto"),
Expand All @@ -581,6 +588,32 @@ func (r *WebhookResource) Schema(_ context.Context, _ resource.SchemaRequest, re
Optional: true,
MarkdownDescription: "Microsoft Teams Workflow webhook.",
},
"microsoft_teams": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"url": schema.StringAttribute{
Optional: true,
MarkdownDescription: "Microsoft Teams URL.",
},
},
Validators: []validator.Object{
objectvalidator.ExactlyOneOf(
path.MatchRelative().AtParent().AtName("custom"),
path.MatchRelative().AtParent().AtName("slack"),
path.MatchRelative().AtParent().AtName("pager_duty"),
path.MatchRelative().AtParent().AtName("sendlog"),
path.MatchRelative().AtParent().AtName("email_group"),
path.MatchRelative().AtParent().AtName("microsoft_teams_workflow"),
path.MatchRelative().AtParent().AtName("jira"),
path.MatchRelative().AtParent().AtName("opsgenie"),
path.MatchRelative().AtParent().AtName("demisto"),
path.MatchRelative().AtParent().AtName("event_bridge"),
),
},
Optional: true,
MarkdownDescription: "Microsoft Teams webhook. (Deprecated, please use microsoft_teams_workflow)",
DeprecationMessage: "Please use microsoft_teams_workflow",
},

"jira": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"api_token": schema.StringAttribute{
Expand Down Expand Up @@ -608,6 +641,7 @@ func (r *WebhookResource) Schema(_ context.Context, _ resource.SchemaRequest, re
path.MatchRelative().AtParent().AtName("sendlog"),
path.MatchRelative().AtParent().AtName("email_group"),
path.MatchRelative().AtParent().AtName("microsoft_teams"),
path.MatchRelative().AtParent().AtName("microsoft_teams_workflow"),
path.MatchRelative().AtParent().AtName("opsgenie"),
path.MatchRelative().AtParent().AtName("demisto"),
path.MatchRelative().AtParent().AtName("event_bridge"),
Expand All @@ -631,6 +665,7 @@ func (r *WebhookResource) Schema(_ context.Context, _ resource.SchemaRequest, re
path.MatchRelative().AtParent().AtName("sendlog"),
path.MatchRelative().AtParent().AtName("email_group"),
path.MatchRelative().AtParent().AtName("microsoft_teams"),
path.MatchRelative().AtParent().AtName("microsoft_teams_workflow"),
path.MatchRelative().AtParent().AtName("jira"),
path.MatchRelative().AtParent().AtName("demisto"),
path.MatchRelative().AtParent().AtName("event_bridge"),
Expand Down Expand Up @@ -665,6 +700,7 @@ func (r *WebhookResource) Schema(_ context.Context, _ resource.SchemaRequest, re
path.MatchRelative().AtParent().AtName("sendlog"),
path.MatchRelative().AtParent().AtName("email_group"),
path.MatchRelative().AtParent().AtName("microsoft_teams"),
path.MatchRelative().AtParent().AtName("microsoft_teams_workflow"),
path.MatchRelative().AtParent().AtName("jira"),
path.MatchRelative().AtParent().AtName("opsgenie"),
path.MatchRelative().AtParent().AtName("event_bridge"),
Expand Down Expand Up @@ -704,6 +740,7 @@ func (r *WebhookResource) Schema(_ context.Context, _ resource.SchemaRequest, re
path.MatchRelative().AtParent().AtName("sendlog"),
path.MatchRelative().AtParent().AtName("email_group"),
path.MatchRelative().AtParent().AtName("microsoft_teams"),
path.MatchRelative().AtParent().AtName("microsoft_teams_workflow"),
path.MatchRelative().AtParent().AtName("jira"),
path.MatchRelative().AtParent().AtName("opsgenie"),
path.MatchRelative().AtParent().AtName("demisto"),
Expand Down Expand Up @@ -758,7 +795,7 @@ func (r *WebhookResource) Create(ctx context.Context, req resource.CreateRequest
getWebhookStr := protojson.Format(getWebhookResp)
log.Printf("[INFO] Reading webhook - %s", getWebhookStr)

plan, diags = flattenWebhook(ctx, getWebhookResp.GetWebhook())
plan, diags = flattenWebhookWrite(ctx, getWebhookResp.GetWebhook())
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand Down Expand Up @@ -862,7 +899,7 @@ func (r WebhookResource) Update(ctx context.Context, req resource.UpdateRequest,
}
log.Printf("[INFO] Received Webhook: %s", protojson.Format(getWebhookResp))

plan, diags = flattenWebhook(ctx, getWebhookResp.GetWebhook())
plan, diags = flattenWebhookWrite(ctx, getWebhookResp.GetWebhook())
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand Down Expand Up @@ -946,6 +983,9 @@ func expandWebhookType(ctx context.Context, plan *WebhookResourceModel, data *we
} else if plan.MsTeamsWorkflow != nil {
data.Config, data.Url = expandMicrosoftTeamsWorkflow(plan.MsTeamsWorkflow)
data.Type = webhooks.WebhookType_MS_TEAMS_WORKFLOW
} else if plan.MsTeams != nil {
data.Config, data.Url = expandMicrosoftTeams(plan.MsTeams)
data.Type = webhooks.WebhookType_MS_TEAMS_WORKFLOW
} else if plan.Jira != nil {
data.Config, data.Url = expandJira(plan.Jira)
data.Type = webhooks.WebhookType_JIRA
Expand Down Expand Up @@ -993,6 +1033,17 @@ func expandMicrosoftTeamsWorkflow(microsoftTeams *MsTeamsWorkflowModel) (*webhoo
}, url
}

func expandMicrosoftTeams(microsoftTeams *MsTeamsWorkflowModel) (*webhooks.OutgoingWebhookInputData_MicrosoftTeams, *wrapperspb.StringValue) {
var url *wrapperspb.StringValue
if planUrl := microsoftTeams.URL; !(planUrl.IsNull() || planUrl.IsUnknown()) {
url = wrapperspb.String(planUrl.ValueString())
}

return &webhooks.OutgoingWebhookInputData_MicrosoftTeams{
MicrosoftTeams: &webhooks.MicrosoftTeamsConfig{},
}, url
}

func expandSlack(ctx context.Context, slack *SlackModel) (*webhooks.OutgoingWebhookInputData_Slack, *wrapperspb.StringValue, diag.Diagnostics) {
digests, diags := expandDigests(ctx, slack.NotifyAbout)
if diags.HasError() {
Expand Down Expand Up @@ -1164,6 +1215,44 @@ func expandDemisto(demisto *DemistoModel) (*webhooks.OutgoingWebhookInputData_De
}, url
}

// Temporary function to prevent the creation of depreacted resources
func flattenWebhookWrite(ctx context.Context, webhook *webhooks.OutgoingWebhook) (*WebhookResourceModel, diag.Diagnostics) {
result := &WebhookResourceModel{
ID: wrapperspbStringToTypeString(webhook.Id),
ExternalID: types.StringValue(strconv.Itoa(int(webhook.GetExternalId().GetValue()))),
Name: wrapperspbStringToTypeString(webhook.Name),
}

url := webhook.GetUrl()
var diags diag.Diagnostics
switch configType := webhook.Config.(type) {
case *webhooks.OutgoingWebhook_Slack:
result.Slack, diags = flattenSlack(ctx, configType.Slack, url)
case *webhooks.OutgoingWebhook_GenericWebhook:
result.CustomWebhook, diags = flattenGenericWebhook(ctx, configType.GenericWebhook, url)
case *webhooks.OutgoingWebhook_PagerDuty:
result.PagerDuty = flattenPagerDuty(configType.PagerDuty)
case *webhooks.OutgoingWebhook_SendLog:
result.SendLog = flattenSendLog(configType.SendLog, url)
case *webhooks.OutgoingWebhook_EmailGroup:
result.EmailGroup = flattenEmailGroup(configType.EmailGroup)
case *webhooks.OutgoingWebhook_MsTeamsWorkflow:
result.MsTeamsWorkflow = flattenMsTeamsWorkflow(configType.MsTeamsWorkflow, url)
case *webhooks.OutgoingWebhook_Jira:
result.Jira = flattenJira(configType.Jira, url)
case *webhooks.OutgoingWebhook_Opsgenie:
result.Opsgenie = flattenOpsgenie(configType.Opsgenie, url)
case *webhooks.OutgoingWebhook_Demisto:
result.Demisto = flattenDemisto(configType.Demisto, url)
case *webhooks.OutgoingWebhook_AwsEventBridge:
result.EventBridge = flattenEventBridge(configType.AwsEventBridge)
default:
diags.AddError("Error flattening webhook", fmt.Sprintf("Unknown webhook type: %T", configType))
}

return result, diags
}

func flattenWebhook(ctx context.Context, webhook *webhooks.OutgoingWebhook) (*WebhookResourceModel, diag.Diagnostics) {
result := &WebhookResourceModel{
ID: wrapperspbStringToTypeString(webhook.Id),
Expand All @@ -1186,6 +1275,8 @@ func flattenWebhook(ctx context.Context, webhook *webhooks.OutgoingWebhook) (*We
result.EmailGroup = flattenEmailGroup(configType.EmailGroup)
case *webhooks.OutgoingWebhook_MsTeamsWorkflow:
result.MsTeamsWorkflow = flattenMsTeamsWorkflow(configType.MsTeamsWorkflow, url)
case *webhooks.OutgoingWebhook_MicrosoftTeams:
result.MsTeams = flattenMicrosoftTeams(configType.MicrosoftTeams, url)
case *webhooks.OutgoingWebhook_Jira:
result.Jira = flattenJira(configType.Jira, url)
case *webhooks.OutgoingWebhook_Opsgenie:
Expand Down Expand Up @@ -1298,6 +1389,12 @@ func flattenMsTeamsWorkflow(_ *webhooks.MSTeamsWorkflowConfig, url *wrapperspb.S
}
}

func flattenMicrosoftTeams(microsoftTeamsConfig *webhooks.MicrosoftTeamsConfig, url *wrapperspb.StringValue) *MsTeamsWorkflowModel {
return &MsTeamsWorkflowModel{
URL: wrapperspbStringToTypeString(url),
}
}

func flattenJira(jira *webhooks.JiraConfig, url *wrapperspb.StringValue) *JiraModel {
return &JiraModel{
ApiKey: wrapperspbStringToTypeString(jira.ApiToken),
Expand Down
4 changes: 2 additions & 2 deletions coralogix/resource_coralogix_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func TestAccCoralogixResourceMicrosoftTeamsWorkflowWebhook(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttr(resourceName, "name", webhook.name),
resource.TestCheckResourceAttr(resourceName, "microsoft_teams.url", webhook.url),
resource.TestCheckResourceAttr(resourceName, "microsoft_teams_workflow.url", webhook.url),
),
},
{
Expand Down Expand Up @@ -463,7 +463,7 @@ func testAccCoralogixResourceSendLogWebhook(w *webhookTestFields) string {
func testAccCoralogixResourceMicrosoftTeamsWorkflowWebhook(w *webhookTestFields) string {
return fmt.Sprintf(`resource "coralogix_webhook" "test" {
name = "%s"
microsoft_teams = {
microsoft_teams_workflow = {
url = "%s"
}
}
Expand Down
18 changes: 12 additions & 6 deletions docs/data-sources/webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ data "coralogix_webhook" "imported_webhook" {
id = coralogix_webhook.slack_webhook.id
}
```



<!-- schema generated by tfplugindocs -->
## Schema

Expand All @@ -34,7 +31,8 @@ data "coralogix_webhook" "imported_webhook" {
- `event_bridge` (Attributes) (see [below for nested schema](#nestedatt--event_bridge))
- `external_id` (String) Webhook external ID. Using to linq webhook to alert.
- `jira` (Attributes) Jira webhook. (see [below for nested schema](#nestedatt--jira))
- `microsoft_teams` (Attributes) Microsoft Teams Workflow webhook. (see [below for nested schema](#nestedatt--microsoft_teams))
- `microsoft_teams` (Attributes) Microsoft Teams webhook. (Deprecated, please use microsoft_teams_workflow) (see [below for nested schema](#nestedatt--microsoft_teams))
- `microsoft_teams_workflow` (Attributes) Microsoft Teams Workflow webhook. (see [below for nested schema](#nestedatt--microsoft_teams_workflow))
- `name` (String) Webhook name.
- `opsgenie` (Attributes) Opsgenie webhook. (see [below for nested schema](#nestedatt--opsgenie))
- `pager_duty` (Attributes) PagerDuty webhook. (see [below for nested schema](#nestedatt--pager_duty))
Expand All @@ -47,7 +45,7 @@ data "coralogix_webhook" "imported_webhook" {
Read-Only:

- `headers` (Map of String) Webhook headers. Map of string to string.
- `method` (String) Webhook method. can be one of: get, post, put
- `method` (String) Webhook method. can be one of: put, get, post
- `payload` (String) Webhook payload. JSON string.
- `url` (String) Webhook URL.
- `uuid` (String) Webhook UUID. Computed automatically.
Expand Down Expand Up @@ -99,6 +97,14 @@ Read-Only:

Read-Only:

- `url` (String) Microsoft Teams URL.


<a id="nestedatt--microsoft_teams_workflow"></a>
### Nested Schema for `microsoft_teams_workflow`

Read-Only:

- `url` (String) Microsoft Teams Workflow URL.


Expand Down Expand Up @@ -134,7 +140,7 @@ Read-Only:
Read-Only:

- `attachments` (Attributes List) Slack attachments. (see [below for nested schema](#nestedatt--slack--attachments))
- `notify_on` (Set of String) Slack notifications. can be one of: spike_anomalies, data_usage, error_and_critical_logs, flow_anomalies
- `notify_on` (Set of String) Slack notifications. can be one of: error_and_critical_logs, flow_anomalies, spike_anomalies, data_usage
- `url` (String) Slack URL.

<a id="nestedatt--slack--attachments"></a>
Expand Down
18 changes: 14 additions & 4 deletions docs/resources/webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ description: "Webhook defines integration. More info - https://coralogix.com/doc
---

# coralogix_webhook (Resource)

Coralogix webhook. For more info please review -https://coralogix.com/docs/alert-webhook-with-slack/.

## Example Usage
Expand All @@ -21,6 +20,7 @@ resource "coralogix_webhook" "slack_webhook" {
```



<!-- schema generated by tfplugindocs -->
## Schema

Expand All @@ -31,7 +31,8 @@ resource "coralogix_webhook" "slack_webhook" {
- `email_group` (Attributes) Email group webhook. (see [below for nested schema](#nestedatt--email_group))
- `event_bridge` (Attributes) (see [below for nested schema](#nestedatt--event_bridge))
- `jira` (Attributes) Jira webhook. (see [below for nested schema](#nestedatt--jira))
- `microsoft_teams` (Attributes) Microsoft Teams Workflow webhook. (see [below for nested schema](#nestedatt--microsoft_teams))
- `microsoft_teams` (Attributes, Deprecated) Microsoft Teams webhook. (Deprecated, please use microsoft_teams_workflow) (see [below for nested schema](#nestedatt--microsoft_teams))
- `microsoft_teams_workflow` (Attributes) Microsoft Teams Workflow webhook. (see [below for nested schema](#nestedatt--microsoft_teams_workflow))
- `name` (String) Webhook name.
- `opsgenie` (Attributes) Opsgenie webhook. (see [below for nested schema](#nestedatt--opsgenie))
- `pager_duty` (Attributes) PagerDuty webhook. (see [below for nested schema](#nestedatt--pager_duty))
Expand All @@ -49,7 +50,7 @@ resource "coralogix_webhook" "slack_webhook" {
Optional:

- `headers` (Map of String) Webhook headers. Map of string to string.
- `method` (String) Webhook method. can be one of: get, post, put
- `method` (String) Webhook method. can be one of: put, get, post
- `payload` (String) Webhook payload. JSON string.
- `url` (String) Webhook URL.
- `uuid` (String) Webhook UUID. Computed automatically.
Expand Down Expand Up @@ -110,6 +111,14 @@ Optional:

Optional:

- `url` (String) Microsoft Teams URL.


<a id="nestedatt--microsoft_teams_workflow"></a>
### Nested Schema for `microsoft_teams_workflow`

Optional:

- `url` (String) Microsoft Teams Workflow URL.


Expand Down Expand Up @@ -177,4 +186,5 @@ grpcurl -H "Authorization: Bearer <api-key>" -d @ ng-api-grpc.<region-domain>:44
EOF
```

[region-domain table](../index.md#region-domain-table)
[region-domain table](../index.md#region-domain-table)

2 changes: 1 addition & 1 deletion examples/webhook/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ resource "coralogix_webhook" "email_group_webhook" {

resource "coralogix_webhook" "microsoft_teams_webhook" {
name = "microsoft-teams-webhook"
microsoft_teams = {
microsoft_teams_workflow = {
url = "https://example-url.com/"
}
}
Expand Down

0 comments on commit a2fea89

Please sign in to comment.