github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_dms_replication_subnet_group.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/aws/aws-sdk-go/aws" 8 dms "github.com/aws/aws-sdk-go/service/databasemigrationservice" 9 "github.com/hashicorp/terraform/helper/schema" 10 ) 11 12 func resourceAwsDmsReplicationSubnetGroup() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceAwsDmsReplicationSubnetGroupCreate, 15 Read: resourceAwsDmsReplicationSubnetGroupRead, 16 Update: resourceAwsDmsReplicationSubnetGroupUpdate, 17 Delete: resourceAwsDmsReplicationSubnetGroupDelete, 18 19 Importer: &schema.ResourceImporter{ 20 State: schema.ImportStatePassthrough, 21 }, 22 23 Schema: map[string]*schema.Schema{ 24 "replication_subnet_group_arn": { 25 Type: schema.TypeString, 26 Computed: true, 27 }, 28 "replication_subnet_group_description": { 29 Type: schema.TypeString, 30 Required: true, 31 }, 32 "replication_subnet_group_id": { 33 Type: schema.TypeString, 34 Required: true, 35 ForceNew: true, 36 ValidateFunc: validateDmsReplicationSubnetGroupId, 37 }, 38 "subnet_ids": { 39 Type: schema.TypeSet, 40 Elem: &schema.Schema{Type: schema.TypeString}, 41 Set: schema.HashString, 42 Required: true, 43 }, 44 "tags": { 45 Type: schema.TypeMap, 46 Optional: true, 47 }, 48 "vpc_id": { 49 Type: schema.TypeString, 50 Computed: true, 51 }, 52 }, 53 } 54 } 55 56 func resourceAwsDmsReplicationSubnetGroupCreate(d *schema.ResourceData, meta interface{}) error { 57 conn := meta.(*AWSClient).dmsconn 58 59 request := &dms.CreateReplicationSubnetGroupInput{ 60 ReplicationSubnetGroupIdentifier: aws.String(d.Get("replication_subnet_group_id").(string)), 61 ReplicationSubnetGroupDescription: aws.String(d.Get("replication_subnet_group_description").(string)), 62 SubnetIds: expandStringList(d.Get("subnet_ids").(*schema.Set).List()), 63 Tags: dmsTagsFromMap(d.Get("tags").(map[string]interface{})), 64 } 65 66 log.Println("[DEBUG] DMS create replication subnet group:", request) 67 68 _, err := conn.CreateReplicationSubnetGroup(request) 69 if err != nil { 70 return err 71 } 72 73 d.SetId(d.Get("replication_subnet_group_id").(string)) 74 return resourceAwsDmsReplicationSubnetGroupRead(d, meta) 75 } 76 77 func resourceAwsDmsReplicationSubnetGroupRead(d *schema.ResourceData, meta interface{}) error { 78 conn := meta.(*AWSClient).dmsconn 79 80 response, err := conn.DescribeReplicationSubnetGroups(&dms.DescribeReplicationSubnetGroupsInput{ 81 Filters: []*dms.Filter{ 82 { 83 Name: aws.String("replication-subnet-group-id"), 84 Values: []*string{aws.String(d.Id())}, // Must use d.Id() to work with import. 85 }, 86 }, 87 }) 88 if err != nil { 89 return err 90 } 91 if len(response.ReplicationSubnetGroups) == 0 { 92 d.SetId("") 93 return nil 94 } 95 96 // The AWS API for DMS subnet groups does not return the ARN which is required to 97 // retrieve tags. This ARN can be built. 98 d.Set("replication_subnet_group_arn", fmt.Sprintf("arn:aws:dms:%s:%s:subgrp:%s", 99 meta.(*AWSClient).region, meta.(*AWSClient).accountid, d.Id())) 100 101 err = resourceAwsDmsReplicationSubnetGroupSetState(d, response.ReplicationSubnetGroups[0]) 102 if err != nil { 103 return err 104 } 105 106 tagsResp, err := conn.ListTagsForResource(&dms.ListTagsForResourceInput{ 107 ResourceArn: aws.String(d.Get("replication_subnet_group_arn").(string)), 108 }) 109 if err != nil { 110 return err 111 } 112 d.Set("tags", dmsTagsToMap(tagsResp.TagList)) 113 114 return nil 115 } 116 117 func resourceAwsDmsReplicationSubnetGroupUpdate(d *schema.ResourceData, meta interface{}) error { 118 conn := meta.(*AWSClient).dmsconn 119 120 // Updates to subnet groups are only valid when sending SubnetIds even if there are no 121 // changes to SubnetIds. 122 request := &dms.ModifyReplicationSubnetGroupInput{ 123 ReplicationSubnetGroupIdentifier: aws.String(d.Get("replication_subnet_group_id").(string)), 124 SubnetIds: expandStringList(d.Get("subnet_ids").(*schema.Set).List()), 125 } 126 127 if d.HasChange("replication_subnet_group_description") { 128 request.ReplicationSubnetGroupDescription = aws.String(d.Get("replication_subnet_group_description").(string)) 129 } 130 131 if d.HasChange("tags") { 132 err := dmsSetTags(d.Get("replication_subnet_group_arn").(string), d, meta) 133 if err != nil { 134 return err 135 } 136 } 137 138 log.Println("[DEBUG] DMS update replication subnet group:", request) 139 140 _, err := conn.ModifyReplicationSubnetGroup(request) 141 if err != nil { 142 return err 143 } 144 145 return resourceAwsDmsReplicationSubnetGroupRead(d, meta) 146 } 147 148 func resourceAwsDmsReplicationSubnetGroupDelete(d *schema.ResourceData, meta interface{}) error { 149 conn := meta.(*AWSClient).dmsconn 150 151 request := &dms.DeleteReplicationSubnetGroupInput{ 152 ReplicationSubnetGroupIdentifier: aws.String(d.Get("replication_subnet_group_id").(string)), 153 } 154 155 log.Printf("[DEBUG] DMS delete replication subnet group: %#v", request) 156 157 _, err := conn.DeleteReplicationSubnetGroup(request) 158 if err != nil { 159 return err 160 } 161 162 return nil 163 } 164 165 func resourceAwsDmsReplicationSubnetGroupSetState(d *schema.ResourceData, group *dms.ReplicationSubnetGroup) error { 166 d.SetId(*group.ReplicationSubnetGroupIdentifier) 167 168 subnet_ids := []string{} 169 for _, subnet := range group.Subnets { 170 subnet_ids = append(subnet_ids, aws.StringValue(subnet.SubnetIdentifier)) 171 } 172 173 d.Set("replication_subnet_group_description", group.ReplicationSubnetGroupDescription) 174 d.Set("replication_subnet_group_id", group.ReplicationSubnetGroupIdentifier) 175 d.Set("subnet_ids", subnet_ids) 176 d.Set("vpc_id", group.VpcId) 177 178 return nil 179 }