|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/aws/aws-sdk-go/aws" |
| 6 | + "github.com/aws/aws-sdk-go/service/emr" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 9 | + "log" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceAwsEMRManagedScalingPolicy() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Create: resourceAwsEMRManagedScalingPolicyCreate, |
| 15 | + Read: resourceAwsEMRManagedScalingPolicyRead, |
| 16 | + Delete: resourceAwsEMRManagedScalingPolicyDelete, |
| 17 | + Importer: &schema.ResourceImporter{ |
| 18 | + State: schema.ImportStatePassthrough, |
| 19 | + }, |
| 20 | + |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "cluster_id": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + ForceNew: true, |
| 26 | + }, |
| 27 | + "compute_limits": { |
| 28 | + Type: schema.TypeSet, |
| 29 | + Required: true, |
| 30 | + ForceNew: true, |
| 31 | + Elem: &schema.Resource{ |
| 32 | + Schema: map[string]*schema.Schema{ |
| 33 | + "unit_type": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Required: true, |
| 36 | + ForceNew: true, |
| 37 | + ValidateFunc: validation.StringInSlice(emr.ComputeLimitsUnitType_Values(), false), |
| 38 | + }, |
| 39 | + "minimum_capacity_units": { |
| 40 | + Type: schema.TypeInt, |
| 41 | + Required: true, |
| 42 | + ForceNew: true, |
| 43 | + }, |
| 44 | + "maximum_capacity_units": { |
| 45 | + Type: schema.TypeInt, |
| 46 | + Required: true, |
| 47 | + ForceNew: true, |
| 48 | + }, |
| 49 | + "maximum_core_capacity_units": { |
| 50 | + Type: schema.TypeInt, |
| 51 | + Optional: true, |
| 52 | + ForceNew: true, |
| 53 | + }, |
| 54 | + "maximum_ondemand_capacity_units": { |
| 55 | + Type: schema.TypeInt, |
| 56 | + Optional: true, |
| 57 | + ForceNew: true, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func resourceAwsEMRManagedScalingPolicyCreate(d *schema.ResourceData, meta interface{}) error { |
| 67 | + conn := meta.(*AWSClient).emrconn |
| 68 | + |
| 69 | + if l := d.Get("compute_limits").(*schema.Set).List(); len(l) > 0 && l[0] != nil { |
| 70 | + cl := l[0].(map[string]interface{}) |
| 71 | + computeLimits := &emr.ComputeLimits{ |
| 72 | + UnitType: aws.String(cl["unit_type"].(string)), |
| 73 | + MinimumCapacityUnits: aws.Int64(int64(cl["minimum_capacity_units"].(int))), |
| 74 | + MaximumCapacityUnits: aws.Int64(int64(cl["maximum_capacity_units"].(int))), |
| 75 | + } |
| 76 | + if v, ok := cl["maximum_core_capacity_units"].(int); ok && v > 0 { |
| 77 | + computeLimits.MaximumCoreCapacityUnits = aws.Int64(int64(v)) |
| 78 | + } |
| 79 | + if v, ok := cl["maximum_ondemand_capacity_units"].(int); ok && v > 0 { |
| 80 | + computeLimits.MaximumOnDemandCapacityUnits = aws.Int64(int64(v)) |
| 81 | + } |
| 82 | + managedScalingPolicy := &emr.ManagedScalingPolicy{ |
| 83 | + ComputeLimits: computeLimits, |
| 84 | + } |
| 85 | + |
| 86 | + _, err := conn.PutManagedScalingPolicy(&emr.PutManagedScalingPolicyInput{ |
| 87 | + ClusterId: aws.String(d.Get("cluster_id").(string)), |
| 88 | + ManagedScalingPolicy: managedScalingPolicy, |
| 89 | + }) |
| 90 | + |
| 91 | + if err != nil { |
| 92 | + log.Printf("[ERROR] EMR.PutManagedScalingPolicy %s", err) |
| 93 | + return fmt.Errorf("error putting EMR Managed Scaling Policy: %w", err) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + d.SetId(d.Get("cluster_id").(string)) |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +func resourceAwsEMRManagedScalingPolicyRead(d *schema.ResourceData, meta interface{}) error { |
| 102 | + conn := meta.(*AWSClient).emrconn |
| 103 | + resp, err := conn.GetManagedScalingPolicy(&emr.GetManagedScalingPolicyInput{ |
| 104 | + ClusterId: aws.String(d.Id()), |
| 105 | + }) |
| 106 | + if err != nil { |
| 107 | + if isAWSErr(err, "InvalidRequestException", "does not exist") { |
| 108 | + log.Printf("[WARN] EMR Managed Scaling Policy (%s) not found, removing from state", d.Get("cluster_id").(string)) |
| 109 | + d.SetId("") |
| 110 | + return nil |
| 111 | + } |
| 112 | + return fmt.Errorf("error getting EMR Managed Scaling Policy (%s): %w", d.Id(), err) |
| 113 | + } |
| 114 | + |
| 115 | + if resp.ManagedScalingPolicy != nil { |
| 116 | + attrs := make(map[string]interface{}) |
| 117 | + attrs["unit_type"] = aws.StringValue(resp.ManagedScalingPolicy.ComputeLimits.UnitType) |
| 118 | + attrs["minimum_capacity_units"] = aws.Int64Value(resp.ManagedScalingPolicy.ComputeLimits.MinimumCapacityUnits) |
| 119 | + attrs["maximum_capacity_units"] = aws.Int64Value(resp.ManagedScalingPolicy.ComputeLimits.MaximumCapacityUnits) |
| 120 | + attrs["maximum_core_capacity_units"] = aws.Int64Value(resp.ManagedScalingPolicy.ComputeLimits.MaximumCoreCapacityUnits) |
| 121 | + attrs["maximum_ondemand_capacity_units"] = aws.Int64Value(resp.ManagedScalingPolicy.ComputeLimits.MaximumOnDemandCapacityUnits) |
| 122 | + |
| 123 | + computeLimits := make([]interface{}, 0) |
| 124 | + computeLimits = append(computeLimits, attrs) |
| 125 | + d.Set("compute_limits", computeLimits) |
| 126 | + } |
| 127 | + |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +func resourceAwsEMRManagedScalingPolicyDelete(d *schema.ResourceData, meta interface{}) error { |
| 132 | + conn := meta.(*AWSClient).emrconn |
| 133 | + _, err := conn.RemoveManagedScalingPolicy(&emr.RemoveManagedScalingPolicyInput{ |
| 134 | + ClusterId: aws.String(d.Get("cluster_id").(string)), |
| 135 | + }) |
| 136 | + if err != nil { |
| 137 | + if isAWSErr(err, "InvalidRequestException", "does not exist") { |
| 138 | + return nil |
| 139 | + } |
| 140 | + return fmt.Errorf("error removing EMR Managed Scaling Policy (%s): %w", d.Id(), err) |
| 141 | + } |
| 142 | + return nil |
| 143 | +} |
0 commit comments