Skip to content

Commit

Permalink
Add import support for aws_batch_compute_environment Relates - hashic…
Browse files Browse the repository at this point in the history
  • Loading branch information
prabusah committed Dec 15, 2019
1 parent 69f3f60 commit 9e8ffe6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 15 deletions.
6 changes: 6 additions & 0 deletions aws/resource_aws_batch_compute_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ func resourceAwsBatchComputeEnvironment() *schema.Resource {
Update: resourceAwsBatchComputeEnvironmentUpdate,
Delete: resourceAwsBatchComputeEnvironmentDelete,

Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
d.Set("compute_environment_name", d.Id())
return []*schema.ResourceData{d}, nil
},
},
Schema: map[string]*schema.Schema{
"compute_environment_name": {
Type: schema.TypeString,
Expand Down
71 changes: 56 additions & 15 deletions aws/resource_aws_batch_compute_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestAccAWSBatchComputeEnvironment_createEc2(t *testing.T) {

func TestAccAWSBatchComputeEnvironment_createEc2WithTags(t *testing.T) {
rInt := acctest.RandInt()

resourceName := "aws_batch_compute_environment.ec2"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) },
Providers: testAccProviders,
Expand All @@ -93,10 +93,15 @@ func TestAccAWSBatchComputeEnvironment_createEc2WithTags(t *testing.T) {
Config: testAccAWSBatchComputeEnvironmentConfigEC2WithTags(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBatchComputeEnvironmentExists(),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_resources.0.tags.%", "1"),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_resources.0.tags.Key1", "Value1"),
resource.TestCheckResourceAttr(resourceName, "compute_resources.0.tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "compute_resources.0.tags.Key1", "Value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -139,6 +144,7 @@ func TestAccAWSBatchComputeEnvironment_createUnmanaged(t *testing.T) {

func TestAccAWSBatchComputeEnvironment_updateMaxvCpus(t *testing.T) {
rInt := acctest.RandInt()
resourceName := "aws_batch_compute_environment.ec2"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) },
Expand All @@ -149,22 +155,28 @@ func TestAccAWSBatchComputeEnvironment_updateMaxvCpus(t *testing.T) {
Config: testAccAWSBatchComputeEnvironmentConfigEC2(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBatchComputeEnvironmentExists(),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_resources.0.max_vcpus", "16"),
resource.TestCheckResourceAttr(resourceName, "compute_resources.0.max_vcpus", "16"),
),
},
{
Config: testAccAWSBatchComputeEnvironmentConfigEC2UpdateMaxvCpus(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBatchComputeEnvironmentExists(),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_resources.0.max_vcpus", "32"),
resource.TestCheckResourceAttr(resourceName, "compute_resources.0.max_vcpus", "32"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSBatchComputeEnvironment_updateInstanceType(t *testing.T) {
rInt := acctest.RandInt()
resourceName := "aws_batch_compute_environment.ec2"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) },
Expand All @@ -175,16 +187,21 @@ func TestAccAWSBatchComputeEnvironment_updateInstanceType(t *testing.T) {
Config: testAccAWSBatchComputeEnvironmentConfigEC2(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBatchComputeEnvironmentExists(),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_resources.0.instance_type.#", "1"),
resource.TestCheckResourceAttr(resourceName, "compute_resources.0.instance_type.#", "1"),
),
},
{
Config: testAccAWSBatchComputeEnvironmentConfigEC2UpdateInstanceType(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBatchComputeEnvironmentExists(),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_resources.0.instance_type.#", "2"),
resource.TestCheckResourceAttr(resourceName, "compute_resources.0.instance_type.#", "2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -193,6 +210,7 @@ func TestAccAWSBatchComputeEnvironment_updateComputeEnvironmentName(t *testing.T
rInt := acctest.RandInt()
expectedName := fmt.Sprintf("tf_acc_test_%d", rInt)
expectedUpdatedName := fmt.Sprintf("tf_acc_test_updated_%d", rInt)
resourceName := "aws_batch_compute_environment.ec2"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) },
Expand All @@ -203,16 +221,21 @@ func TestAccAWSBatchComputeEnvironment_updateComputeEnvironmentName(t *testing.T
Config: testAccAWSBatchComputeEnvironmentConfigEC2(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBatchComputeEnvironmentExists(),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_environment_name", expectedName),
resource.TestCheckResourceAttr(resourceName, "compute_environment_name", expectedName),
),
},
{
Config: testAccAWSBatchComputeEnvironmentConfigEC2UpdateComputeEnvironmentName(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBatchComputeEnvironmentExists(),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_environment_name", expectedUpdatedName),
resource.TestCheckResourceAttr(resourceName, "compute_environment_name", expectedUpdatedName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -254,6 +277,7 @@ func TestAccAWSBatchComputeEnvironment_createUnmanagedWithComputeResources(t *te

func TestAccAWSBatchComputeEnvironment_launchTemplate(t *testing.T) {
rInt := acctest.RandInt()
resourceName := "aws_batch_compute_environment.ec2"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) },
Expand All @@ -264,20 +288,26 @@ func TestAccAWSBatchComputeEnvironment_launchTemplate(t *testing.T) {
Config: testAccAWSBatchComputeEnvironmentConfigLaunchTemplate(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBatchComputeEnvironmentExists(),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2",
resource.TestCheckResourceAttr(resourceName,
"compute_resources.0.launch_template.#",
"1"),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2",
resource.TestCheckResourceAttr(resourceName,
"compute_resources.0.launch_template.0.launch_template_name",
fmt.Sprintf("tf_acc_test_%d", rInt)),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSBatchComputeEnvironment_UpdateLaunchTemplate(t *testing.T) {
rInt := acctest.RandInt()
resourceName := "aws_batch_compute_environment.ec2"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) },
Expand All @@ -288,16 +318,21 @@ func TestAccAWSBatchComputeEnvironment_UpdateLaunchTemplate(t *testing.T) {
Config: testAccAWSBatchComputeEnvironmentUpdateLaunchTemplateInExistingComputeEnvironment(rInt, "$Default"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBatchComputeEnvironmentExists(),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_resources.0.launch_template.0.version", "$Default"),
resource.TestCheckResourceAttr(resourceName, "compute_resources.0.launch_template.0.version", "$Default"),
),
},
{
Config: testAccAWSBatchComputeEnvironmentUpdateLaunchTemplateInExistingComputeEnvironment(rInt, "$Latest"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBatchComputeEnvironmentExists(),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_resources.0.launch_template.0.version", "$Latest"),
resource.TestCheckResourceAttr(resourceName, "compute_resources.0.launch_template.0.version", "$Latest"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -320,6 +355,7 @@ func TestAccAWSBatchComputeEnvironment_createSpotWithoutBidPercentage(t *testing

func TestAccAWSBatchComputeEnvironment_updateState(t *testing.T) {
rInt := acctest.RandInt()
resourceName := "aws_batch_compute_environment.ec2"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSBatch(t) },
Expand All @@ -330,16 +366,21 @@ func TestAccAWSBatchComputeEnvironment_updateState(t *testing.T) {
Config: testAccAWSBatchComputeEnvironmentConfigEC2UpdateState(rInt, batch.CEStateEnabled),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBatchComputeEnvironmentExists(),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "state", batch.CEStateEnabled),
resource.TestCheckResourceAttr(resourceName, "state", batch.CEStateEnabled),
),
},
{
Config: testAccAWSBatchComputeEnvironmentConfigEC2UpdateState(rInt, batch.CEStateDisabled),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsBatchComputeEnvironmentExists(),
resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "state", batch.CEStateDisabled),
resource.TestCheckResourceAttr(resourceName, "state", batch.CEStateDisabled),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/batch_compute_environment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,11 @@ resource "aws_batch_compute_environment" "sample" {
[1]: http://docs.aws.amazon.com/batch/latest/userguide/what-is-batch.html
[2]: http://docs.aws.amazon.com/batch/latest/userguide/compute_environments.html
[3]: http://docs.aws.amazon.com/batch/latest/userguide/troubleshooting.html

## Import

AWS Batch compute can be imported using the `compute_environment_name`, e.g.

```
$ terraform import aws_batch_compute_environment.sample sample
```

0 comments on commit 9e8ffe6

Please sign in to comment.