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

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/Azure/azure-sdk-for-go/arm/network"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func resourceArmLocalNetworkGateway() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceArmLocalNetworkGatewayCreate,
    14  		Read:   resourceArmLocalNetworkGatewayRead,
    15  		Update: resourceArmLocalNetworkGatewayCreate,
    16  		Delete: resourceArmLocalNetworkGatewayDelete,
    17  		Importer: &schema.ResourceImporter{
    18  			State: schema.ImportStatePassthrough,
    19  		},
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": {
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  
    28  			"location": locationSchema(),
    29  
    30  			"resource_group_name": {
    31  				Type:     schema.TypeString,
    32  				Optional: true,
    33  				ForceNew: true,
    34  			},
    35  
    36  			"gateway_address": {
    37  				Type:     schema.TypeString,
    38  				Required: true,
    39  			},
    40  
    41  			"address_space": {
    42  				Type:     schema.TypeList,
    43  				Required: true,
    44  				Elem: &schema.Schema{
    45  					Type: schema.TypeString,
    46  				},
    47  			},
    48  		},
    49  	}
    50  }
    51  
    52  func resourceArmLocalNetworkGatewayCreate(d *schema.ResourceData, meta interface{}) error {
    53  	lnetClient := meta.(*ArmClient).localNetConnClient
    54  
    55  	name := d.Get("name").(string)
    56  	location := d.Get("location").(string)
    57  	resGroup := d.Get("resource_group_name").(string)
    58  	ipAddress := d.Get("gateway_address").(string)
    59  
    60  	// fetch the 'address_space_prefixes:
    61  	prefixes := []string{}
    62  	for _, pref := range d.Get("address_space").([]interface{}) {
    63  		prefixes = append(prefixes, pref.(string))
    64  	}
    65  
    66  	gateway := network.LocalNetworkGateway{
    67  		Name:     &name,
    68  		Location: &location,
    69  		LocalNetworkGatewayPropertiesFormat: &network.LocalNetworkGatewayPropertiesFormat{
    70  			LocalNetworkAddressSpace: &network.AddressSpace{
    71  				AddressPrefixes: &prefixes,
    72  			},
    73  			GatewayIPAddress: &ipAddress,
    74  		},
    75  	}
    76  
    77  	_, err := lnetClient.CreateOrUpdate(resGroup, name, gateway, make(chan struct{}))
    78  	if err != nil {
    79  		return fmt.Errorf("Error creating Azure ARM Local Network Gateway '%s': %s", name, err)
    80  	}
    81  
    82  	read, err := lnetClient.Get(resGroup, name)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	if read.ID == nil {
    87  		return fmt.Errorf("Cannot read Virtual Network %s (resource group %s) ID", name, resGroup)
    88  	}
    89  
    90  	d.SetId(*read.ID)
    91  
    92  	return resourceArmLocalNetworkGatewayRead(d, meta)
    93  }
    94  
    95  // resourceArmLocalNetworkGatewayRead goes ahead and reads the state of the corresponding ARM local network gateway.
    96  func resourceArmLocalNetworkGatewayRead(d *schema.ResourceData, meta interface{}) error {
    97  	lnetClient := meta.(*ArmClient).localNetConnClient
    98  
    99  	id, err := parseAzureResourceID(d.Id())
   100  	if err != nil {
   101  		return err
   102  	}
   103  	name := id.Path["localNetworkGateways"]
   104  	if name == "" {
   105  		return fmt.Errorf("Cannot find 'localNetworkGateways' in '%s', make sure it is specified in the ID parameter", d.Id())
   106  	}
   107  	resGroup := id.ResourceGroup
   108  
   109  	resp, err := lnetClient.Get(resGroup, name)
   110  	if err != nil {
   111  		if resp.StatusCode == http.StatusNotFound {
   112  			d.SetId("")
   113  			return nil
   114  		}
   115  		return fmt.Errorf("Error reading the state of Azure ARM local network gateway '%s': %s", name, err)
   116  	}
   117  
   118  	d.Set("resource_group_name", resGroup)
   119  	d.Set("name", resp.Name)
   120  	d.Set("location", resp.Location)
   121  	d.Set("gateway_address", resp.LocalNetworkGatewayPropertiesFormat.GatewayIPAddress)
   122  
   123  	prefs := []string{}
   124  	if ps := *resp.LocalNetworkGatewayPropertiesFormat.LocalNetworkAddressSpace.AddressPrefixes; ps != nil {
   125  		prefs = ps
   126  	}
   127  	d.Set("address_space", prefs)
   128  
   129  	return nil
   130  }
   131  
   132  // resourceArmLocalNetworkGatewayDelete deletes the specified ARM local network gateway.
   133  func resourceArmLocalNetworkGatewayDelete(d *schema.ResourceData, meta interface{}) error {
   134  	lnetClient := meta.(*ArmClient).localNetConnClient
   135  
   136  	id, err := parseAzureResourceID(d.Id())
   137  	if err != nil {
   138  		return err
   139  	}
   140  	name := id.Path["localNetworkGateways"]
   141  	resGroup := id.ResourceGroup
   142  
   143  	_, err = lnetClient.Delete(resGroup, name, make(chan struct{}))
   144  	if err != nil {
   145  		return fmt.Errorf("Error issuing Azure ARM delete request of local network gateway '%s': %s", name, err)
   146  	}
   147  
   148  	return nil
   149  }