Skip to content

Commit

Permalink
Merge pull request #13564 from zot24/f-data_source_aws_eks_node_group
Browse files Browse the repository at this point in the history
data-source/aws_eks_node_group
  • Loading branch information
ewbankkit authored Sep 14, 2021
2 parents af7f9e9 + 413ff80 commit ed497b8
Show file tree
Hide file tree
Showing 9 changed files with 510 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .changelog/13564.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-data-source
aws_eks_node_groups
```

```release-note:new-data-source
aws_eks_node_group
```
16 changes: 7 additions & 9 deletions aws/data_source_aws_eks_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ func dataSourceAwsEksClustersRead(d *schema.ResourceData, meta interface{}) erro

var clusters []*string

err := conn.ListClustersPages(&eks.ListClustersInput{},
func(page *eks.ListClustersOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}
err := conn.ListClustersPages(&eks.ListClustersInput{}, func(page *eks.ListClustersOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

clusters = append(clusters, page.Clusters...)
clusters = append(clusters, page.Clusters...)

return !lastPage
},
)
return !lastPage
})

if err != nil {
return fmt.Errorf("error listing EKS Clusters: %w", err)
Expand Down
193 changes: 193 additions & 0 deletions aws/data_source_aws_eks_node_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package aws

import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
tfeks "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/eks"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/eks/finder"
)

func dataSourceAwsEksNodeGroup() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAwsEksNodeGroupRead,

Schema: map[string]*schema.Schema{
"ami_type": {
Type: schema.TypeString,
Computed: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"cluster_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},
"disk_size": {
Type: schema.TypeInt,
Computed: true,
},
"instance_types": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"labels": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"node_group_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},
"node_role_arn": {
Type: schema.TypeString,
Computed: true,
},
"release_version": {
Type: schema.TypeString,
Computed: true,
},
"remote_access": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ec2_ssh_key": {
Type: schema.TypeString,
Computed: true,
},
"source_security_group_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"resources": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"autoscaling_groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"remote_access_security_group_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"scaling_config": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"desired_size": {
Type: schema.TypeInt,
Computed: true,
},
"max_size": {
Type: schema.TypeInt,
Computed: true,
},
"min_size": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"subnet_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tagsSchemaComputed(),
"version": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsEksNodeGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*AWSClient).eksconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

clusterName := d.Get("cluster_name").(string)
nodeGroupName := d.Get("node_group_name").(string)
id := tfeks.NodeGroupCreateResourceID(clusterName, nodeGroupName)
nodeGroup, err := finder.NodegroupByClusterNameAndNodegroupName(conn, clusterName, nodeGroupName)

if err != nil {
return diag.Errorf("error reading EKS Node Group (%s): %s", id, err)
}

d.SetId(id)

d.Set("ami_type", nodeGroup.AmiType)
d.Set("arn", nodeGroup.NodegroupArn)
d.Set("cluster_name", nodeGroup.ClusterName)
d.Set("disk_size", nodeGroup.DiskSize)
d.Set("instance_types", nodeGroup.InstanceTypes)
d.Set("labels", nodeGroup.Labels)
d.Set("node_group_name", nodeGroup.NodegroupName)
d.Set("node_role_arn", nodeGroup.NodeRole)
d.Set("release_version", nodeGroup.ReleaseVersion)

if err := d.Set("remote_access", flattenEksRemoteAccessConfig(nodeGroup.RemoteAccess)); err != nil {
return diag.Errorf("error setting remote_access: %s", err)
}

if err := d.Set("resources", flattenEksNodeGroupResources(nodeGroup.Resources)); err != nil {
return diag.Errorf("error setting resources: %s", err)
}

if nodeGroup.ScalingConfig != nil {
if err := d.Set("scaling_config", []interface{}{flattenEksNodeGroupScalingConfig(nodeGroup.ScalingConfig)}); err != nil {
return diag.Errorf("error setting scaling_config: %s", err)
}
} else {
d.Set("scaling_config", nil)
}

d.Set("status", nodeGroup.Status)

if err := d.Set("subnet_ids", aws.StringValueSlice(nodeGroup.Subnets)); err != nil {
return diag.Errorf("error setting subnets: %s", err)
}

if err := d.Set("tags", keyvaluetags.EksKeyValueTags(nodeGroup.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return diag.Errorf("error setting tags: %s", err)
}

d.Set("version", nodeGroup.Version)

return nil
}
65 changes: 65 additions & 0 deletions aws/data_source_aws_eks_node_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package aws

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/eks"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccAWSEksNodegroupDataSource_basic(t *testing.T) {
var nodeGroup eks.Nodegroup
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceResourceName := "data.aws_eks_node_group.test"
resourceName := "aws_eks_node_group.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSEks(t) },
ErrorCheck: testAccErrorCheck(t, eks.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEksClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEksNodeGroupConfigNodeGroupName(rName),
Check: resource.ComposeTestCheckFunc(),
},
{
Config: testAccAWSEksNodeGroupDataSourceConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEksNodeGroupExists(resourceName, &nodeGroup),
resource.TestCheckResourceAttrPair(resourceName, "ami_type", dataSourceResourceName, "ami_type"),
resource.TestCheckResourceAttrPair(resourceName, "arn", dataSourceResourceName, "arn"),
resource.TestCheckResourceAttrPair(resourceName, "cluster_name", dataSourceResourceName, "cluster_name"),
resource.TestCheckResourceAttrPair(resourceName, "disk_size", dataSourceResourceName, "disk_size"),
resource.TestCheckResourceAttr(dataSourceResourceName, "instance_types.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "instance_type", dataSourceResourceName, "instance_type"),
resource.TestCheckResourceAttrPair(resourceName, "labels.%", dataSourceResourceName, "labels.%"),
resource.TestCheckResourceAttrPair(resourceName, "node_group_name", dataSourceResourceName, "node_group_name"),
resource.TestCheckResourceAttrPair(resourceName, "node_role_arn", dataSourceResourceName, "node_role_arn"),
resource.TestCheckResourceAttrPair(resourceName, "release_version", dataSourceResourceName, "release_version"),
resource.TestCheckResourceAttr(dataSourceResourceName, "remote_access.#", "0"),
resource.TestCheckResourceAttr(dataSourceResourceName, "resources.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "resources", dataSourceResourceName, "resources"),
resource.TestCheckResourceAttr(dataSourceResourceName, "scaling_config.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "scaling_config", dataSourceResourceName, "scaling_config"),
resource.TestCheckResourceAttrPair(resourceName, "status", dataSourceResourceName, "status"),
resource.TestCheckResourceAttrPair(resourceName, "subnet_ids.#", dataSourceResourceName, "subnet_ids.#"),
resource.TestCheckResourceAttrPair(resourceName, "subnet_ids", dataSourceResourceName, "subnet_ids"),
resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceResourceName, "tags.%"),
resource.TestCheckResourceAttrPair(resourceName, "version", dataSourceResourceName, "version"),
),
},
},
})
}

func testAccAWSEksNodeGroupDataSourceConfig(rName string) string {
return composeConfig(testAccAWSEksNodeGroupConfigNodeGroupName(rName), fmt.Sprintf(`
data "aws_eks_node_group" "test" {
cluster_name = aws_eks_cluster.test.name
node_group_name = %[1]q
}
`, rName))
}
62 changes: 62 additions & 0 deletions aws/data_source_aws_eks_node_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceAwsEksNodeGroups() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEksNodeGroupsRead,

Schema: map[string]*schema.Schema{
"cluster_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},
"names": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceAwsEksNodeGroupsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).eksconn

clusterName := d.Get("cluster_name").(string)

input := &eks.ListNodegroupsInput{
ClusterName: aws.String(clusterName),
}

var nodegroups []*string

err := conn.ListNodegroupsPages(input, func(page *eks.ListNodegroupsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

nodegroups = append(nodegroups, page.Nodegroups...)

return !lastPage
})

if err != nil {
return fmt.Errorf("error listing EKS Node Groups: %w", err)
}

d.SetId(clusterName)

d.Set("cluster_name", clusterName)
d.Set("names", aws.StringValueSlice(nodegroups))

return nil
}
Loading

0 comments on commit ed497b8

Please sign in to comment.