Skip to content

Commit

Permalink
Merge pull request #7144 from kterada0509/feature/add-elastic_beansta…
Browse files Browse the repository at this point in the history
…lk_application-data-source

Add aws_elastic_beanstalk_application Data Source
  • Loading branch information
bflad authored Jan 15, 2019
2 parents 1b06083 + 8068052 commit 7a1025a
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
87 changes: 87 additions & 0 deletions aws/data_source_aws_elastic_beanstalk_application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package aws

import (
"fmt"

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

func dataSourceAwsElasticBeanstalkApplication() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsElasticBeanstalkApplicationRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"appversion_lifecycle": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"service_role": {
Type: schema.TypeString,
Computed: true,
},
"max_age_in_days": {
Type: schema.TypeInt,
Computed: true,
},
"max_count": {
Type: schema.TypeInt,
Computed: true,
},
"delete_source_from_s3": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
},
}
}

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

// Get the name and description
name := d.Get("name").(string)

resp, err := conn.DescribeApplications(&elasticbeanstalk.DescribeApplicationsInput{
ApplicationNames: []*string{aws.String(name)},
})
if err != nil {
return fmt.Errorf("Error describing Applications (%s): %s", name, err)
}

if len(resp.Applications) > 1 || len(resp.Applications) < 1 {
return fmt.Errorf("Error %d Applications matched, expected 1", len(resp.Applications))
}

app := resp.Applications[0]

d.SetId(name)
d.Set("arn", app.ApplicationArn)
d.Set("name", app.ApplicationName)
d.Set("description", app.Description)

if app.ResourceLifecycleConfig != nil {
d.Set("appversion_lifecycle", flattenResourceLifecycleConfig(app.ResourceLifecycleConfig))
}

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

import (
"fmt"
"testing"

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

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEksClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsElasticBeanstalkApplicationDataSourceConfig_Basic(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceResourceName, "arn"),
resource.TestCheckResourceAttrPair(resourceName, "name", dataSourceResourceName, "name"),
resource.TestCheckResourceAttrPair(resourceName, "description", dataSourceResourceName, "description"),
resource.TestCheckResourceAttr(dataSourceResourceName, "appversion_lifecycle.#", "1"),
resource.TestCheckResourceAttrPair(resourceName, "appversion_lifecycle.0.service_role", dataSourceResourceName, "appversion_lifecycle.0.service_role"),
resource.TestCheckResourceAttrPair(resourceName, "appversion_lifecycle.0.max_age_in_days", dataSourceResourceName, "appversion_lifecycle.0.max_age_in_days"),
resource.TestCheckResourceAttrPair(resourceName, "appversion_lifecycle.0.delete_source_from_s3", dataSourceResourceName, "appversion_lifecycle.0.delete_source_from_s3"),
),
},
},
})
}

func testAccAwsElasticBeanstalkApplicationDataSourceConfig_Basic(rName string) string {
return fmt.Sprintf(`
%s
data "aws_elastic_beanstalk_application" "test" {
name = "${aws_elastic_beanstalk_application.tftest.name}"
}
`, testAccBeanstalkAppConfigWithMaxAge(rName))
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func Provider() terraform.ResourceProvider {
"aws_efs_mount_target": dataSourceAwsEfsMountTarget(),
"aws_eip": dataSourceAwsEip(),
"aws_eks_cluster": dataSourceAwsEksCluster(),
"aws_elastic_beanstalk_application": dataSourceAwsElasticBeanstalkApplication(),
"aws_elastic_beanstalk_hosted_zone": dataSourceAwsElasticBeanstalkHostedZone(),
"aws_elastic_beanstalk_solution_stack": dataSourceAwsElasticBeanstalkSolutionStack(),
"aws_elasticache_cluster": dataSourceAwsElastiCacheCluster(),
Expand Down
44 changes: 44 additions & 0 deletions website/docs/d/elastic_beanstalk_application.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
layout: "aws"
page_title: "AWS: aws_elastic_beanstalk_application"
sidebar_current: "docs-aws-datasource-elastic-beanstalk-application"
description: |-
Retrieve information about an Elastic Beanstalk Application
---

# Data Source: aws_elastic_beanstalk_application

Retrieve information about an Elastic Beanstalk Application.

## Example Usage

```hcl
data "aws_elastic_beanstalk_application" "example" {
name = "example"
}
output "arn" {
value = "${data.aws_elastic_beanstalk_application.example.arn}"
}
output "description" {
value = "${data.aws_elastic_beanstalk_application.example.description}"
}
```

## Argument Reference

* `name` - (Required) The name of the application

## Attributes Reference

* `id` - The name of the application
* `arn` - The Amazon Resource Name (ARN) of the application.
* `description` - Short description of the application

Application version lifecycle (`appversion_lifecycle`) supports the nested attribute containing.

* `service_role` - The ARN of an IAM service role under which the application version is deleted. Elastic Beanstalk must have permission to assume this role.
* `max_count` - The maximum number of application versions to retain.
* `max_age_in_days` - The number of days to retain an application version.
* `delete_source_from_s3` - Specifies whether delete a version's source bundle from S3 when the application version is deleted.

0 comments on commit 7a1025a

Please sign in to comment.