forked from ovh/terraform-provider-ovh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
142 lines (115 loc) · 2.67 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
package ovh
import (
"fmt"
"log"
"sync"
cleanhttp "github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"github.com/ovh/go-ovh/ovh"
)
var providerVersion, providerCommit string
type Config struct {
Account string
Plate string
Endpoint string
// Access token
AccessToken string
// AK / AS / CK authentication information
ApplicationKey string
ApplicationSecret string
ConsumerKey string
// oAuth2 authentication information
ClientID string
ClientSecret string
OVHClient *ovh.Client
authenticated bool
authFailed error
lockAuth *sync.Mutex
}
func clientDefault(c *Config) (*ovh.Client, error) {
var (
client *ovh.Client
err error
)
switch {
case c.AccessToken != "":
client, err = ovh.NewAccessTokenClient(
c.Endpoint,
c.AccessToken,
)
case c.ClientID != "":
client, err = ovh.NewOAuth2Client(
c.Endpoint,
c.ClientID,
c.ClientSecret,
)
default:
client, err = ovh.NewClient(
c.Endpoint,
c.ApplicationKey,
c.ApplicationSecret,
c.ConsumerKey,
)
}
if err != nil {
return nil, err
}
// Retrieve endpoint that is used
for k, v := range ovh.Endpoints {
if v == client.Endpoint() {
c.Endpoint = k
}
}
client.UserAgent = "Terraform/" + providerVersion + "/" + providerCommit
return client, nil
}
func (c *Config) loadAndValidate() error {
if err := c.load(); err != nil {
return err
}
c.lockAuth.Lock()
defer c.lockAuth.Unlock()
if c.authFailed != nil {
return c.authFailed
}
if !c.authenticated {
var details OvhAuthDetails
if err := c.OVHClient.Get("/auth/details", &details); err != nil {
c.authFailed = fmt.Errorf("OVH client seems to be misconfigured: %q", err)
return c.authFailed
}
log.Printf("[DEBUG] Logged in on OVH API")
c.Account = details.Account
c.authenticated = true
}
if c.Plate == "" {
c.Plate = plateFromEndpoint(c.Endpoint)
}
return nil
}
func (c *Config) load() error {
targetClient, err := clientDefault(c)
if err != nil {
return fmt.Errorf("error getting ovh client: %q", err)
}
// decorating the OVH http client with logs
httpClient := targetClient.Client
if targetClient.Client.Transport == nil {
targetClient.Client.Transport = cleanhttp.DefaultTransport()
}
httpClient.Transport = logging.NewTransport("OVH", httpClient.Transport)
c.OVHClient = targetClient
return nil
}
var plateMapping map[string]string = map[string]string{
"ovh-eu": "eu",
"ovh-ca": "ca",
"ovh-us": "us",
"kimsufi-eu": "eu",
"kimsufi-ca": "ca",
"soyoustart-eu": "eu",
"soyoustart-ca": "ca",
}
func plateFromEndpoint(endpoint string) string {
return plateMapping[endpoint]
}