github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  		return fmt.Errorf("Error reading the state of Azure ARM local network gateway '%s': %s", name, err)
   114  	}
   115  	if resp.StatusCode == http.StatusNotFound {
   116  		d.SetId("")
   117  		return nil
   118  	}
   119  
   120  	d.Set("name", resp.Name)
   121  	d.Set("location", resp.Location)
   122  	d.Set("gateway_address", resp.Properties.GatewayIPAddress)
   123  
   124  	prefs := []string{}
   125  	if ps := *resp.Properties.LocalNetworkAddressSpace.AddressPrefixes; ps != nil {
   126  		prefs = ps
   127  	}
   128  	d.Set("address_space", prefs)
   129  
   130  	return nil
   131  }
   132  
   133  // resourceArmLocalNetworkGatewayDelete deletes the specified ARM local network gateway.
   134  func resourceArmLocalNetworkGatewayDelete(d *schema.ResourceData, meta interface{}) error {
   135  	lnetClient := meta.(*ArmClient).localNetConnClient
   136  
   137  	id, err := parseAzureResourceID(d.Id())
   138  	if err != nil {
   139  		return err
   140  	}
   141  	name := id.Path["localNetworkGateways"]
   142  	resGroup := id.ResourceGroup
   143  
   144  	_, err = lnetClient.Delete(resGroup, name, make(chan struct{}))
   145  	if err != nil {
   146  		return fmt.Errorf("Error issuing Azure ARM delete request of local network gateway '%s': %s", name, err)
   147  	}
   148  
   149  	return nil
   150  }