/
utils.go
96 lines (75 loc) · 2.28 KB
/
utils.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
package scalingo
import (
"context"
"errors"
"fmt"
"os"
"github.com/Scalingo/go-scalingo/v6"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
)
const matrixKeyRegion = "region"
const defaultScalingoRegion = "osc-fr1"
func connect(ctx context.Context, d *plugin.QueryData) (*scalingo.Client, error) {
region := d.EqualsQualString(matrixKeyRegion)
if region == "" {
region = defaultScalingoRegion
}
// get scalingo client from cache
cacheKey := fmt.Sprintf("scalingo-%s", region)
if cachedData, ok := d.ConnectionManager.Cache.Get(cacheKey); ok {
return cachedData.(*scalingo.Client), nil
}
token := os.Getenv("SCALINGO_TOKEN")
scalingoConfig := GetConfig(d.Connection)
if scalingoConfig.Token != nil {
token = *scalingoConfig.Token
}
if token == "" {
return nil, errors.New("'token' must be set in the connection configuration. Edit your connection configuration file or set the SCALINGO_TOKEN environment variable and then restart Steampipe")
}
config := scalingo.ClientConfig{
APIToken: token,
Region: region,
Timeout: -1,
}
client, err := scalingo.New(ctx, config)
if err != nil {
return nil, err
}
// Save to cache
d.ConnectionManager.Cache.Set(cacheKey, client)
return client, nil
}
func BuildRegionList(ctx context.Context, d *plugin.QueryData) []map[string]interface{} {
// cache matrix
cacheKey := "RegionListMatrix"
if cachedData, ok := d.ConnectionManager.Cache.Get(cacheKey); ok {
return cachedData.([]map[string]interface{})
}
var regions []string
// retrieve regions from connection config
scalingoConfig := GetConfig(d.Connection)
// handle compatibility with the old region configuration
if scalingoConfig.Region != nil {
regions = append(regions, *scalingoConfig.Region)
}
// Get only the regions as required by config file
if len(*scalingoConfig.Regions) > 0 {
regions = *scalingoConfig.Regions
}
if len(regions) > 0 {
matrix := make([]map[string]interface{}, len(regions))
for i, region := range regions {
matrix[i] = map[string]interface{}{matrixKeyRegion: region}
}
// set cache
d.ConnectionManager.Cache.Set(cacheKey, matrix)
return matrix
}
matrix := []map[string]interface{}{
{matrixKeyRegion: defaultScalingoRegion},
}
// set cache
d.ConnectionManager.Cache.Set(cacheKey, matrix)
return matrix
}