From 58f6c3b7f7e165caa2abcc3e90b3e1541cd1ecca Mon Sep 17 00:00:00 2001 From: Agustin Bettati Date: Mon, 22 Apr 2024 17:27:05 +0200 Subject: [PATCH] add new attribute in schema, convertion functions, and unit tests --- .changelog/1.txt | 11 ++++ .../data_source_alert_configuration.go | 3 + .../model_alert_configuration.go | 3 + .../model_alert_configuration_test.go | 61 ++++++++++--------- .../resource_alert_configuration.go | 4 ++ .../docs/d/alert_configuration.html.markdown | 3 +- .../docs/d/alert_configurations.html.markdown | 3 +- .../docs/d/third_party_integration.markdown | 1 + .../docs/r/alert_configuration.html.markdown | 17 ++++++ 9 files changed, 76 insertions(+), 30 deletions(-) create mode 100644 .changelog/1.txt diff --git a/.changelog/1.txt b/.changelog/1.txt new file mode 100644 index 0000000000..12d7e78c6c --- /dev/null +++ b/.changelog/1.txt @@ -0,0 +1,11 @@ +```release-note:enhancement +resource/mongodbatlas_alert_configuration: Adds `integration_id` attribute +``` + +```release-note:enhancement +data-source/mongodbatlas_alert_configuration: Adds `integration_id` attribute +``` + +```release-note:enhancement +data-source/mongodbatlas_alert_configurations: Adds `integration_id` attribute +``` diff --git a/internal/service/alertconfiguration/data_source_alert_configuration.go b/internal/service/alertconfiguration/data_source_alert_configuration.go index d9c720755d..ca5a140938 100644 --- a/internal/service/alertconfiguration/data_source_alert_configuration.go +++ b/internal/service/alertconfiguration/data_source_alert_configuration.go @@ -206,6 +206,9 @@ var alertConfigDSSchemaAttributes = map[string]schema.Attribute{ "notifier_id": schema.StringAttribute{ Computed: true, }, + "integration_id": schema.StringAttribute{ + Computed: true, + }, "type_name": schema.StringAttribute{ Computed: true, }, diff --git a/internal/service/alertconfiguration/model_alert_configuration.go b/internal/service/alertconfiguration/model_alert_configuration.go index 7dbb5279cd..e727b8ab60 100644 --- a/internal/service/alertconfiguration/model_alert_configuration.go +++ b/internal/service/alertconfiguration/model_alert_configuration.go @@ -46,6 +46,7 @@ func NewNotificationList(list []TfNotificationModel) (*[]admin.AlertsNotificatio MicrosoftTeamsWebhookUrl: n.MicrosoftTeamsWebhookURL.ValueStringPointer(), WebhookSecret: n.WebhookSecret.ValueStringPointer(), WebhookUrl: n.WebhookURL.ValueStringPointer(), + IntegrationId: n.IntegrationID.ValueStringPointer(), } if !n.NotifierID.IsUnknown() { notifications[i].NotifierId = n.NotifierID.ValueStringPointer() @@ -127,6 +128,7 @@ func NewTFNotificationModelList(n []admin.AlertsNotificationRootForGroup, currSt OpsGenieRegion: conversion.StringPtrNullIfEmpty(value.OpsGenieRegion), TeamID: conversion.StringPtrNullIfEmpty(value.TeamId), NotifierID: types.StringPointerValue(value.NotifierId), + IntegrationID: types.StringPointerValue(value.IntegrationId), TypeName: conversion.StringPtrNullIfEmpty(value.TypeName), Username: conversion.StringPtrNullIfEmpty(value.Username), EmailEnabled: types.BoolValue(value.EmailEnabled != nil && *value.EmailEnabled), @@ -153,6 +155,7 @@ func NewTFNotificationModelList(n []admin.AlertsNotificationRootForGroup, currSt WebhookSecret: conversion.StringNullIfEmpty(currState.WebhookSecret.ValueString()), MicrosoftTeamsWebhookURL: conversion.StringNullIfEmpty(currState.MicrosoftTeamsWebhookURL.ValueString()), NotifierID: types.StringPointerValue(value.NotifierId), + IntegrationID: types.StringPointerValue(value.IntegrationId), IntervalMin: types.Int64PointerValue(conversion.IntPtrToInt64Ptr(value.IntervalMin)), DelayMin: types.Int64PointerValue(conversion.IntPtrToInt64Ptr(value.DelayMin)), EmailEnabled: types.BoolValue(value.EmailEnabled != nil && *value.EmailEnabled), diff --git a/internal/service/alertconfiguration/model_alert_configuration_test.go b/internal/service/alertconfiguration/model_alert_configuration_test.go index d12a9a211b..1369c98b55 100644 --- a/internal/service/alertconfiguration/model_alert_configuration_test.go +++ b/internal/service/alertconfiguration/model_alert_configuration_test.go @@ -22,6 +22,7 @@ const ( threshold float64 = 99.0 units string = "RAW" mode string = "AVERAGE" + integrationID string = "fake-intregration-id" ) var ( @@ -39,14 +40,15 @@ func TestNotificationSDKToTFModel(t *testing.T) { name: "Complete SDK response", SDKResp: &[]admin.AlertsNotificationRootForGroup{ { - TypeName: admin.PtrString(group), - IntervalMin: admin.PtrInt(intervalMin), - DelayMin: admin.PtrInt(delayMin), - SmsEnabled: admin.PtrBool(disabled), - EmailEnabled: admin.PtrBool(enabled), - ChannelName: admin.PtrString("#channel"), - Roles: &roles, - ApiToken: admin.PtrString("newApiToken"), + TypeName: admin.PtrString(group), + IntervalMin: admin.PtrInt(intervalMin), + DelayMin: admin.PtrInt(delayMin), + SmsEnabled: admin.PtrBool(disabled), + EmailEnabled: admin.PtrBool(enabled), + ChannelName: admin.PtrString("#channel"), + Roles: &roles, + ApiToken: admin.PtrString("newApiToken"), + IntegrationId: admin.PtrString(integrationID), }, }, currentStateNotifications: []alertconfiguration.TfNotificationModel{ @@ -62,14 +64,15 @@ func TestNotificationSDKToTFModel(t *testing.T) { }, expectedTFModel: []alertconfiguration.TfNotificationModel{ { - TypeName: types.StringValue(group), - IntervalMin: types.Int64Value(int64(intervalMin)), - DelayMin: types.Int64Value(int64(delayMin)), - SMSEnabled: types.BoolValue(disabled), - EmailEnabled: types.BoolValue(enabled), - ChannelName: types.StringNull(), - APIToken: types.StringValue("apiToken"), - Roles: roles, + TypeName: types.StringValue(group), + IntervalMin: types.Int64Value(int64(intervalMin)), + DelayMin: types.Int64Value(int64(delayMin)), + SMSEnabled: types.BoolValue(disabled), + EmailEnabled: types.BoolValue(enabled), + ChannelName: types.StringNull(), + APIToken: types.StringValue("apiToken"), + Roles: roles, + IntegrationID: types.StringValue(integrationID), }, }, }, @@ -265,22 +268,24 @@ func TestNotificationTFModelToSDK(t *testing.T) { name: "Complete TF model", tfModel: []alertconfiguration.TfNotificationModel{ { - TypeName: types.StringValue(group), - IntervalMin: types.Int64Value(int64(intervalMin)), - DelayMin: types.Int64Value(int64(delayMin)), - SMSEnabled: types.BoolValue(disabled), - EmailEnabled: types.BoolValue(enabled), - Roles: roles, + TypeName: types.StringValue(group), + IntervalMin: types.Int64Value(int64(intervalMin)), + DelayMin: types.Int64Value(int64(delayMin)), + SMSEnabled: types.BoolValue(disabled), + EmailEnabled: types.BoolValue(enabled), + Roles: roles, + IntegrationID: types.StringValue(integrationID), }, }, expectedSDKReq: &[]admin.AlertsNotificationRootForGroup{ { - TypeName: admin.PtrString(group), - IntervalMin: admin.PtrInt(intervalMin), - DelayMin: admin.PtrInt(delayMin), - SmsEnabled: admin.PtrBool(disabled), - EmailEnabled: admin.PtrBool(enabled), - Roles: &roles, + TypeName: admin.PtrString(group), + IntervalMin: admin.PtrInt(intervalMin), + DelayMin: admin.PtrInt(delayMin), + SmsEnabled: admin.PtrBool(disabled), + EmailEnabled: admin.PtrBool(enabled), + Roles: &roles, + IntegrationId: admin.PtrString(integrationID), }, }, }, diff --git a/internal/service/alertconfiguration/resource_alert_configuration.go b/internal/service/alertconfiguration/resource_alert_configuration.go index 7023dffe86..2d102dc4e8 100644 --- a/internal/service/alertconfiguration/resource_alert_configuration.go +++ b/internal/service/alertconfiguration/resource_alert_configuration.go @@ -101,6 +101,7 @@ type TfNotificationModel struct { TeamID types.String `tfsdk:"team_id"` TeamName types.String `tfsdk:"team_name"` NotifierID types.String `tfsdk:"notifier_id"` + IntegrationID types.String `tfsdk:"integration_id"` TypeName types.String `tfsdk:"type_name"` ChannelName types.String `tfsdk:"channel_name"` VictorOpsAPIKey types.String `tfsdk:"victor_ops_api_key"` @@ -321,6 +322,9 @@ func (r *alertConfigurationRS) Schema(ctx context.Context, req resource.SchemaRe Computed: true, Optional: true, }, + "integration_id": schema.StringAttribute{ + Optional: true, + }, "type_name": schema.StringAttribute{ Required: true, Validators: []validator.String{ diff --git a/website/docs/d/alert_configuration.html.markdown b/website/docs/d/alert_configuration.html.markdown index 6b2488d2e2..a0e345a002 100644 --- a/website/docs/d/alert_configuration.html.markdown +++ b/website/docs/d/alert_configuration.html.markdown @@ -226,6 +226,7 @@ Notifications to send when an alert condition is detected. - `WEBHOOK` - `MICROSOFT_TEAMS` +* `integration_id` - The id of the associated integration, the credentials of which to use for requests. * `notifier_id` - The notifier id is a system-generated unique identifier assigned to each notification method. This is needed when updating third-party notifications without requiring explicit authentication credentials. * `username` - Name of the Atlas user to which to send notifications. Only a user in the project that owns the alert configuration is allowed here. Required for the `USER` notifications type. * `victor_ops_api_key` - VictorOps API key. Required for the `VICTOR_OPS` notifications type. If the key later becomes invalid, Atlas sends an email to the project owner and eventually removes the key. @@ -235,4 +236,4 @@ Notifications to send when an alert condition is detected. * `microsoft_teams_webhook_url` - Microsoft Teams channel incoming webhook URL. Required for the `MICROSOFT_TEAMS` notifications type. * `roles` - Atlas role in current Project or Organization. Atlas returns this value if you set `type_name` to `ORG` or `GROUP`. -See detailed information for arguments and attributes: [MongoDB API Alert Configuration](https://docs.atlas.mongodb.com/reference/api/alert-configurations-get-config/) \ No newline at end of file +See detailed information for arguments and attributes: [MongoDB API Alert Configuration](https://docs.atlas.mongodb.com/reference/api/alert-configurations-get-config/) diff --git a/website/docs/d/alert_configurations.html.markdown b/website/docs/d/alert_configurations.html.markdown index 11e63d3918..1206f736e4 100644 --- a/website/docs/d/alert_configurations.html.markdown +++ b/website/docs/d/alert_configurations.html.markdown @@ -170,6 +170,7 @@ Notifications to send when an alert condition is detected. - `WEBHOOK` - `MICROSOFT_TEAMS` +* `integration_id` - The id of the associated integration, the credentials of which to use for requests. * `notifier_id` - The notifier id is a system-generated unique identifier assigned to each notification method. This is needed when updating third-party notifications without requiring explicit authentication credentials. * `username` - Name of the Atlas user to which to send notifications. Only a user in the project that owns the alert configuration is allowed here. Required for the `USER` notifications type. * `victor_ops_api_key` - VictorOps API key. Required for the `VICTOR_OPS` notifications type. If the key later becomes invalid, Atlas sends an email to the project owner and eventually removes the key. @@ -180,4 +181,4 @@ Notifications to send when an alert condition is detected. * `roles` - Atlas role in current Project or Organization. Atlas returns this value if you set `type_name` to `ORG` or `GROUP`. For more information see: [MongoDB Atlas API Reference.](https://docs.atlas.mongodb.com/reference/api/alert-configurations/) -Or refer to the individual resource or data_source documentation on alert configuration. \ No newline at end of file +Or refer to the individual resource or data_source documentation on alert configuration. diff --git a/website/docs/d/third_party_integration.markdown b/website/docs/d/third_party_integration.markdown index 46d227d405..5688ccb476 100644 --- a/website/docs/d/third_party_integration.markdown +++ b/website/docs/d/third_party_integration.markdown @@ -25,6 +25,7 @@ resource "mongodbatlas_third_party_integration" "test_datadog" { data "mongodbatlas_third_party_integration" "test" { project_id = mongodbatlas_third_party_integration.test_datadog.project_id + type = "DATADOG" } ``` diff --git a/website/docs/r/alert_configuration.html.markdown b/website/docs/r/alert_configuration.html.markdown index 11af98af28..d4c3b51b5c 100644 --- a/website/docs/r/alert_configuration.html.markdown +++ b/website/docs/r/alert_configuration.html.markdown @@ -119,6 +119,22 @@ resource "mongodbatlas_alert_configuration" "test" { } ``` +### Create third party notification using credentials from existing third party integration + + +```terraform +resource "mongodbatlas_alert_configuration" "test" { + project_id = "PROJECT ID" + enabled = true + event_type = "USERS_WITHOUT_MULTI_FACTOR_AUTH" + + notification { + type_name = "PAGER_DUTY" + integration_id = "THIRD PARTY INTEGRATION ID" + } +} +``` + ## Argument Reference * `project_id` - (Required) The ID of the project where the alert configuration will create. @@ -223,6 +239,7 @@ List of notifications to send when an alert condition is detected. - `WEBHOOK` - `MICROSOFT_TEAMS` +* `integration_id` - The id of the associated integration, the credentials of which to use for requests. * `notifier_id` - The notifier id is a system-generated unique identifier assigned to each notification method. This is needed when updating third-party notifications without requiring explicit authentication credentials. * `username` - Name of the Atlas user to which to send notifications. Only a user in the project that owns the alert configuration is allowed here. Required for the `USER` notifications type. * `victor_ops_api_key` - VictorOps API key. Required for the `VICTOR_OPS` notifications type. If the key later becomes invalid, Atlas sends an email to the project owner and eventually removes the key.