Skip to content

Commit

Permalink
convert data source to v3
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterp committed Jul 30, 2024
1 parent cfaa418 commit 7a3f1f6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
58 changes: 30 additions & 28 deletions exoscale/datasource_exoscale_sks_nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

v2 "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
v3 "github.com/exoscale/egoscale/v3"
"github.com/exoscale/terraform-provider-exoscale/pkg/config"
"github.com/exoscale/terraform-provider-exoscale/pkg/filter"
"github.com/exoscale/terraform-provider-exoscale/pkg/general"
)
Expand Down Expand Up @@ -50,28 +50,28 @@ func dataSourceSKSNodepool() *schema.Resource {
return ret
}

func nodepoolToDataMap(nodepool *v2.SKSNodepool) general.TerraformObject {
func nodepoolToDataMap(nodepool *v3.SKSNodepool) general.TerraformObject {
ret := make(general.TerraformObject)

general.Assign(ret, resSKSNodepoolAttrAntiAffinityGroupIDs, nodepool.AntiAffinityGroupIDs)
general.AssignTime(ret, resSKSNodepoolAttrCreatedAt, nodepool.CreatedAt)
general.Assign(ret, resSKSNodepoolAttrDeployTargetID, nodepool.DeployTargetID)
general.Assign(ret, resSKSNodepoolAttrDescription, nodepool.Description)
general.Assign(ret, resSKSNodepoolAttrDiskSize, nodepool.DiskSize)
general.Assign(ret, resSKSNodepoolAttrInstancePoolID, nodepool.InstancePoolID)
general.Assign(ret, resSKSNodepoolAttrInstancePrefix, nodepool.InstancePrefix)
general.Assign(ret, resSKSNodepoolAttrInstanceType, nodepool.InstanceTypeID)
general.Assign(ret, resSKSNodepoolAttrLabels, nodepool.Labels)
general.Assign(ret, resSKSNodepoolAttrName, nodepool.Name)
general.Assign(ret, resSKSNodepoolAttrPrivateNetworkIDs, nodepool.PrivateNetworkIDs)
// TODO
general.Assign(ret, resSKSNodepoolAttrSecurityGroupIDs, nodepool.SecurityGroupIDs)
general.Assign(ret, resSKSNodepoolAttrSize, nodepool.Size)
general.Assign(ret, resSKSNodepoolAttrState, nodepool.State)
general.Assign(ret, resSKSNodepoolAttrTaints, nodepool.Taints)
general.Assign(ret, resSKSNodepoolAttrTemplateID, nodepool.TemplateID)
general.Assign(ret, resSKSNodepoolAttrVersion, nodepool.Version)
general.Assign(ret, dsSKSNodepoolID, nodepool.ID)
general.Assign(ret, resSKSNodepoolAttrAntiAffinityGroupIDs, &nodepool.AntiAffinityGroups)
general.AssignTime(ret, resSKSNodepoolAttrCreatedAt, &nodepool.CreatedAT)
general.Assign(ret, resSKSNodepoolAttrDeployTargetID, &nodepool.DeployTarget.ID)
general.Assign(ret, resSKSNodepoolAttrDescription, &nodepool.Description)
general.Assign(ret, resSKSNodepoolAttrDiskSize, &nodepool.DiskSize)
general.Assign(ret, resSKSNodepoolAttrInstancePoolID, &nodepool.InstancePool.ID)
general.Assign(ret, resSKSNodepoolAttrInstancePrefix, &nodepool.InstancePrefix)
general.Assign(ret, resSKSNodepoolAttrInstanceType, &nodepool.InstanceType.ID)
general.Assign(ret, resSKSNodepoolAttrLabels, &nodepool.Labels)
general.Assign(ret, resSKSNodepoolAttrName, &nodepool.Name)
general.Assign(ret, resSKSNodepoolAttrPrivateNetworkIDs, &nodepool.PrivateNetworks)
general.Assign(ret, resSKSNodepoolAttrPublicIPAssignment, &nodepool.PublicIPAssignment)
general.Assign(ret, resSKSNodepoolAttrSecurityGroupIDs, &nodepool.SecurityGroups)
general.Assign(ret, resSKSNodepoolAttrSize, &nodepool.Size)
general.Assign(ret, resSKSNodepoolAttrState, &nodepool.State)
general.Assign(ret, resSKSNodepoolAttrTaints, &nodepool.Taints)
general.Assign(ret, resSKSNodepoolAttrTemplateID, &nodepool.Template.ID)
general.Assign(ret, resSKSNodepoolAttrVersion, &nodepool.Version)
general.Assign(ret, dsSKSNodepoolID, &nodepool.ID)

return ret
}
Expand All @@ -84,13 +84,15 @@ func dataSourceSKSNodepoolRead(ctx context.Context, d *schema.ResourceData, meta
zone := d.Get(resSKSClusterAttrZone).(string)

ctx, cancel := context.WithTimeout(ctx, d.Timeout(schema.TimeoutRead))
ctx = exoapi.WithEndpoint(ctx, exoapi.NewReqEndpoint(getEnvironment(meta), zone))
defer cancel()

client := getClient(meta)
client, err := config.GetClientV3WithZone(ctx, meta, zone)
if err != nil {
return diag.FromErr(err)
}

clusterID := d.Get(resSKSNodepoolAttrClusterID).(string)
cluster, err := client.GetSKSCluster(ctx, zone, clusterID)
clusterID := v3.UUID(d.Get(resSKSNodepoolAttrClusterID).(string))
cluster, err := client.GetSKSCluster(ctx, clusterID)
if err != nil {
return diag.Errorf("error getting cluster %q: %s", clusterID, err)
}
Expand All @@ -104,12 +106,12 @@ func dataSourceSKSNodepoolRead(ctx context.Context, d *schema.ResourceData, meta
nMatches := 0

for _, nodepool := range cluster.Nodepools {
nodepoolData := nodepoolToDataMap(nodepool)
nodepoolData := nodepoolToDataMap(&nodepool)
nodepoolData[resSKSNodepoolAttrClusterID] = clusterID
nodepoolData[resSKSNodepoolAttrZone] = zone
if filter.CheckForMatch(nodepoolData, filters) {
if nMatches < 1 {
d.SetId(*nodepool.ID)
d.SetId(nodepool.ID.String())

matchingNodePool = nodepoolData

Expand Down
29 changes: 21 additions & 8 deletions exoscale/datasource_exoscale_sks_nodepool_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

v2 "github.com/exoscale/egoscale/v2"
v3 "github.com/exoscale/egoscale/v3"
"github.com/exoscale/terraform-provider-exoscale/pkg/config"
"github.com/exoscale/terraform-provider-exoscale/pkg/general"
"github.com/exoscale/terraform-provider-exoscale/pkg/list"
)
Expand All @@ -27,28 +28,40 @@ func dataSourceSKSNodepoolList() *schema.Resource {
return list.FilterableListDataSource(dsSKSNodepoolsListIdentifier, dsSKSNodepoolsListAttributeIdentifier, resSKSNodepoolAttrZone, getNodepoolList, nodepoolToDataMap, generateSKSNodepoolListID, dataSourceSKSNodepoolListGetElementScheme)
}

func generateSKSNodepoolListID(nodepools []*v2.SKSNodepool) string {
func generateSKSNodepoolListID(nodepools []*v3.SKSNodepool) string {
ids := make([]string, 0, len(nodepools))

for _, cluster := range nodepools {
ids = append(ids, *cluster.ID)
ids = append(ids, cluster.ID.String())
}

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)
func getNodepoolList(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*v3.SKSNodepool, error) {
zone := d.Get(resSKSClusterAttrZone).(string)

ctx, cancel := context.WithTimeout(ctx, d.Timeout(schema.TimeoutRead))
defer cancel()

client, err := config.GetClientV3WithZone(ctx, meta, zone)
if err != nil {
return nil, err
}

var nodepools []*v2.SKSNodepool
clusters, err := client.ListSKSClusters(ctx)
if err != nil {
return nil, fmt.Errorf("error getting cluster list from zone %q: %s", zone, err)
}

var nodepools []*v3.SKSNodepool

for _, cluster := range clusters {
nodepools = append(nodepools, cluster.Nodepools...)
for _, cluster := range clusters.SKSClusters {
for _, np := range cluster.Nodepools {
nodepools = append(nodepools, &np)
}
}

return nodepools, nil
Expand Down

0 comments on commit 7a3f1f6

Please sign in to comment.