/
datasource_exoscale_sks_nodepool_list.go
55 lines (41 loc) · 1.44 KB
/
datasource_exoscale_sks_nodepool_list.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
package exoscale
import (
"context"
"crypto/md5"
"fmt"
"sort"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
v2 "github.com/exoscale/egoscale/v2"
"github.com/exoscale/terraform-provider-exoscale/pkg/general"
"github.com/exoscale/terraform-provider-exoscale/pkg/list"
)
const (
dsSKSNodepoolsListIdentifier = "exoscale_sks_nodepool_list"
dsSKSNodepoolsListAttributeIdentifier = "nodepools"
)
func dataSourceSKSNodepoolListGetElementScheme() general.SchemaMap {
return dataSourceSKSNodepool().Schema
}
func dataSourceSKSNodepoolList() *schema.Resource {
return list.FilterableListDataSource(dsSKSNodepoolsListIdentifier, dsSKSNodepoolsListAttributeIdentifier, resSKSNodepoolAttrZone, getNodepoolList, nodepoolToDataMap, generateSKSNodepoolListID, dataSourceSKSNodepoolListGetElementScheme)
}
func generateSKSNodepoolListID(nodepools []*v2.SKSNodepool) string {
ids := make([]string, 0, len(nodepools))
for _, cluster := range nodepools {
ids = append(ids, *cluster.ID)
}
sort.Strings(ids)
return fmt.Sprintf("%x", md5.Sum([]byte(strings.Join(ids, ""))))
}
func getNodepoolList(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*v2.SKSNodepool, error) {
clusters, err := getClusterList(ctx, d, meta)
if err != nil {
return nil, err
}
var nodepools []*v2.SKSNodepool
for _, cluster := range clusters {
nodepools = append(nodepools, cluster.Nodepools...)
}
return nodepools, nil
}