/
client.go
62 lines (51 loc) · 1.34 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
package api
import (
"context"
"time"
cleanhttp "github.com/hashicorp/go-cleanhttp"
"github.com/ovh/go-ovh/ovh"
)
type OvhAuthCurrentCredential struct {
OvhSupport bool `json:"ovhSupport"`
Status string `json:"status"`
ApplicationId int64 `json:"applicationId"`
CredentialId int64 `json:"credentialId"`
Rules []ovh.AccessRule `json:"rules"`
Expiration time.Time `json:"expiration"`
LastUse time.Time `json:"lastUse"`
Creation time.Time `json:"creation"`
}
type OVHCredentials struct {
Endpoint string
ApplicationKey string
ApplicationSecret string
ConsumerKey string
}
type APIClient struct {
Client *ovh.Client
ctx context.Context
}
func GetClient(data OVHCredentials, ctx context.Context) (*APIClient, error) {
var cred OvhAuthCurrentCredential
client, err := ovh.NewClient(
data.Endpoint,
data.ApplicationKey,
data.ApplicationSecret,
data.ConsumerKey,
)
if err != nil {
return nil, err
}
// TODO: add terraform version
client.UserAgent = "Terraform"
if client.Client.Transport == nil {
client.Client.Transport = cleanhttp.DefaultTransport()
}
if err := client.Get("/auth/currentCredential", &cred); err != nil {
return nil, err
}
return &APIClient{
Client: client,
ctx: ctx,
}, nil
}