github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_dms_endpoint.go (about) 1 package aws 2 3 import ( 4 "log" 5 "strings" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/private/waiter" 10 dms "github.com/aws/aws-sdk-go/service/databasemigrationservice" 11 "github.com/hashicorp/terraform/helper/schema" 12 "github.com/hashicorp/terraform/helper/validation" 13 ) 14 15 func resourceAwsDmsEndpoint() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceAwsDmsEndpointCreate, 18 Read: resourceAwsDmsEndpointRead, 19 Update: resourceAwsDmsEndpointUpdate, 20 Delete: resourceAwsDmsEndpointDelete, 21 22 Importer: &schema.ResourceImporter{ 23 State: schema.ImportStatePassthrough, 24 }, 25 26 Schema: map[string]*schema.Schema{ 27 "certificate_arn": { 28 Type: schema.TypeString, 29 Computed: true, 30 Optional: true, 31 ValidateFunc: validateArn, 32 }, 33 "database_name": { 34 Type: schema.TypeString, 35 Optional: true, 36 }, 37 "endpoint_arn": { 38 Type: schema.TypeString, 39 Computed: true, 40 }, 41 "endpoint_id": { 42 Type: schema.TypeString, 43 Required: true, 44 ForceNew: true, 45 ValidateFunc: validateDmsEndpointId, 46 }, 47 "endpoint_type": { 48 Type: schema.TypeString, 49 Required: true, 50 ValidateFunc: validation.StringInSlice([]string{ 51 "source", 52 "target", 53 }, false), 54 }, 55 "engine_name": { 56 Type: schema.TypeString, 57 Required: true, 58 ValidateFunc: validation.StringInSlice([]string{ 59 "mysql", 60 "oracle", 61 "postgres", 62 "mariadb", 63 "aurora", 64 "redshift", 65 "sybase", 66 "sqlserver", 67 }, false), 68 }, 69 "extra_connection_attributes": { 70 Type: schema.TypeString, 71 Computed: true, 72 Optional: true, 73 }, 74 "kms_key_arn": { 75 Type: schema.TypeString, 76 Computed: true, 77 Optional: true, 78 ForceNew: true, 79 ValidateFunc: validateArn, 80 }, 81 "password": { 82 Type: schema.TypeString, 83 Required: true, 84 Sensitive: true, 85 }, 86 "port": { 87 Type: schema.TypeInt, 88 Required: true, 89 }, 90 "server_name": { 91 Type: schema.TypeString, 92 Required: true, 93 }, 94 "ssl_mode": { 95 Type: schema.TypeString, 96 Computed: true, 97 Optional: true, 98 ValidateFunc: validation.StringInSlice([]string{ 99 "none", 100 "require", 101 "verify-ca", 102 "verify-full", 103 }, false), 104 }, 105 "tags": { 106 Type: schema.TypeMap, 107 Optional: true, 108 }, 109 "username": { 110 Type: schema.TypeString, 111 Required: true, 112 }, 113 }, 114 } 115 } 116 117 func resourceAwsDmsEndpointCreate(d *schema.ResourceData, meta interface{}) error { 118 conn := meta.(*AWSClient).dmsconn 119 120 request := &dms.CreateEndpointInput{ 121 EndpointIdentifier: aws.String(d.Get("endpoint_id").(string)), 122 EndpointType: aws.String(d.Get("endpoint_type").(string)), 123 EngineName: aws.String(d.Get("engine_name").(string)), 124 Password: aws.String(d.Get("password").(string)), 125 Port: aws.Int64(int64(d.Get("port").(int))), 126 ServerName: aws.String(d.Get("server_name").(string)), 127 Tags: dmsTagsFromMap(d.Get("tags").(map[string]interface{})), 128 Username: aws.String(d.Get("username").(string)), 129 } 130 131 if v, ok := d.GetOk("database_name"); ok { 132 request.DatabaseName = aws.String(v.(string)) 133 } 134 if v, ok := d.GetOk("certificate_arn"); ok { 135 request.CertificateArn = aws.String(v.(string)) 136 } 137 if v, ok := d.GetOk("extra_connection_attributes"); ok { 138 request.ExtraConnectionAttributes = aws.String(v.(string)) 139 } 140 if v, ok := d.GetOk("kms_key_arn"); ok { 141 request.KmsKeyId = aws.String(v.(string)) 142 } 143 if v, ok := d.GetOk("ssl_mode"); ok { 144 request.SslMode = aws.String(v.(string)) 145 } 146 147 log.Println("[DEBUG] DMS create endpoint:", request) 148 149 _, err := conn.CreateEndpoint(request) 150 if err != nil { 151 return err 152 } 153 154 d.SetId(d.Get("endpoint_id").(string)) 155 return resourceAwsDmsEndpointRead(d, meta) 156 } 157 158 func resourceAwsDmsEndpointRead(d *schema.ResourceData, meta interface{}) error { 159 conn := meta.(*AWSClient).dmsconn 160 161 response, err := conn.DescribeEndpoints(&dms.DescribeEndpointsInput{ 162 Filters: []*dms.Filter{ 163 { 164 Name: aws.String("endpoint-id"), 165 Values: []*string{aws.String(d.Id())}, // Must use d.Id() to work with import. 166 }, 167 }, 168 }) 169 if err != nil { 170 if dmserr, ok := err.(awserr.Error); ok && dmserr.Code() == "ResourceNotFoundFault" { 171 d.SetId("") 172 return nil 173 } 174 return err 175 } 176 177 err = resourceAwsDmsEndpointSetState(d, response.Endpoints[0]) 178 if err != nil { 179 return err 180 } 181 182 tagsResp, err := conn.ListTagsForResource(&dms.ListTagsForResourceInput{ 183 ResourceArn: aws.String(d.Get("endpoint_arn").(string)), 184 }) 185 if err != nil { 186 return err 187 } 188 d.Set("tags", dmsTagsToMap(tagsResp.TagList)) 189 190 return nil 191 } 192 193 func resourceAwsDmsEndpointUpdate(d *schema.ResourceData, meta interface{}) error { 194 conn := meta.(*AWSClient).dmsconn 195 196 request := &dms.ModifyEndpointInput{ 197 EndpointArn: aws.String(d.Get("endpoint_arn").(string)), 198 } 199 hasChanges := false 200 201 if d.HasChange("certificate_arn") { 202 request.CertificateArn = aws.String(d.Get("certificate_arn").(string)) 203 hasChanges = true 204 } 205 206 if d.HasChange("database_name") { 207 request.DatabaseName = aws.String(d.Get("database_name").(string)) 208 hasChanges = true 209 } 210 211 if d.HasChange("endpoint_type") { 212 request.EndpointType = aws.String(d.Get("endpoint_type").(string)) 213 hasChanges = true 214 } 215 216 if d.HasChange("engine_name") { 217 request.EngineName = aws.String(d.Get("engine_name").(string)) 218 hasChanges = true 219 } 220 221 if d.HasChange("extra_connection_attributes") { 222 request.ExtraConnectionAttributes = aws.String(d.Get("extra_connection_attributes").(string)) 223 hasChanges = true 224 } 225 226 if d.HasChange("password") { 227 request.Password = aws.String(d.Get("password").(string)) 228 hasChanges = true 229 } 230 231 if d.HasChange("port") { 232 request.Port = aws.Int64(int64(d.Get("port").(int))) 233 hasChanges = true 234 } 235 236 if d.HasChange("server_name") { 237 request.ServerName = aws.String(d.Get("server_name").(string)) 238 hasChanges = true 239 } 240 241 if d.HasChange("ssl_mode") { 242 request.SslMode = aws.String(d.Get("ssl_mode").(string)) 243 hasChanges = true 244 } 245 246 if d.HasChange("username") { 247 request.Username = aws.String(d.Get("username").(string)) 248 hasChanges = true 249 } 250 251 if d.HasChange("tags") { 252 err := dmsSetTags(d.Get("endpoint_arn").(string), d, meta) 253 if err != nil { 254 return err 255 } 256 } 257 258 if hasChanges { 259 log.Println("[DEBUG] DMS update endpoint:", request) 260 261 _, err := conn.ModifyEndpoint(request) 262 if err != nil { 263 return err 264 } 265 266 return resourceAwsDmsEndpointRead(d, meta) 267 } 268 269 return nil 270 } 271 272 func resourceAwsDmsEndpointDelete(d *schema.ResourceData, meta interface{}) error { 273 conn := meta.(*AWSClient).dmsconn 274 275 request := &dms.DeleteEndpointInput{ 276 EndpointArn: aws.String(d.Get("endpoint_arn").(string)), 277 } 278 279 log.Printf("[DEBUG] DMS delete endpoint: %#v", request) 280 281 _, err := conn.DeleteEndpoint(request) 282 if err != nil { 283 return err 284 } 285 286 waitErr := waitForEndpointDelete(conn, d.Get("endpoint_id").(string), 30, 20) 287 if waitErr != nil { 288 return waitErr 289 } 290 291 return nil 292 } 293 294 func resourceAwsDmsEndpointSetState(d *schema.ResourceData, endpoint *dms.Endpoint) error { 295 d.SetId(*endpoint.EndpointIdentifier) 296 297 d.Set("certificate_arn", endpoint.CertificateArn) 298 d.Set("database_name", endpoint.DatabaseName) 299 d.Set("endpoint_arn", endpoint.EndpointArn) 300 d.Set("endpoint_id", endpoint.EndpointIdentifier) 301 // For some reason the AWS API only accepts lowercase type but returns it as uppercase 302 d.Set("endpoint_type", strings.ToLower(*endpoint.EndpointType)) 303 d.Set("engine_name", endpoint.EngineName) 304 d.Set("extra_connection_attributes", endpoint.ExtraConnectionAttributes) 305 d.Set("kms_key_arn", endpoint.KmsKeyId) 306 d.Set("port", endpoint.Port) 307 d.Set("server_name", endpoint.ServerName) 308 d.Set("ssl_mode", endpoint.SslMode) 309 d.Set("username", endpoint.Username) 310 311 return nil 312 } 313 314 func waitForEndpointDelete(client *dms.DatabaseMigrationService, endpointId string, delay int, maxAttempts int) error { 315 input := &dms.DescribeEndpointsInput{ 316 Filters: []*dms.Filter{ 317 { 318 Name: aws.String("endpoint-id"), 319 Values: []*string{aws.String(endpointId)}, 320 }, 321 }, 322 } 323 324 config := waiter.Config{ 325 Operation: "DescribeEndpoints", 326 Delay: delay, 327 MaxAttempts: maxAttempts, 328 Acceptors: []waiter.WaitAcceptor{ 329 { 330 State: "success", 331 Matcher: "path", 332 Argument: "length(Endpoints[]) > `0`", 333 Expected: false, 334 }, 335 }, 336 } 337 338 w := waiter.Waiter{ 339 Client: client, 340 Input: input, 341 Config: config, 342 } 343 344 return w.Wait() 345 }