From 4e8198339a7fa7d0cd8c7303fec6632dd6fda8b3 Mon Sep 17 00:00:00 2001 From: Max Cai Date: Fri, 26 Jun 2020 10:12:50 -0400 Subject: [PATCH 01/20] add file --- aws/resource_aws_emr_managed_scaling_policy.go | 0 aws/resource_aws_emr_managed_scaling_policy_test.go | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 aws/resource_aws_emr_managed_scaling_policy.go create mode 100644 aws/resource_aws_emr_managed_scaling_policy_test.go diff --git a/aws/resource_aws_emr_managed_scaling_policy.go b/aws/resource_aws_emr_managed_scaling_policy.go new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/aws/resource_aws_emr_managed_scaling_policy_test.go b/aws/resource_aws_emr_managed_scaling_policy_test.go new file mode 100644 index 000000000000..e69de29bb2d1 From 7a68637bad221b469ba44aed225c30bacd3e10ea Mon Sep 17 00:00:00 2001 From: Max Cai Date: Fri, 26 Jun 2020 17:52:36 -0400 Subject: [PATCH 02/20] add resource_aws_emr_managed_scaling_policy --- aws/provider.go | 1 + ...resource_aws_emr_managed_scaling_policy.go | 151 ++++++++++++++++++ ...rce_aws_emr_managed_scaling_policy_test.go | 1 + aws/validators.go | 9 ++ 4 files changed, 162 insertions(+) diff --git a/aws/provider.go b/aws/provider.go index 34e5c2925841..550b235a5ddb 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -601,6 +601,7 @@ func Provider() *schema.Provider { "aws_elb_attachment": resourceAwsElbAttachment(), "aws_emr_cluster": resourceAwsEMRCluster(), "aws_emr_instance_group": resourceAwsEMRInstanceGroup(), + "aws_emr_managed_scaling_policy": resourceAwsEMRManagedScalingPolicy(), "aws_emr_security_configuration": resourceAwsEMRSecurityConfiguration(), "aws_flow_log": resourceAwsFlowLog(), "aws_fsx_lustre_file_system": resourceAwsFsxLustreFileSystem(), diff --git a/aws/resource_aws_emr_managed_scaling_policy.go b/aws/resource_aws_emr_managed_scaling_policy.go index e69de29bb2d1..f9598e084f49 100644 --- a/aws/resource_aws_emr_managed_scaling_policy.go +++ b/aws/resource_aws_emr_managed_scaling_policy.go @@ -0,0 +1,151 @@ +package aws + +import ( + "log" + // "time" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/emr" + // "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + // "github.com/hashicorp/terraform-plugin-sdk/helper/validation" +) + +func resourceAwsEMRManagedScalingPolicy() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsEMRManagedScalingPolicyCreate, + Read: resourceAwsEMRManagedScalingPolicyRead, + Delete: resourceAwsEMRManagedScalingPolicyDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "cluster_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "compute_limits": { + Type: schema.TypeSet, + Required: true, + ForceNew: true, + Elem: computeLimitsSchema(), + }, + }, + } +} +func computeLimitsSchema() *schema.Resource { + return &schema.Resource{ + Schema: map[string]*schema.Schema{ + "unit_type": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + ValidateFunc: validateAwsEmrComputeLimitsUnitType(), + }, + "minimum_capacity_units": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + Default: 1, + }, + "maximum_capacity_units": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + Default: 1, + }, + "maximum_core_capacity_units": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + Default: 1, + }, + "maximum_ondemand_capacity_units": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + Default: 1, + }, + }, + } +} + +func resourceAwsEMRManagedScalingPolicyCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).emrconn + + if l := d.Get("compute_limits").(*schema.Set).List(); len(l) > 0 && l[0] != nil { + cl := l[0].(map[string]interface{}) + computeLimits := &emr.ComputeLimits{ + UnitType: aws.String(cl["unit_type"].(string)), + MinimumCapacityUnits: aws.Int64(int64(cl["minimum_capacity_units"].(int))), + MaximumCapacityUnits: aws.Int64(int64(cl["maximum_capacity_units"].(int))), + MaximumCoreCapacityUnits: aws.Int64(int64(cl["maximum_core_capacity_units"].(int))), + MaximumOnDemandCapacityUnits: aws.Int64(int64(cl["maximum_ondemand_capacity_units"].(int))), + } + managedScalingPolicy := &emr.ManagedScalingPolicy{ + ComputeLimits: computeLimits, + } + + _, err := conn.PutManagedScalingPolicy(&emr.PutManagedScalingPolicyInput{ + ClusterId: aws.String(d.Get("cluster_id").(string)), + ManagedScalingPolicy: managedScalingPolicy, + }) + + if err != nil { + log.Printf("[ERROR] EMR.PutManagedScalingPolicy %s", err) + return err + } + } + + d.SetId(d.Get("cluster_id").(string)) + + return nil + // return resourceAwsEMRManagedScalingPolicyRead(d, meta) +} + +func resourceAwsEMRManagedScalingPolicyRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).emrconn + resp, err := conn.GetManagedScalingPolicy(&emr.GetManagedScalingPolicyInput{ + ClusterId: aws.String(d.Get("cluster_id").(string)), + }) + if err != nil { + if isAWSErr(err, "InvalidRequestException", "does not exist") { + log.Printf("[WARN] EMR Managed Scaling Policy (%s) not found, removing from state", d.Get("cluster_id").(string)) + d.SetId("") + return nil + } + return err + } + + if resp.ManagedScalingPolicy != nil { + attrs := make(map[string]interface{}) + attrs["unit_type"] = *resp.ManagedScalingPolicy.ComputeLimits.UnitType + attrs["minimum_capacity_units"] = *resp.ManagedScalingPolicy.ComputeLimits.MinimumCapacityUnits + attrs["maximum_capacity_units"] = *resp.ManagedScalingPolicy.ComputeLimits.MaximumCapacityUnits + attrs["maximum_core_capacity_units"] = *resp.ManagedScalingPolicy.ComputeLimits.MaximumCoreCapacityUnits + attrs["maximum_ondemand_capacity_units"] = *resp.ManagedScalingPolicy.ComputeLimits.MaximumOnDemandCapacityUnits + + computeLimits := make([]interface{}, 0) + computeLimits = append(computeLimits, attrs) + d.Set("compute_limits", computeLimits) + } + + return nil +} + +func resourceAwsEMRManagedScalingPolicyDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).emrconn + _, err := conn.RemoveManagedScalingPolicy(&emr.RemoveManagedScalingPolicyInput{ + ClusterId: aws.String(d.Get("cluster_id").(string)), + }) + if err != nil { + if isAWSErr(err, "InvalidRequestException", "does not exist") { + return nil + } + return err + } + return nil +} diff --git a/aws/resource_aws_emr_managed_scaling_policy_test.go b/aws/resource_aws_emr_managed_scaling_policy_test.go index e69de29bb2d1..a1f9c0e3beb4 100644 --- a/aws/resource_aws_emr_managed_scaling_policy_test.go +++ b/aws/resource_aws_emr_managed_scaling_policy_test.go @@ -0,0 +1 @@ +package aws diff --git a/aws/validators.go b/aws/validators.go index 12ae9401c9e1..ba0b012df738 100644 --- a/aws/validators.go +++ b/aws/validators.go @@ -14,6 +14,7 @@ import ( "github.com/aws/aws-sdk-go/service/apigateway" "github.com/aws/aws-sdk-go/service/cognitoidentity" "github.com/aws/aws-sdk-go/service/configservice" + "github.com/aws/aws-sdk-go/service/emr" "github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/waf" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -1150,6 +1151,14 @@ func validateAwsEmrCustomAmiId(v interface{}, k string) (ws []string, errors []e return } +func validateAwsEmrComputeLimitsUnitType() schema.SchemaValidateFunc { + return validation.StringInSlice([]string{ + emr.ComputeLimitsUnitTypeInstanceFleetUnits, + emr.ComputeLimitsUnitTypeInstances, + emr.ComputeLimitsUnitTypeVcpu, + }, false) +} + func validateSfnStateMachineName(v interface{}, k string) (ws []string, errors []error) { value := v.(string) if len(value) > 80 { From 82285945be24b49aafdd53d7a5b6cea872f68140 Mon Sep 17 00:00:00 2001 From: Max Cai Date: Fri, 26 Jun 2020 17:53:18 -0400 Subject: [PATCH 03/20] upgrade aws-sdk-go to 1.32.10 --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index d636b655a529..eccda1f9571e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/terraform-providers/terraform-provider-aws go 1.14 require ( - github.com/aws/aws-sdk-go v1.34.4 + github.com/aws/aws-sdk-go v1.32.10 github.com/beevik/etree v1.1.0 github.com/bflad/tfproviderdocs v0.7.0 github.com/bflad/tfproviderlint v0.18.0 From de89fecd3f163d1376a53ec14a94b2d1fbb4632d Mon Sep 17 00:00:00 2001 From: Max Cai Date: Fri, 26 Jun 2020 17:55:57 -0400 Subject: [PATCH 04/20] format --- aws/resource_aws_emr_managed_scaling_policy.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/aws/resource_aws_emr_managed_scaling_policy.go b/aws/resource_aws_emr_managed_scaling_policy.go index f9598e084f49..87c7d334975a 100644 --- a/aws/resource_aws_emr_managed_scaling_policy.go +++ b/aws/resource_aws_emr_managed_scaling_policy.go @@ -101,9 +101,7 @@ func resourceAwsEMRManagedScalingPolicyCreate(d *schema.ResourceData, meta inter } d.SetId(d.Get("cluster_id").(string)) - return nil - // return resourceAwsEMRManagedScalingPolicyRead(d, meta) } func resourceAwsEMRManagedScalingPolicyRead(d *schema.ResourceData, meta interface{}) error { From bf826cb8a57bea49c2a3739d3302e02a32e76dba Mon Sep 17 00:00:00 2001 From: Max Cai Date: Wed, 5 Aug 2020 22:32:29 -0400 Subject: [PATCH 05/20] go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index eccda1f9571e..36c592c9d9cc 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/terraform-providers/terraform-provider-aws go 1.14 require ( - github.com/aws/aws-sdk-go v1.32.10 + github.com/aws/aws-sdk-go v1.32.12 github.com/beevik/etree v1.1.0 github.com/bflad/tfproviderdocs v0.7.0 github.com/bflad/tfproviderlint v0.18.0 From 44437b668447da73c8a228830032644a6f7597f9 Mon Sep 17 00:00:00 2001 From: Max Cai Date: Thu, 6 Aug 2020 09:43:01 -0400 Subject: [PATCH 06/20] add document --- .../emr_managed_scaling_policy.html.markdown | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 website/docs/r/emr_managed_scaling_policy.html.markdown diff --git a/website/docs/r/emr_managed_scaling_policy.html.markdown b/website/docs/r/emr_managed_scaling_policy.html.markdown new file mode 100644 index 000000000000..0ab249d842dd --- /dev/null +++ b/website/docs/r/emr_managed_scaling_policy.html.markdown @@ -0,0 +1,52 @@ +--- +subcategory: "Elastic Map Reduce (EMR)" +layout: "aws" +page_title: "AWS: aws_emr_managed_scaling_policy" +description: |- + Provides a resource for EMR Managed Scaling policy +--- + +# Resource: aws_emr_managed_scaling_policy + +Provides a Managed Scaling policy for EMR Cluster. With Amazon EMR versions 5.30.0 and later (except for Amazon EMR 6.0.0), you can enable EMR managed scaling to automatically increase or decrease the number of instances or units in your cluster based on workload See [Using EMR Managed Scaling in Amazon EMR](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-managed-scaling.html/) +for more information. + +## Example Usage + +```hcl +resource "aws_emr_cluster" "sample" { + name = "emr-sample-cluster" + release_label = "emr-5.30.0" + + master_instance_group { + instance_type = "m4.large" + } + + core_instance_group { + instance_type = "c4.large" + } +... +} + +resource "aws_emr_managed_scaling_policy" "samplepolicy" { + cluster_id = aws_emr_cluster.sample.id + unit_type = "Instances" + minimum_capacity_units = 2 + maximum_capacity_units = 10 + maximum_ondemand_capacity_units = 2 + maximum_core_capacity_units = 10 +} +``` + +## Argument Reference + +The following arguments are supported: + +* `cluster_id` - (Required) The id of the EMR cluster +* `unit_type` - (Required) The unit type used for specifying a managed scaling policy. Valid Values: `InstanceFleetUnits` | `Instances` | `VCPU` +* `minimum_capacity_units` - (Required) The lower boundary of EC2 units. It is measured through VCPU cores or instances for instance groups and measured through units for instance fleets. Managed scaling activities are not allowed beyond this boundary. The limit only applies to the core and task nodes. The master node cannot be scaled after initial configuration. +* `maximum_capacity_units` - (Required) The upper boundary of EC2 units. It is measured through VCPU cores or instances for instance groups and measured through units for instance fleets. Managed scaling activities are not allowed beyond this boundary. The limit only applies to the core and task nodes. The master node cannot be scaled after initial configuration. +* `maximum_ondemand_capacity_units` - (Optional) The upper boundary of On-Demand EC2 units. It is measured through VCPU cores or instances for instance groups and measured through units for instance fleets. The On-Demand units are not allowed to scale beyond this boundary. The parameter is used to split capacity allocation between On-Demand and Spot instances. +* `maximum_core_capacity_units` - (Optional) The upper boundary of EC2 units for core node type in a cluster. It is measured through VCPU cores or instances for instance groups and measured through units for instance fleets. The core units are not allowed to scale beyond this boundary. The parameter is used to split capacity allocation between core and task nodes. + +## Import From 64bc218086cca4beba45e185296a86263d7c0256 Mon Sep 17 00:00:00 2001 From: Max Cai Date: Thu, 6 Aug 2020 09:46:41 -0400 Subject: [PATCH 07/20] document fmt --- website/docs/r/emr_managed_scaling_policy.html.markdown | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/website/docs/r/emr_managed_scaling_policy.html.markdown b/website/docs/r/emr_managed_scaling_policy.html.markdown index 0ab249d842dd..3e272ee5ffba 100644 --- a/website/docs/r/emr_managed_scaling_policy.html.markdown +++ b/website/docs/r/emr_managed_scaling_policy.html.markdown @@ -8,8 +8,7 @@ description: |- # Resource: aws_emr_managed_scaling_policy -Provides a Managed Scaling policy for EMR Cluster. With Amazon EMR versions 5.30.0 and later (except for Amazon EMR 6.0.0), you can enable EMR managed scaling to automatically increase or decrease the number of instances or units in your cluster based on workload See [Using EMR Managed Scaling in Amazon EMR](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-managed-scaling.html/) -for more information. +Provides a Managed Scaling policy for EMR Cluster. With Amazon EMR versions 5.30.0 and later (except for Amazon EMR 6.0.0), you can enable EMR managed scaling to automatically increase or decrease the number of instances or units in your cluster based on workload. See [Using EMR Managed Scaling in Amazon EMR](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-managed-scaling.html/) for more information. ## Example Usage From 08fcfdb94626050958fb6fa70eb461b3062e6c4f Mon Sep 17 00:00:00 2001 From: Max Cai Date: Thu, 6 Aug 2020 09:54:58 -0400 Subject: [PATCH 08/20] document fmt --- website/docs/r/emr_managed_scaling_policy.html.markdown | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/website/docs/r/emr_managed_scaling_policy.html.markdown b/website/docs/r/emr_managed_scaling_policy.html.markdown index 3e272ee5ffba..c6ca4d7275dd 100644 --- a/website/docs/r/emr_managed_scaling_policy.html.markdown +++ b/website/docs/r/emr_managed_scaling_policy.html.markdown @@ -22,9 +22,9 @@ resource "aws_emr_cluster" "sample" { } core_instance_group { - instance_type = "c4.large" + instance_type = "c4.large" } -... + # skip ... } resource "aws_emr_managed_scaling_policy" "samplepolicy" { @@ -49,3 +49,5 @@ The following arguments are supported: * `maximum_core_capacity_units` - (Optional) The upper boundary of EC2 units for core node type in a cluster. It is measured through VCPU cores or instances for instance groups and measured through units for instance fleets. The core units are not allowed to scale beyond this boundary. The parameter is used to split capacity allocation between core and task nodes. ## Import + +EMR Managed Scaling policy cannot be imported at this time. From e34b0c7987bdcaa6e9de3d9ef8587e37baa2853a Mon Sep 17 00:00:00 2001 From: Max Cai Date: Thu, 6 Aug 2020 09:57:15 -0400 Subject: [PATCH 09/20] document fmt --- .../r/emr_managed_scaling_policy.html.markdown | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/website/docs/r/emr_managed_scaling_policy.html.markdown b/website/docs/r/emr_managed_scaling_policy.html.markdown index c6ca4d7275dd..335a8cb90a45 100644 --- a/website/docs/r/emr_managed_scaling_policy.html.markdown +++ b/website/docs/r/emr_managed_scaling_policy.html.markdown @@ -28,12 +28,14 @@ resource "aws_emr_cluster" "sample" { } resource "aws_emr_managed_scaling_policy" "samplepolicy" { - cluster_id = aws_emr_cluster.sample.id - unit_type = "Instances" - minimum_capacity_units = 2 - maximum_capacity_units = 10 - maximum_ondemand_capacity_units = 2 - maximum_core_capacity_units = 10 + cluster_id = aws_emr_cluster.sample.id + compute_limits { + unit_type = "Instances" + minimum_capacity_units = 2 + maximum_capacity_units = 10 + maximum_ondemand_capacity_units = 2 + maximum_core_capacity_units = 10 + } } ``` From 1e5af9924241ade997c6bfd4840d799fe011022e Mon Sep 17 00:00:00 2001 From: Max Cai Date: Thu, 6 Aug 2020 10:07:51 -0400 Subject: [PATCH 10/20] document fmt --- website/docs/r/emr_managed_scaling_policy.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/emr_managed_scaling_policy.html.markdown b/website/docs/r/emr_managed_scaling_policy.html.markdown index 335a8cb90a45..7f5a9b66091c 100644 --- a/website/docs/r/emr_managed_scaling_policy.html.markdown +++ b/website/docs/r/emr_managed_scaling_policy.html.markdown @@ -8,7 +8,7 @@ description: |- # Resource: aws_emr_managed_scaling_policy -Provides a Managed Scaling policy for EMR Cluster. With Amazon EMR versions 5.30.0 and later (except for Amazon EMR 6.0.0), you can enable EMR managed scaling to automatically increase or decrease the number of instances or units in your cluster based on workload. See [Using EMR Managed Scaling in Amazon EMR](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-managed-scaling.html/) for more information. +Provides a Managed Scaling policy for EMR Cluster. With Amazon EMR versions 5.30.0 and later (except for Amazon EMR 6.0.0), you can enable EMR managed scaling to automatically increase or decrease the number of instances or units in your cluster based on workload. See [Using EMR Managed Scaling in Amazon EMR](https://docs.aws.amazon.com/emr/latest/ManagementGuide/emr-managed-scaling.html) for more information. ## Example Usage From dde888ef9dc211ed487d5449c819fd0777d186b3 Mon Sep 17 00:00:00 2001 From: Max Cai Date: Wed, 19 Aug 2020 10:00:32 -0400 Subject: [PATCH 11/20] Terraform Plugin SDK V2 --- aws/resource_aws_emr_managed_scaling_policy.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/aws/resource_aws_emr_managed_scaling_policy.go b/aws/resource_aws_emr_managed_scaling_policy.go index 87c7d334975a..e0215e3cb056 100644 --- a/aws/resource_aws_emr_managed_scaling_policy.go +++ b/aws/resource_aws_emr_managed_scaling_policy.go @@ -6,9 +6,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/emr" - // "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - // "github.com/hashicorp/terraform-plugin-sdk/helper/validation" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) func resourceAwsEMRManagedScalingPolicy() *schema.Resource { From 636221a8e03d89aeecc56b58bdc671f2216c47ff Mon Sep 17 00:00:00 2001 From: Max Cai Date: Wed, 19 Aug 2020 10:49:52 -0400 Subject: [PATCH 12/20] fix based on comments --- ...resource_aws_emr_managed_scaling_policy.go | 37 ++++++++----------- aws/validators.go | 9 ----- .../emr_managed_scaling_policy.html.markdown | 5 ++- 3 files changed, 19 insertions(+), 32 deletions(-) diff --git a/aws/resource_aws_emr_managed_scaling_policy.go b/aws/resource_aws_emr_managed_scaling_policy.go index e0215e3cb056..eb50c9e8fbfa 100644 --- a/aws/resource_aws_emr_managed_scaling_policy.go +++ b/aws/resource_aws_emr_managed_scaling_policy.go @@ -2,8 +2,6 @@ package aws import ( "log" - // "time" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/emr" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -37,35 +35,30 @@ func computeLimitsSchema() *schema.Resource { return &schema.Resource{ Schema: map[string]*schema.Schema{ "unit_type": { - Type: schema.TypeString, - Optional: true, - Computed: true, - ForceNew: true, - ValidateFunc: validateAwsEmrComputeLimitsUnitType(), + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice(emr.ComputeLimitsUnitType_Values(), false), }, "minimum_capacity_units": { Type: schema.TypeInt, - Optional: true, + Required: true, ForceNew: true, - Default: 1, }, "maximum_capacity_units": { Type: schema.TypeInt, - Optional: true, + Required: true, ForceNew: true, - Default: 1, }, "maximum_core_capacity_units": { Type: schema.TypeInt, Optional: true, ForceNew: true, - Default: 1, }, "maximum_ondemand_capacity_units": { Type: schema.TypeInt, Optional: true, ForceNew: true, - Default: 1, }, }, } @@ -94,7 +87,7 @@ func resourceAwsEMRManagedScalingPolicyCreate(d *schema.ResourceData, meta inter if err != nil { log.Printf("[ERROR] EMR.PutManagedScalingPolicy %s", err) - return err + return fmt.Errorf("error putting EMR Managed Scaling Policy: %w", err) } } @@ -105,7 +98,7 @@ func resourceAwsEMRManagedScalingPolicyCreate(d *schema.ResourceData, meta inter func resourceAwsEMRManagedScalingPolicyRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).emrconn resp, err := conn.GetManagedScalingPolicy(&emr.GetManagedScalingPolicyInput{ - ClusterId: aws.String(d.Get("cluster_id").(string)), + ClusterId: aws.String(d.Id()), }) if err != nil { if isAWSErr(err, "InvalidRequestException", "does not exist") { @@ -113,16 +106,16 @@ func resourceAwsEMRManagedScalingPolicyRead(d *schema.ResourceData, meta interfa d.SetId("") return nil } - return err + return fmt.Errorf("error getting EMR Managed Scaling Policy (%s): %w", d.Id(), err) } if resp.ManagedScalingPolicy != nil { attrs := make(map[string]interface{}) - attrs["unit_type"] = *resp.ManagedScalingPolicy.ComputeLimits.UnitType - attrs["minimum_capacity_units"] = *resp.ManagedScalingPolicy.ComputeLimits.MinimumCapacityUnits - attrs["maximum_capacity_units"] = *resp.ManagedScalingPolicy.ComputeLimits.MaximumCapacityUnits - attrs["maximum_core_capacity_units"] = *resp.ManagedScalingPolicy.ComputeLimits.MaximumCoreCapacityUnits - attrs["maximum_ondemand_capacity_units"] = *resp.ManagedScalingPolicy.ComputeLimits.MaximumOnDemandCapacityUnits + attrs["unit_type"] = aws.StringValue(resp.ManagedScalingPolicy.ComputeLimits.UnitType) + attrs["minimum_capacity_units"] = aws.Int64Value(resp.ManagedScalingPolicy.ComputeLimits.MinimumCapacityUnits) + attrs["maximum_capacity_units"] = aws.Int64Value(resp.ManagedScalingPolicy.ComputeLimits.MaximumCapacityUnits) + attrs["maximum_core_capacity_units"] = aws.Int64Value(resp.ManagedScalingPolicy.ComputeLimits.MaximumCoreCapacityUnits) + attrs["maximum_ondemand_capacity_units"] = aws.Int64Value(resp.ManagedScalingPolicy.ComputeLimits.MaximumOnDemandCapacityUnits) computeLimits := make([]interface{}, 0) computeLimits = append(computeLimits, attrs) @@ -141,7 +134,7 @@ func resourceAwsEMRManagedScalingPolicyDelete(d *schema.ResourceData, meta inter if isAWSErr(err, "InvalidRequestException", "does not exist") { return nil } - return err + return fmt.Errorf("error removing EMR Managed Scaling Policy (%s): %w", d.Id(), err) } return nil } diff --git a/aws/validators.go b/aws/validators.go index ba0b012df738..12ae9401c9e1 100644 --- a/aws/validators.go +++ b/aws/validators.go @@ -14,7 +14,6 @@ import ( "github.com/aws/aws-sdk-go/service/apigateway" "github.com/aws/aws-sdk-go/service/cognitoidentity" "github.com/aws/aws-sdk-go/service/configservice" - "github.com/aws/aws-sdk-go/service/emr" "github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/waf" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -1151,14 +1150,6 @@ func validateAwsEmrCustomAmiId(v interface{}, k string) (ws []string, errors []e return } -func validateAwsEmrComputeLimitsUnitType() schema.SchemaValidateFunc { - return validation.StringInSlice([]string{ - emr.ComputeLimitsUnitTypeInstanceFleetUnits, - emr.ComputeLimitsUnitTypeInstances, - emr.ComputeLimitsUnitTypeVcpu, - }, false) -} - func validateSfnStateMachineName(v interface{}, k string) (ws []string, errors []error) { value := v.(string) if len(value) > 80 { diff --git a/website/docs/r/emr_managed_scaling_policy.html.markdown b/website/docs/r/emr_managed_scaling_policy.html.markdown index 7f5a9b66091c..a35f75f67331 100644 --- a/website/docs/r/emr_managed_scaling_policy.html.markdown +++ b/website/docs/r/emr_managed_scaling_policy.html.markdown @@ -52,4 +52,7 @@ The following arguments are supported: ## Import -EMR Managed Scaling policy cannot be imported at this time. +EMR Managed Scaling Policies can be imported via the EMR Cluster identifier, e.g. +```console +$ terraform import aws_emr_managed_scaling_policy.example j-123456ABCDEF +``` From bbf85789ba1272a369284738ebb80259dd6d3dd4 Mon Sep 17 00:00:00 2001 From: Max Cai Date: Wed, 19 Aug 2020 11:00:23 -0400 Subject: [PATCH 13/20] fix --- aws/resource_aws_emr_managed_scaling_policy.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/aws/resource_aws_emr_managed_scaling_policy.go b/aws/resource_aws_emr_managed_scaling_policy.go index eb50c9e8fbfa..b2b711cbe011 100644 --- a/aws/resource_aws_emr_managed_scaling_policy.go +++ b/aws/resource_aws_emr_managed_scaling_policy.go @@ -1,10 +1,11 @@ package aws import ( - "log" + "fmt" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/emr" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "log" ) func resourceAwsEMRManagedScalingPolicy() *schema.Resource { @@ -35,9 +36,9 @@ func computeLimitsSchema() *schema.Resource { return &schema.Resource{ Schema: map[string]*schema.Schema{ "unit_type": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, ValidateFunc: validation.StringInSlice(emr.ComputeLimitsUnitType_Values(), false), }, "minimum_capacity_units": { From c8b52093b2f244e4cd4abb528d20d04473ef1138 Mon Sep 17 00:00:00 2001 From: Max Cai Date: Wed, 19 Aug 2020 11:28:21 -0400 Subject: [PATCH 14/20] add missing package --- aws/resource_aws_emr_managed_scaling_policy.go | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/resource_aws_emr_managed_scaling_policy.go b/aws/resource_aws_emr_managed_scaling_policy.go index b2b711cbe011..98251552d0c9 100644 --- a/aws/resource_aws_emr_managed_scaling_policy.go +++ b/aws/resource_aws_emr_managed_scaling_policy.go @@ -5,6 +5,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/emr" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "log" ) From cdd8e49ae74ed5e4c15aae292536afcc606fee67 Mon Sep 17 00:00:00 2001 From: Max Cai Date: Wed, 19 Aug 2020 12:49:38 -0400 Subject: [PATCH 15/20] rebase --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 36c592c9d9cc..d636b655a529 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/terraform-providers/terraform-provider-aws go 1.14 require ( - github.com/aws/aws-sdk-go v1.32.12 + github.com/aws/aws-sdk-go v1.34.4 github.com/beevik/etree v1.1.0 github.com/bflad/tfproviderdocs v0.7.0 github.com/bflad/tfproviderlint v0.18.0 From dc0eaee946ab56f3cbc8357d639f58ccf913279f Mon Sep 17 00:00:00 2001 From: Max Cai Date: Wed, 19 Aug 2020 16:04:17 -0400 Subject: [PATCH 16/20] add test --- ...rce_aws_emr_managed_scaling_policy_test.go | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/aws/resource_aws_emr_managed_scaling_policy_test.go b/aws/resource_aws_emr_managed_scaling_policy_test.go index a1f9c0e3beb4..fa3a4d458113 100644 --- a/aws/resource_aws_emr_managed_scaling_policy_test.go +++ b/aws/resource_aws_emr_managed_scaling_policy_test.go @@ -1 +1,74 @@ package aws + +import ( + "fmt" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/service/emr" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +func TestAccAwsEmrManagedScalingPolicy_basic(t *testing.T) { + resourceName := "aws_emr_managed_scaling_policy.testpolicy" + rInt := acctest.RandInt() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSEmrManagedScalingPolicyDestroy, + + Steps: []resource.TestStep{ + { + Config: testAccAWSEmrManagedScalingPolicy_basic(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSEmrManagedScalingPolicyExists(resourceName), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccAWSEmrManagedScalingPolicy_basic(r int) string { + return fmt.Sprintf(testAccAWSEmrInstanceGroupBase+` +resource "aws_emr_managed_scaling_policy" "testpolicy" { + cluster_id = aws_emr_cluster.tf-test-cluster.id + compute_limits { + unit_type = "Instances" + minimum_capacity_units = 1 + maximum_capacity_units = 2 + maximum_ondemand_capacity_units = 2 + maximum_core_capacity_units = 2 + } +} +`, r) +} + +func testAccCheckAWSEmrManagedScalingPolicyExists(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No EMR Managed Scaling Policy ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).emrconn + resp, err := conn.GetManagedScalingPolicy(&emr.GetManagedScalingPolicyInput{ + ClusterId: aws.String(rs.Primary.Attributes["cluster_id"]), + }) + if err != nil { + return err + } + + return nil + } +} From de02135ba2b84aeff0814a0e40a521cec4dc798c Mon Sep 17 00:00:00 2001 From: Max Cai Date: Wed, 19 Aug 2020 17:51:42 -0400 Subject: [PATCH 17/20] fix testin --- ...rce_aws_emr_managed_scaling_policy_test.go | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_emr_managed_scaling_policy_test.go b/aws/resource_aws_emr_managed_scaling_policy_test.go index fa3a4d458113..a2c21456dee4 100644 --- a/aws/resource_aws_emr_managed_scaling_policy_test.go +++ b/aws/resource_aws_emr_managed_scaling_policy_test.go @@ -2,8 +2,9 @@ package aws import ( "fmt" + "testing" + "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/emr" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -69,6 +70,38 @@ func testAccCheckAWSEmrManagedScalingPolicyExists(n string) resource.TestCheckFu return err } + if resp.ManagedScalingPolicy == nil { + return fmt.Errorf("EMR Managed Scaling Policy is empty which shouldn't happen") + } return nil } } + +func testAccCheckAWSEmrManagedScalingPolicyDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).emrconn + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_emr_managed_scaling_policy" { + continue + } + + resp, err := conn.DescribeSecurityConfiguration(&emr.DescribeSecurityConfigurationInput{ + Name: aws.String(rs.Primary.ID), + }) + + if isAWSErr(err, "InvalidRequestException", "does not exist") { + return nil + } + + if err != nil { + return err + } + + if resp != nil { + return fmt.Errorf("Error: EMR Managed Scaling Policy still exists") + } + + return nil + } + + return nil +} From 1d8017c1bd6ff8bd70075b036b3a55da3d11bbdd Mon Sep 17 00:00:00 2001 From: Max Cai Date: Thu, 20 Aug 2020 21:17:06 -0400 Subject: [PATCH 18/20] fix acct test --- ...resource_aws_emr_managed_scaling_policy.go | 63 ++-- ...rce_aws_emr_managed_scaling_policy_test.go | 318 +++++++++++++++++- 2 files changed, 343 insertions(+), 38 deletions(-) diff --git a/aws/resource_aws_emr_managed_scaling_policy.go b/aws/resource_aws_emr_managed_scaling_policy.go index 98251552d0c9..7448f3693a54 100644 --- a/aws/resource_aws_emr_managed_scaling_policy.go +++ b/aws/resource_aws_emr_managed_scaling_policy.go @@ -28,39 +28,36 @@ func resourceAwsEMRManagedScalingPolicy() *schema.Resource { Type: schema.TypeSet, Required: true, ForceNew: true, - Elem: computeLimitsSchema(), - }, - }, - } -} -func computeLimitsSchema() *schema.Resource { - return &schema.Resource{ - Schema: map[string]*schema.Schema{ - "unit_type": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice(emr.ComputeLimitsUnitType_Values(), false), - }, - "minimum_capacity_units": { - Type: schema.TypeInt, - Required: true, - ForceNew: true, - }, - "maximum_capacity_units": { - Type: schema.TypeInt, - Required: true, - ForceNew: true, - }, - "maximum_core_capacity_units": { - Type: schema.TypeInt, - Optional: true, - ForceNew: true, - }, - "maximum_ondemand_capacity_units": { - Type: schema.TypeInt, - Optional: true, - ForceNew: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "unit_type": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice(emr.ComputeLimitsUnitType_Values(), false), + }, + "minimum_capacity_units": { + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, + "maximum_capacity_units": { + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, + "maximum_core_capacity_units": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, + "maximum_ondemand_capacity_units": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, + }, + }, }, }, } diff --git a/aws/resource_aws_emr_managed_scaling_policy_test.go b/aws/resource_aws_emr_managed_scaling_policy_test.go index a2c21456dee4..00959b5c0ab9 100644 --- a/aws/resource_aws_emr_managed_scaling_policy_test.go +++ b/aws/resource_aws_emr_managed_scaling_policy_test.go @@ -37,9 +37,317 @@ func TestAccAwsEmrManagedScalingPolicy_basic(t *testing.T) { } func testAccAWSEmrManagedScalingPolicy_basic(r int) string { - return fmt.Sprintf(testAccAWSEmrInstanceGroupBase+` + return fmt.Sprintf(` +data "aws_availability_zones" "available" { + # Many instance types are not available in this availability zone + exclude_zone_ids = ["usw2-az4"] + state = "available" + filter { + name = "opt-in-status" + values = ["opt-in-not-required"] + } +} + +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + enable_dns_hostnames = true + + tags = { + Name = "tf-acc-test-emr-cluster" + } +} + +resource "aws_internet_gateway" "test" { + vpc_id = aws_vpc.test.id + + tags = { + Name = "tf-acc-test-emr-cluster" + } +} + +resource "aws_security_group" "test" { + vpc_id = aws_vpc.test.id + + ingress { + from_port = 0 + protocol = "-1" + self = true + to_port = 0 + } + + egress { + cidr_blocks = ["0.0.0.0/0"] + from_port = 0 + protocol = "-1" + to_port = 0 + } + + tags = { + Name = "tf-acc-test-emr-cluster" + } + + # EMR will modify ingress rules + lifecycle { + ignore_changes = [ingress] + } +} + +resource "aws_subnet" "test" { + availability_zone = data.aws_availability_zones.available.names[0] + cidr_block = "10.0.0.0/24" + map_public_ip_on_launch = false + vpc_id = aws_vpc.test.id + + tags = { + Name = "tf-acc-test-emr-cluster" + } +} + +resource "aws_route_table" "test" { + vpc_id = aws_vpc.test.id + + route { + cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.test.id + } +} + +resource "aws_route_table_association" "test" { + route_table_id = aws_route_table.test.id + subnet_id = aws_subnet.test.id +} + +resource "aws_iam_role" "emr_service" { + name = "%[1]s_default_role" + + assume_role_policy = < Date: Thu, 20 Aug 2020 21:25:24 -0400 Subject: [PATCH 19/20] fix acct test --- aws/resource_aws_emr_managed_scaling_policy_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/aws/resource_aws_emr_managed_scaling_policy_test.go b/aws/resource_aws_emr_managed_scaling_policy_test.go index 00959b5c0ab9..228f39e76aa6 100644 --- a/aws/resource_aws_emr_managed_scaling_policy_test.go +++ b/aws/resource_aws_emr_managed_scaling_policy_test.go @@ -13,8 +13,7 @@ import ( func TestAccAwsEmrManagedScalingPolicy_basic(t *testing.T) { resourceName := "aws_emr_managed_scaling_policy.testpolicy" - rInt := acctest.RandInt() - + rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -22,7 +21,7 @@ func TestAccAwsEmrManagedScalingPolicy_basic(t *testing.T) { Steps: []resource.TestStep{ { - Config: testAccAWSEmrManagedScalingPolicy_basic(rInt), + Config: testAccAWSEmrManagedScalingPolicy_basic(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAWSEmrManagedScalingPolicyExists(resourceName), ), @@ -36,7 +35,7 @@ func TestAccAwsEmrManagedScalingPolicy_basic(t *testing.T) { }) } -func testAccAWSEmrManagedScalingPolicy_basic(r int) string { +func testAccAWSEmrManagedScalingPolicy_basic(r string) string { return fmt.Sprintf(` data "aws_availability_zones" "available" { # Many instance types are not available in this availability zone From 44340eb74c33405f69065d6ffea9dd0356a45454 Mon Sep 17 00:00:00 2001 From: Max Cai Date: Fri, 21 Aug 2020 20:55:22 -0400 Subject: [PATCH 20/20] add more acc test --- ...resource_aws_emr_managed_scaling_policy.go | 14 +- ...rce_aws_emr_managed_scaling_policy_test.go | 273 +++++++++++------- .../emr_managed_scaling_policy.html.markdown | 4 + 3 files changed, 178 insertions(+), 113 deletions(-) diff --git a/aws/resource_aws_emr_managed_scaling_policy.go b/aws/resource_aws_emr_managed_scaling_policy.go index 7448f3693a54..9df131a4cd12 100644 --- a/aws/resource_aws_emr_managed_scaling_policy.go +++ b/aws/resource_aws_emr_managed_scaling_policy.go @@ -69,11 +69,15 @@ func resourceAwsEMRManagedScalingPolicyCreate(d *schema.ResourceData, meta inter if l := d.Get("compute_limits").(*schema.Set).List(); len(l) > 0 && l[0] != nil { cl := l[0].(map[string]interface{}) computeLimits := &emr.ComputeLimits{ - UnitType: aws.String(cl["unit_type"].(string)), - MinimumCapacityUnits: aws.Int64(int64(cl["minimum_capacity_units"].(int))), - MaximumCapacityUnits: aws.Int64(int64(cl["maximum_capacity_units"].(int))), - MaximumCoreCapacityUnits: aws.Int64(int64(cl["maximum_core_capacity_units"].(int))), - MaximumOnDemandCapacityUnits: aws.Int64(int64(cl["maximum_ondemand_capacity_units"].(int))), + UnitType: aws.String(cl["unit_type"].(string)), + MinimumCapacityUnits: aws.Int64(int64(cl["minimum_capacity_units"].(int))), + MaximumCapacityUnits: aws.Int64(int64(cl["maximum_capacity_units"].(int))), + } + if v, ok := cl["maximum_core_capacity_units"].(int); ok && v > 0 { + computeLimits.MaximumCoreCapacityUnits = aws.Int64(int64(v)) + } + if v, ok := cl["maximum_ondemand_capacity_units"].(int); ok && v > 0 { + computeLimits.MaximumOnDemandCapacityUnits = aws.Int64(int64(v)) } managedScalingPolicy := &emr.ManagedScalingPolicy{ ComputeLimits: computeLimits, diff --git a/aws/resource_aws_emr_managed_scaling_policy_test.go b/aws/resource_aws_emr_managed_scaling_policy_test.go index 228f39e76aa6..03a8b5a709ac 100644 --- a/aws/resource_aws_emr_managed_scaling_policy_test.go +++ b/aws/resource_aws_emr_managed_scaling_policy_test.go @@ -35,8 +35,171 @@ func TestAccAwsEmrManagedScalingPolicy_basic(t *testing.T) { }) } +func TestAccAwsEmrManagedScalingPolicy_ComputeLimits_MaximumCoreCapacityUnits(t *testing.T) { + resourceName := "aws_emr_managed_scaling_policy.testpolicy" + rName := acctest.RandomWithPrefix("tf-acc-test") + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSEmrManagedScalingPolicyDestroy, + + Steps: []resource.TestStep{ + { + Config: testAccAWSEmrManagedScalingPolicy_ComputeLimits_MaximumCoreCapacityUnits(rName, 2), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSEmrManagedScalingPolicyExists(resourceName), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAwsEmrManagedScalingPolicy_ComputeLimits_MaximumOndemandCapacityUnits(t *testing.T) { + resourceName := "aws_emr_managed_scaling_policy.testpolicy" + rName := acctest.RandomWithPrefix("tf-acc-test") + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSEmrManagedScalingPolicyDestroy, + + Steps: []resource.TestStep{ + { + Config: testAccAWSEmrManagedScalingPolicy_ComputeLimits_MaximumOndemandCapacityUnits(rName, 2), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSEmrManagedScalingPolicyExists(resourceName), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAwsEmrManagedScalingPolicy_disappears(t *testing.T) { + resourceName := "aws_emr_managed_scaling_policy.testpolicy" + rName := acctest.RandomWithPrefix("tf-acc-test") + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSEmrManagedScalingPolicyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSEmrManagedScalingPolicy_basic(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSEmrManagedScalingPolicyExists(resourceName), + testAccCheckResourceDisappears(testAccProvider, resourceAwsEMRManagedScalingPolicy(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func testAccAWSEmrManagedScalingPolicy_basic(r string) string { - return fmt.Sprintf(` + return fmt.Sprintf(testAccAWSEmrManagedScalingPolicyBase+` +resource "aws_emr_managed_scaling_policy" "testpolicy" { + cluster_id = aws_emr_cluster.test.id + compute_limits { + unit_type = "Instances" + minimum_capacity_units = 1 + maximum_capacity_units = 2 + } +} +`, r) +} + +func testAccAWSEmrManagedScalingPolicy_ComputeLimits_MaximumCoreCapacityUnits(r string, maximumCoreCapacityUnits int) string { + return fmt.Sprintf(testAccAWSEmrManagedScalingPolicyBase+` +resource "aws_emr_managed_scaling_policy" "testpolicy" { + cluster_id = aws_emr_cluster.test.id + compute_limits { + unit_type = "Instances" + minimum_capacity_units = 1 + maximum_capacity_units = 2 + maximum_core_capacity_units = %[2]d + } +} +`, r, maximumCoreCapacityUnits) +} + +func testAccAWSEmrManagedScalingPolicy_ComputeLimits_MaximumOndemandCapacityUnits(r string, maximumOndemandCapacityUnits int) string { + return fmt.Sprintf(testAccAWSEmrManagedScalingPolicyBase+` +resource "aws_emr_managed_scaling_policy" "testpolicy" { + cluster_id = aws_emr_cluster.test.id + compute_limits { + unit_type = "Instances" + minimum_capacity_units = 1 + maximum_capacity_units = 2 + maximum_ondemand_capacity_units = %[2]d + } +} +`, r, maximumOndemandCapacityUnits) +} + +func testAccCheckAWSEmrManagedScalingPolicyExists(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No EMR Managed Scaling Policy ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).emrconn + resp, err := conn.GetManagedScalingPolicy(&emr.GetManagedScalingPolicyInput{ + ClusterId: aws.String(rs.Primary.ID), + }) + if err != nil { + return err + } + + if resp.ManagedScalingPolicy == nil { + return fmt.Errorf("EMR Managed Scaling Policy is empty which shouldn't happen") + } + return nil + } +} + +func testAccCheckAWSEmrManagedScalingPolicyDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).emrconn + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_emr_managed_scaling_policy" { + continue + } + + resp, err := conn.GetManagedScalingPolicy(&emr.GetManagedScalingPolicyInput{ + ClusterId: aws.String(rs.Primary.ID), + }) + + if isAWSErr(err, "InvalidRequestException", "does not exist") { + return nil + } + + if err != nil { + return err + } + + if resp != nil { + return fmt.Errorf("Error: EMR Managed Scaling Policy still exists") + } + + return nil + } + + return nil +} + +const testAccAWSEmrManagedScalingPolicyBase = ` data "aws_availability_zones" "available" { # Many instance types are not available in this availability zone exclude_zone_ids = ["usw2-az4"] @@ -275,23 +438,6 @@ resource "aws_iam_policy" "emr_instance_profile" { EOT } -resource "aws_s3_bucket" "tester" { - bucket = "%[1]s" - acl = "public-read" -} - -resource "aws_s3_bucket_object" "testobject" { - bucket = aws_s3_bucket.tester.bucket - key = "testscript.sh" - content = <