github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_autoscaling_policy.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/service/autoscaling" 9 "github.com/hashicorp/terraform/helper/schema" 10 ) 11 12 func resourceAwsAutoscalingPolicy() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceAwsAutoscalingPolicyCreate, 15 Read: resourceAwsAutoscalingPolicyRead, 16 Update: resourceAwsAutoscalingPolicyUpdate, 17 Delete: resourceAwsAutoscalingPolicyDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "arn": &schema.Schema{ 21 Type: schema.TypeString, 22 Computed: true, 23 }, 24 "name": &schema.Schema{ 25 Type: schema.TypeString, 26 Required: true, 27 ForceNew: true, 28 }, 29 "adjustment_type": &schema.Schema{ 30 Type: schema.TypeString, 31 Required: true, 32 }, 33 "autoscaling_group_name": &schema.Schema{ 34 Type: schema.TypeString, 35 Required: true, 36 ForceNew: true, 37 }, 38 "cooldown": &schema.Schema{ 39 Type: schema.TypeInt, 40 Optional: true, 41 }, 42 "min_adjustment_step": &schema.Schema{ 43 Type: schema.TypeInt, 44 Optional: true, 45 }, 46 "scaling_adjustment": &schema.Schema{ 47 Type: schema.TypeInt, 48 Required: true, 49 }, 50 }, 51 } 52 } 53 54 func resourceAwsAutoscalingPolicyCreate(d *schema.ResourceData, meta interface{}) error { 55 autoscalingconn := meta.(*AWSClient).autoscalingconn 56 57 params := getAwsAutoscalingPutScalingPolicyInput(d) 58 59 log.Printf("[DEBUG] AutoScaling PutScalingPolicy: %#v", params) 60 resp, err := autoscalingconn.PutScalingPolicy(¶ms) 61 if err != nil { 62 return fmt.Errorf("Error putting scaling policy: %s", err) 63 } 64 65 d.Set("arn", resp.PolicyARN) 66 d.SetId(d.Get("name").(string)) 67 log.Printf("[INFO] AutoScaling Scaling PolicyARN: %s", d.Get("arn").(string)) 68 69 return resourceAwsAutoscalingPolicyRead(d, meta) 70 } 71 72 func resourceAwsAutoscalingPolicyRead(d *schema.ResourceData, meta interface{}) error { 73 p, err := getAwsAutoscalingPolicy(d, meta) 74 if err != nil { 75 return err 76 } 77 if p == nil { 78 d.SetId("") 79 return nil 80 } 81 82 log.Printf("[DEBUG] Read Scaling Policy: ASG: %s, SP: %s, Obj: %#v", d.Get("autoscaling_group_name"), d.Get("name"), p) 83 84 d.Set("adjustment_type", p.AdjustmentType) 85 d.Set("autoscaling_group_name", p.AutoScalingGroupName) 86 d.Set("cooldown", p.Cooldown) 87 d.Set("min_adjustment_step", p.MinAdjustmentStep) 88 d.Set("arn", p.PolicyARN) 89 d.Set("name", p.PolicyName) 90 d.Set("scaling_adjustment", p.ScalingAdjustment) 91 92 return nil 93 } 94 95 func resourceAwsAutoscalingPolicyUpdate(d *schema.ResourceData, meta interface{}) error { 96 autoscalingconn := meta.(*AWSClient).autoscalingconn 97 98 params := getAwsAutoscalingPutScalingPolicyInput(d) 99 100 log.Printf("[DEBUG] Autoscaling Update Scaling Policy: %#v", params) 101 _, err := autoscalingconn.PutScalingPolicy(¶ms) 102 if err != nil { 103 return err 104 } 105 106 return resourceAwsAutoscalingPolicyRead(d, meta) 107 } 108 109 func resourceAwsAutoscalingPolicyDelete(d *schema.ResourceData, meta interface{}) error { 110 autoscalingconn := meta.(*AWSClient).autoscalingconn 111 p, err := getAwsAutoscalingPolicy(d, meta) 112 if err != nil { 113 return err 114 } 115 if p == nil { 116 return nil 117 } 118 119 params := autoscaling.DeletePolicyInput{ 120 AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)), 121 PolicyName: aws.String(d.Get("name").(string)), 122 } 123 if _, err := autoscalingconn.DeletePolicy(¶ms); err != nil { 124 return fmt.Errorf("Autoscaling Scaling Policy: %s ", err) 125 } 126 127 d.SetId("") 128 return nil 129 } 130 131 // PutScalingPolicy seems to require all params to be resent, so create and update can share this common function 132 func getAwsAutoscalingPutScalingPolicyInput(d *schema.ResourceData) autoscaling.PutScalingPolicyInput { 133 var params = autoscaling.PutScalingPolicyInput{ 134 AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)), 135 PolicyName: aws.String(d.Get("name").(string)), 136 } 137 138 if v, ok := d.GetOk("adjustment_type"); ok { 139 params.AdjustmentType = aws.String(v.(string)) 140 } 141 142 if v, ok := d.GetOk("cooldown"); ok { 143 params.Cooldown = aws.Int64(int64(v.(int))) 144 } 145 146 if v, ok := d.GetOk("scaling_adjustment"); ok { 147 params.ScalingAdjustment = aws.Int64(int64(v.(int))) 148 } 149 150 if v, ok := d.GetOk("min_adjustment_step"); ok { 151 params.MinAdjustmentStep = aws.Int64(int64(v.(int))) 152 } 153 154 return params 155 } 156 157 func getAwsAutoscalingPolicy(d *schema.ResourceData, meta interface{}) (*autoscaling.ScalingPolicy, error) { 158 autoscalingconn := meta.(*AWSClient).autoscalingconn 159 160 params := autoscaling.DescribePoliciesInput{ 161 AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)), 162 PolicyNames: []*string{aws.String(d.Get("name").(string))}, 163 } 164 165 log.Printf("[DEBUG] AutoScaling Scaling Policy Describe Params: %#v", params) 166 resp, err := autoscalingconn.DescribePolicies(¶ms) 167 if err != nil { 168 return nil, fmt.Errorf("Error retrieving scaling policies: %s", err) 169 } 170 171 // find scaling policy 172 name := d.Get("name") 173 for idx, sp := range resp.ScalingPolicies { 174 if *sp.PolicyName == name { 175 return resp.ScalingPolicies[idx], nil 176 } 177 } 178 179 // policy not found 180 return nil, nil 181 }