github.com/mhlias/terraform@v0.6.12-0.20161118140322-a5d6410b912a/builtin/providers/aws/resource_aws_dc_intra_virtual_interface.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/service/directconnect" 10 11 "github.com/hashicorp/terraform/helper/hashcode" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/helper/schema" 14 ) 15 16 func resourceAwsDirectConnectIntraVirtualInterface() *schema.Resource { 17 return &schema.Resource{ 18 Create: resourceAwsDirectConnectIntraVirtualInterfaceCreate, 19 Read: resourceAwsDirectConnectIntraVirtualInterfaceRead, 20 Delete: resourceAwsDirectConnectIntraVirtualInterfaceDelete, 21 22 Schema: map[string]*schema.Schema{ 23 "connection_id": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 }, 28 29 "owner_account_id": &schema.Schema{ 30 Type: schema.TypeString, 31 Required: true, 32 ForceNew: true, 33 }, 34 35 "interface_type": &schema.Schema{ 36 Type: schema.TypeString, 37 Optional: true, 38 ForceNew: true, 39 }, 40 41 "asn": &schema.Schema{ 42 Type: schema.TypeInt, 43 Required: true, 44 ForceNew: true, 45 }, 46 47 "virtual_interface_name": &schema.Schema{ 48 Type: schema.TypeString, 49 Required: true, 50 ForceNew: true, 51 }, 52 53 "vlan": &schema.Schema{ 54 Type: schema.TypeInt, 55 Required: true, 56 ForceNew: true, 57 }, 58 59 "amazon_address": &schema.Schema{ 60 Type: schema.TypeString, 61 Optional: true, 62 Computed: true, 63 ForceNew: true, 64 }, 65 66 "customer_address": &schema.Schema{ 67 Type: schema.TypeString, 68 Optional: true, 69 Computed: true, 70 ForceNew: true, 71 }, 72 73 "auth_key": &schema.Schema{ 74 Type: schema.TypeString, 75 Optional: true, 76 Computed: true, 77 ForceNew: true, 78 }, 79 80 "route_filter_prefixes": &schema.Schema{ 81 Type: schema.TypeSet, 82 Optional: true, 83 ForceNew: true, 84 Elem: &schema.Schema{Type: schema.TypeString}, 85 Set: func(v interface{}) int { 86 return hashcode.String(v.(string)) 87 }, 88 }, 89 }, 90 } 91 } 92 93 func resourceAwsDirectConnectIntraVirtualInterfaceCreate(d *schema.ResourceData, meta interface{}) error { 94 conn := meta.(*AWSClient).dcconn 95 96 var err error 97 var resp *directconnect.VirtualInterface 98 99 if v, ok := d.GetOk("interface_type"); ok && v.(string) == "public" { 100 101 createOpts := &directconnect.AllocatePublicVirtualInterfaceInput{ 102 ConnectionId: aws.String(d.Get("connection_id").(string)), 103 NewPublicVirtualInterfaceAllocation: &directconnect.NewPublicVirtualInterfaceAllocation{ 104 Asn: aws.Int64(int64(d.Get("asn").(int))), 105 VirtualInterfaceName: aws.String(d.Get("virtual_interface_name").(string)), 106 Vlan: aws.Int64(int64(d.Get("vlan").(int))), 107 RouteFilterPrefixes: []*directconnect.RouteFilterPrefix{}, 108 }, 109 OwnerAccount: aws.String(d.Get("owner_account_id").(string)), 110 } 111 112 if v, ok := d.GetOk("amazon_address"); ok { 113 createOpts.NewPublicVirtualInterfaceAllocation.AmazonAddress = aws.String(v.(string)) 114 } 115 116 if v, ok := d.GetOk("auth_key"); ok { 117 createOpts.NewPublicVirtualInterfaceAllocation.AuthKey = aws.String(v.(string)) 118 } 119 120 if v, ok := d.GetOk("customer_address"); ok { 121 createOpts.NewPublicVirtualInterfaceAllocation.CustomerAddress = aws.String(v.(string)) 122 } 123 124 if prefixesSet, ok := d.Get("route_filter_prefixes").(*schema.Set); ok { 125 126 for _, cidr := range prefixesSet.List() { 127 createOpts.NewPublicVirtualInterfaceAllocation.RouteFilterPrefixes = append(createOpts.NewPublicVirtualInterfaceAllocation.RouteFilterPrefixes, &directconnect.RouteFilterPrefix{Cidr: aws.String(cidr.(string))}) 128 } 129 130 } 131 132 log.Printf("[DEBUG] Creating DirectConnect public virtual interface") 133 resp, err = conn.AllocatePublicVirtualInterface(createOpts) 134 if err != nil { 135 return fmt.Errorf("Error creating DirectConnect Virtual Interface: %s", err) 136 } 137 138 } else { 139 140 createOpts := &directconnect.AllocatePrivateVirtualInterfaceInput{ 141 ConnectionId: aws.String(d.Get("connection_id").(string)), 142 NewPrivateVirtualInterfaceAllocation: &directconnect.NewPrivateVirtualInterfaceAllocation{ 143 Asn: aws.Int64(int64(d.Get("asn").(int))), 144 VirtualInterfaceName: aws.String(d.Get("virtual_interface_name").(string)), 145 Vlan: aws.Int64(int64(d.Get("vlan").(int))), 146 }, 147 OwnerAccount: aws.String(d.Get("owner_account_id").(string)), 148 } 149 150 if v, ok := d.GetOk("amazon_address"); ok { 151 createOpts.NewPrivateVirtualInterfaceAllocation.AmazonAddress = aws.String(v.(string)) 152 } 153 154 if v, ok := d.GetOk("auth_key"); ok { 155 createOpts.NewPrivateVirtualInterfaceAllocation.AuthKey = aws.String(v.(string)) 156 } 157 158 if v, ok := d.GetOk("customer_address"); ok { 159 createOpts.NewPrivateVirtualInterfaceAllocation.CustomerAddress = aws.String(v.(string)) 160 } 161 162 log.Printf("[DEBUG] Creating DirectConnect private virtual interface") 163 resp, err = conn.AllocatePrivateVirtualInterface(createOpts) 164 if err != nil { 165 return fmt.Errorf("Error creating DirectConnect Virtual Interface: %s", err) 166 } 167 168 } 169 170 // Store the ID 171 VirtualInterface := resp 172 d.SetId(*VirtualInterface.VirtualInterfaceId) 173 log.Printf("[INFO] VirtualInterface ID: %s", *VirtualInterface.VirtualInterfaceId) 174 175 stateConf := &resource.StateChangeConf{ 176 Pending: []string{"pending"}, 177 Target: []string{"available", "confirming", "verifying", "pending"}, 178 Refresh: DirectConnectIntraVirtualInterfaceRefreshFunc(conn, *VirtualInterface.VirtualInterfaceId), 179 Timeout: 10 * time.Minute, 180 Delay: 10 * time.Second, 181 MinTimeout: 10 * time.Second, 182 } 183 184 _, stateErr := stateConf.WaitForState() 185 if stateErr != nil { 186 return fmt.Errorf( 187 "Error waiting for DirectConnect PrivateVirtualInterface (%s) to become ready: %s", 188 *VirtualInterface.VirtualInterfaceId, err) 189 } 190 191 // Read off the API to populate our RO fields. 192 return resourceAwsDirectConnectIntraVirtualInterfaceRead(d, meta) 193 } 194 195 func DirectConnectIntraVirtualInterfaceRefreshFunc(conn *directconnect.DirectConnect, virtualinterfaceId string) resource.StateRefreshFunc { 196 return func() (interface{}, string, error) { 197 198 resp, err := conn.DescribeVirtualInterfaces(&directconnect.DescribeVirtualInterfacesInput{ 199 VirtualInterfaceId: aws.String(virtualinterfaceId), 200 }) 201 202 if err != nil { 203 204 log.Printf("Error on DirectConnectPrivateVirtualInterfaceRefresh: %s", err) 205 return nil, "", err 206 207 } 208 209 if resp == nil || len(resp.VirtualInterfaces) == 0 { 210 return nil, "", nil 211 } 212 213 virtualInterface := resp.VirtualInterfaces[0] 214 return virtualInterface, *virtualInterface.VirtualInterfaceState, nil 215 } 216 } 217 218 func resourceAwsDirectConnectIntraVirtualInterfaceRead(d *schema.ResourceData, meta interface{}) error { 219 conn := meta.(*AWSClient).dcconn 220 221 resp, err := conn.DescribeVirtualInterfaces(&directconnect.DescribeVirtualInterfacesInput{ 222 VirtualInterfaceId: aws.String(d.Id()), 223 }) 224 225 if err != nil { 226 227 log.Printf("[ERROR] Error finding DirectConnect PrivateVirtualInterface: %s", err) 228 return err 229 230 } 231 232 vifsCount := len(resp.VirtualInterfaces) 233 234 if vifsCount != 1 { 235 return fmt.Errorf("[ERROR] Error finding DirectConnect PrivateVirtualInterface or unexpected number of %d VirtualInterfaces was returned: %s", vifsCount, d.Id()) 236 } 237 238 virtualInterface := resp.VirtualInterfaces[0] 239 240 // Set attributes under the user's control. 241 d.Set("connection_id", virtualInterface.ConnectionId) 242 d.Set("asn", virtualInterface.Asn) 243 d.Set("virtual_interface_name", virtualInterface.VirtualInterfaceName) 244 d.Set("vlan", virtualInterface.Vlan) 245 d.Set("amazon_address", virtualInterface.AmazonAddress) 246 d.Set("customer_address", virtualInterface.CustomerAddress) 247 // d.Set("auth_key", *virtualInterface.AuthKey) 248 249 // Set read only attributes. 250 d.Set("owner_account_id", virtualInterface.OwnerAccount) 251 252 return nil 253 } 254 255 func resourceAwsDirectConnectIntraVirtualInterfaceDelete(d *schema.ResourceData, meta interface{}) error { 256 conn := meta.(*AWSClient).dcconn 257 258 _, err := conn.DeleteVirtualInterface(&directconnect.DeleteVirtualInterfaceInput{ 259 VirtualInterfaceId: aws.String(d.Id()), 260 }) 261 262 if err != nil { 263 264 log.Printf("[ERROR] Error deleting DirectConnect PrivateVirtualInterface connection: %s", err) 265 return err 266 267 } 268 269 stateConf := &resource.StateChangeConf{ 270 Pending: []string{"deleting"}, 271 Target: []string{"deleted"}, 272 Refresh: DirectConnectIntraVirtualInterfaceRefreshFunc(conn, d.Id()), 273 Timeout: 10 * time.Minute, 274 Delay: 10 * time.Second, 275 MinTimeout: 10 * time.Second, 276 } 277 278 _, stateErr := stateConf.WaitForState() 279 if stateErr != nil { 280 return fmt.Errorf( 281 "Error waiting for DirectConnect PrivateVirtualInterface (%s) to delete: %s", d.Id(), err) 282 } 283 284 return nil 285 }