github.com/ricardclau/terraform@v0.6.17-0.20160519222547-283e3ae6b5a9/builtin/providers/aws/resource_aws_redshift_cluster.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "regexp" 7 "strings" 8 "time" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/aws/awserr" 12 "github.com/aws/aws-sdk-go/service/redshift" 13 "github.com/hashicorp/terraform/helper/resource" 14 "github.com/hashicorp/terraform/helper/schema" 15 ) 16 17 func resourceAwsRedshiftCluster() *schema.Resource { 18 return &schema.Resource{ 19 Create: resourceAwsRedshiftClusterCreate, 20 Read: resourceAwsRedshiftClusterRead, 21 Update: resourceAwsRedshiftClusterUpdate, 22 Delete: resourceAwsRedshiftClusterDelete, 23 24 Schema: map[string]*schema.Schema{ 25 "database_name": &schema.Schema{ 26 Type: schema.TypeString, 27 Optional: true, 28 Computed: true, 29 ValidateFunc: validateRedshiftClusterDbName, 30 }, 31 32 "cluster_identifier": &schema.Schema{ 33 Type: schema.TypeString, 34 Required: true, 35 ForceNew: true, 36 ValidateFunc: validateRedshiftClusterIdentifier, 37 }, 38 "cluster_type": &schema.Schema{ 39 Type: schema.TypeString, 40 Optional: true, 41 Computed: true, 42 }, 43 44 "node_type": &schema.Schema{ 45 Type: schema.TypeString, 46 Required: true, 47 }, 48 49 "master_username": &schema.Schema{ 50 Type: schema.TypeString, 51 Required: true, 52 ValidateFunc: validateRedshiftClusterMasterUsername, 53 }, 54 55 "master_password": &schema.Schema{ 56 Type: schema.TypeString, 57 Required: true, 58 }, 59 60 "cluster_security_groups": &schema.Schema{ 61 Type: schema.TypeSet, 62 Optional: true, 63 Computed: true, 64 Elem: &schema.Schema{Type: schema.TypeString}, 65 Set: schema.HashString, 66 }, 67 68 "vpc_security_group_ids": &schema.Schema{ 69 Type: schema.TypeSet, 70 Optional: true, 71 Computed: true, 72 Elem: &schema.Schema{Type: schema.TypeString}, 73 Set: schema.HashString, 74 }, 75 76 "cluster_subnet_group_name": &schema.Schema{ 77 Type: schema.TypeString, 78 Optional: true, 79 ForceNew: true, 80 Computed: true, 81 }, 82 83 "availability_zone": &schema.Schema{ 84 Type: schema.TypeString, 85 Optional: true, 86 Computed: true, 87 }, 88 89 "preferred_maintenance_window": &schema.Schema{ 90 Type: schema.TypeString, 91 Optional: true, 92 Computed: true, 93 StateFunc: func(val interface{}) string { 94 if val == nil { 95 return "" 96 } 97 return strings.ToLower(val.(string)) 98 }, 99 }, 100 101 "cluster_parameter_group_name": &schema.Schema{ 102 Type: schema.TypeString, 103 Optional: true, 104 Computed: true, 105 }, 106 107 "automated_snapshot_retention_period": &schema.Schema{ 108 Type: schema.TypeInt, 109 Optional: true, 110 Default: 1, 111 ValidateFunc: func(v interface{}, k string) (ws []string, es []error) { 112 value := v.(int) 113 if value > 35 { 114 es = append(es, fmt.Errorf( 115 "backup retention period cannot be more than 35 days")) 116 } 117 return 118 }, 119 }, 120 121 "port": &schema.Schema{ 122 Type: schema.TypeInt, 123 Optional: true, 124 Default: 5439, 125 }, 126 127 "cluster_version": &schema.Schema{ 128 Type: schema.TypeString, 129 Optional: true, 130 Default: "1.0", 131 }, 132 133 "allow_version_upgrade": &schema.Schema{ 134 Type: schema.TypeBool, 135 Optional: true, 136 Default: true, 137 }, 138 139 "number_of_nodes": &schema.Schema{ 140 Type: schema.TypeInt, 141 Optional: true, 142 Default: 1, 143 }, 144 145 "publicly_accessible": &schema.Schema{ 146 Type: schema.TypeBool, 147 Optional: true, 148 Default: true, 149 }, 150 151 "encrypted": &schema.Schema{ 152 Type: schema.TypeBool, 153 Optional: true, 154 Computed: true, 155 }, 156 157 "kms_key_id": &schema.Schema{ 158 Type: schema.TypeString, 159 Optional: true, 160 Computed: true, 161 ForceNew: true, 162 }, 163 164 "elastic_ip": &schema.Schema{ 165 Type: schema.TypeString, 166 Optional: true, 167 }, 168 169 "final_snapshot_identifier": &schema.Schema{ 170 Type: schema.TypeString, 171 Optional: true, 172 ValidateFunc: validateRedshiftClusterFinalSnapshotIdentifier, 173 }, 174 175 "skip_final_snapshot": &schema.Schema{ 176 Type: schema.TypeBool, 177 Optional: true, 178 Default: true, 179 }, 180 181 "endpoint": &schema.Schema{ 182 Type: schema.TypeString, 183 Optional: true, 184 Computed: true, 185 }, 186 187 "cluster_public_key": &schema.Schema{ 188 Type: schema.TypeString, 189 Optional: true, 190 Computed: true, 191 }, 192 193 "cluster_revision_number": &schema.Schema{ 194 Type: schema.TypeString, 195 Optional: true, 196 Computed: true, 197 }, 198 }, 199 } 200 } 201 202 func resourceAwsRedshiftClusterCreate(d *schema.ResourceData, meta interface{}) error { 203 conn := meta.(*AWSClient).redshiftconn 204 205 log.Printf("[INFO] Building Redshift Cluster Options") 206 createOpts := &redshift.CreateClusterInput{ 207 ClusterIdentifier: aws.String(d.Get("cluster_identifier").(string)), 208 Port: aws.Int64(int64(d.Get("port").(int))), 209 MasterUserPassword: aws.String(d.Get("master_password").(string)), 210 MasterUsername: aws.String(d.Get("master_username").(string)), 211 ClusterVersion: aws.String(d.Get("cluster_version").(string)), 212 NodeType: aws.String(d.Get("node_type").(string)), 213 DBName: aws.String(d.Get("database_name").(string)), 214 AllowVersionUpgrade: aws.Bool(d.Get("allow_version_upgrade").(bool)), 215 PubliclyAccessible: aws.Bool(d.Get("publicly_accessible").(bool)), 216 AutomatedSnapshotRetentionPeriod: aws.Int64(int64(d.Get("automated_snapshot_retention_period").(int))), 217 } 218 219 if v := d.Get("number_of_nodes").(int); v > 1 { 220 createOpts.ClusterType = aws.String("multi-node") 221 createOpts.NumberOfNodes = aws.Int64(int64(d.Get("number_of_nodes").(int))) 222 } else { 223 createOpts.ClusterType = aws.String("single-node") 224 } 225 226 if v := d.Get("cluster_security_groups").(*schema.Set); v.Len() > 0 { 227 createOpts.ClusterSecurityGroups = expandStringList(v.List()) 228 } 229 230 if v := d.Get("vpc_security_group_ids").(*schema.Set); v.Len() > 0 { 231 createOpts.VpcSecurityGroupIds = expandStringList(v.List()) 232 } 233 234 if v, ok := d.GetOk("cluster_subnet_group_name"); ok { 235 createOpts.ClusterSubnetGroupName = aws.String(v.(string)) 236 } 237 238 if v, ok := d.GetOk("availability_zone"); ok { 239 createOpts.AvailabilityZone = aws.String(v.(string)) 240 } 241 242 if v, ok := d.GetOk("preferred_maintenance_window"); ok { 243 createOpts.PreferredMaintenanceWindow = aws.String(v.(string)) 244 } 245 246 if v, ok := d.GetOk("cluster_parameter_group_name"); ok { 247 createOpts.ClusterParameterGroupName = aws.String(v.(string)) 248 } 249 250 if v, ok := d.GetOk("encrypted"); ok { 251 createOpts.Encrypted = aws.Bool(v.(bool)) 252 } 253 254 if v, ok := d.GetOk("kms_key_id"); ok { 255 createOpts.KmsKeyId = aws.String(v.(string)) 256 } 257 258 if v, ok := d.GetOk("elastic_ip"); ok { 259 createOpts.ElasticIp = aws.String(v.(string)) 260 } 261 262 log.Printf("[DEBUG] Redshift Cluster create options: %s", createOpts) 263 resp, err := conn.CreateCluster(createOpts) 264 if err != nil { 265 log.Printf("[ERROR] Error creating Redshift Cluster: %s", err) 266 return err 267 } 268 269 log.Printf("[DEBUG]: Cluster create response: %s", resp) 270 d.SetId(*resp.Cluster.ClusterIdentifier) 271 272 stateConf := &resource.StateChangeConf{ 273 Pending: []string{"creating", "backing-up", "modifying"}, 274 Target: []string{"available"}, 275 Refresh: resourceAwsRedshiftClusterStateRefreshFunc(d, meta), 276 Timeout: 40 * time.Minute, 277 MinTimeout: 10 * time.Second, 278 } 279 280 _, err = stateConf.WaitForState() 281 if err != nil { 282 return fmt.Errorf("[WARN] Error waiting for Redshift Cluster state to be \"available\": %s", err) 283 } 284 285 return resourceAwsRedshiftClusterRead(d, meta) 286 } 287 288 func resourceAwsRedshiftClusterRead(d *schema.ResourceData, meta interface{}) error { 289 conn := meta.(*AWSClient).redshiftconn 290 291 log.Printf("[INFO] Reading Redshift Cluster Information: %s", d.Id()) 292 resp, err := conn.DescribeClusters(&redshift.DescribeClustersInput{ 293 ClusterIdentifier: aws.String(d.Id()), 294 }) 295 296 if err != nil { 297 if awsErr, ok := err.(awserr.Error); ok { 298 if "ClusterNotFound" == awsErr.Code() { 299 d.SetId("") 300 log.Printf("[DEBUG] Redshift Cluster (%s) not found", d.Id()) 301 return nil 302 } 303 } 304 log.Printf("[DEBUG] Error describing Redshift Cluster (%s)", d.Id()) 305 return err 306 } 307 308 var rsc *redshift.Cluster 309 for _, c := range resp.Clusters { 310 if *c.ClusterIdentifier == d.Id() { 311 rsc = c 312 } 313 } 314 315 if rsc == nil { 316 log.Printf("[WARN] Redshift Cluster (%s) not found", d.Id()) 317 d.SetId("") 318 return nil 319 } 320 321 d.Set("database_name", rsc.DBName) 322 d.Set("cluster_subnet_group_name", rsc.ClusterSubnetGroupName) 323 d.Set("availability_zone", rsc.AvailabilityZone) 324 d.Set("encrypted", rsc.Encrypted) 325 d.Set("kms_key_id", rsc.KmsKeyId) 326 d.Set("automated_snapshot_retention_period", rsc.AutomatedSnapshotRetentionPeriod) 327 d.Set("preferred_maintenance_window", rsc.PreferredMaintenanceWindow) 328 if rsc.Endpoint != nil && rsc.Endpoint.Address != nil { 329 endpoint := *rsc.Endpoint.Address 330 if rsc.Endpoint.Port != nil { 331 endpoint = fmt.Sprintf("%s:%d", endpoint, *rsc.Endpoint.Port) 332 } 333 d.Set("endpoint", endpoint) 334 } 335 d.Set("cluster_parameter_group_name", rsc.ClusterParameterGroups[0].ParameterGroupName) 336 if len(rsc.ClusterNodes) > 1 { 337 d.Set("cluster_type", "multi-node") 338 } else { 339 d.Set("cluster_type", "single-node") 340 } 341 342 var vpcg []string 343 for _, g := range rsc.VpcSecurityGroups { 344 vpcg = append(vpcg, *g.VpcSecurityGroupId) 345 } 346 if err := d.Set("vpc_security_group_ids", vpcg); err != nil { 347 return fmt.Errorf("[DEBUG] Error saving VPC Security Group IDs to state for Redshift Cluster (%s): %s", d.Id(), err) 348 } 349 350 var csg []string 351 for _, g := range rsc.ClusterSecurityGroups { 352 csg = append(csg, *g.ClusterSecurityGroupName) 353 } 354 if err := d.Set("cluster_security_groups", csg); err != nil { 355 return fmt.Errorf("[DEBUG] Error saving Cluster Security Group Names to state for Redshift Cluster (%s): %s", d.Id(), err) 356 } 357 358 d.Set("cluster_public_key", rsc.ClusterPublicKey) 359 d.Set("cluster_revision_number", rsc.ClusterRevisionNumber) 360 361 return nil 362 } 363 364 func resourceAwsRedshiftClusterUpdate(d *schema.ResourceData, meta interface{}) error { 365 conn := meta.(*AWSClient).redshiftconn 366 367 log.Printf("[INFO] Building Redshift Modify Cluster Options") 368 req := &redshift.ModifyClusterInput{ 369 ClusterIdentifier: aws.String(d.Id()), 370 } 371 372 if d.HasChange("cluster_type") { 373 req.ClusterType = aws.String(d.Get("cluster_type").(string)) 374 } 375 376 if d.HasChange("node_type") { 377 req.NodeType = aws.String(d.Get("node_type").(string)) 378 } 379 380 if d.HasChange("number_of_nodes") { 381 if v := d.Get("number_of_nodes").(int); v > 1 { 382 req.ClusterType = aws.String("multi-node") 383 req.NumberOfNodes = aws.Int64(int64(d.Get("number_of_nodes").(int))) 384 } else { 385 req.ClusterType = aws.String("single-node") 386 } 387 req.NodeType = aws.String(d.Get("node_type").(string)) 388 } 389 390 if d.HasChange("cluster_security_groups") { 391 req.ClusterSecurityGroups = expandStringList(d.Get("cluster_security_groups").(*schema.Set).List()) 392 } 393 394 if d.HasChange("vpc_security_group_ips") { 395 req.VpcSecurityGroupIds = expandStringList(d.Get("vpc_security_group_ips").(*schema.Set).List()) 396 } 397 398 if d.HasChange("master_password") { 399 req.MasterUserPassword = aws.String(d.Get("master_password").(string)) 400 } 401 402 if d.HasChange("cluster_parameter_group_name") { 403 req.ClusterParameterGroupName = aws.String(d.Get("cluster_parameter_group_name").(string)) 404 } 405 406 if d.HasChange("automated_snapshot_retention_period") { 407 req.AutomatedSnapshotRetentionPeriod = aws.Int64(int64(d.Get("automated_snapshot_retention_period").(int))) 408 } 409 410 if d.HasChange("preferred_maintenance_window") { 411 req.PreferredMaintenanceWindow = aws.String(d.Get("preferred_maintenance_window").(string)) 412 } 413 414 if d.HasChange("cluster_version") { 415 req.ClusterVersion = aws.String(d.Get("cluster_version").(string)) 416 } 417 418 if d.HasChange("allow_version_upgrade") { 419 req.AllowVersionUpgrade = aws.Bool(d.Get("allow_version_upgrade").(bool)) 420 } 421 422 if d.HasChange("publicly_accessible") { 423 req.PubliclyAccessible = aws.Bool(d.Get("publicly_accessible").(bool)) 424 } 425 426 log.Printf("[INFO] Modifying Redshift Cluster: %s", d.Id()) 427 log.Printf("[DEBUG] Redshift Cluster Modify options: %s", req) 428 _, err := conn.ModifyCluster(req) 429 if err != nil { 430 return fmt.Errorf("[WARN] Error modifying Redshift Cluster (%s): %s", d.Id(), err) 431 } 432 433 stateConf := &resource.StateChangeConf{ 434 Pending: []string{"creating", "deleting", "rebooting", "resizing", "renaming", "modifying"}, 435 Target: []string{"available"}, 436 Refresh: resourceAwsRedshiftClusterStateRefreshFunc(d, meta), 437 Timeout: 40 * time.Minute, 438 MinTimeout: 10 * time.Second, 439 } 440 441 // Wait, catching any errors 442 _, err = stateConf.WaitForState() 443 if err != nil { 444 return fmt.Errorf("[WARN] Error Modifying Redshift Cluster (%s): %s", d.Id(), err) 445 } 446 447 return resourceAwsRedshiftClusterRead(d, meta) 448 } 449 450 func resourceAwsRedshiftClusterDelete(d *schema.ResourceData, meta interface{}) error { 451 conn := meta.(*AWSClient).redshiftconn 452 log.Printf("[DEBUG] Destroying Redshift Cluster (%s)", d.Id()) 453 454 deleteOpts := redshift.DeleteClusterInput{ 455 ClusterIdentifier: aws.String(d.Id()), 456 } 457 458 skipFinalSnapshot := d.Get("skip_final_snapshot").(bool) 459 deleteOpts.SkipFinalClusterSnapshot = aws.Bool(skipFinalSnapshot) 460 461 if !skipFinalSnapshot { 462 if name, present := d.GetOk("final_snapshot_identifier"); present { 463 deleteOpts.FinalClusterSnapshotIdentifier = aws.String(name.(string)) 464 } else { 465 return fmt.Errorf("Redshift Cluster Instance FinalSnapshotIdentifier is required when a final snapshot is required") 466 } 467 } 468 469 log.Printf("[DEBUG] Redshift Cluster delete options: %s", deleteOpts) 470 _, err := conn.DeleteCluster(&deleteOpts) 471 if err != nil { 472 return fmt.Errorf("[ERROR] Error deleting Redshift Cluster (%s): %s", d.Id(), err) 473 } 474 475 stateConf := &resource.StateChangeConf{ 476 Pending: []string{"available", "creating", "deleting", "rebooting", "resizing", "renaming"}, 477 Target: []string{"destroyed"}, 478 Refresh: resourceAwsRedshiftClusterStateRefreshFunc(d, meta), 479 Timeout: 40 * time.Minute, 480 MinTimeout: 5 * time.Second, 481 } 482 483 // Wait, catching any errors 484 _, err = stateConf.WaitForState() 485 if err != nil { 486 return fmt.Errorf("[ERROR] Error deleting Redshift Cluster (%s): %s", d.Id(), err) 487 } 488 489 log.Printf("[INFO] Redshift Cluster %s successfully deleted", d.Id()) 490 491 return nil 492 } 493 494 func resourceAwsRedshiftClusterStateRefreshFunc(d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc { 495 return func() (interface{}, string, error) { 496 conn := meta.(*AWSClient).redshiftconn 497 498 log.Printf("[INFO] Reading Redshift Cluster Information: %s", d.Id()) 499 resp, err := conn.DescribeClusters(&redshift.DescribeClustersInput{ 500 ClusterIdentifier: aws.String(d.Id()), 501 }) 502 503 if err != nil { 504 if awsErr, ok := err.(awserr.Error); ok { 505 if "ClusterNotFound" == awsErr.Code() { 506 return 42, "destroyed", nil 507 } 508 } 509 log.Printf("[WARN] Error on retrieving Redshift Cluster (%s) when waiting: %s", d.Id(), err) 510 return nil, "", err 511 } 512 513 var rsc *redshift.Cluster 514 515 for _, c := range resp.Clusters { 516 if *c.ClusterIdentifier == d.Id() { 517 rsc = c 518 } 519 } 520 521 if rsc == nil { 522 return 42, "destroyed", nil 523 } 524 525 if rsc.ClusterStatus != nil { 526 log.Printf("[DEBUG] Redshift Cluster status (%s): %s", d.Id(), *rsc.ClusterStatus) 527 } 528 529 return rsc, *rsc.ClusterStatus, nil 530 } 531 } 532 533 func validateRedshiftClusterIdentifier(v interface{}, k string) (ws []string, errors []error) { 534 value := v.(string) 535 if !regexp.MustCompile(`^[0-9a-z-]+$`).MatchString(value) { 536 errors = append(errors, fmt.Errorf( 537 "only lowercase alphanumeric characters and hyphens allowed in %q", k)) 538 } 539 if !regexp.MustCompile(`^[a-z]`).MatchString(value) { 540 errors = append(errors, fmt.Errorf( 541 "first character of %q must be a letter", k)) 542 } 543 if regexp.MustCompile(`--`).MatchString(value) { 544 errors = append(errors, fmt.Errorf( 545 "%q cannot contain two consecutive hyphens", k)) 546 } 547 if regexp.MustCompile(`-$`).MatchString(value) { 548 errors = append(errors, fmt.Errorf( 549 "%q cannot end with a hyphen", k)) 550 } 551 return 552 } 553 554 func validateRedshiftClusterDbName(v interface{}, k string) (ws []string, errors []error) { 555 value := v.(string) 556 if !regexp.MustCompile(`^[a-z]+$`).MatchString(value) { 557 errors = append(errors, fmt.Errorf( 558 "only lowercase letters characters allowed in %q", k)) 559 } 560 if len(value) > 64 { 561 errors = append(errors, fmt.Errorf( 562 "%q cannot be longer than 64 characters: %q", k, value)) 563 } 564 if value == "" { 565 errors = append(errors, fmt.Errorf( 566 "%q cannot be an empty string", k)) 567 } 568 569 return 570 } 571 572 func validateRedshiftClusterFinalSnapshotIdentifier(v interface{}, k string) (ws []string, errors []error) { 573 value := v.(string) 574 if !regexp.MustCompile(`^[0-9A-Za-z-]+$`).MatchString(value) { 575 errors = append(errors, fmt.Errorf( 576 "only alphanumeric characters and hyphens allowed in %q", k)) 577 } 578 if regexp.MustCompile(`--`).MatchString(value) { 579 errors = append(errors, fmt.Errorf("%q cannot contain two consecutive hyphens", k)) 580 } 581 if regexp.MustCompile(`-$`).MatchString(value) { 582 errors = append(errors, fmt.Errorf("%q cannot end in a hyphen", k)) 583 } 584 if len(value) > 255 { 585 errors = append(errors, fmt.Errorf("%q cannot be more than 255 characters", k)) 586 } 587 return 588 } 589 590 func validateRedshiftClusterMasterUsername(v interface{}, k string) (ws []string, errors []error) { 591 value := v.(string) 592 if !regexp.MustCompile(`^\w+$`).MatchString(value) { 593 errors = append(errors, fmt.Errorf( 594 "only alphanumeric characters in %q", k)) 595 } 596 if !regexp.MustCompile(`^[A-Za-z]`).MatchString(value) { 597 errors = append(errors, fmt.Errorf( 598 "first character of %q must be a letter", k)) 599 } 600 if len(value) > 128 { 601 errors = append(errors, fmt.Errorf("%q cannot be more than 128 characters", k)) 602 } 603 return 604 }