Skip to content

Commit

Permalink
Fix logic to create/delete/update sensitive variables
Browse files Browse the repository at this point in the history
  • Loading branch information
alfespa17 committed May 13, 2024
1 parent 43729d1 commit a2bb11d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
32 changes: 28 additions & 4 deletions internal/provider/organization_variable_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,24 @@ func (r *OrganizationVariableResource) Create(ctx context.Context, req resource.
organizationVariable := &client.OrganizationVariableEntity{}

err = jsonapi.UnmarshalPayload(strings.NewReader(string(bodyResponse)), organizationVariable)

tflog.Info(ctx, string(bodyResponse))
if err != nil {
resp.Diagnostics.AddError("Error unmarshal payload response", fmt.Sprintf("Error unmarshal payload response: %s", err))
return
}

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

b := *organizationVariable.Sensitive
if b == true {
tflog.Info(ctx, "Variable value is not included in response, setting values the same as the plan for sensitive=true...")
plan.Value = types.StringValue(plan.Value.ValueString())
} else {
tflog.Info(ctx, "Variable value is included in response...")
plan.Value = types.StringValue(organizationVariable.Value)
}

plan.Key = types.StringValue(organizationVariable.Key)
plan.Value = types.StringValue(organizationVariable.Value)
plan.Description = types.StringValue(organizationVariable.Description)
plan.Category = types.StringValue(organizationVariable.Category)
plan.Sensitive = types.BoolValue(*organizationVariable.Sensitive)
Expand Down Expand Up @@ -231,8 +239,16 @@ func (r *OrganizationVariableResource) Read(ctx context.Context, req resource.Re

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

b := *organizationVariable.Sensitive
if b == true {
tflog.Info(ctx, "Variable value is not included in response, setting values the same as the current state value")
state.Value = types.StringValue(state.Value.ValueString())
} else {
tflog.Info(ctx, "Variable value is included in response...")
state.Value = types.StringValue(organizationVariable.Value)
}

state.Key = types.StringValue(organizationVariable.Key)
state.Value = types.StringValue(organizationVariable.Value)
state.Description = types.StringValue(organizationVariable.Description)
state.Category = types.StringValue(organizationVariable.Category)
state.Sensitive = types.BoolValue(*organizationVariable.Sensitive)
Expand Down Expand Up @@ -330,7 +346,15 @@ func (r *OrganizationVariableResource) Update(ctx context.Context, req resource.

plan.ID = types.StringValue(state.ID.ValueString())
plan.Key = types.StringValue(organizationVariable.Key)
plan.Value = types.StringValue(organizationVariable.Value)
b := *organizationVariable.Sensitive
if b == true {
tflog.Info(ctx, "Variable value is not included in response, setting values the same as the plan for sensitive=true...")
plan.Value = types.StringValue(plan.Value.ValueString())
} else {
tflog.Info(ctx, "Variable value is included in response...")
plan.Value = types.StringValue(organizationVariable.Value)
}

plan.Description = types.StringValue(organizationVariable.Description)
plan.Category = types.StringValue(organizationVariable.Category)
plan.Sensitive = types.BoolValue(*organizationVariable.Sensitive)
Expand Down
30 changes: 27 additions & 3 deletions internal/provider/workspace_variable_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,16 @@ func (r *WorkspaceVariableResource) Create(ctx context.Context, req resource.Cre

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

b := workspaceVariable.Sensitive
if b == true {
tflog.Info(ctx, "Variable value is not included in response, setting values the same as the plan for sensitive=true...")
plan.Value = types.StringValue(plan.Value.ValueString())
} else {
tflog.Info(ctx, "Variable value is included in response...")
plan.Value = types.StringValue(workspaceVariable.Value)
}

plan.Key = types.StringValue(workspaceVariable.Key)
plan.Value = types.StringValue(workspaceVariable.Value)
plan.Description = types.StringValue(workspaceVariable.Description)
plan.Category = types.StringValue(workspaceVariable.Category)
plan.Sensitive = types.BoolValue(workspaceVariable.Sensitive)
Expand Down Expand Up @@ -235,8 +243,16 @@ func (r *WorkspaceVariableResource) Read(ctx context.Context, req resource.ReadR

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

b := workspaceVariable.Sensitive
if b == true {
tflog.Info(ctx, "Variable value is not included in response, setting values the same as the current state value")
state.Value = types.StringValue(state.Value.ValueString())
} else {
tflog.Info(ctx, "Variable value is included in response...")
state.Value = types.StringValue(workspaceVariable.Value)
}

state.Key = types.StringValue(workspaceVariable.Key)
state.Value = types.StringValue(workspaceVariable.Value)
state.Description = types.StringValue(workspaceVariable.Description)
state.Category = types.StringValue(workspaceVariable.Category)
state.Sensitive = types.BoolValue(workspaceVariable.Sensitive)
Expand Down Expand Up @@ -331,9 +347,17 @@ func (r *WorkspaceVariableResource) Update(ctx context.Context, req resource.Upd
return
}

b := workspaceVariable.Sensitive
if b == true {
tflog.Info(ctx, "Variable value is not included in response, setting values the same as the plan for sensitive=true...")
plan.Value = types.StringValue(plan.Value.ValueString())
} else {
tflog.Info(ctx, "Variable value is included in response...")
plan.Value = types.StringValue(workspaceVariable.Value)
}

plan.ID = types.StringValue(state.ID.ValueString())
plan.Key = types.StringValue(workspaceVariable.Key)
plan.Value = types.StringValue(workspaceVariable.Value)
plan.Description = types.StringValue(workspaceVariable.Description)
plan.Category = types.StringValue(workspaceVariable.Category)
plan.Sensitive = types.BoolValue(workspaceVariable.Sensitive)
Expand Down

0 comments on commit a2bb11d

Please sign in to comment.