-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
181 lines (151 loc) · 5.45 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-Present Datadog, Inc.
package cloudcraft
import (
"net/url"
"time"
"github.com/DataDog/cloudcraft-go/internal/xerrors"
"github.com/DataDog/cloudcraft-go/internal/xos"
)
const (
// ErrInvalidEndpoint is returned when the endpoint is not a valid URL.
ErrInvalidEndpoint xerrors.Error = "invalid endpoint"
// ErrMissingEndpointScheme is returned when a Config is created without a
// scheme for the endpoint.
ErrMissingEndpointScheme xerrors.Error = "missing endpoint scheme"
// ErrMissingEndpointHost is returned when a Config is created without a
// host for the endpoint.
ErrMissingEndpointHost xerrors.Error = "missing endpoint host"
// ErrMissingKey is returned when a Config is created without an API key.
ErrMissingKey xerrors.Error = "missing API key"
// ErrInvalidKey is returned when a Config is created with an invalid API
// key.
ErrInvalidKey xerrors.Error = "invalid API key; length must be 44"
)
const (
// DefaultSceme is the default protocol scheme, such as "http" or "https".
DefaultScheme string = "https"
// DefaultHost is the default host name or IP address of the Cloudcraft API.
DefaultHost string = "api.cloudcraft.co"
// DefaultPort is the default port number of the Cloudcraft API.
DefaultPort string = "443"
// DefaultPath is the default path to the Cloudcraft API.
DefaultPath string = "/"
// DefaultMaxRetries is the default maximum number of times the client will
// retry a request if it fails.
DefaultMaxRetries int = 3
// DefaultTimeout is the default timeout for requests made by the Cloudcraft
// API client.
DefaultTimeout time.Duration = time.Second * 120
)
// Environment variables used to configure the Config struct.
const (
EnvScheme string = "CLOUDCRAFT_PROTOCOL"
EnvHost string = "CLOUDCRAFT_HOST"
EnvPort string = "CLOUDCRAFT_PORT"
EnvPath string = "CLOUDCRAFT_PATH"
EnvMaxRetries string = "CLOUDCRAFT_MAX_RETRIES"
EnvTimeout string = "CLOUDCRAFT_TIMEOUT"
EnvAPIKey string = "CLOUDCRAFT_API_KEY" //nolint:gosec // false positive
)
// Config holds the basic configuration for the Cloudcraft API.
type Config struct {
// endpoint specifies the base URL of the Cloudcraft API for HTTP requests.
// It is constructed from the Scheme, Host, Port, and Path fields.
endpoint *url.URL
// Scheme is the protocol scheme, such as "http" or "https", to use when
// calling the API.
//
// If not set, the value of the CLOUDCRAFT_PROTOCOL environment variable is
// used. If the environment variable is not set, the default value is
// "https".
//
// This field is optional.
Scheme string
// Host is the host name or IP address of the Cloudcraft API.
//
// If not set, the value of the CLOUDCRAFT_HOST environment variable is
// used. If the environment variable is not set, the default value is the
// public instance of Cloudcraft, "api.cloudcraft.co".
//
// This field is optional.
Host string
// Port is the port number of the Cloudcraft API.
//
// If not set, the value of the CLOUDCRAFT_PORT environment variable is
// used. If the environment variable is not set, the default value is "443".
//
// This field is optional.
Port string
// Path is the path to the Cloudcraft API.
//
// If not set, the value of the CLOUDCRAFT_PATH environment variable is
// used. If the environment variable is not set, the default value is "/".
//
// This field is optional.
Path string
// Key is the API key used to authenticate with the Cloudcraft API.
//
// This field is required. [Learn more].
//
// [Learn more]: https://developers.cloudcraft.co/#authentication
Key string
// MaxRetries is the maximum number of times the client will retry a request
// if it fails.
//
// If not set, the value of the CLOUDCRAFT_MAX_RETRIES environment variable
// is used. If the environment variable is not set, the default value is 3.
//
// This field is optional.
MaxRetries int
// Timeout is the time limit for requests made by the Cloudcraft API client
// to the Cloudcraft API.
//
// If not set, the value of the CLOUDCRAFT_TIMEOUT environment variable is
// used. If the environment variable is not set, the default value is 80
// seconds.
//
// This field is optional.
Timeout time.Duration
}
// NewConfig returns a new Config with the given API key.
func NewConfig(key string) *Config {
return &Config{
Scheme: DefaultScheme,
Host: DefaultHost,
Port: DefaultPort,
Path: DefaultPath,
MaxRetries: DefaultMaxRetries,
Key: key,
Timeout: DefaultTimeout,
}
}
// NewConfigFromEnv returns a new Config from values set in the environment.
func NewConfigFromEnv() *Config {
return &Config{
Scheme: xos.GetEnv(EnvScheme, DefaultScheme),
Host: xos.GetEnv(EnvHost, DefaultHost),
Port: xos.GetEnv(EnvPort, DefaultPort),
Path: xos.GetEnv(EnvPath, DefaultPath),
MaxRetries: xos.GetIntEnv(EnvMaxRetries, DefaultMaxRetries),
Key: xos.GetEnv(EnvAPIKey, ""),
Timeout: xos.GetDurationEnv(EnvTimeout, DefaultTimeout),
}
}
// Validate checks that the Config is valid.
func (c *Config) Validate() error {
if c.Scheme == "" {
return ErrMissingEndpointScheme
}
if c.Host == "" {
return ErrMissingEndpointHost
}
if c.Key == "" {
return ErrMissingKey
}
if len(c.Key) != 44 {
return ErrInvalidKey
}
return nil
}