@@ -36,6 +36,7 @@ type streamConnectionRS struct {
36
36
type TFStreamConnectionModel struct {
37
37
ID types.String `tfsdk:"id"`
38
38
ProjectID types.String `tfsdk:"project_id"`
39
+ WorkspaceName types.String `tfsdk:"workspace_name"`
39
40
InstanceName types.String `tfsdk:"instance_name"`
40
41
ConnectionName types.String `tfsdk:"connection_name"`
41
42
Type types.String `tfsdk:"type"`
@@ -128,6 +129,17 @@ func (r *streamConnectionRS) Schema(ctx context.Context, req resource.SchemaRequ
128
129
conversion .UpdateSchemaDescription (& resp .Schema )
129
130
}
130
131
132
+ // getEffectiveWorkspaceName returns the workspace name from workspace_name or instance_name field
133
+ func getEffectiveWorkspaceName (model * TFStreamConnectionModel ) string {
134
+ if ! model .WorkspaceName .IsNull () && ! model .WorkspaceName .IsUnknown () {
135
+ return model .WorkspaceName .ValueString ()
136
+ }
137
+ if ! model .InstanceName .IsNull () && ! model .InstanceName .IsUnknown () {
138
+ return model .InstanceName .ValueString ()
139
+ }
140
+ return ""
141
+ }
142
+
131
143
func (r * streamConnectionRS ) Create (ctx context.Context , req resource.CreateRequest , resp * resource.CreateResponse ) {
132
144
var streamConnectionPlan TFStreamConnectionModel
133
145
resp .Diagnostics .Append (req .Plan .Get (ctx , & streamConnectionPlan )... )
@@ -137,19 +149,27 @@ func (r *streamConnectionRS) Create(ctx context.Context, req resource.CreateRequ
137
149
138
150
connV2 := r .Client .AtlasV2
139
151
projectID := streamConnectionPlan .ProjectID .ValueString ()
140
- instanceName := streamConnectionPlan .InstanceName .ValueString ()
152
+ effectiveWorkspaceName := getEffectiveWorkspaceName (& streamConnectionPlan )
153
+ if effectiveWorkspaceName == "" {
154
+ resp .Diagnostics .AddError ("validation error" , "workspace_name must be provided" )
155
+ return
156
+ }
157
+
141
158
streamConnectionReq , diags := NewStreamConnectionReq (ctx , & streamConnectionPlan )
142
159
if diags .HasError () {
143
160
resp .Diagnostics .Append (diags ... )
144
161
return
145
162
}
146
- apiResp , _ , err := connV2 .StreamsApi .CreateStreamConnection (ctx , projectID , instanceName , streamConnectionReq ).Execute ()
163
+ apiResp , _ , err := connV2 .StreamsApi .CreateStreamConnection (ctx , projectID , effectiveWorkspaceName , streamConnectionReq ).Execute ()
147
164
if err != nil {
148
165
resp .Diagnostics .AddError ("error creating resource" , err .Error ())
149
166
return
150
167
}
151
168
152
- newStreamConnectionModel , diags := NewTFStreamConnection (ctx , projectID , instanceName , & streamConnectionPlan .Authentication , apiResp )
169
+ instanceName := streamConnectionPlan .InstanceName .ValueString ()
170
+ workspaceName := streamConnectionPlan .WorkspaceName .ValueString ()
171
+
172
+ newStreamConnectionModel , diags := NewTFStreamConnectionWithInstanceName (ctx , projectID , instanceName , workspaceName , & streamConnectionPlan .Authentication , apiResp )
153
173
if diags .HasError () {
154
174
resp .Diagnostics .Append (diags ... )
155
175
return
@@ -166,9 +186,13 @@ func (r *streamConnectionRS) Read(ctx context.Context, req resource.ReadRequest,
166
186
167
187
connV2 := r .Client .AtlasV2
168
188
projectID := streamConnectionState .ProjectID .ValueString ()
169
- instanceName := streamConnectionState .InstanceName .ValueString ()
189
+ effectiveWorkspaceName := getEffectiveWorkspaceName (& streamConnectionState )
190
+ if effectiveWorkspaceName == "" {
191
+ resp .Diagnostics .AddError ("validation error" , "workspace_name must be provided" )
192
+ return
193
+ }
170
194
connectionName := streamConnectionState .ConnectionName .ValueString ()
171
- apiResp , getResp , err := connV2 .StreamsApi .GetStreamConnection (ctx , projectID , instanceName , connectionName ).Execute ()
195
+ apiResp , getResp , err := connV2 .StreamsApi .GetStreamConnection (ctx , projectID , effectiveWorkspaceName , connectionName ).Execute ()
172
196
if err != nil {
173
197
if validate .StatusNotFound (getResp ) {
174
198
resp .State .RemoveResource (ctx )
@@ -178,7 +202,9 @@ func (r *streamConnectionRS) Read(ctx context.Context, req resource.ReadRequest,
178
202
return
179
203
}
180
204
181
- newStreamConnectionModel , diags := NewTFStreamConnection (ctx , projectID , instanceName , & streamConnectionState .Authentication , apiResp )
205
+ instanceName := streamConnectionState .InstanceName .ValueString ()
206
+ workspaceName := streamConnectionState .WorkspaceName .ValueString ()
207
+ newStreamConnectionModel , diags := NewTFStreamConnectionWithInstanceName (ctx , projectID , instanceName , workspaceName , & streamConnectionState .Authentication , apiResp )
182
208
if diags .HasError () {
183
209
resp .Diagnostics .Append (diags ... )
184
210
return
@@ -195,20 +221,26 @@ func (r *streamConnectionRS) Update(ctx context.Context, req resource.UpdateRequ
195
221
196
222
connV2 := r .Client .AtlasV2
197
223
projectID := streamConnectionPlan .ProjectID .ValueString ()
198
- instanceName := streamConnectionPlan .InstanceName .ValueString ()
224
+ effectiveWorkspaceName := getEffectiveWorkspaceName (& streamConnectionPlan )
225
+ if effectiveWorkspaceName == "" {
226
+ resp .Diagnostics .AddError ("validation error" , "workspace_name must be provided" )
227
+ return
228
+ }
199
229
connectionName := streamConnectionPlan .ConnectionName .ValueString ()
200
230
streamConnectionReq , diags := NewStreamConnectionUpdateReq (ctx , & streamConnectionPlan )
201
231
if diags .HasError () {
202
232
resp .Diagnostics .Append (diags ... )
203
233
return
204
234
}
205
- apiResp , _ , err := connV2 .StreamsApi .UpdateStreamConnection (ctx , projectID , instanceName , connectionName , streamConnectionReq ).Execute ()
235
+ apiResp , _ , err := connV2 .StreamsApi .UpdateStreamConnection (ctx , projectID , effectiveWorkspaceName , connectionName , streamConnectionReq ).Execute ()
206
236
if err != nil {
207
237
resp .Diagnostics .AddError ("error updating resource" , err .Error ())
208
238
return
209
239
}
210
240
211
- newStreamConnectionModel , diags := NewTFStreamConnection (ctx , projectID , instanceName , & streamConnectionPlan .Authentication , apiResp )
241
+ instanceName := streamConnectionPlan .InstanceName .ValueString ()
242
+ workspaceName := streamConnectionPlan .WorkspaceName .ValueString ()
243
+ newStreamConnectionModel , diags := NewTFStreamConnectionWithInstanceName (ctx , projectID , instanceName , workspaceName , & streamConnectionPlan .Authentication , apiResp )
212
244
if diags .HasError () {
213
245
resp .Diagnostics .Append (diags ... )
214
246
return
@@ -225,7 +257,11 @@ func (r *streamConnectionRS) Delete(ctx context.Context, req resource.DeleteRequ
225
257
226
258
connV2 := r .Client .AtlasV2
227
259
projectID := streamConnectionState .ProjectID .ValueString ()
228
- instanceName := streamConnectionState .InstanceName .ValueString ()
260
+ instanceName := getEffectiveWorkspaceName (streamConnectionState )
261
+ if instanceName == "" {
262
+ resp .Diagnostics .AddError ("validation error" , "workspace_name must be provided" )
263
+ return
264
+ }
229
265
connectionName := streamConnectionState .ConnectionName .ValueString ()
230
266
if err := DeleteStreamConnection (ctx , connV2 .StreamsApi , projectID , instanceName , connectionName , 10 * time .Minute ); err != nil {
231
267
resp .Diagnostics .AddError ("error deleting resource" , err .Error ())
0 commit comments