terraform-provider-stackitp.../stackit/core/core.go
Henrique Santos 4e8514df00
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
2023-09-21 14:52:52 +01:00

56 lines
1.5 KiB
Go

package core
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
// Separator used for concatenation of TF-internal resource ID
const Separator = ","
type ProviderData struct {
RoundTripper http.RoundTripper
ServiceAccountEmail string
Region string
DnsCustomEndpoint string
PostgreSQLCustomEndpoint string
PostgresFlexCustomEndpoint string
LogMeCustomEndpoint string
RabbitMQCustomEndpoint string
MariaDBCustomEndpoint string
OpenSearchCustomEndpoint string
RedisCustomEndpoint string
ArgusCustomEndpoint string
SKECustomEndpoint string
ResourceManagerCustomEndpoint string
}
// DiagsToError Converts TF diagnostics' errors into an error with a human-readable description.
// If there are no errors, the output is nil
func DiagsToError(diags diag.Diagnostics) error {
if !diags.HasError() {
return nil
}
diagsError := diags.Errors()
diagsStrings := make([]string, 0)
for _, diagnostic := range diagsError {
diagsStrings = append(diagsStrings, fmt.Sprintf(
"(%s) %s",
diagnostic.Summary(),
diagnostic.Detail(),
))
}
return fmt.Errorf("%s", strings.Join(diagsStrings, ";"))
}
// LogAndAddError Logs the error and adds it to the diags
func LogAndAddError(ctx context.Context, diags *diag.Diagnostics, summary, detail string) {
tflog.Error(ctx, summary)
diags.AddError(summary, detail)
}