github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/builtin/providers/azurerm/resource_arm_local_network_gateway.go (about)

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