github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 resGroup := id.ResourceGroup 105 106 resp, err := lnetClient.Get(resGroup, name) 107 if err != nil { 108 if resp.StatusCode == http.StatusNotFound { 109 d.SetId("") 110 return nil 111 } 112 return fmt.Errorf("Error reading the state of Azure ARM local network gateway '%s': %s", name, err) 113 } 114 115 d.Set("resource_group_name", resGroup) 116 d.Set("name", resp.Name) 117 d.Set("location", resp.Location) 118 d.Set("gateway_address", resp.LocalNetworkGatewayPropertiesFormat.GatewayIPAddress) 119 120 prefs := []string{} 121 if ps := *resp.LocalNetworkGatewayPropertiesFormat.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 }