/
client.go
159 lines (135 loc) · 4.05 KB
/
client.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
package client
import (
"context"
"errors"
"fmt"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
awscfg "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/smithy-go/logging"
"github.com/cloudquery/cq-provider-sdk/provider/diag"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
"github.com/digitalocean/godo"
"github.com/hashicorp/go-hclog"
)
type Client struct {
// This is a client that you need to create and initialize in Configure
// It will be passed for each resource fetcher.
logger hclog.Logger
DoClient *godo.Client
Regions []string
SpacesRegion string
CredentialStatus DoCredentialStruct
S3 *s3.Client
}
type DoCredentialStruct struct {
Api bool
Spaces bool
}
type SpacesCredentialsProvider struct {
SpacesAccessKey string
SpacesAccessKeyId string
}
type AwsLogger struct {
l hclog.Logger
}
type SpacesEndpointResolver struct{}
const MaxItemsPerPage = 200
var defaultSpacesRegions = []string{"nyc3", "sfo3", "ams3", "sgp1", "fra1"}
func (c *Client) Logger() hclog.Logger {
return c.logger
}
func (c *Client) WithSpacesRegion(region string) *Client {
return &Client{
logger: c.Logger().With("spaces_region", region),
DoClient: c.DoClient,
SpacesRegion: region,
S3: c.S3,
}
}
func (s SpacesCredentialsProvider) Retrieve(ctx context.Context) (aws.Credentials, error) {
return aws.Credentials{
AccessKeyID: s.SpacesAccessKeyId,
SecretAccessKey: s.SpacesAccessKey,
Source: "digitalocean",
}, nil
}
func (SpacesEndpointResolver) ResolveEndpoint(_, region string) (aws.Endpoint, error) {
return aws.Endpoint{
URL: fmt.Sprintf("https://%s.digitaloceanspaces.com", region),
Source: aws.EndpointSourceCustom,
}, nil
}
func Configure(logger hclog.Logger, config interface{}) (schema.ClientMeta, diag.Diagnostics) {
providerConfig := config.(*Config)
if providerConfig.Token == "" {
providerConfig.Token = getTokenFromEnv()
}
if providerConfig.Token == "" {
return nil, diag.FromError(errors.New("missing API token"), diag.USER)
}
credStatus := DoCredentialStruct{
Api: true,
Spaces: true,
}
if providerConfig.SpacesAccessKey == "" || providerConfig.SpacesAccessKeyId == "" {
providerConfig.SpacesAccessKeyId, providerConfig.SpacesAccessKey = getSpacesTokenFromEnv()
}
if providerConfig.SpacesAccessKey == "" || providerConfig.SpacesAccessKeyId == "" {
credStatus.Spaces = false
}
awsCfg, err := awscfg.LoadDefaultConfig(context.Background(),
awscfg.WithCredentialsProvider(SpacesCredentialsProvider{providerConfig.SpacesAccessKey, providerConfig.SpacesAccessKeyId}),
awscfg.WithEndpointResolver(SpacesEndpointResolver{}),
)
if err != nil {
return nil, diag.FromError(err, diag.INTERNAL)
}
if providerConfig.SpacesDebugLogging {
awsCfg.ClientLogMode = aws.LogRequest | aws.LogResponse | aws.LogRetries
awsCfg.Logger = AwsLogger{logger}
}
spacesRegions := defaultSpacesRegions
if len(providerConfig.SpacesRegions) > 0 {
spacesRegions = providerConfig.SpacesRegions
}
client := Client{
logger: logger,
DoClient: godo.NewFromToken(providerConfig.Token),
Regions: spacesRegions,
SpacesRegion: "nyc3",
S3: s3.NewFromConfig(awsCfg),
CredentialStatus: credStatus,
}
return &client, nil
}
func getTokenFromEnv() string {
doToken := os.Getenv("DIGITALOCEAN_TOKEN")
doAccessToken := os.Getenv("DIGITALOCEAN_ACCESS_TOKEN")
if doToken != "" {
return doToken
}
if doAccessToken != "" {
return doAccessToken
}
return ""
}
func getSpacesTokenFromEnv() (string, string) {
spacesAccessKey := os.Getenv("SPACES_ACCESS_KEY_ID")
spacesSecretKey := os.Getenv("SPACES_SECRET_ACCESS_KEY")
if spacesAccessKey == "" {
return "", ""
}
if spacesSecretKey == "" {
return "", ""
}
return spacesAccessKey, spacesSecretKey
}
func (a AwsLogger) Logf(classification logging.Classification, format string, v ...interface{}) {
if classification == logging.Warn {
a.l.Warn(fmt.Sprintf(format, v...))
} else {
a.l.Debug(fmt.Sprintf(format, v...))
}
}