/
settings.go
125 lines (107 loc) · 2.85 KB
/
settings.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
package awsds
import (
"bytes"
"encoding/json"
"fmt"
"github.com/famarks/grafarg-plugin-sdk-go/backend"
)
const defaultRegion = "default"
type AuthType int
const (
AuthTypeDefault AuthType = iota
AuthTypeSharedCreds
AuthTypeKeys
AuthTypeEC2IAMRole
)
func (at AuthType) String() string {
switch at {
case AuthTypeDefault:
return "default"
case AuthTypeSharedCreds:
return "credentials"
case AuthTypeKeys:
return "keys"
case AuthTypeEC2IAMRole:
return "ec2_iam_role"
default:
panic(fmt.Sprintf("Unrecognized auth type %d", at))
}
}
func ToAuthType(authType string) AuthType {
switch authType {
case "credentials", "sharedCreds":
return AuthTypeSharedCreds
case "keys":
return AuthTypeKeys
case "default":
return AuthTypeDefault
case "ec2_iam_role":
return AuthTypeEC2IAMRole
case "arn":
return AuthTypeDefault
default:
panic(fmt.Sprintf("Unrecognized auth type %s", authType))
}
}
// MarshalJSON marshals the enum as a quoted json string
func (at *AuthType) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(at.String())
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}
// UnmarshalJSON unmashals a quoted json string to the enum value
func (at *AuthType) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
switch j {
case "credentials": // Old name
fallthrough
case "sharedCreds":
*at = AuthTypeSharedCreds
case "keys":
*at = AuthTypeKeys
case "ec2_iam_role":
*at = AuthTypeEC2IAMRole
case "default":
fallthrough
default:
*at = AuthTypeDefault // For old 'arn' option
}
return nil
}
// DatasourceSettings holds basic connection info
type AWSDatasourceSettings struct {
Profile string `json:"profile"`
Region string `json:"region"`
AuthType AuthType `json:"authType"`
AssumeRoleARN string `json:"assumeRoleARN"`
ExternalID string `json:"externalId"`
// Override the client endpoint
Endpoint string `json:"endpoint"`
//go:deprecated Use Region instead
DefaultRegion string `json:"defaultRegion"`
// Loaded from DecryptedSecureJSONData (not the json object)
AccessKey string `json:"-"`
SecretKey string `json:"-"`
}
// LoadSettings will read and validate Settings from the DataSourceConfg
func (s *AWSDatasourceSettings) Load(config backend.DataSourceInstanceSettings) error {
if config.JSONData != nil && len(config.JSONData) > 1 {
if err := json.Unmarshal(config.JSONData, s); err != nil {
return fmt.Errorf("could not unmarshal DatasourceSettings json: %w", err)
}
}
if s.Region == defaultRegion || s.Region == "" {
s.Region = s.DefaultRegion
}
if s.Profile == "" {
s.Profile = config.Database // legacy support (only for cloudwatch?)
}
s.AccessKey = config.DecryptedSecureJSONData["accessKey"]
s.SecretKey = config.DecryptedSecureJSONData["secretKey"]
return nil
}