/
config.go
144 lines (118 loc) · 3.33 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package helper
import (
"context"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"github.com/linode/linodego"
"github.com/linode/terraform-provider-linode/v2/version"
)
const UAEnvVar = "TF_APPEND_USER_AGENT"
// DefaultLinodeURL is the Linode APIv4 URL to use.
const DefaultLinodeURL = "https://api.linode.com"
type ProviderMeta struct {
Client linodego.Client
Config *Config
}
// Config represents the Linode provider configuration.
type Config struct {
AccessToken string
APIURL string
APIVersion string
UAPrefix string
ConfigPath string
ConfigProfile string
TerraformVersion string
SkipInstanceReadyPoll bool
SkipInstanceDeletePoll bool
SkipImplicitReboots bool
DisableInternalCache bool
MinRetryDelayMilliseconds int
MaxRetryDelayMilliseconds int
EventPollMilliseconds int
LKEEventPollMilliseconds int
LKENodeReadyPollMilliseconds int
ObjAccessKey string
ObjSecretKey string
ObjUseTempKeys bool
}
// Client returns a fully initialized Linode client.
func (c *Config) Client(ctx context.Context) (*linodego.Client, error) {
loggingTransport := NewAPILoggerTransport(
logging.NewSubsystemLoggingHTTPTransport(
APILoggerSubsystem,
http.DefaultTransport,
),
)
oauth2Client := &http.Client{
Transport: loggingTransport,
}
client := linodego.NewClient(oauth2Client)
client.SetBaseURL(DefaultLinodeURL)
// Load the config file if it exists
if _, err := os.Stat(c.ConfigPath); err == nil {
tflog.Info(ctx, "Using Linode profile", map[string]any{
"config_path": c.ConfigPath,
})
err = client.LoadConfig(&linodego.LoadConfigOptions{
Path: c.ConfigPath,
Profile: c.ConfigProfile,
})
if err != nil {
return nil, err
}
} else {
tflog.Info(ctx, "Linode config does not exist, skipping..")
}
// Overrides
if c.AccessToken != "" {
client.SetToken(c.AccessToken)
}
if c.APIURL != "" {
client.SetBaseURL(c.APIURL)
}
if len(c.APIVersion) > 0 {
client.SetAPIVersion(c.APIVersion)
}
client.UseCache(!c.DisableInternalCache)
if c.EventPollMilliseconds != 0 {
client.SetPollDelay(time.Duration(c.EventPollMilliseconds) * time.Millisecond)
}
if c.MinRetryDelayMilliseconds != 0 {
client.SetRetryWaitTime(time.Duration(c.MinRetryDelayMilliseconds) * time.Millisecond)
}
if c.MaxRetryDelayMilliseconds != 0 {
client.SetRetryMaxWaitTime(time.Duration(c.MaxRetryDelayMilliseconds) * time.Millisecond)
}
tfUserAgent := terraformUserAgent(c.TerraformVersion)
userAgent := strings.TrimSpace(fmt.Sprintf("%s terraform-provider-linode/%s",
tfUserAgent, version.ProviderVersion))
if c.UAPrefix != "" {
userAgent = c.UAPrefix + " " + userAgent
}
client.SetUserAgent(userAgent)
ApplyAllRetryConditions(&client)
// We always want to disable resty debugging in favor
// of Terraform transport debugging.
client.SetDebug(false)
return &client, nil
}
func terraformUserAgent(version string) string {
ua := fmt.Sprintf(
"HashiCorp Terraform/%s (+https://www.terraform.io) Terraform-Plugin-SDK/%s",
version, GetSDKv2Version(),
)
if add := os.Getenv(UAEnvVar); add != "" {
add = strings.TrimSpace(add)
if len(add) > 0 {
ua += " " + add
log.Printf("[DEBUG] Using modified User-Agent: %s", ua)
}
}
return ua
}