-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_instance.go
77 lines (61 loc) · 2.14 KB
/
service_instance.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
package ibm
import (
"context"
"strings"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/platform-services-go-sdk/resourcecontrollerv2"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
)
// BuildServiceInstanceList :: return a list of matrix items, one per service instance
func BuildServiceInstanceList(ctx context.Context, d *plugin.QueryData) []map[string]interface{} {
// cache service instance region matrix
cacheKey := "ServiceInstanceList"
if cachedData, ok := d.ConnectionManager.Cache.Get(cacheKey); ok {
return cachedData.([]map[string]interface{})
}
// get all the service instances in the account
serviceInstances, err := listAllServiceInstances(ctx, d, d.Connection)
if err != nil {
panic(err)
}
matrix := make([]map[string]interface{}, len(serviceInstances))
for i, instance := range serviceInstances {
splitID := strings.Split(*instance, ":")
matrix[i] = map[string]interface{}{
"instance_id": splitID[7],
"instance_crn": *instance,
"region": splitID[5],
"service_type": splitID[4],
}
}
// set ServiceInstanceList cache
d.ConnectionManager.Cache.Set(cacheKey, matrix)
return matrix
}
func listAllServiceInstances(ctx context.Context, d *plugin.QueryData, connection *plugin.Connection) ([]*string, error) {
// Create Session
session, err := resourceControllerService(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("listAllServiceInstances", "connection_error", err)
return nil, err
}
serviceCacheKey := "listAllServiceInstances"
if cachedData, ok := d.ConnectionManager.Cache.Get(serviceCacheKey); ok {
return cachedData.([]*string), nil
}
var serviceInstanceCRNs []*string
opts := &resourcecontrollerv2.ListResourceInstancesOptions{
Type: core.StringPtr("service_instance"),
}
response, _, err := session.ListResourceInstances(opts)
if err != nil {
plugin.Logger(ctx).Error("listAllServiceInstances", "query_error", err)
return nil, err
}
for _, i := range response.Resources {
serviceInstanceCRNs = append(serviceInstanceCRNs, i.CRN)
}
// save service instances in cache
d.ConnectionManager.Cache.Set(serviceCacheKey, serviceInstanceCRNs)
return serviceInstanceCRNs, err
}