Provide tunnel info from XML config.

This commit is contained in:
Cameron Stokes 2016-02-06 12:39:49 -08:00
parent eea664247c
commit 6a5cb5b109

View File

@ -2,8 +2,10 @@ package aws
import (
"bytes"
"encoding/xml"
"fmt"
"log"
"sort"
"time"
"github.com/aws/aws-sdk-go/aws"
@ -15,6 +17,34 @@ import (
"github.com/hashicorp/terraform/helper/schema"
)
type XmlVpnConnectionConfig struct {
Tunnels []XmlIpsecTunnel `xml:"ipsec_tunnel"`
}
type XmlIpsecTunnel struct {
OutsideAddress string `xml:"vpn_gateway>tunnel_outside_address>ip_address"`
PreSharedKey string `xml:"ike>pre_shared_key"`
}
type TunnelInfo struct {
Tunnel1Address string
Tunnel1PreSharedKey string
Tunnel2Address string
Tunnel2PreSharedKey string
}
func (slice XmlVpnConnectionConfig) Len() int {
return len(slice.Tunnels)
}
func (slice XmlVpnConnectionConfig) Less(i, j int) bool {
return slice.Tunnels[i].OutsideAddress < slice.Tunnels[j].OutsideAddress
}
func (slice XmlVpnConnectionConfig) Swap(i, j int) {
slice.Tunnels[i], slice.Tunnels[j] = slice.Tunnels[j], slice.Tunnels[i]
}
func resourceAwsVpnConnection() *schema.Resource {
return &schema.Resource{
Create: resourceAwsVpnConnectionCreate,
@ -56,6 +86,26 @@ func resourceAwsVpnConnection() *schema.Resource {
Optional: true,
},
"tunnel1_address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"tunnel1_preshared_key": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"tunnel2_address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"tunnel2_preshared_key": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"routes": &schema.Schema{
Type: schema.TypeSet,
Computed: true,
@ -254,6 +304,13 @@ func resourceAwsVpnConnectionRead(d *schema.ResourceData, meta interface{}) erro
// Set read only attributes.
d.Set("customer_gateway_configuration", vpnConnection.CustomerGatewayConfiguration)
tunnelInfo := xmlConfigToTunnelInfo(*vpnConnection.CustomerGatewayConfiguration)
d.Set("tunnel1_address", tunnelInfo.Tunnel1Address)
d.Set("tunnel1_preshared_key", tunnelInfo.Tunnel1PreSharedKey)
d.Set("tunnel2_address", tunnelInfo.Tunnel2Address)
d.Set("tunnel2_preshared_key", tunnelInfo.Tunnel2PreSharedKey)
if err := d.Set("vgw_telemetry", telemetryToMapList(vpnConnection.VgwTelemetry)); err != nil {
return err
}
@ -355,3 +412,21 @@ func telemetryToMapList(telemetry []*ec2.VgwTelemetry) []map[string]interface{}
return result
}
func xmlConfigToTunnelInfo(xmlConfig string) TunnelInfo {
var vpnConfig XmlVpnConnectionConfig
xml.Unmarshal([]byte(xmlConfig), &vpnConfig)
// don't expect consistent ordering from the XML
sort.Sort(vpnConfig)
tunnelInfo := TunnelInfo{
Tunnel1Address: vpnConfig.Tunnels[0].OutsideAddress,
Tunnel1PreSharedKey: vpnConfig.Tunnels[0].PreSharedKey,
Tunnel2Address: vpnConfig.Tunnels[1].OutsideAddress,
Tunnel2PreSharedKey: vpnConfig.Tunnels[1].PreSharedKey,
}
return tunnelInfo
}