Skip to content

Commit c744bb6

Browse files
dharrisiostack72
authored andcommitted
provider/aws: import Elastic Beanstalk Application and Environment (hashicorp#7444)
1 parent 480542d commit c744bb6

4 files changed

Lines changed: 82 additions & 6 deletions
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package aws
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform/helper/resource"
7+
)
8+
9+
func TestAWSElasticBeanstalkApplication_importBasic(t *testing.T) {
10+
resourceName := "aws_elastic_beanstalk_application.tftest"
11+
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
Providers: testAccProviders,
15+
CheckDestroy: testAccCheckBeanstalkAppDestroy,
16+
Steps: []resource.TestStep{
17+
resource.TestStep{
18+
Config: testAccBeanstalkAppConfig,
19+
},
20+
21+
resource.TestStep{
22+
ResourceName: resourceName,
23+
ImportState: true,
24+
ImportStateVerify: true,
25+
},
26+
},
27+
})
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package aws
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform/helper/resource"
7+
)
8+
9+
func TestAWSElasticBeanstalkEnvironment_importBasic(t *testing.T) {
10+
resourceName := "aws_elastic_beanstalk_application.tftest"
11+
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
Providers: testAccProviders,
15+
CheckDestroy: testAccCheckBeanstalkAppDestroy,
16+
Steps: []resource.TestStep{
17+
resource.TestStep{
18+
Config: testAccBeanstalkEnvConfig,
19+
},
20+
21+
resource.TestStep{
22+
ResourceName: resourceName,
23+
ImportState: true,
24+
ImportStateVerify: true,
25+
},
26+
},
27+
})
28+
}

builtin/providers/aws/resource_aws_elastic_beanstalk_application.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ func resourceAwsElasticBeanstalkApplication() *schema.Resource {
1919
Read: resourceAwsElasticBeanstalkApplicationRead,
2020
Update: resourceAwsElasticBeanstalkApplicationUpdate,
2121
Delete: resourceAwsElasticBeanstalkApplicationDelete,
22+
Importer: &schema.ResourceImporter{
23+
State: schema.ImportStatePassthrough,
24+
},
2225

2326
Schema: map[string]*schema.Schema{
2427
"name": &schema.Schema{
@@ -94,6 +97,7 @@ func resourceAwsElasticBeanstalkApplicationRead(d *schema.ResourceData, meta int
9497
return err
9598
}
9699

100+
d.Set("name", a.ApplicationName)
97101
d.Set("description", a.Description)
98102
return nil
99103
}

builtin/providers/aws/resource_aws_elastic_beanstalk_environment.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ func resourceAwsElasticBeanstalkEnvironment() *schema.Resource {
4646
Read: resourceAwsElasticBeanstalkEnvironmentRead,
4747
Update: resourceAwsElasticBeanstalkEnvironmentUpdate,
4848
Delete: resourceAwsElasticBeanstalkEnvironmentDelete,
49+
Importer: &schema.ResourceImporter{
50+
State: schema.ImportStatePassthrough,
51+
},
4952

5053
SchemaVersion: 1,
5154
MigrateState: resourceAwsElasticBeanstalkEnvironmentMigrateState,
@@ -339,15 +342,12 @@ func resourceAwsElasticBeanstalkEnvironmentUpdate(d *schema.ResourceData, meta i
339342
func resourceAwsElasticBeanstalkEnvironmentRead(d *schema.ResourceData, meta interface{}) error {
340343
conn := meta.(*AWSClient).elasticbeanstalkconn
341344

342-
app := d.Get("application").(string)
343345
envId := d.Id()
344-
tier := d.Get("tier").(string)
345346

346347
log.Printf("[DEBUG] Elastic Beanstalk environment read %s: id %s", d.Get("name").(string), d.Id())
347348

348349
resp, err := conn.DescribeEnvironments(&elasticbeanstalk.DescribeEnvironmentsInput{
349-
ApplicationName: aws.String(app),
350-
EnvironmentIds: []*string{aws.String(envId)},
350+
EnvironmentIds: []*string{aws.String(envId)},
351351
})
352352

353353
if err != nil {
@@ -380,6 +380,14 @@ func resourceAwsElasticBeanstalkEnvironmentRead(d *schema.ResourceData, meta int
380380
return err
381381
}
382382

383+
if err := d.Set("name", env.EnvironmentName); err != nil {
384+
return err
385+
}
386+
387+
if err := d.Set("application", env.ApplicationName); err != nil {
388+
return err
389+
}
390+
383391
if err := d.Set("description", env.Description); err != nil {
384392
return err
385393
}
@@ -388,8 +396,12 @@ func resourceAwsElasticBeanstalkEnvironmentRead(d *schema.ResourceData, meta int
388396
return err
389397
}
390398

391-
if tier == "WebServer" && env.CNAME != nil {
392-
beanstalkCnamePrefixRegexp := regexp.MustCompile(`(^[^.]+).\w{2}-\w{4,9}-\d.elasticbeanstalk.com$`)
399+
if err := d.Set("tier", *env.Tier.Name); err != nil {
400+
return err
401+
}
402+
403+
if env.CNAME != nil {
404+
beanstalkCnamePrefixRegexp := regexp.MustCompile(`(^[^.]+)(.\w{2}-\w{4,9}-\d)?.elasticbeanstalk.com$`)
393405
var cnamePrefix string
394406
cnamePrefixMatch := beanstalkCnamePrefixRegexp.FindStringSubmatch(*env.CNAME)
395407

@@ -402,6 +414,10 @@ func resourceAwsElasticBeanstalkEnvironmentRead(d *schema.ResourceData, meta int
402414
if err := d.Set("cname_prefix", cnamePrefix); err != nil {
403415
return err
404416
}
417+
} else {
418+
if err := d.Set("cname_prefix", ""); err != nil {
419+
return err
420+
}
405421
}
406422

407423
if err := d.Set("autoscaling_groups", flattenBeanstalkAsg(resources.EnvironmentResources.AutoScalingGroups)); err != nil {

0 commit comments

Comments
 (0)