Logging and error handling improvements, bug fixes (#21)

- Uniformed logs and diagnostics:
  - Logging and adding to diagnostics is done by the highest level function (Create/Read/Update/Delete/Import) using `LogAndAddError`
  - Lower-level routines' signature changed to return error instead of writing to diagnostics
  - Standardize summary and details across services
  - Removed manual adding of relevant variables to details (they're in the context, TF adds them to logs)
- Changed validators to be closer to official implementation
- Fix logging wrong output after wait
- Fix Argus checking wrong diagnostics
- Fix Resource Manager not updating state after project update
- Fix unnecessary pointer in LogAndAddError
This commit is contained in:
Henrique Santos 2023-09-21 14:52:52 +01:00 committed by GitHub
parent 29b8c91999
commit 4e8514df00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
51 changed files with 1389 additions and 1092 deletions

View file

@ -49,7 +49,7 @@ func (r *credentialResource) Metadata(_ context.Context, req resource.MetadataRe
}
// Configure adds the provider configured client to the resource.
func (r *credentialResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
func (r *credentialResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
@ -57,7 +57,7 @@ func (r *credentialResource) Configure(_ context.Context, req resource.Configure
providerData, ok := req.ProviderData.(core.ProviderData)
if !ok {
resp.Diagnostics.AddError("Unexpected Data Source Configure Type", fmt.Sprintf("Expected stackit.ProviderData, got %T", req.ProviderData))
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
return
}
@ -76,10 +76,12 @@ func (r *credentialResource) Configure(_ context.Context, req resource.Configure
}
if err != nil {
resp.Diagnostics.AddError("Could not Configure API Client", err.Error())
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", err.Error())
return
}
r.client = apiClient
tflog.Info(ctx, "Argus credential client configured")
}
func (r *credentialResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
@ -148,17 +150,20 @@ func (r *credentialResource) Create(ctx context.Context, req resource.CreateRequ
got, err := r.client.CreateCredential(ctx, instanceId, projectId).Execute()
if err != nil {
resp.Diagnostics.AddError("Error creating credential", fmt.Sprintf("Calling API: %v", err))
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Calling API: %v", err))
return
}
err = mapFields(got.Credentials, &model)
if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error mapping fields", err.Error())
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating credential", fmt.Sprintf("Processing API payload: %v", err))
return
}
diags = resp.State.Set(ctx, &model)
resp.Diagnostics.Append(diags...)
tflog.Info(ctx, "ARGUS credential created")
if resp.Diagnostics.HasError() {
return
}
tflog.Info(ctx, "Argus credential created")
}
func mapFields(r *argus.Credential, model *Model) error {
@ -202,17 +207,20 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
userName := model.Username.ValueString()
_, err := r.client.GetCredential(ctx, instanceId, projectId, userName).Execute()
if err != nil {
resp.Diagnostics.AddError("Error reading credential", fmt.Sprintf("Project id = %s, instance id = %s, username = %s: %v", projectId, instanceId, userName, err))
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
return
}
diags = resp.State.Set(ctx, model)
resp.Diagnostics.Append(diags...)
tflog.Info(ctx, "ARGUS credential read")
if resp.Diagnostics.HasError() {
return
}
tflog.Info(ctx, "Argus credential read")
}
func (r *credentialResource) Update(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { // nolint:gocritic // function signature required by Terraform
func (r *credentialResource) Update(ctx context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) { // nolint:gocritic // function signature required by Terraform
// Update shouldn't be called
resp.Diagnostics.AddError("Error updating credentials", "credentials can't be updated")
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating credential", "Credential can't be updated")
}
// Delete deletes the resource and removes the Terraform state on success.
@ -229,8 +237,8 @@ func (r *credentialResource) Delete(ctx context.Context, req resource.DeleteRequ
userName := model.Username.ValueString()
_, err := r.client.DeleteCredential(ctx, instanceId, projectId, userName).Execute()
if err != nil {
resp.Diagnostics.AddError("Error deleting credential", "project id = "+projectId+", instance id = "+instanceId+", username = "+userName+", "+err.Error())
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting credential", fmt.Sprintf("Calling API: %v", err))
return
}
tflog.Info(ctx, "ARGUS credential deleted")
tflog.Info(ctx, "Argus credential deleted")
}