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

Issue 13952 #13965

Merged
merged 20 commits into from
Aug 25, 2020
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
139 changes: 139 additions & 0 deletions aws/resource_aws_emr_managed_scaling_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package aws

import (
"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"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"log"
)

func resourceAwsEMRManagedScalingPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceAwsEMRManagedScalingPolicyCreate,
Read: resourceAwsEMRManagedScalingPolicyRead,
Delete: resourceAwsEMRManagedScalingPolicyDelete,
Importer: &schema.ResourceImporter{
bflad marked this conversation as resolved.
Show resolved Hide resolved
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: &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,
},
},
},
},
},
}
}

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))),
c4po marked this conversation as resolved.
Show resolved Hide resolved
}
managedScalingPolicy := &emr.ManagedScalingPolicy{
ComputeLimits: computeLimits,
}

_, err := conn.PutManagedScalingPolicy(&emr.PutManagedScalingPolicyInput{
Copy link
Contributor

Choose a reason for hiding this comment

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

Certainly does not need to be for this pull request, but since this API uses a PUT-type call, it should allow the resource to support an Update function (essentially similar to this Create function, except no d.SetId(), then removing all ForceNew from the compute_limits attributes). May be worth creating a followup GitHub issue if it is not implemented since the forced recreation may be a little confusing for operators.

ClusterId: aws.String(d.Get("cluster_id").(string)),
ManagedScalingPolicy: managedScalingPolicy,
})

if err != nil {
log.Printf("[ERROR] EMR.PutManagedScalingPolicy %s", err)
return fmt.Errorf("error putting EMR Managed Scaling Policy: %w", err)
}
}

d.SetId(d.Get("cluster_id").(string))
return nil
}

func resourceAwsEMRManagedScalingPolicyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).emrconn
resp, err := conn.GetManagedScalingPolicy(&emr.GetManagedScalingPolicyInput{
ClusterId: aws.String(d.Id()),
})
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 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"] = 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)
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 fmt.Errorf("error removing EMR Managed Scaling Policy (%s): %w", d.Id(), err)
}
return nil
}
Loading