From 6bcda8602fba0a3129c290df47bb161b694696ae Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 2 Aug 2024 09:49:02 +0200 Subject: [PATCH 01/23] support new networking attribute in stream connection --- .../data_source_stream_connection.go | 16 +++++++++ .../model_stream_connection.go | 27 ++++++++++++++ .../model_stream_connection_test.go | 5 +++ .../resource_stream_connection.go | 36 +++++++++++++++++++ .../resource_stream_connection_test.go | 5 +++ 5 files changed, 89 insertions(+) diff --git a/internal/service/streamconnection/data_source_stream_connection.go b/internal/service/streamconnection/data_source_stream_connection.go index 24687115a4..c6c1a5f668 100644 --- a/internal/service/streamconnection/data_source_stream_connection.go +++ b/internal/service/streamconnection/data_source_stream_connection.go @@ -103,6 +103,22 @@ func DSAttributes(withArguments bool) map[string]schema.Attribute { }, }, }, + "networking": schema.SingleNestedAttribute{ + Computed: true, + Attributes: map[string]schema.Attribute{ + "access": schema.SingleNestedAttribute{ + Computed: true, + Attributes: map[string]schema.Attribute{ + "name": schema.StringAttribute{ + Computed: true, + }, + "type": schema.StringAttribute{ + Computed: true, + }, + }, + }, + }, + }, } } diff --git a/internal/service/streamconnection/model_stream_connection.go b/internal/service/streamconnection/model_stream_connection.go index 0c2a0ece7d..974a5fcff4 100644 --- a/internal/service/streamconnection/model_stream_connection.go +++ b/internal/service/streamconnection/model_stream_connection.go @@ -60,6 +60,19 @@ func NewStreamConnectionReq(ctx context.Context, plan *TFStreamConnectionModel) } } + if !plan.Networking.IsNull() && !plan.Networking.IsUnknown() { + networkingModel := &TFNetworkingModel{} + if diags := plan.Networking.As(ctx, networkingModel, basetypes.ObjectAsOptions{}); diags.HasError() { + return nil, diags + } + streamConnection.Networking = &admin.StreamsKafkaNetworking{ + Access: &admin.StreamsKafkaNetworkingAccess{ + Name: networkingModel.Access.Name.ValueStringPointer(), + Type: networkingModel.Access.Type.ValueStringPointer(), + }, + } + } + return &streamConnection, nil } @@ -114,6 +127,20 @@ func NewTFStreamConnection(ctx context.Context, projID, instanceName string, cur connectionModel.DBRoleToExecute = dbRoleToExecuteModel } + connectionModel.Networking = types.ObjectNull(NetworkingObjectType.AttrTypes) + if apiResp.Networking != nil { + networkingModel, diags := types.ObjectValueFrom(ctx, NetworkingObjectType.AttrTypes, TFNetworkingModel{ + Access: TFNetworkingAccessModel{ + Name: types.StringPointerValue(apiResp.Networking.Access.Name), + Type: types.StringPointerValue(apiResp.Networking.Access.Type), + }, + }) + if diags.HasError() { + return nil, diags + } + connectionModel.Networking = networkingModel + } + return &connectionModel, nil } diff --git a/internal/service/streamconnection/model_stream_connection_test.go b/internal/service/streamconnection/model_stream_connection_test.go index 16ef34747d..e583941576 100644 --- a/internal/service/streamconnection/model_stream_connection_test.go +++ b/internal/service/streamconnection/model_stream_connection_test.go @@ -67,6 +67,7 @@ func TestStreamConnectionSDKToTFModel(t *testing.T) { Config: types.MapNull(types.StringType), Security: types.ObjectNull(streamconnection.ConnectionSecurityObjectType.AttrTypes), DBRoleToExecute: tfDBRoleToExecuteObject(t, dbRole, dbRoleType), + Networking: types.ObjectNull(streamconnection.NetworkingObjectType.AttrTypes), }, }, { @@ -98,6 +99,7 @@ func TestStreamConnectionSDKToTFModel(t *testing.T) { Config: tfConfigMap(t, configMap), Security: tfSecurityObject(t, DummyCACert, securityProtocol), DBRoleToExecute: types.ObjectNull(streamconnection.DBRoleToExecuteObjectType.AttrTypes), + Networking: types.ObjectNull(streamconnection.NetworkingObjectType.AttrTypes), }, }, { @@ -118,6 +120,7 @@ func TestStreamConnectionSDKToTFModel(t *testing.T) { Config: types.MapNull(types.StringType), Security: types.ObjectNull(streamconnection.ConnectionSecurityObjectType.AttrTypes), DBRoleToExecute: types.ObjectNull(streamconnection.DBRoleToExecuteObjectType.AttrTypes), + Networking: types.ObjectNull(streamconnection.NetworkingObjectType.AttrTypes), }, }, { @@ -149,6 +152,7 @@ func TestStreamConnectionSDKToTFModel(t *testing.T) { Config: tfConfigMap(t, configMap), Security: tfSecurityObject(t, DummyCACert, securityProtocol), DBRoleToExecute: types.ObjectNull(streamconnection.DBRoleToExecuteObjectType.AttrTypes), + Networking: types.ObjectNull(streamconnection.NetworkingObjectType.AttrTypes), }, }, { @@ -168,6 +172,7 @@ func TestStreamConnectionSDKToTFModel(t *testing.T) { Config: types.MapNull(types.StringType), Security: types.ObjectNull(streamconnection.ConnectionSecurityObjectType.AttrTypes), DBRoleToExecute: types.ObjectNull(streamconnection.DBRoleToExecuteObjectType.AttrTypes), + Networking: types.ObjectNull(streamconnection.NetworkingObjectType.AttrTypes), }, }, } diff --git a/internal/service/streamconnection/resource_stream_connection.go b/internal/service/streamconnection/resource_stream_connection.go index 530e855021..f24356c628 100644 --- a/internal/service/streamconnection/resource_stream_connection.go +++ b/internal/service/streamconnection/resource_stream_connection.go @@ -47,6 +47,7 @@ type TFStreamConnectionModel struct { Config types.Map `tfsdk:"config"` Security types.Object `tfsdk:"security"` DBRoleToExecute types.Object `tfsdk:"db_role_to_execute"` + Networking types.Object `tfsdk:"networking"` } type TFConnectionAuthenticationModel struct { @@ -81,6 +82,24 @@ var DBRoleToExecuteObjectType = types.ObjectType{AttrTypes: map[string]attr.Type "type": types.StringType, }} +type TFNetworkingAccessModel struct { + Name types.String `tfsdk:"name"` + Type types.String `tfsdk:"type"` +} + +var NetworkingAccessObjectType = types.ObjectType{AttrTypes: map[string]attr.Type{ + "name": types.StringType, + "type": types.StringType, +}} + +type TFNetworkingModel struct { + Access TFNetworkingAccessModel `tfsdk:"access"` +} + +var NetworkingObjectType = types.ObjectType{AttrTypes: map[string]attr.Type{ + "access": NetworkingAccessObjectType, +}} + func (r *streamConnectionRS) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = schema.Schema{ Attributes: map[string]schema.Attribute{ @@ -165,6 +184,23 @@ func (r *streamConnectionRS) Schema(ctx context.Context, req resource.SchemaRequ }, }, }, + "networking": schema.SingleNestedAttribute{ + Optional: true, + Computed: true, + Attributes: map[string]schema.Attribute{ + "access": schema.SingleNestedAttribute{ + Required: true, + Attributes: map[string]schema.Attribute{ + "name": schema.StringAttribute{ + Optional: true, + }, + "type": schema.StringAttribute{ + Required: true, + }, + }, + }, + }, + }, }, } } diff --git a/internal/service/streamconnection/resource_stream_connection_test.go b/internal/service/streamconnection/resource_stream_connection_test.go index f65dce618b..56f44e4364 100644 --- a/internal/service/streamconnection/resource_stream_connection_test.go +++ b/internal/service/streamconnection/resource_stream_connection_test.go @@ -153,6 +153,11 @@ func kafkaStreamConnectionConfig(projectID, instanceName, username, password, bo config = { "auto.offset.reset": %[5]q } + networking = { + access = { + type = "PUBLIC" + } + } %[6]s } `, projectAndStreamInstanceConfig, username, password, bootstrapServers, configValue, securityConfig) From f70fcd8f6e355d03facdb3444d288bb79e9f7143 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 2 Aug 2024 09:58:01 +0200 Subject: [PATCH 02/23] add changelog entry --- .changelog/2474.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .changelog/2474.txt diff --git a/.changelog/2474.txt b/.changelog/2474.txt new file mode 100644 index 0000000000..bbf0a0859e --- /dev/null +++ b/.changelog/2474.txt @@ -0,0 +1,11 @@ +```release-note:enhancement +resource/mongodbatlas_stream_connection: Supports new networking attribute +``` + +```release-note:enhancement +data-source/mongodbatlas_stream_connection: Supports new networking attribute +``` + +```release-note:enhancement +data-source/mongodbatlas_stream_connections: Supports new networking attribute +``` \ No newline at end of file From 922f11ac9af506a62663893f864c1ba0aa04fd08 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 2 Aug 2024 10:23:16 +0200 Subject: [PATCH 03/23] fix unit tests --- .../model_stream_connection_test.go | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/internal/service/streamconnection/model_stream_connection_test.go b/internal/service/streamconnection/model_stream_connection_test.go index e583941576..2c749a5aac 100644 --- a/internal/service/streamconnection/model_stream_connection_test.go +++ b/internal/service/streamconnection/model_stream_connection_test.go @@ -24,6 +24,7 @@ const ( dbRole = "customRole" dbRoleType = "CUSTOM" sampleConnectionName = "sample_stream_solar" + networkingType = "PUBLIC" ) var configMap = map[string]string{ @@ -217,6 +218,11 @@ func TestStreamConnectionsSDKToTFModel(t *testing.T) { Protocol: admin.PtrString(securityProtocol), BrokerPublicCertificate: admin.PtrString(DummyCACert), }, + Networking: &admin.StreamsKafkaNetworking{ + Access: &admin.StreamsKafkaNetworkingAccess{ + Type: admin.PtrString(networkingType), + }, + }, }, { Name: admin.PtrString(connectionName), @@ -258,6 +264,7 @@ func TestStreamConnectionsSDKToTFModel(t *testing.T) { Config: tfConfigMap(t, configMap), Security: tfSecurityObject(t, DummyCACert, securityProtocol), DBRoleToExecute: types.ObjectNull(streamconnection.DBRoleToExecuteObjectType.AttrTypes), + Networking: tfNetworkingObject(t, networkingType), }, { ID: types.StringValue(fmt.Sprintf("%s-%s-%s", instanceName, dummyProjectID, connectionName)), @@ -270,6 +277,7 @@ func TestStreamConnectionsSDKToTFModel(t *testing.T) { Config: types.MapNull(types.StringType), Security: types.ObjectNull(streamconnection.ConnectionSecurityObjectType.AttrTypes), DBRoleToExecute: tfDBRoleToExecuteObject(t, dbRole, dbRoleType), + Networking: types.ObjectNull(streamconnection.NetworkingObjectType.AttrTypes), }, { ID: types.StringValue(fmt.Sprintf("%s-%s-%s", instanceName, dummyProjectID, sampleConnectionName)), @@ -282,6 +290,7 @@ func TestStreamConnectionsSDKToTFModel(t *testing.T) { Config: types.MapNull(types.StringType), Security: types.ObjectNull(streamconnection.ConnectionSecurityObjectType.AttrTypes), DBRoleToExecute: types.ObjectNull(streamconnection.DBRoleToExecuteObjectType.AttrTypes), + Networking: types.ObjectNull(streamconnection.NetworkingObjectType.AttrTypes), }, }, }, @@ -475,3 +484,16 @@ func tfDBRoleToExecuteObject(t *testing.T, role, roleType string) types.Object { } return auth } + +func tfNetworkingObject(t *testing.T, networkingType string) types.Object { + t.Helper() + networking, diags := types.ObjectValueFrom(context.Background(), streamconnection.NetworkingObjectType.AttrTypes, streamconnection.TFNetworkingModel{ + Access: streamconnection.TFNetworkingAccessModel{ + Type: types.StringValue(networkingType), + }, + }) + if diags.HasError() { + t.Errorf("failed to create terraform data model: %s", diags.Errors()[0].Summary()) + } + return networking +} From 08e8cec53b5cea028e631072002798f1ac9ff75b Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 2 Aug 2024 10:42:55 +0200 Subject: [PATCH 04/23] documentation --- docs/data-sources/stream_connection.md | 8 ++++++++ docs/data-sources/stream_connections.md | 8 ++++++++ docs/resources/stream_connection.md | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/docs/data-sources/stream_connection.md b/docs/data-sources/stream_connection.md index 242837f186..9085f16cb2 100644 --- a/docs/data-sources/stream_connection.md +++ b/docs/data-sources/stream_connection.md @@ -31,6 +31,7 @@ If `type` is of value `Kafka` the following additional attributes are defined: * `bootstrap_servers` - Comma separated list of server addresses. * `config` - A map of Kafka key-value pairs for optional configuration. This is a flat object, and keys can have '.' characters. * `security` - Properties for the secure transport connection to Kafka. For SSL, this can include the trusted certificate to use. See [security](#security). +* `networking` - Networking Access Type can either be `PUBLIC` (default) or `VPC`. See [networking](#networking). ### Authentication @@ -48,5 +49,12 @@ If `type` is of value `Kafka` the following additional attributes are defined: * `role` - The name of the role to use. Can be a built in role or a custom role. * `type` - Type of the DB role. Can be either BUILT_IN or CUSTOM. +### Networking +* `access` - Information about the networking access. See [access](#access). + +### Access +* `name` - Id of the vpc peer when the type is `VPC`. +* `type` - Selected networking type. Either `PUBLIC` or `VPC`. Defaults to `PUBLIC`. + To learn more, see: [MongoDB Atlas API - Stream Connection](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Streams/operation/getStreamConnection) Documentation. The [Terraform Provider Examples Section](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/master/examples/mongodbatlas_stream_instance/atlas-streams-user-journey.md) also contains details on the overall support for Atlas Streams Processing in Terraform. diff --git a/docs/data-sources/stream_connections.md b/docs/data-sources/stream_connections.md index 6bdbfe2261..c25ee9a7b4 100644 --- a/docs/data-sources/stream_connections.md +++ b/docs/data-sources/stream_connections.md @@ -43,6 +43,7 @@ If `type` is of value `Kafka` the following additional attributes are defined: * `bootstrap_servers` - Comma separated list of server addresses. * `config` - A map of Kafka key-value pairs for optional configuration. This is a flat object, and keys can have '.' characters. * `security` - Properties for the secure transport connection to Kafka. For SSL, this can include the trusted certificate to use. See [security](#security). +* `networking` - Networking Access Type can either be `PUBLIC` (default) or `VPC`. See [networking](#networking). ### Authentication @@ -60,5 +61,12 @@ If `type` is of value `Kafka` the following additional attributes are defined: * `role` - The name of the role to use. Can be a built in role or a custom role. * `type` - Type of the DB role. Can be either BUILT_IN or CUSTOM. +### Networking +* `access` - Information about the networking access. See [access](#access). + +### Access +* `name` - Id of the vpc peer when the type is `VPC`. +* `type` - Networking type. Either `PUBLIC` or `VPC`. Default is `PUBLIC`. + To learn more, see: [MongoDB Atlas API - Stream Connection](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Streams/operation/listStreamConnections) Documentation. The [Terraform Provider Examples Section](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/master/examples/mongodbatlas_stream_instance/atlas-streams-user-journey.md) also contains details on the overall support for Atlas Streams Processing in Terraform. diff --git a/docs/resources/stream_connection.md b/docs/resources/stream_connection.md index 962ca1831f..7f0802a35a 100644 --- a/docs/resources/stream_connection.md +++ b/docs/resources/stream_connection.md @@ -82,6 +82,7 @@ If `type` is of value `Kafka` the following additional arguments are defined: * `bootstrap_servers` - Comma separated list of server addresses. * `config` - A map of Kafka key-value pairs for optional configuration. This is a flat object, and keys can have '.' characters. * `security` - Properties for the secure transport connection to Kafka. For SSL, this can include the trusted certificate to use. See [security](#security). +* `networking` - Networking Access Type can either be `PUBLIC` (default) or `VPC`. See [networking](#networking). ### Authentication @@ -99,6 +100,13 @@ If `type` is of value `Kafka` the following additional arguments are defined: * `role` - The name of the role to use. Can be a built in role or a custom role. * `type` - Type of the DB role. Can be either BUILT_IN or CUSTOM. +### Networking +* `access` - Information about the networking access. See [access](#access). + +### Access +* `name` - Id of the vpc peer when the type is `VPC`. +* `type` - Selected networking type. Either `PUBLIC` or `VPC`. Defaults to `PUBLIC`. + ## Import You can import a stream connection resource using the instance name, project ID, and connection name. The format must be `INSTANCE_NAME-PROJECT_ID-CONNECTION_NAME`. For example: From a0c05aa96a4254503b93f04d44d964e089cdaf53 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 2 Aug 2024 10:52:31 +0200 Subject: [PATCH 05/23] fix migration tests --- .../data_source_stream_connection_test.go | 4 +-- .../data_source_stream_connections_test.go | 4 +-- ...source_stream_connection_migration_test.go | 8 +++--- .../resource_stream_connection_test.go | 26 ++++++++++++------- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/internal/service/streamconnection/data_source_stream_connection_test.go b/internal/service/streamconnection/data_source_stream_connection_test.go index de6e62e828..302518c195 100644 --- a/internal/service/streamconnection/data_source_stream_connection_test.go +++ b/internal/service/streamconnection/data_source_stream_connection_test.go @@ -20,7 +20,7 @@ func TestAccStreamDSStreamConnection_kafkaPlaintext(t *testing.T) { CheckDestroy: CheckDestroyStreamConnection, Steps: []resource.TestStep{ { - Config: streamConnectionDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false)), + Config: streamConnectionDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, true)), Check: kafkaStreamConnectionAttributeChecks(dataSourceName, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, false), }, }, @@ -39,7 +39,7 @@ func TestAccStreamDSStreamConnection_kafkaSSL(t *testing.T) { CheckDestroy: CheckDestroyStreamConnection, Steps: []resource.TestStep{ { - Config: streamConnectionDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true)), + Config: streamConnectionDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true)), Check: kafkaStreamConnectionAttributeChecks(dataSourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, false), }, }, diff --git a/internal/service/streamconnection/data_source_stream_connections_test.go b/internal/service/streamconnection/data_source_stream_connections_test.go index ca480ae389..dabeb6a7ed 100644 --- a/internal/service/streamconnection/data_source_stream_connections_test.go +++ b/internal/service/streamconnection/data_source_stream_connections_test.go @@ -21,7 +21,7 @@ func TestAccStreamDSStreamConnections_basic(t *testing.T) { CheckDestroy: CheckDestroyStreamConnection, Steps: []resource.TestStep{ { - Config: streamConnectionsDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false)), + Config: streamConnectionsDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, true)), Check: streamConnectionsAttributeChecks(dataSourceName, nil, nil, 1), }, }, @@ -40,7 +40,7 @@ func TestAccStreamDSStreamConnections_withPageConfig(t *testing.T) { CheckDestroy: CheckDestroyStreamConnection, Steps: []resource.TestStep{ { - Config: streamConnectionsWithPageAttrDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false)), + Config: streamConnectionsWithPageAttrDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, true)), Check: streamConnectionsAttributeChecks(dataSourceName, admin.PtrInt(2), admin.PtrInt(1), 0), }, }, diff --git a/internal/service/streamconnection/resource_stream_connection_migration_test.go b/internal/service/streamconnection/resource_stream_connection_migration_test.go index eb17b4fb2e..12b6731951 100644 --- a/internal/service/streamconnection/resource_stream_connection_migration_test.go +++ b/internal/service/streamconnection/resource_stream_connection_migration_test.go @@ -24,12 +24,12 @@ func TestMigStreamRSStreamConnection_kafkaPlaintext(t *testing.T) { Steps: []resource.TestStep{ { ExternalProviders: mig.ExternalProviders(), - Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false), + Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, false), Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, true), }, { ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false), + Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, true), ConfigPlanChecks: resource.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{ acc.DebugPlan(), @@ -55,12 +55,12 @@ func TestMigStreamRSStreamConnection_kafkaSSL(t *testing.T) { Steps: []resource.TestStep{ { ExternalProviders: mig.ExternalProviders(), - Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true), + Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, false), Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), }, { ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true), + Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), ConfigPlanChecks: resource.ConfigPlanChecks{ PreApply: []plancheck.PlanCheck{ acc.DebugPlan(), diff --git a/internal/service/streamconnection/resource_stream_connection_test.go b/internal/service/streamconnection/resource_stream_connection_test.go index 56f44e4364..b8e0e10439 100644 --- a/internal/service/streamconnection/resource_stream_connection_test.go +++ b/internal/service/streamconnection/resource_stream_connection_test.go @@ -26,11 +26,11 @@ func TestAccStreamRSStreamConnection_kafkaPlaintext(t *testing.T) { CheckDestroy: CheckDestroyStreamConnection, Steps: []resource.TestStep{ { - Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false), + Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, true), Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, true), }, { - Config: kafkaStreamConnectionConfig(projectID, instanceName, "user2", "otherpassword", "localhost:9093", "latest", false), + Config: kafkaStreamConnectionConfig(projectID, instanceName, "user2", "otherpassword", "localhost:9093", "latest", false, true), Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user2", "otherpassword", "localhost:9093", "latest", false, true), }, { @@ -56,7 +56,7 @@ func TestAccStreamRSStreamConnection_kafkaSSL(t *testing.T) { CheckDestroy: CheckDestroyStreamConnection, Steps: []resource.TestStep{ { - Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true), + Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), }, { @@ -121,7 +121,7 @@ func TestAccStreamRSStreamConnection_sample(t *testing.T) { }) } -func kafkaStreamConnectionConfig(projectID, instanceName, username, password, bootstrapServers, configValue string, useSSL bool) string { +func kafkaStreamConnectionConfig(projectID, instanceName, username, password, bootstrapServers, configValue string, useSSL, useNetworking bool) string { projectAndStreamInstanceConfig := acc.StreamInstanceConfig(projectID, instanceName, "VIRGINIA_USA", "AWS") securityConfig := ` security = { @@ -136,6 +136,16 @@ func kafkaStreamConnectionConfig(projectID, instanceName, username, password, bo }`, DummyCACert) } + networkingConfig := "" + if useNetworking { + networkingConfig = ` + networking = { + access = { + type = "PUBLIC" + } + }` + } + return fmt.Sprintf(` %[1]s @@ -153,14 +163,10 @@ func kafkaStreamConnectionConfig(projectID, instanceName, username, password, bo config = { "auto.offset.reset": %[5]q } - networking = { - access = { - type = "PUBLIC" - } - } %[6]s + %[7]s } - `, projectAndStreamInstanceConfig, username, password, bootstrapServers, configValue, securityConfig) + `, projectAndStreamInstanceConfig, username, password, bootstrapServers, configValue, networkingConfig, securityConfig) } func sampleStreamConnectionConfig(projectID, instanceName, sampleName string) string { From 12ac07c12b8a411ac931dcf44f824db18f90685b Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 2 Aug 2024 11:32:47 +0200 Subject: [PATCH 06/23] include netwerking in examples for stream_connection --- examples/mongodbatlas_stream_connection/main.tf | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/mongodbatlas_stream_connection/main.tf b/examples/mongodbatlas_stream_connection/main.tf index 0dc56c6cad..31e4ebdfdf 100644 --- a/examples/mongodbatlas_stream_connection/main.tf +++ b/examples/mongodbatlas_stream_connection/main.tf @@ -36,6 +36,11 @@ resource "mongodbatlas_stream_connection" "example-kafka-plaintext" { security = { protocol = "PLAINTEXT" } + networking = { + access = { + type = "PUBLIC" + } + } } resource "mongodbatlas_stream_connection" "example-kafka-ssl" { From e4c553dad29ee74d0fe7ac1a09830b831351f6bf Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 2 Aug 2024 14:31:48 +0200 Subject: [PATCH 07/23] improve changelog entry --- .changelog/2474.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.changelog/2474.txt b/.changelog/2474.txt index bbf0a0859e..594ffed249 100644 --- a/.changelog/2474.txt +++ b/.changelog/2474.txt @@ -1,11 +1,11 @@ ```release-note:enhancement -resource/mongodbatlas_stream_connection: Supports new networking attribute +resource/mongodbatlas_stream_connection: Supports new `networking` attribute ``` ```release-note:enhancement -data-source/mongodbatlas_stream_connection: Supports new networking attribute +data-source/mongodbatlas_stream_connection: Supports new `networking` attribute ``` ```release-note:enhancement -data-source/mongodbatlas_stream_connections: Supports new networking attribute +data-source/mongodbatlas_stream_connections: Supports new `networking` attribute ``` \ No newline at end of file From e31ff04027f0099970106399743df8c4cbd3ba2a Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Fri, 2 Aug 2024 14:35:38 +0200 Subject: [PATCH 08/23] include acces.name attribute in model unit tests --- .../streamconnection/model_stream_connection_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/service/streamconnection/model_stream_connection_test.go b/internal/service/streamconnection/model_stream_connection_test.go index 2c749a5aac..a965ee2ca1 100644 --- a/internal/service/streamconnection/model_stream_connection_test.go +++ b/internal/service/streamconnection/model_stream_connection_test.go @@ -25,6 +25,7 @@ const ( dbRoleType = "CUSTOM" sampleConnectionName = "sample_stream_solar" networkingType = "PUBLIC" + accessName = "name" ) var configMap = map[string]string{ @@ -221,6 +222,7 @@ func TestStreamConnectionsSDKToTFModel(t *testing.T) { Networking: &admin.StreamsKafkaNetworking{ Access: &admin.StreamsKafkaNetworkingAccess{ Type: admin.PtrString(networkingType), + Name: admin.PtrString(accessName), }, }, }, @@ -264,7 +266,7 @@ func TestStreamConnectionsSDKToTFModel(t *testing.T) { Config: tfConfigMap(t, configMap), Security: tfSecurityObject(t, DummyCACert, securityProtocol), DBRoleToExecute: types.ObjectNull(streamconnection.DBRoleToExecuteObjectType.AttrTypes), - Networking: tfNetworkingObject(t, networkingType), + Networking: tfNetworkingObject(t, networkingType, accessName), }, { ID: types.StringValue(fmt.Sprintf("%s-%s-%s", instanceName, dummyProjectID, connectionName)), @@ -485,11 +487,12 @@ func tfDBRoleToExecuteObject(t *testing.T, role, roleType string) types.Object { return auth } -func tfNetworkingObject(t *testing.T, networkingType string) types.Object { +func tfNetworkingObject(t *testing.T, networkingType, name string) types.Object { t.Helper() networking, diags := types.ObjectValueFrom(context.Background(), streamconnection.NetworkingObjectType.AttrTypes, streamconnection.TFNetworkingModel{ Access: streamconnection.TFNetworkingAccessModel{ Type: types.StringValue(networkingType), + Name: types.StringValue(name), }, }) if diags.HasError() { From 01884a25a510aed625b0bab900b0e3f15f2a78bf Mon Sep 17 00:00:00 2001 From: EspenAlbert Date: Mon, 5 Aug 2024 10:13:33 +0100 Subject: [PATCH 09/23] fix: add PlanModifiers to avoid unexpected plan changes --- .../service/streamconnection/resource_stream_connection.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/service/streamconnection/resource_stream_connection.go b/internal/service/streamconnection/resource_stream_connection.go index f24356c628..be6f82521d 100644 --- a/internal/service/streamconnection/resource_stream_connection.go +++ b/internal/service/streamconnection/resource_stream_connection.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -105,6 +106,9 @@ func (r *streamConnectionRS) Schema(ctx context.Context, req resource.SchemaRequ Attributes: map[string]schema.Attribute{ "id": schema.StringAttribute{ Computed: true, + PlanModifiers: []planmodifier.String{ + stringplanmodifier.UseStateForUnknown(), + }, }, "project_id": schema.StringAttribute{ Required: true, @@ -187,6 +191,9 @@ func (r *streamConnectionRS) Schema(ctx context.Context, req resource.SchemaRequ "networking": schema.SingleNestedAttribute{ Optional: true, Computed: true, + PlanModifiers: []planmodifier.Object{ + objectplanmodifier.UseStateForUnknown(), + }, Attributes: map[string]schema.Attribute{ "access": schema.SingleNestedAttribute{ Required: true, From c459ae2ca37bbb833e4a2d2175f0eedea931cbc4 Mon Sep 17 00:00:00 2001 From: EspenAlbert Date: Tue, 20 Aug 2024 11:08:07 +0100 Subject: [PATCH 10/23] test: refactor the kafkaStreamConnectionConfig and use normal `mig.CreateAndRunTest` --- .../data_source_stream_connection_test.go | 4 +- .../data_source_stream_connections_test.go | 4 +- ...source_stream_connection_migration_test.go | 59 +------------- .../resource_stream_connection_test.go | 79 +++++++++++++++---- 4 files changed, 68 insertions(+), 78 deletions(-) diff --git a/internal/service/streamconnection/data_source_stream_connection_test.go b/internal/service/streamconnection/data_source_stream_connection_test.go index 302518c195..a934f7530e 100644 --- a/internal/service/streamconnection/data_source_stream_connection_test.go +++ b/internal/service/streamconnection/data_source_stream_connection_test.go @@ -20,7 +20,7 @@ func TestAccStreamDSStreamConnection_kafkaPlaintext(t *testing.T) { CheckDestroy: CheckDestroyStreamConnection, Steps: []resource.TestStep{ { - Config: streamConnectionDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, true)), + Config: streamConnectionDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", kafkaNetworkingPublic, false)), Check: kafkaStreamConnectionAttributeChecks(dataSourceName, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, false), }, }, @@ -39,7 +39,7 @@ func TestAccStreamDSStreamConnection_kafkaSSL(t *testing.T) { CheckDestroy: CheckDestroyStreamConnection, Steps: []resource.TestStep{ { - Config: streamConnectionDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true)), + Config: streamConnectionDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", kafkaNetworkingPublic, true)), Check: kafkaStreamConnectionAttributeChecks(dataSourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, false), }, }, diff --git a/internal/service/streamconnection/data_source_stream_connections_test.go b/internal/service/streamconnection/data_source_stream_connections_test.go index dabeb6a7ed..8636f50724 100644 --- a/internal/service/streamconnection/data_source_stream_connections_test.go +++ b/internal/service/streamconnection/data_source_stream_connections_test.go @@ -21,7 +21,7 @@ func TestAccStreamDSStreamConnections_basic(t *testing.T) { CheckDestroy: CheckDestroyStreamConnection, Steps: []resource.TestStep{ { - Config: streamConnectionsDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, true)), + Config: streamConnectionsDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", kafkaNetworkingPublic, false)), Check: streamConnectionsAttributeChecks(dataSourceName, nil, nil, 1), }, }, @@ -40,7 +40,7 @@ func TestAccStreamDSStreamConnections_withPageConfig(t *testing.T) { CheckDestroy: CheckDestroyStreamConnection, Steps: []resource.TestStep{ { - Config: streamConnectionsWithPageAttrDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, true)), + Config: streamConnectionsWithPageAttrDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", kafkaNetworkingPublic, false)), Check: streamConnectionsAttributeChecks(dataSourceName, admin.PtrInt(2), admin.PtrInt(1), 0), }, }, diff --git a/internal/service/streamconnection/resource_stream_connection_migration_test.go b/internal/service/streamconnection/resource_stream_connection_migration_test.go index 12b6731951..c93e2c074c 100644 --- a/internal/service/streamconnection/resource_stream_connection_migration_test.go +++ b/internal/service/streamconnection/resource_stream_connection_migration_test.go @@ -11,65 +11,8 @@ import ( ) func TestMigStreamRSStreamConnection_kafkaPlaintext(t *testing.T) { - var ( - resourceName = "mongodbatlas_stream_connection.test" - projectID = acc.ProjectIDExecution(t) - instanceName = acc.RandomName() - ) mig.SkipIfVersionBelow(t, "1.16.0") // when reached GA - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t) }, - CheckDestroy: CheckDestroyStreamConnection, - Steps: []resource.TestStep{ - { - ExternalProviders: mig.ExternalProviders(), - Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, false), - Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, true), - }, - { - ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, true), - ConfigPlanChecks: resource.ConfigPlanChecks{ - PreApply: []plancheck.PlanCheck{ - acc.DebugPlan(), - plancheck.ExpectEmptyPlan(), - }, - }, - }, - }, - }) -} - -func TestMigStreamRSStreamConnection_kafkaSSL(t *testing.T) { - var ( - resourceName = "mongodbatlas_stream_connection.test" - projectID = acc.ProjectIDExecution(t) - instanceName = acc.RandomName() - ) - mig.SkipIfVersionBelow(t, "1.16.0") // when reached GA - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t) }, - CheckDestroy: CheckDestroyStreamConnection, - Steps: []resource.TestStep{ - { - ExternalProviders: mig.ExternalProviders(), - Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, false), - Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), - }, - { - ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), - ConfigPlanChecks: resource.ConfigPlanChecks{ - PreApply: []plancheck.PlanCheck{ - acc.DebugPlan(), - plancheck.ExpectEmptyPlan(), - }, - }, - }, - }, - }) + mig.CreateAndRunTest(t, testCaseKafkaPlaintext(t)) } func TestMigStreamRSStreamConnection_cluster(t *testing.T) { diff --git a/internal/service/streamconnection/resource_stream_connection_test.go b/internal/service/streamconnection/resource_stream_connection_test.go index b8e0e10439..70c58b358c 100644 --- a/internal/service/streamconnection/resource_stream_connection_test.go +++ b/internal/service/streamconnection/resource_stream_connection_test.go @@ -13,24 +13,46 @@ import ( //go:embed testdata/dummy-ca.pem var DummyCACert string +var ( + networkingNameVPC = "vpc-096df0a10902ad9a9" + networkingTypeVPC = "VPC" + networkingTypePublic = "PUBLIC" + kafkaNetworkingVPC = fmt.Sprintf(`networking = { + access = { + type = %[1]q + name = %[2]q + } + }`, networkingTypeVPC, networkingNameVPC) + kafkaNetworkingPublic = fmt.Sprintf(`networking = { + access = { + type = %[1]q + } + }`, networkingTypePublic) +) func TestAccStreamRSStreamConnection_kafkaPlaintext(t *testing.T) { + testCase := testCaseKafkaPlaintext(t) + resource.ParallelTest(t, *testCase) +} + +func testCaseKafkaPlaintext(t *testing.T) *resource.TestCase { + t.Helper() var ( resourceName = "mongodbatlas_stream_connection.test" projectID = acc.ProjectIDExecution(t) instanceName = acc.RandomName() ) - resource.ParallelTest(t, resource.TestCase{ + return &resource.TestCase{ PreCheck: func() { acc.PreCheckBasic(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, CheckDestroy: CheckDestroyStreamConnection, Steps: []resource.TestStep{ { - Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, true), + Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", "", false), Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, true), }, { - Config: kafkaStreamConnectionConfig(projectID, instanceName, "user2", "otherpassword", "localhost:9093", "latest", false, true), + Config: kafkaStreamConnectionConfig(projectID, instanceName, "user2", "otherpassword", "localhost:9093", "latest", kafkaNetworkingPublic, false), Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user2", "otherpassword", "localhost:9093", "latest", false, true), }, { @@ -41,6 +63,42 @@ func TestAccStreamRSStreamConnection_kafkaPlaintext(t *testing.T) { ImportStateVerifyIgnore: []string{"authentication.password"}, }, }, + } +} + +func TestAccStreamRSStreamConnection_kafkaNetworkingVPC(t *testing.T) { + var ( + resourceName = "mongodbatlas_stream_connection.test" + projectID = acc.ProjectIDExecution(t) + instanceName = acc.RandomName() + ) + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acc.PreCheckBasic(t) }, + ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, + CheckDestroy: CheckDestroyStreamConnection, + Steps: []resource.TestStep{ + { + Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", kafkaNetworkingPublic, true), + Check: resource.ComposeAggregateTestCheckFunc( + kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), + resource.TestCheckResourceAttr(resourceName, "networking.access.type", networkingTypePublic), + ), + }, + { + Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", kafkaNetworkingVPC, true), + Check: resource.ComposeAggregateTestCheckFunc(kafkaStreamConnectionAttributeChecks( + resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), + resource.TestCheckResourceAttr(resourceName, "networking.access.type", networkingTypeVPC), + resource.TestCheckResourceAttr(resourceName, "networking.access.name", networkingNameVPC)), + }, + { + ResourceName: resourceName, + ImportStateIdFunc: checkStreamConnectionImportStateIDFunc(resourceName), + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"authentication.password"}, + }, + }, }) } @@ -56,7 +114,7 @@ func TestAccStreamRSStreamConnection_kafkaSSL(t *testing.T) { CheckDestroy: CheckDestroyStreamConnection, Steps: []resource.TestStep{ { - Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), + Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", networkingTypePublic, true), Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), }, { @@ -121,7 +179,7 @@ func TestAccStreamRSStreamConnection_sample(t *testing.T) { }) } -func kafkaStreamConnectionConfig(projectID, instanceName, username, password, bootstrapServers, configValue string, useSSL, useNetworking bool) string { +func kafkaStreamConnectionConfig(projectID, instanceName, username, password, bootstrapServers, configValue, networkingConfig string, useSSL bool) string { projectAndStreamInstanceConfig := acc.StreamInstanceConfig(projectID, instanceName, "VIRGINIA_USA", "AWS") securityConfig := ` security = { @@ -135,17 +193,6 @@ func kafkaStreamConnectionConfig(projectID, instanceName, username, password, bo protocol = "SSL" }`, DummyCACert) } - - networkingConfig := "" - if useNetworking { - networkingConfig = ` - networking = { - access = { - type = "PUBLIC" - } - }` - } - return fmt.Sprintf(` %[1]s From c666c9697cdee2dee64415493d659c2627cd4c0e Mon Sep 17 00:00:00 2001 From: EspenAlbert Date: Tue, 20 Aug 2024 11:12:02 +0100 Subject: [PATCH 11/23] test: refactor `testCaseCluster` and re-use in migration test --- ...source_stream_connection_migration_test.go | 31 +------------------ .../resource_stream_connection_test.go | 10 ++++-- 2 files changed, 9 insertions(+), 32 deletions(-) diff --git a/internal/service/streamconnection/resource_stream_connection_migration_test.go b/internal/service/streamconnection/resource_stream_connection_migration_test.go index c93e2c074c..de1596577f 100644 --- a/internal/service/streamconnection/resource_stream_connection_migration_test.go +++ b/internal/service/streamconnection/resource_stream_connection_migration_test.go @@ -4,9 +4,6 @@ import ( _ "embed" "testing" - "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/plancheck" - "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig" ) @@ -16,32 +13,6 @@ func TestMigStreamRSStreamConnection_kafkaPlaintext(t *testing.T) { } func TestMigStreamRSStreamConnection_cluster(t *testing.T) { - var ( - resourceName = "mongodbatlas_stream_connection.test" - projectID, clusterName = acc.ClusterNameExecution(t) - instanceName = acc.RandomName() - ) mig.SkipIfVersionBelow(t, "1.16.0") // when reached GA - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t) }, - CheckDestroy: CheckDestroyStreamConnection, - Steps: []resource.TestStep{ - { - ExternalProviders: mig.ExternalProviders(), - Config: clusterStreamConnectionConfig(projectID, instanceName, clusterName), - Check: clusterStreamConnectionAttributeChecks(resourceName, clusterName), - }, - { - ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, - Config: clusterStreamConnectionConfig(projectID, instanceName, clusterName), - ConfigPlanChecks: resource.ConfigPlanChecks{ - PreApply: []plancheck.PlanCheck{ - acc.DebugPlan(), - plancheck.ExpectEmptyPlan(), - }, - }, - }, - }, - }) + mig.CreateAndRunTest(t, testCaseCluster(t)) } diff --git a/internal/service/streamconnection/resource_stream_connection_test.go b/internal/service/streamconnection/resource_stream_connection_test.go index 70c58b358c..833db4060a 100644 --- a/internal/service/streamconnection/resource_stream_connection_test.go +++ b/internal/service/streamconnection/resource_stream_connection_test.go @@ -129,12 +129,18 @@ func TestAccStreamRSStreamConnection_kafkaSSL(t *testing.T) { } func TestAccStreamRSStreamConnection_cluster(t *testing.T) { + testCase := testCaseCluster(t) + resource.ParallelTest(t, *testCase) +} + +func testCaseCluster(t *testing.T) *resource.TestCase { + t.Helper() var ( resourceName = "mongodbatlas_stream_connection.test" projectID, clusterName = acc.ClusterNameExecution(t) instanceName = acc.RandomName() ) - resource.ParallelTest(t, resource.TestCase{ + return &resource.TestCase{ PreCheck: func() { acc.PreCheckBasic(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, CheckDestroy: CheckDestroyStreamConnection, @@ -150,7 +156,7 @@ func TestAccStreamRSStreamConnection_cluster(t *testing.T) { ImportStateVerify: true, }, }, - }) + } } func TestAccStreamRSStreamConnection_sample(t *testing.T) { From 7935642f7b0d6f4569d20a7d1b2463fb29d243d3 Mon Sep 17 00:00:00 2001 From: EspenAlbert Date: Tue, 20 Aug 2024 11:13:17 +0100 Subject: [PATCH 12/23] test: improve check using actual name --- .../service/streamconnection/resource_stream_connection_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/streamconnection/resource_stream_connection_test.go b/internal/service/streamconnection/resource_stream_connection_test.go index 833db4060a..739ddafd31 100644 --- a/internal/service/streamconnection/resource_stream_connection_test.go +++ b/internal/service/streamconnection/resource_stream_connection_test.go @@ -254,9 +254,9 @@ func kafkaStreamConnectionAttributeChecks( resourceChecks := []resource.TestCheckFunc{ checkStreamConnectionExists(), resource.TestCheckResourceAttrSet(resourceName, "project_id"), - resource.TestCheckResourceAttrSet(resourceName, "instance_name"), resource.TestCheckResourceAttrSet(resourceName, "connection_name"), resource.TestCheckResourceAttr(resourceName, "type", "Kafka"), + resource.TestCheckResourceAttr(resourceName, "instance_name", instanceName), resource.TestCheckResourceAttr(resourceName, "authentication.mechanism", "PLAIN"), resource.TestCheckResourceAttr(resourceName, "authentication.username", username), resource.TestCheckResourceAttr(resourceName, "bootstrap_servers", bootstrapServers), From 74a3e3ea85c83770032dbbaf956d81e85934a012 Mon Sep 17 00:00:00 2001 From: EspenAlbert Date: Tue, 20 Aug 2024 11:16:17 +0100 Subject: [PATCH 13/23] chore: add revertable commit to avoid pending resources in CI --- .../streamconnection/resource_stream_connection_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/service/streamconnection/resource_stream_connection_test.go b/internal/service/streamconnection/resource_stream_connection_test.go index 739ddafd31..d31053f3bf 100644 --- a/internal/service/streamconnection/resource_stream_connection_test.go +++ b/internal/service/streamconnection/resource_stream_connection_test.go @@ -5,6 +5,7 @@ import ( _ "embed" "fmt" "testing" + "time" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" @@ -65,6 +66,10 @@ func testCaseKafkaPlaintext(t *testing.T) *resource.TestCase { }, } } +func DummySleep(*terraform.State) error { + time.Sleep(60 * time.Second) + return nil +} func TestAccStreamRSStreamConnection_kafkaNetworkingVPC(t *testing.T) { var ( @@ -86,7 +91,7 @@ func TestAccStreamRSStreamConnection_kafkaNetworkingVPC(t *testing.T) { }, { Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", kafkaNetworkingVPC, true), - Check: resource.ComposeAggregateTestCheckFunc(kafkaStreamConnectionAttributeChecks( + Check: resource.ComposeAggregateTestCheckFunc(DummySleep, kafkaStreamConnectionAttributeChecks( resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), resource.TestCheckResourceAttr(resourceName, "networking.access.type", networkingTypeVPC), resource.TestCheckResourceAttr(resourceName, "networking.access.name", networkingNameVPC)), From fa4dc53b23834ce88cd5775b67827bd2f26c57dc Mon Sep 17 00:00:00 2001 From: EspenAlbert Date: Mon, 26 Aug 2024 09:52:13 +0100 Subject: [PATCH 14/23] refactor: remove `networking.access.name` attribute (tests will fail until blocked issue is resolved) --- .../service/streamconnection/model_stream_connection.go | 2 -- .../streamconnection/model_stream_connection_test.go | 7 ++----- .../service/streamconnection/resource_stream_connection.go | 5 ----- .../streamconnection/resource_stream_connection_test.go | 6 ++---- 4 files changed, 4 insertions(+), 16 deletions(-) diff --git a/internal/service/streamconnection/model_stream_connection.go b/internal/service/streamconnection/model_stream_connection.go index 974a5fcff4..e7f1f73128 100644 --- a/internal/service/streamconnection/model_stream_connection.go +++ b/internal/service/streamconnection/model_stream_connection.go @@ -67,7 +67,6 @@ func NewStreamConnectionReq(ctx context.Context, plan *TFStreamConnectionModel) } streamConnection.Networking = &admin.StreamsKafkaNetworking{ Access: &admin.StreamsKafkaNetworkingAccess{ - Name: networkingModel.Access.Name.ValueStringPointer(), Type: networkingModel.Access.Type.ValueStringPointer(), }, } @@ -131,7 +130,6 @@ func NewTFStreamConnection(ctx context.Context, projID, instanceName string, cur if apiResp.Networking != nil { networkingModel, diags := types.ObjectValueFrom(ctx, NetworkingObjectType.AttrTypes, TFNetworkingModel{ Access: TFNetworkingAccessModel{ - Name: types.StringPointerValue(apiResp.Networking.Access.Name), Type: types.StringPointerValue(apiResp.Networking.Access.Type), }, }) diff --git a/internal/service/streamconnection/model_stream_connection_test.go b/internal/service/streamconnection/model_stream_connection_test.go index a965ee2ca1..2c749a5aac 100644 --- a/internal/service/streamconnection/model_stream_connection_test.go +++ b/internal/service/streamconnection/model_stream_connection_test.go @@ -25,7 +25,6 @@ const ( dbRoleType = "CUSTOM" sampleConnectionName = "sample_stream_solar" networkingType = "PUBLIC" - accessName = "name" ) var configMap = map[string]string{ @@ -222,7 +221,6 @@ func TestStreamConnectionsSDKToTFModel(t *testing.T) { Networking: &admin.StreamsKafkaNetworking{ Access: &admin.StreamsKafkaNetworkingAccess{ Type: admin.PtrString(networkingType), - Name: admin.PtrString(accessName), }, }, }, @@ -266,7 +264,7 @@ func TestStreamConnectionsSDKToTFModel(t *testing.T) { Config: tfConfigMap(t, configMap), Security: tfSecurityObject(t, DummyCACert, securityProtocol), DBRoleToExecute: types.ObjectNull(streamconnection.DBRoleToExecuteObjectType.AttrTypes), - Networking: tfNetworkingObject(t, networkingType, accessName), + Networking: tfNetworkingObject(t, networkingType), }, { ID: types.StringValue(fmt.Sprintf("%s-%s-%s", instanceName, dummyProjectID, connectionName)), @@ -487,12 +485,11 @@ func tfDBRoleToExecuteObject(t *testing.T, role, roleType string) types.Object { return auth } -func tfNetworkingObject(t *testing.T, networkingType, name string) types.Object { +func tfNetworkingObject(t *testing.T, networkingType string) types.Object { t.Helper() networking, diags := types.ObjectValueFrom(context.Background(), streamconnection.NetworkingObjectType.AttrTypes, streamconnection.TFNetworkingModel{ Access: streamconnection.TFNetworkingAccessModel{ Type: types.StringValue(networkingType), - Name: types.StringValue(name), }, }) if diags.HasError() { diff --git a/internal/service/streamconnection/resource_stream_connection.go b/internal/service/streamconnection/resource_stream_connection.go index be6f82521d..e9c9e67f86 100644 --- a/internal/service/streamconnection/resource_stream_connection.go +++ b/internal/service/streamconnection/resource_stream_connection.go @@ -84,12 +84,10 @@ var DBRoleToExecuteObjectType = types.ObjectType{AttrTypes: map[string]attr.Type }} type TFNetworkingAccessModel struct { - Name types.String `tfsdk:"name"` Type types.String `tfsdk:"type"` } var NetworkingAccessObjectType = types.ObjectType{AttrTypes: map[string]attr.Type{ - "name": types.StringType, "type": types.StringType, }} @@ -198,9 +196,6 @@ func (r *streamConnectionRS) Schema(ctx context.Context, req resource.SchemaRequ "access": schema.SingleNestedAttribute{ Required: true, Attributes: map[string]schema.Attribute{ - "name": schema.StringAttribute{ - Optional: true, - }, "type": schema.StringAttribute{ Required: true, }, diff --git a/internal/service/streamconnection/resource_stream_connection_test.go b/internal/service/streamconnection/resource_stream_connection_test.go index d31053f3bf..ede5911db2 100644 --- a/internal/service/streamconnection/resource_stream_connection_test.go +++ b/internal/service/streamconnection/resource_stream_connection_test.go @@ -15,15 +15,13 @@ import ( //go:embed testdata/dummy-ca.pem var DummyCACert string var ( - networkingNameVPC = "vpc-096df0a10902ad9a9" networkingTypeVPC = "VPC" networkingTypePublic = "PUBLIC" kafkaNetworkingVPC = fmt.Sprintf(`networking = { access = { type = %[1]q - name = %[2]q } - }`, networkingTypeVPC, networkingNameVPC) + }`, networkingTypeVPC) kafkaNetworkingPublic = fmt.Sprintf(`networking = { access = { type = %[1]q @@ -94,7 +92,7 @@ func TestAccStreamRSStreamConnection_kafkaNetworkingVPC(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc(DummySleep, kafkaStreamConnectionAttributeChecks( resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), resource.TestCheckResourceAttr(resourceName, "networking.access.type", networkingTypeVPC), - resource.TestCheckResourceAttr(resourceName, "networking.access.name", networkingNameVPC)), + ), }, { ResourceName: resourceName, From e4c2a82065cfbc24fa8a627bde584721e4a331bc Mon Sep 17 00:00:00 2001 From: EspenAlbert Date: Mon, 26 Aug 2024 12:56:29 +0100 Subject: [PATCH 15/23] chore: remove name also from data source schema --- .../service/streamconnection/data_source_stream_connection.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/internal/service/streamconnection/data_source_stream_connection.go b/internal/service/streamconnection/data_source_stream_connection.go index c6c1a5f668..aa5967ed1c 100644 --- a/internal/service/streamconnection/data_source_stream_connection.go +++ b/internal/service/streamconnection/data_source_stream_connection.go @@ -109,9 +109,6 @@ func DSAttributes(withArguments bool) map[string]schema.Attribute { "access": schema.SingleNestedAttribute{ Computed: true, Attributes: map[string]schema.Attribute{ - "name": schema.StringAttribute{ - Computed: true, - }, "type": schema.StringAttribute{ Computed: true, }, From a20d77afd26ffca41ec21075214dde870000b4a5 Mon Sep 17 00:00:00 2001 From: EspenAlbert Date: Mon, 26 Aug 2024 13:55:18 +0100 Subject: [PATCH 16/23] test: fix wrong parameter for kafkaNetworking --- .../service/streamconnection/resource_stream_connection_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/streamconnection/resource_stream_connection_test.go b/internal/service/streamconnection/resource_stream_connection_test.go index ede5911db2..5a57ddace0 100644 --- a/internal/service/streamconnection/resource_stream_connection_test.go +++ b/internal/service/streamconnection/resource_stream_connection_test.go @@ -117,7 +117,7 @@ func TestAccStreamRSStreamConnection_kafkaSSL(t *testing.T) { CheckDestroy: CheckDestroyStreamConnection, Steps: []resource.TestStep{ { - Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", networkingTypePublic, true), + Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", kafkaNetworkingPublic, true), Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), }, { From 41371ba451b0d38bc7e860f1ae4b1b5047af24b3 Mon Sep 17 00:00:00 2001 From: Oriol Arbusi Date: Wed, 18 Sep 2024 11:32:25 +0200 Subject: [PATCH 17/23] clean up test after merge --- ...source_stream_connection_migration_test.go | 45 +------------------ 1 file changed, 1 insertion(+), 44 deletions(-) diff --git a/internal/service/streamconnection/resource_stream_connection_migration_test.go b/internal/service/streamconnection/resource_stream_connection_migration_test.go index 74844dbc96..de1596577f 100644 --- a/internal/service/streamconnection/resource_stream_connection_migration_test.go +++ b/internal/service/streamconnection/resource_stream_connection_migration_test.go @@ -4,8 +4,6 @@ import ( _ "embed" "testing" - "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig" ) @@ -14,48 +12,7 @@ func TestMigStreamRSStreamConnection_kafkaPlaintext(t *testing.T) { mig.CreateAndRunTest(t, testCaseKafkaPlaintext(t)) } -func TestMigStreamRSStreamConnection_kafkaSSL(t *testing.T) { - var ( - resourceName = "mongodbatlas_stream_connection.test" - projectID = acc.ProjectIDExecution(t) - instanceName = acc.RandomName() - config = kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", "", true) - ) - mig.SkipIfVersionBelow(t, "1.16.0") // when reached GA - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t) }, - CheckDestroy: CheckDestroyStreamConnection, - Steps: []resource.TestStep{ - { - ExternalProviders: mig.ExternalProviders(), - Config: config, - Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), - }, - mig.TestStepCheckEmptyPlan(config), - }, - }) -} - func TestMigStreamRSStreamConnection_cluster(t *testing.T) { - var ( - resourceName = "mongodbatlas_stream_connection.test" - projectID, clusterName = acc.ClusterNameExecution(t) - instanceName = acc.RandomName() - config = clusterStreamConnectionConfig(projectID, instanceName, clusterName) - ) mig.SkipIfVersionBelow(t, "1.16.0") // when reached GA - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t) }, - CheckDestroy: CheckDestroyStreamConnection, - Steps: []resource.TestStep{ - { - ExternalProviders: mig.ExternalProviders(), - Config: config, - Check: clusterStreamConnectionAttributeChecks(resourceName, clusterName), - }, - mig.TestStepCheckEmptyPlan(config), - }, - }) + mig.CreateAndRunTest(t, testCaseCluster(t)) } From 6f35f2a4e9b54f4d296ad6e55b085b767039861a Mon Sep 17 00:00:00 2001 From: Espen Albert Date: Mon, 23 Dec 2024 09:19:46 +0100 Subject: [PATCH 18/23] move into var block --- .../streamconnection/resource_stream_connection_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/streamconnection/resource_stream_connection_test.go b/internal/service/streamconnection/resource_stream_connection_test.go index 5a57ddace0..5d1cc5e756 100644 --- a/internal/service/streamconnection/resource_stream_connection_test.go +++ b/internal/service/streamconnection/resource_stream_connection_test.go @@ -12,9 +12,9 @@ import ( "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" ) -//go:embed testdata/dummy-ca.pem -var DummyCACert string var ( + //go:embed testdata/dummy-ca.pem + DummyCACert string networkingTypeVPC = "VPC" networkingTypePublic = "PUBLIC" kafkaNetworkingVPC = fmt.Sprintf(`networking = { From 0f965c50b5d71792eb9e730a74600c2078f21b10 Mon Sep 17 00:00:00 2001 From: Espen Albert Date: Mon, 23 Dec 2024 09:21:13 +0100 Subject: [PATCH 19/23] address PR comments --- .changelog/2474.txt | 6 +++--- .../streamconnection/resource_stream_connection_test.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.changelog/2474.txt b/.changelog/2474.txt index 594ffed249..fc7943c4a1 100644 --- a/.changelog/2474.txt +++ b/.changelog/2474.txt @@ -1,11 +1,11 @@ ```release-note:enhancement -resource/mongodbatlas_stream_connection: Supports new `networking` attribute +resource/mongodbatlas_stream_connection: Adds `networking` attribute ``` ```release-note:enhancement -data-source/mongodbatlas_stream_connection: Supports new `networking` attribute +data-source/mongodbatlas_stream_connection: Adds `networking` attribute ``` ```release-note:enhancement -data-source/mongodbatlas_stream_connections: Supports new `networking` attribute +data-source/mongodbatlas_stream_connections: Adds `networking` attribute ``` \ No newline at end of file diff --git a/internal/service/streamconnection/resource_stream_connection_test.go b/internal/service/streamconnection/resource_stream_connection_test.go index 5d1cc5e756..2310f048da 100644 --- a/internal/service/streamconnection/resource_stream_connection_test.go +++ b/internal/service/streamconnection/resource_stream_connection_test.go @@ -14,7 +14,7 @@ import ( var ( //go:embed testdata/dummy-ca.pem - DummyCACert string + DummyCACert string networkingTypeVPC = "VPC" networkingTypePublic = "PUBLIC" kafkaNetworkingVPC = fmt.Sprintf(`networking = { From 86e0e357d9ce9399394228edea56023fc88269e4 Mon Sep 17 00:00:00 2001 From: Espen Albert Date: Mon, 23 Dec 2024 09:25:39 +0100 Subject: [PATCH 20/23] test: remove old sleep function from stream connection tests --- .../streamconnection/resource_stream_connection_test.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/internal/service/streamconnection/resource_stream_connection_test.go b/internal/service/streamconnection/resource_stream_connection_test.go index 2310f048da..8ce86d0710 100644 --- a/internal/service/streamconnection/resource_stream_connection_test.go +++ b/internal/service/streamconnection/resource_stream_connection_test.go @@ -5,7 +5,6 @@ import ( _ "embed" "fmt" "testing" - "time" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" @@ -64,10 +63,6 @@ func testCaseKafkaPlaintext(t *testing.T) *resource.TestCase { }, } } -func DummySleep(*terraform.State) error { - time.Sleep(60 * time.Second) - return nil -} func TestAccStreamRSStreamConnection_kafkaNetworkingVPC(t *testing.T) { var ( @@ -89,7 +84,7 @@ func TestAccStreamRSStreamConnection_kafkaNetworkingVPC(t *testing.T) { }, { Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", kafkaNetworkingVPC, true), - Check: resource.ComposeAggregateTestCheckFunc(DummySleep, kafkaStreamConnectionAttributeChecks( + Check: resource.ComposeAggregateTestCheckFunc(kafkaStreamConnectionAttributeChecks( resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), resource.TestCheckResourceAttr(resourceName, "networking.access.type", networkingTypeVPC), ), From 6a0aeecb6db576a633752a63515e59754783397b Mon Sep 17 00:00:00 2001 From: Espen Albert Date: Mon, 23 Dec 2024 09:37:52 +0100 Subject: [PATCH 21/23] test: refactor networking.access.type check to all test cases --- .../data_source_stream_connection_test.go | 4 ++-- .../resource_stream_connection_test.go | 17 +++++++---------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/internal/service/streamconnection/data_source_stream_connection_test.go b/internal/service/streamconnection/data_source_stream_connection_test.go index a934f7530e..612f7c8779 100644 --- a/internal/service/streamconnection/data_source_stream_connection_test.go +++ b/internal/service/streamconnection/data_source_stream_connection_test.go @@ -21,7 +21,7 @@ func TestAccStreamDSStreamConnection_kafkaPlaintext(t *testing.T) { Steps: []resource.TestStep{ { Config: streamConnectionDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", kafkaNetworkingPublic, false)), - Check: kafkaStreamConnectionAttributeChecks(dataSourceName, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, false), + Check: kafkaStreamConnectionAttributeChecks(dataSourceName, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", networkingTypePublic, false, false), }, }, }) @@ -40,7 +40,7 @@ func TestAccStreamDSStreamConnection_kafkaSSL(t *testing.T) { Steps: []resource.TestStep{ { Config: streamConnectionDataSourceConfig(kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", kafkaNetworkingPublic, true)), - Check: kafkaStreamConnectionAttributeChecks(dataSourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, false), + Check: kafkaStreamConnectionAttributeChecks(dataSourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", networkingTypePublic, true, false), }, }, }) diff --git a/internal/service/streamconnection/resource_stream_connection_test.go b/internal/service/streamconnection/resource_stream_connection_test.go index 8ce86d0710..d4f8ed14a3 100644 --- a/internal/service/streamconnection/resource_stream_connection_test.go +++ b/internal/service/streamconnection/resource_stream_connection_test.go @@ -47,11 +47,11 @@ func testCaseKafkaPlaintext(t *testing.T) *resource.TestCase { Steps: []resource.TestStep{ { Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", "", false), - Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", false, true), + Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092,localhost:9092", "earliest", networkingTypePublic, false, true), }, { Config: kafkaStreamConnectionConfig(projectID, instanceName, "user2", "otherpassword", "localhost:9093", "latest", kafkaNetworkingPublic, false), - Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user2", "otherpassword", "localhost:9093", "latest", false, true), + Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user2", "otherpassword", "localhost:9093", "latest", networkingTypePublic, false, true), }, { ResourceName: resourceName, @@ -78,16 +78,12 @@ func TestAccStreamRSStreamConnection_kafkaNetworkingVPC(t *testing.T) { { Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", kafkaNetworkingPublic, true), Check: resource.ComposeAggregateTestCheckFunc( - kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), - resource.TestCheckResourceAttr(resourceName, "networking.access.type", networkingTypePublic), + kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", networkingTypePublic, true, true), ), }, { Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", kafkaNetworkingVPC, true), - Check: resource.ComposeAggregateTestCheckFunc(kafkaStreamConnectionAttributeChecks( - resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), - resource.TestCheckResourceAttr(resourceName, "networking.access.type", networkingTypeVPC), - ), + Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", networkingTypeVPC, true, true), }, { ResourceName: resourceName, @@ -113,7 +109,7 @@ func TestAccStreamRSStreamConnection_kafkaSSL(t *testing.T) { Steps: []resource.TestStep{ { Config: kafkaStreamConnectionConfig(projectID, instanceName, "user", "rawpassword", "localhost:9092", "earliest", kafkaNetworkingPublic, true), - Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", true, true), + Check: kafkaStreamConnectionAttributeChecks(resourceName, instanceName, "user", "rawpassword", "localhost:9092", "earliest", networkingTypePublic, true, true), }, { ResourceName: resourceName, @@ -248,7 +244,7 @@ func sampleStreamConnectionAttributeChecks( } func kafkaStreamConnectionAttributeChecks( - resourceName, instanceName, username, password, bootstrapServers, configValue string, usesSSL, checkPassword bool) resource.TestCheckFunc { + resourceName, instanceName, username, password, bootstrapServers, configValue, networkingType string, usesSSL, checkPassword bool) resource.TestCheckFunc { resourceChecks := []resource.TestCheckFunc{ checkStreamConnectionExists(), resource.TestCheckResourceAttrSet(resourceName, "project_id"), @@ -259,6 +255,7 @@ func kafkaStreamConnectionAttributeChecks( resource.TestCheckResourceAttr(resourceName, "authentication.username", username), resource.TestCheckResourceAttr(resourceName, "bootstrap_servers", bootstrapServers), resource.TestCheckResourceAttr(resourceName, "config.auto.offset.reset", configValue), + resource.TestCheckResourceAttr(resourceName, "networking.access.type", networkingType), } if checkPassword { resourceChecks = append(resourceChecks, resource.TestCheckResourceAttr(resourceName, "authentication.password", password)) From e49fa85c3e108779419df219b0f9ad04e1e5999e Mon Sep 17 00:00:00 2001 From: Espen Albert Date: Mon, 23 Dec 2024 10:07:36 +0100 Subject: [PATCH 22/23] test: conditionally check networking.access.type based on provider version --- .../streamconnection/resource_stream_connection_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/service/streamconnection/resource_stream_connection_test.go b/internal/service/streamconnection/resource_stream_connection_test.go index d4f8ed14a3..f7cebd7074 100644 --- a/internal/service/streamconnection/resource_stream_connection_test.go +++ b/internal/service/streamconnection/resource_stream_connection_test.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" + "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig" ) var ( @@ -255,7 +256,9 @@ func kafkaStreamConnectionAttributeChecks( resource.TestCheckResourceAttr(resourceName, "authentication.username", username), resource.TestCheckResourceAttr(resourceName, "bootstrap_servers", bootstrapServers), resource.TestCheckResourceAttr(resourceName, "config.auto.offset.reset", configValue), - resource.TestCheckResourceAttr(resourceName, "networking.access.type", networkingType), + } + if mig.IsProviderVersionAtLeast("1.25.0") { + resourceChecks = append(resourceChecks, resource.TestCheckResourceAttr(resourceName, "networking.access.type", networkingType)) } if checkPassword { resourceChecks = append(resourceChecks, resource.TestCheckResourceAttr(resourceName, "authentication.password", password)) From 7fe005e86803bba0a58e62f72452ff2723bef6fd Mon Sep 17 00:00:00 2001 From: Espen Albert Date: Mon, 23 Dec 2024 11:59:23 +0100 Subject: [PATCH 23/23] feat: implement DeleteStreamConnection with retry logic and add tests --- .../resource_stream_connection.go | 3 +- .../streamconnection/state_transition.go | 25 +++++++++ .../streamconnection/state_transition_test.go | 51 +++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 internal/service/streamconnection/state_transition.go create mode 100644 internal/service/streamconnection/state_transition_test.go diff --git a/internal/service/streamconnection/resource_stream_connection.go b/internal/service/streamconnection/resource_stream_connection.go index 5d30df56a9..56ac82bb2f 100644 --- a/internal/service/streamconnection/resource_stream_connection.go +++ b/internal/service/streamconnection/resource_stream_connection.go @@ -5,6 +5,7 @@ import ( "errors" "net/http" "regexp" + "time" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/path" @@ -199,7 +200,7 @@ func (r *streamConnectionRS) Delete(ctx context.Context, req resource.DeleteRequ projectID := streamConnectionState.ProjectID.ValueString() instanceName := streamConnectionState.InstanceName.ValueString() connectionName := streamConnectionState.ConnectionName.ValueString() - if _, _, err := connV2.StreamsApi.DeleteStreamConnection(ctx, projectID, instanceName, connectionName).Execute(); err != nil { + if err := DeleteStreamConnection(ctx, connV2.StreamsApi, projectID, instanceName, connectionName, time.Minute); err != nil { resp.Diagnostics.AddError("error deleting resource", err.Error()) return } diff --git a/internal/service/streamconnection/state_transition.go b/internal/service/streamconnection/state_transition.go new file mode 100644 index 0000000000..6a21ca9b49 --- /dev/null +++ b/internal/service/streamconnection/state_transition.go @@ -0,0 +1,25 @@ +package streamconnection + +import ( + "context" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" + "go.mongodb.org/atlas-sdk/v20241113003/admin" +) + +func DeleteStreamConnection(ctx context.Context, api admin.StreamsApi, projectID, instanceName, connectionName string, timeout time.Duration) error { + return retry.RetryContext(ctx, timeout, func() *retry.RetryError { + _, resp, err := api.DeleteStreamConnection(ctx, projectID, instanceName, connectionName).Execute() + if err == nil { + return nil + } + if admin.IsErrorCode(err, "STREAM_KAFKA_CONNECTION_IS_DEPLOYING") { + return retry.RetryableError(err) + } + if resp != nil && resp.StatusCode == 404 { + return nil + } + return retry.NonRetryableError(err) + }) +} diff --git a/internal/service/streamconnection/state_transition_test.go b/internal/service/streamconnection/state_transition_test.go new file mode 100644 index 0000000000..e42a263e95 --- /dev/null +++ b/internal/service/streamconnection/state_transition_test.go @@ -0,0 +1,51 @@ +package streamconnection_test + +import ( + "context" + "net/http" + "testing" + "time" + + "go.mongodb.org/atlas-sdk/v20241113003/admin" + "go.mongodb.org/atlas-sdk/v20241113003/mockadmin" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + + "github.com/mongodb/terraform-provider-mongodbatlas/internal/service/streamconnection" +) + +func TestStreamConnectionDeletion(t *testing.T) { + var ( + m = mockadmin.NewStreamsApi(t) + projectID = "projectID" + instanceName = "instanceName" + connectionName = "connectionName" + errDeleteInProgress = admin.ApiError{ + ErrorCode: "STREAM_KAFKA_CONNECTION_IS_DEPLOYING", + Error: 409, + } + genericErr = admin.GenericOpenAPIError{} + ) + genericErr.SetError("error") + genericErr.SetModel(errDeleteInProgress) + m.EXPECT().DeleteStreamConnection(mock.Anything, projectID, instanceName, connectionName).Return(admin.DeleteStreamConnectionApiRequest{ApiService: m}).Times(3) + m.EXPECT().DeleteStreamConnectionExecute(mock.Anything).Once().Return(nil, nil, &genericErr) + m.EXPECT().DeleteStreamConnectionExecute(mock.Anything).Once().Return(nil, nil, &genericErr) + m.EXPECT().DeleteStreamConnectionExecute(mock.Anything).Once().Return(nil, nil, nil) + err := streamconnection.DeleteStreamConnection(context.Background(), m, projectID, instanceName, connectionName, time.Minute) + assert.NoError(t, err) +} + +func TestStreamConnectionDeletion404(t *testing.T) { + var ( + m = mockadmin.NewStreamsApi(t) + projectID = "projectID" + instanceName = "instanceName" + connectionName = "connectionName" + ) + m.EXPECT().DeleteStreamConnection(mock.Anything, projectID, instanceName, connectionName).Return(admin.DeleteStreamConnectionApiRequest{ApiService: m}).Once() + m.EXPECT().DeleteStreamConnectionExecute(mock.Anything).Once().Return(nil, &http.Response{StatusCode: 404}, nil) + err := streamconnection.DeleteStreamConnection(context.Background(), m, projectID, instanceName, connectionName, time.Minute) + assert.NoError(t, err) +}