Skip to content

Commit 4d06c1e

Browse files
committed
feat: add workspace_name to stream connection schema and models
1 parent 31304ee commit 4d06c1e

File tree

3 files changed

+112
-12
lines changed

3 files changed

+112
-12
lines changed

internal/service/streamconnection/model_stream_connection.go

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,16 @@ func NewStreamConnectionUpdateReq(ctx context.Context, plan *TFStreamConnectionM
123123
return streamConnection, nil
124124
}
125125

126-
func NewTFStreamConnection(ctx context.Context, projID, instanceName string, currAuthConfig *types.Object, apiResp *admin.StreamsConnection) (*TFStreamConnectionModel, diag.Diagnostics) {
126+
func NewTFStreamConnection(ctx context.Context, projID, workspaceName string, currAuthConfig *types.Object, apiResp *admin.StreamsConnection) (*TFStreamConnectionModel, diag.Diagnostics) {
127+
return NewTFStreamConnectionWithInstanceName(ctx, projID, "", workspaceName, currAuthConfig, apiResp)
128+
}
129+
130+
// 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) {
127132
rID := fmt.Sprintf("%s-%s-%s", instanceName, projID, conversion.SafeString(apiResp.Name))
128133
connectionModel := TFStreamConnectionModel{
129134
ID: types.StringValue(rID),
130135
ProjectID: types.StringValue(projID),
131-
InstanceName: types.StringValue(instanceName),
132136
ConnectionName: types.StringPointerValue(apiResp.Name),
133137
Type: types.StringPointerValue(apiResp.Type),
134138
ClusterName: types.StringPointerValue(apiResp.ClusterName),
@@ -137,6 +141,19 @@ func NewTFStreamConnection(ctx context.Context, projID, instanceName string, cur
137141
URL: types.StringPointerValue(apiResp.Url),
138142
}
139143

144+
if workspaceName != "" && instanceName != "" {
145+
return nil, diag.Diagnostics{diag.NewErrorDiagnostic("Attribute \"instance_name\" cannot be specified when \"workspace_name\" is specified", "")}
146+
}
147+
// Set the appropriate field based on the original model
148+
if workspaceName != "" {
149+
connectionModel.WorkspaceName = types.StringValue(workspaceName)
150+
connectionModel.InstanceName = types.StringNull()
151+
} else {
152+
// Default to instance_name for backward compatibility
153+
connectionModel.InstanceName = types.StringValue(instanceName)
154+
connectionModel.WorkspaceName = types.StringNull()
155+
}
156+
140157
authModel, diags := newTFConnectionAuthenticationModel(ctx, currAuthConfig, apiResp.Authentication)
141158
if diags.HasError() {
142159
return nil, diags
@@ -254,22 +271,30 @@ func NewTFStreamConnections(ctx context.Context,
254271
paginatedResult *admin.PaginatedApiStreamsConnection) (*TFStreamConnectionsDSModel, diag.Diagnostics) {
255272
input := paginatedResult.GetResults()
256273
results := make([]TFStreamConnectionModel, len(input))
274+
275+
workspaceName := streamConnectionsConfig.WorkspaceName.ValueString()
276+
instanceName := streamConnectionsConfig.InstanceName.ValueString()
277+
if workspaceName != "" && instanceName != "" {
278+
return nil, diag.Diagnostics{diag.NewErrorDiagnostic("Attribute \"instance_name\" cannot be specified when \"workspace_name\" is specified", "")}
279+
}
280+
257281
for i := range input {
258282
projectID := streamConnectionsConfig.ProjectID.ValueString()
259-
instanceName := streamConnectionsConfig.InstanceName.ValueString()
260-
connectionModel, diags := NewTFStreamConnection(ctx, projectID, instanceName, nil, &input[i])
283+
connectionModel, diags := NewTFStreamConnectionWithInstanceName(ctx, projectID, instanceName, workspaceName, nil, &input[i])
261284
if diags.HasError() {
262285
return nil, diags
263286
}
264287
results[i] = *connectionModel
265288
}
289+
266290
return &TFStreamConnectionsDSModel{
267-
ID: types.StringValue(id.UniqueId()),
268-
ProjectID: streamConnectionsConfig.ProjectID,
269-
InstanceName: streamConnectionsConfig.InstanceName,
270-
Results: results,
271-
PageNum: streamConnectionsConfig.PageNum,
272-
ItemsPerPage: streamConnectionsConfig.ItemsPerPage,
273-
TotalCount: types.Int64PointerValue(conversion.IntPtrToInt64Ptr(paginatedResult.TotalCount)),
291+
ID: types.StringValue(id.UniqueId()),
292+
ProjectID: streamConnectionsConfig.ProjectID,
293+
InstanceName: streamConnectionsConfig.InstanceName,
294+
WorkspaceName: streamConnectionsConfig.WorkspaceName,
295+
Results: results,
296+
PageNum: streamConnectionsConfig.PageNum,
297+
ItemsPerPage: streamConnectionsConfig.ItemsPerPage,
298+
TotalCount: types.Int64PointerValue(conversion.IntPtrToInt64Ptr(paginatedResult.TotalCount)),
274299
}, nil
275300
}

internal/service/streamconnection/model_stream_connection_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package streamconnection_test
22

33
import (
44
"fmt"
5+
"strings"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -483,6 +484,25 @@ func TestStreamConnectionsSDKToTFModel(t *testing.T) {
483484
Results: []streamconnection.TFStreamConnectionModel{},
484485
},
485486
},
487+
{
488+
name: "With workspace name and no page options",
489+
SDKResp: &admin.PaginatedApiStreamsConnection{
490+
Results: &[]admin.StreamsConnection{},
491+
TotalCount: admin.PtrInt(0),
492+
},
493+
providedConfig: &streamconnection.TFStreamConnectionsDSModel{
494+
ProjectID: types.StringValue(dummyProjectID),
495+
WorkspaceName: types.StringValue(instanceName),
496+
},
497+
expectedTFModel: &streamconnection.TFStreamConnectionsDSModel{
498+
ProjectID: types.StringValue(dummyProjectID),
499+
WorkspaceName: types.StringValue(instanceName),
500+
PageNum: types.Int64Null(),
501+
ItemsPerPage: types.Int64Null(),
502+
TotalCount: types.Int64Value(0),
503+
Results: []streamconnection.TFStreamConnectionModel{},
504+
},
505+
},
486506
}
487507

488508
for _, tc := range testCases {
@@ -499,6 +519,44 @@ func TestStreamConnectionsSDKToTFModel(t *testing.T) {
499519
}
500520
}
501521

522+
type connectionsSDKToTFModelErrorTestCase struct {
523+
SDKResp *admin.PaginatedApiStreamsConnection
524+
providedConfig *streamconnection.TFStreamConnectionsDSModel
525+
expectedTFModel *streamconnection.TFStreamConnectionsDSModel
526+
expectedErrorString string
527+
name string
528+
}
529+
530+
func TestStreamConnectionsSDKToTFModelError(t *testing.T) {
531+
testCases := []connectionsSDKToTFModelErrorTestCase{
532+
{
533+
name: "With workspace name and instance name",
534+
SDKResp: &admin.PaginatedApiStreamsConnection{
535+
Results: &[]admin.StreamsConnection{},
536+
TotalCount: admin.PtrInt(0),
537+
},
538+
providedConfig: &streamconnection.TFStreamConnectionsDSModel{
539+
ProjectID: types.StringValue(dummyProjectID),
540+
WorkspaceName: types.StringValue(instanceName),
541+
InstanceName: types.StringValue(instanceName),
542+
},
543+
expectedErrorString: "Attribute \"instance_name\" cannot be specified when \"workspace_name\" is specified",
544+
},
545+
}
546+
547+
for _, tc := range testCases {
548+
t.Run(tc.name, func(t *testing.T) {
549+
_, diags := streamconnection.NewTFStreamConnections(t.Context(), tc.providedConfig, tc.SDKResp)
550+
if !diags.HasError() {
551+
t.Fatalf("expected error but got none")
552+
}
553+
if !strings.Contains(diags.Errors()[0].Summary(), tc.expectedErrorString) {
554+
t.Fatalf("expected error %s but got %s", tc.expectedErrorString, diags.Errors()[0].Summary())
555+
}
556+
})
557+
}
558+
}
559+
502560
type tfToSDKCreateModelTestCase struct {
503561
tfModel *streamconnection.TFStreamConnectionModel
504562
expectedSDKReq *admin.StreamsConnection

internal/service/streamconnection/resource_schema.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
7+
"github.com/hashicorp/terraform-plugin-framework/path"
78
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
89
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
910
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -28,10 +29,26 @@ func ResourceSchema(ctx context.Context) schema.Schema {
2829
},
2930
},
3031
"instance_name": schema.StringAttribute{
31-
Required: true,
32+
Optional: true,
3233
PlanModifiers: []planmodifier.String{
3334
stringplanmodifier.RequiresReplace(),
3435
},
36+
Validators: []validator.String{
37+
stringvalidator.ConflictsWith(path.Expressions{
38+
path.MatchRelative().AtParent().AtName("workspace_name"),
39+
}...),
40+
},
41+
},
42+
"workspace_name": schema.StringAttribute{
43+
Optional: true,
44+
PlanModifiers: []planmodifier.String{
45+
stringplanmodifier.RequiresReplace(),
46+
},
47+
Validators: []validator.String{
48+
stringvalidator.ConflictsWith(path.Expressions{
49+
path.MatchRelative().AtParent().AtName("instance_name"),
50+
}...),
51+
},
3552
},
3653
"connection_name": schema.StringAttribute{
3754
Required: true,

0 commit comments

Comments
 (0)