github.com/memsql/terraform@v0.7.0-rc2.0.20160706152241-21e2173e0a32/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": { 20 Type: schema.TypeString, 21 Required: true, 22 ForceNew: true, 23 }, 24 25 "location": { 26 Type: schema.TypeString, 27 Optional: true, 28 ForceNew: true, 29 StateFunc: azureRMNormalizeLocation, 30 }, 31 32 "resource_group_name": { 33 Type: schema.TypeString, 34 Optional: true, 35 ForceNew: true, 36 }, 37 38 "gateway_address": { 39 Type: schema.TypeString, 40 Required: true, 41 }, 42 43 "address_space": { 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 gateway := 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 79 _, err := lnetClient.CreateOrUpdate(resGroup, name, gateway, make(chan struct{})) 80 if err != nil { 81 return fmt.Errorf("Error creating Azure ARM Local Network Gateway '%s': %s", name, err) 82 } 83 84 read, err := lnetClient.Get(resGroup, name) 85 if err != nil { 86 return err 87 } 88 if read.ID == nil { 89 return fmt.Errorf("Cannot read Virtual Network %s (resource group %s) ID", name, resGroup) 90 } 91 92 d.SetId(*read.ID) 93 94 return resourceArmLocalNetworkGatewayRead(d, meta) 95 } 96 97 // resourceArmLocalNetworkGatewayRead goes ahead and reads the state of the corresponding ARM local network gateway. 98 func resourceArmLocalNetworkGatewayRead(d *schema.ResourceData, meta interface{}) error { 99 lnetClient := meta.(*ArmClient).localNetConnClient 100 101 id, err := parseAzureResourceID(d.Id()) 102 if err != nil { 103 return err 104 } 105 name := id.Path["localNetworkGateways"] 106 resGroup := id.ResourceGroup 107 108 resp, err := lnetClient.Get(resGroup, name) 109 if err != nil { 110 if resp.StatusCode == http.StatusNotFound { 111 d.SetId("") 112 return nil 113 } 114 115 return fmt.Errorf("Error reading the state of Azure ARM local network gateway '%s': %s", name, err) 116 } 117 118 d.Set("gateway_address", resp.Properties.GatewayIPAddress) 119 120 prefs := []string{} 121 if ps := *resp.Properties.LocalNetworkAddressSpace.AddressPrefixes; ps != nil { 122 prefs = ps 123 } 124 d.Set("address_space", prefs) 125 126 return nil 127 } 128 129 // resourceArmLocalNetworkGatewayDelete deletes the specified ARM local network gateway. 130 func resourceArmLocalNetworkGatewayDelete(d *schema.ResourceData, meta interface{}) error { 131 lnetClient := meta.(*ArmClient).localNetConnClient 132 133 id, err := parseAzureResourceID(d.Id()) 134 if err != nil { 135 return err 136 } 137 name := id.Path["localNetworkGateways"] 138 resGroup := id.ResourceGroup 139 140 _, err = lnetClient.Delete(resGroup, name, make(chan struct{})) 141 if err != nil { 142 return fmt.Errorf("Error issuing Azure ARM delete request of local network gateway '%s': %s", name, err) 143 } 144 145 return nil 146 }