-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
90 lines (78 loc) · 3.16 KB
/
config.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package stackit
import (
"context"
"errors"
"fmt"
"os"
client "github.com/SchwarzIT/community-stackit-go-client"
"github.com/SchwarzIT/community-stackit-go-client/pkg/clients"
"github.com/SchwarzIT/community-stackit-go-client/pkg/services"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/types"
)
func (p *StackitProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
// Retrieve provider data from configuration
var config providerSchema
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Token flow
if config.ServiceAccountEmail.IsUnknown() || config.ServiceAccountEmail.IsNull() {
config.ServiceAccountEmail = types.StringValue(os.Getenv(ServiceAccountEmail))
}
if config.ServiceAccountToken.IsUnknown() || config.ServiceAccountToken.IsNull() {
config.ServiceAccountToken = types.StringValue(os.Getenv(ServiceAccountToken))
}
// Key flow
if config.ServiceAccountKey.IsUnknown() || config.ServiceAccountKey.IsNull() {
config.ServiceAccountKey = types.StringValue(os.Getenv(ServiceAccountKey))
}
if config.ServiceAccountKeyPath.IsUnknown() || config.ServiceAccountKeyPath.IsNull() {
config.ServiceAccountKeyPath = types.StringValue(os.Getenv(ServiceAccountKeyPath))
}
if config.PrivateKey.IsUnknown() || config.PrivateKey.IsNull() {
config.PrivateKey = types.StringValue(os.Getenv(PrivateKey))
}
if config.PrivateKeyPath.IsUnknown() || config.PrivateKeyPath.IsNull() {
config.PrivateKeyPath = types.StringValue(os.Getenv(PrivateKeyPath))
}
if os.Getenv("TF_ACC") == "1" {
config.EnableTraceContext = types.BoolValue(true)
}
var err error
kfcl, err := keyFlow(ctx, config)
if err == nil {
resp.DataSourceData = kfcl
resp.ResourceData = kfcl
return
}
tfcl, err2 := tokenFlow(ctx, config)
if err2 == nil {
resp.DataSourceData = tfcl
resp.ResourceData = tfcl
return
}
resp.Diagnostics.AddError("couldn't initialize client with an authentication flow", fmt.Sprintf("key flow client auth:\n%s\n\ntoken flow client auth:\n%s", err.Error(), err2.Error()))
}
func keyFlow(ctx context.Context, config providerSchema) (*services.Services, error) {
return client.NewClientWithKeyAuth(ctx, clients.KeyFlowConfig{
ServiceAccountKey: []byte(config.ServiceAccountKey.ValueString()),
PrivateKey: []byte(config.PrivateKey.ValueString()),
ServiceAccountKeyPath: config.ServiceAccountKeyPath.ValueString(),
PrivateKeyPath: config.PrivateKeyPath.ValueString(),
EnableTraceparent: config.EnableTraceContext.ValueBool(),
})
}
func tokenFlow(ctx context.Context, config providerSchema) (*services.Services, error) {
if config.ServiceAccountEmail.ValueString() != "" &&
config.ServiceAccountToken.ValueString() != "" {
return client.NewClientWithTokenAuth(ctx, clients.TokenFlowConfig{
ServiceAccountEmail: config.ServiceAccountEmail.ValueString(),
ServiceAccountToken: config.ServiceAccountToken.ValueString(),
EnableTraceparent: config.EnableTraceContext.ValueBool(),
})
}
return nil, errors.New("no proper settings found for token flow")
}