github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_virtual_network_peering.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"sync"
     8  
     9  	"github.com/Azure/azure-sdk-for-go/arm/network"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  // peerMutex is used to prevet multiple Peering resources being creaed, updated
    14  // or deleted at the same time
    15  var peerMutex = &sync.Mutex{}
    16  
    17  func resourceArmVirtualNetworkPeering() *schema.Resource {
    18  	return &schema.Resource{
    19  		Create: resourceArmVirtualNetworkPeeringCreate,
    20  		Read:   resourceArmVirtualNetworkPeeringRead,
    21  		Update: resourceArmVirtualNetworkPeeringCreate,
    22  		Delete: resourceArmVirtualNetworkPeeringDelete,
    23  		Importer: &schema.ResourceImporter{
    24  			State: schema.ImportStatePassthrough,
    25  		},
    26  
    27  		Schema: map[string]*schema.Schema{
    28  			"name": {
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  
    34  			"resource_group_name": {
    35  				Type:     schema.TypeString,
    36  				Required: true,
    37  				ForceNew: true,
    38  			},
    39  
    40  			"virtual_network_name": {
    41  				Type:     schema.TypeString,
    42  				Required: true,
    43  				ForceNew: true,
    44  			},
    45  
    46  			"remote_virtual_network_id": {
    47  				Type:     schema.TypeString,
    48  				Required: true,
    49  				ForceNew: true,
    50  			},
    51  
    52  			"allow_virtual_network_access": {
    53  				Type:     schema.TypeBool,
    54  				Optional: true,
    55  				Computed: true,
    56  			},
    57  
    58  			"allow_forwarded_traffic": {
    59  				Type:     schema.TypeBool,
    60  				Optional: true,
    61  				Computed: true,
    62  			},
    63  
    64  			"allow_gateway_transit": {
    65  				Type:     schema.TypeBool,
    66  				Optional: true,
    67  				Computed: true,
    68  			},
    69  
    70  			"use_remote_gateways": {
    71  				Type:     schema.TypeBool,
    72  				Optional: true,
    73  				Computed: true,
    74  			},
    75  		},
    76  	}
    77  }
    78  
    79  func resourceArmVirtualNetworkPeeringCreate(d *schema.ResourceData, meta interface{}) error {
    80  	client := meta.(*ArmClient).vnetPeeringsClient
    81  
    82  	log.Printf("[INFO] preparing arguments for Azure ARM virtual network peering creation.")
    83  
    84  	name := d.Get("name").(string)
    85  	vnetName := d.Get("virtual_network_name").(string)
    86  	resGroup := d.Get("resource_group_name").(string)
    87  
    88  	peer := network.VirtualNetworkPeering{
    89  		Name: &name,
    90  		VirtualNetworkPeeringPropertiesFormat: getVirtualNetworkPeeringProperties(d),
    91  	}
    92  
    93  	peerMutex.Lock()
    94  	defer peerMutex.Unlock()
    95  
    96  	_, err := client.CreateOrUpdate(resGroup, vnetName, name, peer, make(chan struct{}))
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	read, err := client.Get(resGroup, vnetName, name)
   102  	if err != nil {
   103  		return err
   104  	}
   105  	if read.ID == nil {
   106  		return fmt.Errorf("Cannot read Virtual Network Peering %s (resource group %s) ID", name, resGroup)
   107  	}
   108  
   109  	d.SetId(*read.ID)
   110  
   111  	return resourceArmVirtualNetworkPeeringRead(d, meta)
   112  }
   113  
   114  func resourceArmVirtualNetworkPeeringRead(d *schema.ResourceData, meta interface{}) error {
   115  	client := meta.(*ArmClient).vnetPeeringsClient
   116  
   117  	id, err := parseAzureResourceID(d.Id())
   118  	if err != nil {
   119  		return err
   120  	}
   121  	resGroup := id.ResourceGroup
   122  	vnetName := id.Path["virtualNetworks"]
   123  	name := id.Path["virtualNetworkPeerings"]
   124  
   125  	resp, err := client.Get(resGroup, vnetName, name)
   126  	if err != nil {
   127  		if resp.StatusCode == http.StatusNotFound {
   128  			d.SetId("")
   129  			return nil
   130  		}
   131  		return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
   132  	}
   133  
   134  	peer := *resp.VirtualNetworkPeeringPropertiesFormat
   135  
   136  	// update appropriate values
   137  	d.Set("resource_group_name", resGroup)
   138  	d.Set("name", resp.Name)
   139  	d.Set("virtual_network_name", vnetName)
   140  	d.Set("allow_virtual_network_access", peer.AllowVirtualNetworkAccess)
   141  	d.Set("allow_forwarded_traffic", peer.AllowForwardedTraffic)
   142  	d.Set("allow_gateway_transit", peer.AllowGatewayTransit)
   143  	d.Set("use_remote_gateways", peer.UseRemoteGateways)
   144  	d.Set("remote_virtual_network_id", peer.RemoteVirtualNetwork.ID)
   145  
   146  	return nil
   147  }
   148  
   149  func resourceArmVirtualNetworkPeeringDelete(d *schema.ResourceData, meta interface{}) error {
   150  	client := meta.(*ArmClient).vnetPeeringsClient
   151  
   152  	id, err := parseAzureResourceID(d.Id())
   153  	if err != nil {
   154  		return err
   155  	}
   156  	resGroup := id.ResourceGroup
   157  	vnetName := id.Path["virtualNetworks"]
   158  	name := id.Path["virtualNetworkPeerings"]
   159  
   160  	peerMutex.Lock()
   161  	defer peerMutex.Unlock()
   162  
   163  	_, err = client.Delete(resGroup, vnetName, name, make(chan struct{}))
   164  
   165  	return err
   166  }
   167  
   168  func getVirtualNetworkPeeringProperties(d *schema.ResourceData) *network.VirtualNetworkPeeringPropertiesFormat {
   169  	allowVirtualNetworkAccess := d.Get("allow_virtual_network_access").(bool)
   170  	allowForwardedTraffic := d.Get("allow_forwarded_traffic").(bool)
   171  	allowGatewayTransit := d.Get("allow_gateway_transit").(bool)
   172  	useRemoteGateways := d.Get("use_remote_gateways").(bool)
   173  	remoteVirtualNetworkID := d.Get("remote_virtual_network_id").(string)
   174  
   175  	return &network.VirtualNetworkPeeringPropertiesFormat{
   176  		AllowVirtualNetworkAccess: &allowVirtualNetworkAccess,
   177  		AllowForwardedTraffic:     &allowForwardedTraffic,
   178  		AllowGatewayTransit:       &allowGatewayTransit,
   179  		UseRemoteGateways:         &useRemoteGateways,
   180  		RemoteVirtualNetwork: &network.SubResource{
   181  			ID: &remoteVirtualNetworkID,
   182  		},
   183  	}
   184  }