Skip to content

Commit 6a67352

Browse files
authored
Fix logic to create/delete/update sensitive variables (#37)
* Fix logic to create/delete/update sensitive variables
1 parent 43729d1 commit 6a67352

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

internal/provider/organization_variable_resource.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,23 @@ func (r *OrganizationVariableResource) Create(ctx context.Context, req resource.
172172
organizationVariable := &client.OrganizationVariableEntity{}
173173

174174
err = jsonapi.UnmarshalPayload(strings.NewReader(string(bodyResponse)), organizationVariable)
175-
175+
tflog.Info(ctx, string(bodyResponse))
176176
if err != nil {
177177
resp.Diagnostics.AddError("Error unmarshal payload response", fmt.Sprintf("Error unmarshal payload response: %s", err))
178178
return
179179
}
180180

181181
tflog.Info(ctx, "Body Response", map[string]any{"bodyResponse": string(bodyResponse)})
182182

183+
if *organizationVariable.Sensitive {
184+
tflog.Info(ctx, "Variable value is not included in response, setting values the same as the plan for sensitive=true...")
185+
plan.Value = types.StringValue(plan.Value.ValueString())
186+
} else {
187+
tflog.Info(ctx, "Variable value is included in response...")
188+
plan.Value = types.StringValue(organizationVariable.Value)
189+
}
190+
183191
plan.Key = types.StringValue(organizationVariable.Key)
184-
plan.Value = types.StringValue(organizationVariable.Value)
185192
plan.Description = types.StringValue(organizationVariable.Description)
186193
plan.Category = types.StringValue(organizationVariable.Category)
187194
plan.Sensitive = types.BoolValue(*organizationVariable.Sensitive)
@@ -231,8 +238,15 @@ func (r *OrganizationVariableResource) Read(ctx context.Context, req resource.Re
231238

232239
tflog.Info(ctx, "Body Response", map[string]any{"bodyResponse": string(bodyResponse)})
233240

241+
if *organizationVariable.Sensitive {
242+
tflog.Info(ctx, "Variable value is not included in response, setting values the same as the current state value")
243+
state.Value = types.StringValue(state.Value.ValueString())
244+
} else {
245+
tflog.Info(ctx, "Variable value is included in response...")
246+
state.Value = types.StringValue(organizationVariable.Value)
247+
}
248+
234249
state.Key = types.StringValue(organizationVariable.Key)
235-
state.Value = types.StringValue(organizationVariable.Value)
236250
state.Description = types.StringValue(organizationVariable.Description)
237251
state.Category = types.StringValue(organizationVariable.Category)
238252
state.Sensitive = types.BoolValue(*organizationVariable.Sensitive)
@@ -330,7 +344,15 @@ func (r *OrganizationVariableResource) Update(ctx context.Context, req resource.
330344

331345
plan.ID = types.StringValue(state.ID.ValueString())
332346
plan.Key = types.StringValue(organizationVariable.Key)
333-
plan.Value = types.StringValue(organizationVariable.Value)
347+
348+
if *organizationVariable.Sensitive {
349+
tflog.Info(ctx, "Variable value is not included in response, setting values the same as the plan for sensitive=true...")
350+
plan.Value = types.StringValue(plan.Value.ValueString())
351+
} else {
352+
tflog.Info(ctx, "Variable value is included in response...")
353+
plan.Value = types.StringValue(organizationVariable.Value)
354+
}
355+
334356
plan.Description = types.StringValue(organizationVariable.Description)
335357
plan.Category = types.StringValue(organizationVariable.Category)
336358
plan.Sensitive = types.BoolValue(*organizationVariable.Sensitive)

internal/provider/workspace_variable_resource.go

+24-3
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,15 @@ func (r *WorkspaceVariableResource) Create(ctx context.Context, req resource.Cre
184184

185185
tflog.Info(ctx, "Body Response", map[string]any{"bodyResponse": string(bodyResponse)})
186186

187+
if workspaceVariable.Sensitive {
188+
tflog.Info(ctx, "Variable value is not included in response, setting values the same as the plan for sensitive=true...")
189+
plan.Value = types.StringValue(plan.Value.ValueString())
190+
} else {
191+
tflog.Info(ctx, "Variable value is included in response...")
192+
plan.Value = types.StringValue(workspaceVariable.Value)
193+
}
194+
187195
plan.Key = types.StringValue(workspaceVariable.Key)
188-
plan.Value = types.StringValue(workspaceVariable.Value)
189196
plan.Description = types.StringValue(workspaceVariable.Description)
190197
plan.Category = types.StringValue(workspaceVariable.Category)
191198
plan.Sensitive = types.BoolValue(workspaceVariable.Sensitive)
@@ -235,8 +242,15 @@ func (r *WorkspaceVariableResource) Read(ctx context.Context, req resource.ReadR
235242

236243
tflog.Info(ctx, "Body Response", map[string]any{"bodyResponse": string(bodyResponse)})
237244

245+
if workspaceVariable.Sensitive {
246+
tflog.Info(ctx, "Variable value is not included in response, setting values the same as the current state value")
247+
state.Value = types.StringValue(state.Value.ValueString())
248+
} else {
249+
tflog.Info(ctx, "Variable value is included in response...")
250+
state.Value = types.StringValue(workspaceVariable.Value)
251+
}
252+
238253
state.Key = types.StringValue(workspaceVariable.Key)
239-
state.Value = types.StringValue(workspaceVariable.Value)
240254
state.Description = types.StringValue(workspaceVariable.Description)
241255
state.Category = types.StringValue(workspaceVariable.Category)
242256
state.Sensitive = types.BoolValue(workspaceVariable.Sensitive)
@@ -331,9 +345,16 @@ func (r *WorkspaceVariableResource) Update(ctx context.Context, req resource.Upd
331345
return
332346
}
333347

348+
if workspaceVariable.Sensitive {
349+
tflog.Info(ctx, "Variable value is not included in response, setting values the same as the plan for sensitive=true...")
350+
plan.Value = types.StringValue(plan.Value.ValueString())
351+
} else {
352+
tflog.Info(ctx, "Variable value is included in response...")
353+
plan.Value = types.StringValue(workspaceVariable.Value)
354+
}
355+
334356
plan.ID = types.StringValue(state.ID.ValueString())
335357
plan.Key = types.StringValue(workspaceVariable.Key)
336-
plan.Value = types.StringValue(workspaceVariable.Value)
337358
plan.Description = types.StringValue(workspaceVariable.Description)
338359
plan.Category = types.StringValue(workspaceVariable.Category)
339360
plan.Sensitive = types.BoolValue(workspaceVariable.Sensitive)

0 commit comments

Comments
 (0)