github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_opsworks_rds_db_instance.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/aws/awserr" 10 "github.com/aws/aws-sdk-go/service/opsworks" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/helper/schema" 13 ) 14 15 func resourceAwsOpsworksRdsDbInstance() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceAwsOpsworksRdsDbInstanceRegister, 18 Update: resourceAwsOpsworksRdsDbInstanceUpdate, 19 Delete: resourceAwsOpsworksRdsDbInstanceDeregister, 20 Read: resourceAwsOpsworksRdsDbInstanceRead, 21 22 Schema: map[string]*schema.Schema{ 23 "id": { 24 Type: schema.TypeString, 25 Computed: true, 26 }, 27 "stack_id": { 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 }, 32 "rds_db_instance_arn": { 33 Type: schema.TypeString, 34 Required: true, 35 ForceNew: true, 36 }, 37 "db_password": { 38 Type: schema.TypeString, 39 Required: true, 40 Sensitive: true, 41 }, 42 "db_user": { 43 Type: schema.TypeString, 44 Required: true, 45 }, 46 }, 47 } 48 } 49 50 func resourceAwsOpsworksRdsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error { 51 client := meta.(*AWSClient).opsworksconn 52 53 d.Partial(true) 54 55 d.SetPartial("rds_db_instance_arn") 56 req := &opsworks.UpdateRdsDbInstanceInput{ 57 RdsDbInstanceArn: aws.String(d.Get("rds_db_instance_arn").(string)), 58 } 59 60 requestUpdate := false 61 if d.HasChange("db_user") { 62 d.SetPartial("db_user") 63 req.DbUser = aws.String(d.Get("db_user").(string)) 64 requestUpdate = true 65 } 66 if d.HasChange("db_password") { 67 d.SetPartial("db_password") 68 req.DbPassword = aws.String(d.Get("db_password").(string)) 69 requestUpdate = true 70 } 71 72 if true == requestUpdate { 73 log.Printf("[DEBUG] Opsworks RDS DB Instance Modification request: %s", req) 74 75 err := resource.Retry(2*time.Minute, func() *resource.RetryError { 76 var cerr error 77 _, cerr = client.UpdateRdsDbInstance(req) 78 if cerr != nil { 79 log.Printf("[INFO] client error") 80 if opserr, ok := cerr.(awserr.Error); ok { 81 log.Printf("[ERROR] OpsWorks error: %s message: %s", opserr.Code(), opserr.Message()) 82 } 83 return resource.NonRetryableError(cerr) 84 } 85 return nil 86 }) 87 88 if err != nil { 89 return err 90 } 91 92 } 93 94 d.Partial(false) 95 96 return resourceAwsOpsworksRdsDbInstanceRead(d, meta) 97 } 98 99 func resourceAwsOpsworksRdsDbInstanceDeregister(d *schema.ResourceData, meta interface{}) error { 100 client := meta.(*AWSClient).opsworksconn 101 102 req := &opsworks.DeregisterRdsDbInstanceInput{ 103 RdsDbInstanceArn: aws.String(d.Get("rds_db_instance_arn").(string)), 104 } 105 106 log.Printf("[DEBUG] Unregistering rds db instance '%s' from stack: %s", d.Get("rds_db_instance_arn"), d.Get("stack_id")) 107 108 err := resource.Retry(2*time.Minute, func() *resource.RetryError { 109 var cerr error 110 _, cerr = client.DeregisterRdsDbInstance(req) 111 if cerr != nil { 112 log.Printf("[INFO] client error") 113 if opserr, ok := cerr.(awserr.Error); ok { 114 if opserr.Code() == "ResourceNotFoundException" { 115 log.Printf("[INFO] The db instance could not be found. Remove it from state.") 116 d.SetId("") 117 118 return nil 119 } 120 log.Printf("[ERROR] OpsWorks error: %s message: %s", opserr.Code(), opserr.Message()) 121 } 122 return resource.NonRetryableError(cerr) 123 } 124 125 return nil 126 }) 127 128 if err != nil { 129 return err 130 } 131 132 return nil 133 } 134 135 func resourceAwsOpsworksRdsDbInstanceRead(d *schema.ResourceData, meta interface{}) error { 136 client := meta.(*AWSClient).opsworksconn 137 138 req := &opsworks.DescribeRdsDbInstancesInput{ 139 StackId: aws.String(d.Get("stack_id").(string)), 140 } 141 142 log.Printf("[DEBUG] Reading OpsWorks registerd rds db instances for stack: %s", d.Get("stack_id")) 143 144 resp, err := client.DescribeRdsDbInstances(req) 145 if err != nil { 146 return err 147 } 148 149 found := false 150 id := "" 151 for _, instance := range resp.RdsDbInstances { 152 id = fmt.Sprintf("%s%s", *instance.RdsDbInstanceArn, *instance.StackId) 153 154 if fmt.Sprintf("%s%s", d.Get("rds_db_instance_arn").(string), d.Get("stack_id").(string)) == id { 155 found = true 156 d.SetId(id) 157 d.Set("id", id) 158 d.Set("stack_id", instance.StackId) 159 d.Set("rds_db_instance_arn", instance.RdsDbInstanceArn) 160 d.Set("db_user", instance.DbUser) 161 } 162 163 } 164 165 if false == found { 166 d.SetId("") 167 log.Printf("[INFO] The rds instance '%s' could not be found for stack: '%s'", d.Get("rds_db_instance_arn"), d.Get("stack_id")) 168 } 169 170 return nil 171 } 172 173 func resourceAwsOpsworksRdsDbInstanceRegister(d *schema.ResourceData, meta interface{}) error { 174 client := meta.(*AWSClient).opsworksconn 175 176 req := &opsworks.RegisterRdsDbInstanceInput{ 177 StackId: aws.String(d.Get("stack_id").(string)), 178 RdsDbInstanceArn: aws.String(d.Get("rds_db_instance_arn").(string)), 179 DbUser: aws.String(d.Get("db_user").(string)), 180 DbPassword: aws.String(d.Get("db_password").(string)), 181 } 182 183 err := resource.Retry(2*time.Minute, func() *resource.RetryError { 184 var cerr error 185 _, cerr = client.RegisterRdsDbInstance(req) 186 if cerr != nil { 187 log.Printf("[INFO] client error") 188 if opserr, ok := cerr.(awserr.Error); ok { 189 log.Printf("[ERROR] OpsWorks error: %s message: %s", opserr.Code(), opserr.Message()) 190 } 191 return resource.NonRetryableError(cerr) 192 } 193 194 return nil 195 }) 196 197 if err != nil { 198 return err 199 } 200 201 return resourceAwsOpsworksRdsDbInstanceRead(d, meta) 202 }