-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathutil.go
More file actions
70 lines (63 loc) · 2.1 KB
/
util.go
File metadata and controls
70 lines (63 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package utils
import (
"context"
"errors"
"fmt"
"strings"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/services/sfs"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
)
func ConfigureClient(ctx context.Context, providerData *core.ProviderData, diags *diag.Diagnostics) *sfs.APIClient {
apiClientConfigOptions := []config.ConfigurationOption{
config.WithCustomAuth(providerData.RoundTripper),
utils.UserAgentConfigOption(providerData.Version),
}
if providerData.SfsCustomEndpoint != "" {
apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(providerData.SfsCustomEndpoint))
}
apiClient, err := sfs.NewAPIClient(apiClientConfigOptions...)
if err != nil {
core.LogAndAddError(ctx, diags, "Error configuring API client", fmt.Sprintf("Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration", err))
return nil
}
return apiClient
}
func DescribeValidationError(err sfs.ValidationError) string {
var sb strings.Builder
if err.Title != nil {
sb.WriteString(*err.Title)
sb.WriteRune('\n')
}
if fields := err.Fields; fields != nil {
for _, field := range *fields {
sb.WriteRune('\n')
sb.WriteString("Field: ")
if field.Field != nil {
sb.WriteString(*field.Field)
}
sb.WriteString(" | Reason: ")
if field.Reason != nil {
sb.WriteString(*field.Reason)
}
}
}
return sb.String()
}
func LogAndAddError(ctx context.Context, diags *diag.Diagnostics, summary, detail string, err error) {
if err == nil {
return
}
message := err.Error()
var oapiErr *oapierror.GenericOpenAPIError
if errors.As(err, &oapiErr) {
errModel := oapiErr.Model
if validationErr, ok := errModel.(sfs.ValidationError); ok {
message = DescribeValidationError(validationErr)
}
}
core.LogAndAddError(ctx, diags, summary, fmt.Sprintf("%s: %v", detail, message))
}