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