Skip to content

Commit bdeb3ef

Browse files
committed
fix ds schema and model tests
1 parent 4530ef5 commit bdeb3ef

File tree

4 files changed

+57
-10
lines changed

4 files changed

+57
-10
lines changed

internal/service/streamconnection/data_source_stream_connection.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ package streamconnection
33
import (
44
"context"
55

6+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
67
"github.com/hashicorp/terraform-plugin-framework/datasource"
8+
dsschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
9+
"github.com/hashicorp/terraform-plugin-framework/path"
10+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
711
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
812
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
913
)
@@ -26,6 +30,26 @@ type streamConnectionDS struct {
2630
func (d *streamConnectionDS) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
2731
resp.Schema = conversion.DataSourceSchemaFromResource(ResourceSchema(ctx), &conversion.DataSourceSchemaRequest{
2832
RequiredFields: []string{"project_id", "connection_name"},
33+
OverridenFields: map[string]dsschema.Attribute{
34+
"instance_name": dsschema.StringAttribute{
35+
Optional: true,
36+
MarkdownDescription: "Human-readable label that identifies the stream instance. Conflicts with `workspace_name`.",
37+
Validators: []validator.String{
38+
stringvalidator.ConflictsWith(path.Expressions{
39+
path.MatchRelative().AtParent().AtName("workspace_name"),
40+
}...),
41+
},
42+
},
43+
"workspace_name": dsschema.StringAttribute{
44+
Optional: true,
45+
MarkdownDescription: "Human-readable label that identifies the stream instance. This is an alias for `instance_name`. Conflicts with `instance_name`.",
46+
Validators: []validator.String{
47+
stringvalidator.ConflictsWith(path.Expressions{
48+
path.MatchRelative().AtParent().AtName("instance_name"),
49+
}...),
50+
},
51+
},
52+
},
2953
})
3054
}
3155

internal/service/streamconnection/data_source_stream_connections.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
78
"github.com/hashicorp/terraform-plugin-framework/datasource"
9+
dsschema "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
10+
"github.com/hashicorp/terraform-plugin-framework/path"
11+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
812
"github.com/hashicorp/terraform-plugin-framework/types"
913
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
1014
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
@@ -30,6 +34,26 @@ func (d *streamConnectionsDS) Schema(ctx context.Context, req datasource.SchemaR
3034
resp.Schema = conversion.PluralDataSourceSchemaFromResource(ResourceSchema(ctx), &conversion.PluralDataSourceSchemaRequest{
3135
RequiredFields: []string{"project_id"},
3236
HasLegacyFields: true,
37+
OverridenRootFields: map[string]dsschema.Attribute{
38+
"instance_name": dsschema.StringAttribute{
39+
Optional: true,
40+
MarkdownDescription: "Human-readable label that identifies the stream instance. Conflicts with `workspace_name`.",
41+
Validators: []validator.String{
42+
stringvalidator.ConflictsWith(path.Expressions{
43+
path.MatchRelative().AtParent().AtName("workspace_name"),
44+
}...),
45+
},
46+
},
47+
"workspace_name": dsschema.StringAttribute{
48+
Optional: true,
49+
MarkdownDescription: "Human-readable label that identifies the stream instance. This is an alias for `instance_name`. Conflicts with `instance_name`.",
50+
Validators: []validator.String{
51+
stringvalidator.ConflictsWith(path.Expressions{
52+
path.MatchRelative().AtParent().AtName("instance_name"),
53+
}...),
54+
},
55+
},
56+
},
3357
})
3458
}
3559

internal/service/streamconnection/model_stream_connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func NewTFStreamConnection(ctx context.Context, projID, workspaceName string, cu
128128
}
129129

130130
// determines if the original model was created with instance_name or workspace_name and sets the appropriate field
131-
func NewTFStreamConnectionWithInstanceName(ctx context.Context, projID, instanceName string, workspaceName string, currAuthConfig *types.Object, apiResp *admin.StreamsConnection) (*TFStreamConnectionModel, diag.Diagnostics) {
131+
func NewTFStreamConnectionWithInstanceName(ctx context.Context, projID, instanceName, workspaceName string, currAuthConfig *types.Object, apiResp *admin.StreamsConnection) (*TFStreamConnectionModel, diag.Diagnostics) {
132132
rID := fmt.Sprintf("%s-%s-%s", instanceName, projID, conversion.SafeString(apiResp.Name))
133133
connectionModel := TFStreamConnectionModel{
134134
ID: types.StringValue(rID),

internal/service/streamconnection/model_stream_connection_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestStreamConnectionSDKToTFModel(t *testing.T) {
7878
providedAuthConfig: nil,
7979
expectedTFModel: &streamconnection.TFStreamConnectionModel{
8080
ProjectID: types.StringValue(dummyProjectID),
81-
InstanceName: types.StringValue(instanceName),
81+
WorkspaceName: types.StringValue(instanceName),
8282
ConnectionName: types.StringValue(connectionName),
8383
Type: types.StringValue("Cluster"),
8484
ClusterName: types.StringValue(clusterName),
@@ -108,7 +108,7 @@ func TestStreamConnectionSDKToTFModel(t *testing.T) {
108108
providedAuthConfig: nil,
109109
expectedTFModel: &streamconnection.TFStreamConnectionModel{
110110
ProjectID: types.StringValue(dummyProjectID),
111-
InstanceName: types.StringValue(instanceName),
111+
WorkspaceName: types.StringValue(instanceName),
112112
ConnectionName: types.StringValue(connectionName),
113113
Type: types.StringValue("Cluster"),
114114
ClusterName: types.StringValue(clusterName),
@@ -143,7 +143,7 @@ func TestStreamConnectionSDKToTFModel(t *testing.T) {
143143
providedAuthConfig: &authConfigWithPasswordDefined,
144144
expectedTFModel: &streamconnection.TFStreamConnectionModel{
145145
ProjectID: types.StringValue(dummyProjectID),
146-
InstanceName: types.StringValue(instanceName),
146+
WorkspaceName: types.StringValue(instanceName),
147147
ConnectionName: types.StringValue(connectionName),
148148
Type: types.StringValue("Kafka"),
149149
Authentication: tfAuthenticationObject(t, authMechanism, authUsername, "raw password"), // password value is obtained from config, not api resp.
@@ -181,7 +181,7 @@ func TestStreamConnectionSDKToTFModel(t *testing.T) {
181181
providedAuthConfig: &authConfigWithOAuth,
182182
expectedTFModel: &streamconnection.TFStreamConnectionModel{
183183
ProjectID: types.StringValue(dummyProjectID),
184-
InstanceName: types.StringValue(instanceName),
184+
WorkspaceName: types.StringValue(instanceName),
185185
ConnectionName: types.StringValue(connectionName),
186186
Type: types.StringValue("Kafka"),
187187
Authentication: tfAuthenticationObjectForOAuth(t, authMechanismOAuth, clientID, clientSecret, tokenEndpointURL, scope, saslOauthbearerExtentions, httpsCaPem), // password value is obtained from config, not api resp.
@@ -205,7 +205,7 @@ func TestStreamConnectionSDKToTFModel(t *testing.T) {
205205
providedAuthConfig: nil,
206206
expectedTFModel: &streamconnection.TFStreamConnectionModel{
207207
ProjectID: types.StringValue(dummyProjectID),
208-
InstanceName: types.StringValue(instanceName),
208+
WorkspaceName: types.StringValue(instanceName),
209209
ConnectionName: types.StringValue(connectionName),
210210
Type: types.StringValue("Kafka"),
211211
Authentication: types.ObjectNull(streamconnection.ConnectionAuthenticationObjectType.AttrTypes),
@@ -238,7 +238,7 @@ func TestStreamConnectionSDKToTFModel(t *testing.T) {
238238
providedAuthConfig: nil,
239239
expectedTFModel: &streamconnection.TFStreamConnectionModel{
240240
ProjectID: types.StringValue(dummyProjectID),
241-
InstanceName: types.StringValue(instanceName),
241+
WorkspaceName: types.StringValue(instanceName),
242242
ConnectionName: types.StringValue(connectionName),
243243
Type: types.StringValue("Kafka"),
244244
Authentication: tfAuthenticationObjectWithNoPassword(t, authMechanism, authUsername),
@@ -261,7 +261,7 @@ func TestStreamConnectionSDKToTFModel(t *testing.T) {
261261
providedInstanceName: instanceName,
262262
expectedTFModel: &streamconnection.TFStreamConnectionModel{
263263
ProjectID: types.StringValue(dummyProjectID),
264-
InstanceName: types.StringValue(instanceName),
264+
WorkspaceName: types.StringValue(instanceName),
265265
ConnectionName: types.StringValue(sampleConnectionName),
266266
Type: types.StringValue("Sample"),
267267
Authentication: types.ObjectNull(streamconnection.ConnectionAuthenticationObjectType.AttrTypes),
@@ -284,7 +284,7 @@ func TestStreamConnectionSDKToTFModel(t *testing.T) {
284284
providedInstanceName: instanceName,
285285
expectedTFModel: &streamconnection.TFStreamConnectionModel{
286286
ProjectID: types.StringValue(dummyProjectID),
287-
InstanceName: types.StringValue(instanceName),
287+
WorkspaceName: types.StringValue(instanceName),
288288
ConnectionName: types.StringValue(awslambdaConnectionName),
289289
Type: types.StringValue("AWSLambda"),
290290
Authentication: types.ObjectNull(streamconnection.ConnectionAuthenticationObjectType.AttrTypes),
@@ -522,7 +522,6 @@ func TestStreamConnectionsSDKToTFModel(t *testing.T) {
522522
type connectionsSDKToTFModelErrorTestCase struct {
523523
SDKResp *admin.PaginatedApiStreamsConnection
524524
providedConfig *streamconnection.TFStreamConnectionsDSModel
525-
expectedTFModel *streamconnection.TFStreamConnectionsDSModel
526525
expectedErrorString string
527526
name string
528527
}

0 commit comments

Comments
 (0)