Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Resource and Data Source: aws_eks_cluster #4749

Merged
merged 3 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/efs"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
elasticsearch "github.com/aws/aws-sdk-go/service/elasticsearchservice"
Expand Down Expand Up @@ -168,6 +169,7 @@ type AWSClient struct {
ecrconn *ecr.ECR
ecsconn *ecs.ECS
efsconn *efs.EFS
eksconn *eks.EKS
elbconn *elb.ELB
elbv2conn *elbv2.ELBV2
emrconn *emr.EMR
Expand Down Expand Up @@ -476,6 +478,7 @@ func (c *Config) Client() (interface{}, error) {
client.ecrconn = ecr.New(awsEcrSess)
client.ecsconn = ecs.New(awsEcsSess)
client.efsconn = efs.New(awsEfsSess)
client.eksconn = eks.New(sess)
client.elasticacheconn = elasticache.New(sess)
client.elasticbeanstalkconn = elasticbeanstalk.New(sess)
client.elastictranscoderconn = elastictranscoder.New(sess)
Expand Down
117 changes: 117 additions & 0 deletions aws/data_source_aws_eks_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package aws

import (
"fmt"
"log"

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

func dataSourceAwsEksCluster() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEksClusterRead,

Schema: map[string]*schema.Schema{
"certificate_authority": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"data": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"created_at": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this field actually useful for anyone/anything? Not a big deal if not, but I thought historically we avoided encoding fields which aren't useful from user's perspective to the schema.

Type: schema.TypeString,
Computed: true,
},
"endpoint": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},
"role_arn": {
Type: schema.TypeString,
Computed: true,
},
"vpc_config": {
Type: schema.TypeList,
MaxItems: 1,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"security_group_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"subnet_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"version": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsEksClusterRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).eksconn
name := d.Get("name").(string)

input := &eks.DescribeClusterInput{
Name: aws.String(name),
}

log.Printf("[DEBUG] Reading EKS Cluster: %s", input)
output, err := conn.DescribeCluster(input)
if err != nil {
return fmt.Errorf("error reading EKS Cluster (%s): %s", name, err)
}

cluster := output.Cluster
if cluster == nil {
return fmt.Errorf("EKS Cluster (%s) not found", name)
}

d.SetId(name)
d.Set("arn", cluster.Arn)

if err := d.Set("certificate_authority", flattenEksCertificate(cluster.CertificateAuthority)); err != nil {
return fmt.Errorf("error setting certificate_authority: %s", err)
}

d.Set("created_at", aws.TimeValue(cluster.CreatedAt).String())
d.Set("endpoint", cluster.Endpoint)
d.Set("name", cluster.Name)
d.Set("role_arn", cluster.RoleArn)
d.Set("version", cluster.Version)

if err := d.Set("vpc_config", flattenEksVpcConfigResponse(cluster.ResourcesVpcConfig)); err != nil {
return fmt.Errorf("error setting vpc_config: %s", err)
}

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

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAWSEksClusterDataSource_basic(t *testing.T) {
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5))
dataSourceResourceName := "data.aws_eks_cluster.test"
resourceName := "aws_eks_cluster.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEksClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEksClusterDataSourceConfig_Basic(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceResourceName, "certificate_authority.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "certificate_authority.0.data", dataSourceResourceName, "certificate_authority.0.data"),
resource.TestCheckResourceAttrPair(resourceName, "created_at", dataSourceResourceName, "created_at"),
resource.TestCheckResourceAttrPair(resourceName, "endpoint", dataSourceResourceName, "endpoint"),
resource.TestCheckResourceAttrPair(resourceName, "role_arn", dataSourceResourceName, "role_arn"),
resource.TestCheckResourceAttrPair(resourceName, "version", dataSourceResourceName, "version"),
resource.TestCheckResourceAttr(dataSourceResourceName, "vpc_config.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "vpc_config.0.security_group_ids.#", dataSourceResourceName, "vpc_config.0.security_group_ids.#"),
resource.TestCheckResourceAttrPair(resourceName, "vpc_config.0.subnet_ids.#", dataSourceResourceName, "vpc_config.0.subnet_ids.#"),
resource.TestCheckResourceAttrPair(resourceName, "vpc_config.0.vpc_id", dataSourceResourceName, "vpc_config.0.vpc_id"),
),
},
},
})
}

func testAccAWSEksClusterDataSourceConfig_Basic(rName string) string {
return fmt.Sprintf(`
%s

data "aws_eks_cluster" "test" {
name = "${aws_eks_cluster.test.name}"
}
`, testAccAWSEksClusterConfig_Required(rName))
}
2 changes: 2 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func Provider() terraform.ResourceProvider {
"aws_efs_file_system": dataSourceAwsEfsFileSystem(),
"aws_efs_mount_target": dataSourceAwsEfsMountTarget(),
"aws_eip": dataSourceAwsEip(),
"aws_eks_cluster": dataSourceAwsEksCluster(),
"aws_elastic_beanstalk_hosted_zone": dataSourceAwsElasticBeanstalkHostedZone(),
"aws_elastic_beanstalk_solution_stack": dataSourceAwsElasticBeanstalkSolutionStack(),
"aws_elasticache_cluster": dataSourceAwsElastiCacheCluster(),
Expand Down Expand Up @@ -381,6 +382,7 @@ func Provider() terraform.ResourceProvider {
"aws_egress_only_internet_gateway": resourceAwsEgressOnlyInternetGateway(),
"aws_eip": resourceAwsEip(),
"aws_eip_association": resourceAwsEipAssociation(),
"aws_eks_cluster": resourceAwsEksCluster(),
"aws_elasticache_cluster": resourceAwsElasticacheCluster(),
"aws_elasticache_parameter_group": resourceAwsElasticacheParameterGroup(),
"aws_elasticache_replication_group": resourceAwsElasticacheReplicationGroup(),
Expand Down
Loading