github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 dms "github.com/aws/aws-sdk-go/service/databasemigrationservice" 10 "github.com/hashicorp/terraform/helper/schema" 11 "github.com/hashicorp/terraform/helper/validation" 12 ) 13 14 func resourceAwsDmsEndpoint() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceAwsDmsEndpointCreate, 17 Read: resourceAwsDmsEndpointRead, 18 Update: resourceAwsDmsEndpointUpdate, 19 Delete: resourceAwsDmsEndpointDelete, 20 21 Importer: &schema.ResourceImporter{ 22 State: schema.ImportStatePassthrough, 23 }, 24 25 Schema: map[string]*schema.Schema{ 26 "certificate_arn": { 27 Type: schema.TypeString, 28 Computed: true, 29 Optional: true, 30 ValidateFunc: validateArn, 31 }, 32 "database_name": { 33 Type: schema.TypeString, 34 Optional: true, 35 }, 36 "endpoint_arn": { 37 Type: schema.TypeString, 38 Computed: true, 39 }, 40 "endpoint_id": { 41 Type: schema.TypeString, 42 Required: true, 43 ForceNew: true, 44 ValidateFunc: validateDmsEndpointId, 45 }, 46 "endpoint_type": { 47 Type: schema.TypeString, 48 Required: true, 49 ValidateFunc: validation.StringInSlice([]string{ 50 "source", 51 "target", 52 }, false), 53 }, 54 "engine_name": { 55 Type: schema.TypeString, 56 Required: true, 57 ValidateFunc: validation.StringInSlice([]string{ 58 "mysql", 59 "oracle", 60 "postgres", 61 "mariadb", 62 "aurora", 63 "redshift", 64 "sybase", 65 "sqlserver", 66 }, false), 67 }, 68 "extra_connection_attributes": { 69 Type: schema.TypeString, 70 Computed: true, 71 Optional: true, 72 }, 73 "kms_key_arn": { 74 Type: schema.TypeString, 75 Computed: true, 76 Optional: true, 77 ForceNew: true, 78 ValidateFunc: validateArn, 79 }, 80 "password": { 81 Type: schema.TypeString, 82 Required: true, 83 Sensitive: true, 84 }, 85 "port": { 86 Type: schema.TypeInt, 87 Required: true, 88 }, 89 "server_name": { 90 Type: schema.TypeString, 91 Required: true, 92 }, 93 "ssl_mode": { 94 Type: schema.TypeString, 95 Computed: true, 96 Optional: true, 97 ValidateFunc: validation.StringInSlice([]string{ 98 "none", 99 "require", 100 "verify-ca", 101 "verify-full", 102 }, false), 103 }, 104 "tags": { 105 Type: schema.TypeMap, 106 Optional: true, 107 }, 108 "username": { 109 Type: schema.TypeString, 110 Required: true, 111 }, 112 }, 113 } 114 } 115 116 func resourceAwsDmsEndpointCreate(d *schema.ResourceData, meta interface{}) error { 117 conn := meta.(*AWSClient).dmsconn 118 119 request := &dms.CreateEndpointInput{ 120 EndpointIdentifier: aws.String(d.Get("endpoint_id").(string)), 121 EndpointType: aws.String(d.Get("endpoint_type").(string)), 122 EngineName: aws.String(d.Get("engine_name").(string)), 123 Password: aws.String(d.Get("password").(string)), 124 Port: aws.Int64(int64(d.Get("port").(int))), 125 ServerName: aws.String(d.Get("server_name").(string)), 126 Tags: dmsTagsFromMap(d.Get("tags").(map[string]interface{})), 127 Username: aws.String(d.Get("username").(string)), 128 } 129 130 if v, ok := d.GetOk("database_name"); ok { 131 request.DatabaseName = aws.String(v.(string)) 132 } 133 if v, ok := d.GetOk("certificate_arn"); ok { 134 request.CertificateArn = aws.String(v.(string)) 135 } 136 if v, ok := d.GetOk("extra_connection_attributes"); ok { 137 request.ExtraConnectionAttributes = aws.String(v.(string)) 138 } 139 if v, ok := d.GetOk("kms_key_arn"); ok { 140 request.KmsKeyId = aws.String(v.(string)) 141 } 142 if v, ok := d.GetOk("ssl_mode"); ok { 143 request.SslMode = aws.String(v.(string)) 144 } 145 146 log.Println("[DEBUG] DMS create endpoint:", request) 147 148 _, err := conn.CreateEndpoint(request) 149 if err != nil { 150 return err 151 } 152 153 d.SetId(d.Get("endpoint_id").(string)) 154 return resourceAwsDmsEndpointRead(d, meta) 155 } 156 157 func resourceAwsDmsEndpointRead(d *schema.ResourceData, meta interface{}) error { 158 conn := meta.(*AWSClient).dmsconn 159 160 response, err := conn.DescribeEndpoints(&dms.DescribeEndpointsInput{ 161 Filters: []*dms.Filter{ 162 { 163 Name: aws.String("endpoint-id"), 164 Values: []*string{aws.String(d.Id())}, // Must use d.Id() to work with import. 165 }, 166 }, 167 }) 168 if err != nil { 169 if dmserr, ok := err.(awserr.Error); ok && dmserr.Code() == "ResourceNotFoundFault" { 170 log.Printf("[DEBUG] DMS Replication Endpoint %q Not Found", d.Id()) 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 return nil 287 } 288 289 func resourceAwsDmsEndpointSetState(d *schema.ResourceData, endpoint *dms.Endpoint) error { 290 d.SetId(*endpoint.EndpointIdentifier) 291 292 d.Set("certificate_arn", endpoint.CertificateArn) 293 d.Set("database_name", endpoint.DatabaseName) 294 d.Set("endpoint_arn", endpoint.EndpointArn) 295 d.Set("endpoint_id", endpoint.EndpointIdentifier) 296 // For some reason the AWS API only accepts lowercase type but returns it as uppercase 297 d.Set("endpoint_type", strings.ToLower(*endpoint.EndpointType)) 298 d.Set("engine_name", endpoint.EngineName) 299 d.Set("extra_connection_attributes", endpoint.ExtraConnectionAttributes) 300 d.Set("kms_key_arn", endpoint.KmsKeyId) 301 d.Set("port", endpoint.Port) 302 d.Set("server_name", endpoint.ServerName) 303 d.Set("ssl_mode", endpoint.SslMode) 304 d.Set("username", endpoint.Username) 305 306 return nil 307 }