|
| 1 | +package azurerm |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/Azure/azure-sdk-for-go/arm/network" |
| 8 | + "github.com/Azure/azure-sdk-for-go/core/http" |
| 9 | + "github.com/hashicorp/terraform/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +// resourceArmLocalNetworkGateway returns the schema.Resource |
| 13 | +// associated to an Azure local network gateway. |
| 14 | +func resourceArmLocalNetworkGateway() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Create: resourceArmLocalNetworkGatewayCreate, |
| 17 | + Read: resourceArmLocalNetworkGatewayRead, |
| 18 | + Update: resourceArmLocalNetworkGatewayUpdate, |
| 19 | + Delete: resourceArmLocalNetworkGatewayDelete, |
| 20 | + |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "name": &schema.Schema{ |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + ForceNew: true, |
| 26 | + }, |
| 27 | + |
| 28 | + "location": &schema.Schema{ |
| 29 | + Type: schema.TypeString, |
| 30 | + Optional: true, |
| 31 | + ForceNew: true, |
| 32 | + StateFunc: azureRMNormalizeLocation, |
| 33 | + }, |
| 34 | + |
| 35 | + "resource_group_name": &schema.Schema{ |
| 36 | + Type: schema.TypeString, |
| 37 | + Optional: true, |
| 38 | + ForceNew: true, |
| 39 | + }, |
| 40 | + |
| 41 | + "resource_guid": &schema.Schema{ |
| 42 | + Type: schema.TypeString, |
| 43 | + Optional: true, |
| 44 | + }, |
| 45 | + |
| 46 | + "gateway_address": &schema.Schema{ |
| 47 | + Type: schema.TypeString, |
| 48 | + Required: true, |
| 49 | + }, |
| 50 | + |
| 51 | + "address_space": &schema.Schema{ |
| 52 | + Type: schema.TypeList, |
| 53 | + Required: true, |
| 54 | + Elem: &schema.Schema{ |
| 55 | + Type: schema.TypeString, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// resourceArmLocalNetworkGatewayCreate goes ahead and creates the specified ARM local network gateway. |
| 63 | +func resourceArmLocalNetworkGatewayCreate(d *schema.ResourceData, meta interface{}) error { |
| 64 | + lnetClient := meta.(*ArmClient).localNetConnClient |
| 65 | + |
| 66 | + name := d.Get("name").(string) |
| 67 | + location := d.Get("location").(string) |
| 68 | + resGroup := d.Get("resource_group_name").(string) |
| 69 | + ipAddress := d.Get("gateway_address").(string) |
| 70 | + |
| 71 | + // NOTE: due to the including-but-different relationship between the ASM |
| 72 | + // and ARM APIs, one may set the following local network gateway type to |
| 73 | + // "Classic" and basically get an old ASM local network connection through |
| 74 | + // the ARM API. This functionality is redundant with respect to the old |
| 75 | + // ASM-based implementation which we already have, so we just use the |
| 76 | + // new Resource Manager APIs here: |
| 77 | + typ := "Resource Manager" |
| 78 | + |
| 79 | + // fetch the 'address_space_prefix'es: |
| 80 | + prefixes := []string{} |
| 81 | + for _, pref := range d.Get("addres_space").([]interface{}) { |
| 82 | + prefixes = append(prefixes, pref.(string)) |
| 83 | + } |
| 84 | + |
| 85 | + // NOTE: result ignored here; review below... |
| 86 | + resp, err := lnetClient.CreateOrUpdate(resGroup, name, network.LocalNetworkGateway{ |
| 87 | + Name: &name, |
| 88 | + Location: &location, |
| 89 | + Type: &typ, |
| 90 | + Properties: &network.LocalNetworkGatewayPropertiesFormat{ |
| 91 | + LocalNetworkAddressSpace: &network.AddressSpace{ |
| 92 | + AddressPrefixes: &prefixes, |
| 93 | + }, |
| 94 | + GatewayIPAddress: &ipAddress, |
| 95 | + }, |
| 96 | + }) |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("Error reading the state of Azure ARM Local Network Gateway '%s': %s", name, err) |
| 99 | + } |
| 100 | + |
| 101 | + // NOTE: we either call read here or basically repeat the reading process |
| 102 | + // with the ignored network.LocalNetworkGateway result of the above: |
| 103 | + d.SetId(*resp.ID) |
| 104 | + return resourceArmLocalNetworkGatewayRead(d, meta) |
| 105 | +} |
| 106 | + |
| 107 | +// resourceArmLocalNetworkGatewayRead goes ahead and reads the state of the corresponding ARM local network gateway. |
| 108 | +func resourceArmLocalNetworkGatewayRead(d *schema.ResourceData, meta interface{}) error { |
| 109 | + lnetClient := meta.(*ArmClient).localNetConnClient |
| 110 | + |
| 111 | + name := d.Get("name").(string) |
| 112 | + resGroup := d.Get("resource_group_name").(string) |
| 113 | + |
| 114 | + log.Printf("[INFO] Sending GET request to Azure ARM for local network gateway '%s'.", name) |
| 115 | + lnet, err := lnetClient.Get(resGroup, name) |
| 116 | + if lnet.StatusCode == http.StatusNotFound { |
| 117 | + // it means that the resource has been deleted in the meantime... |
| 118 | + d.SetId("") |
| 119 | + return nil |
| 120 | + } |
| 121 | + if err != nil { |
| 122 | + return fmt.Errorf("Error reading the state of Azure ARM local network gateway '%s': %s", name, err) |
| 123 | + } |
| 124 | + |
| 125 | + d.Set("resource_guid", *lnet.Properties.ResourceGUID) |
| 126 | + d.Set("gateway_address", *lnet.Properties.GatewayIPAddress) |
| 127 | + |
| 128 | + prefs := []string{} |
| 129 | + if ps := *lnet.Properties.LocalNetworkAddressSpace.AddressPrefixes; ps != nil { |
| 130 | + prefs = ps |
| 131 | + } |
| 132 | + d.Set("address_space", prefs) |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +// resourceArmLocalNetworkGatewayUpdate goes ahead and updates the corresponding ARM local network gateway. |
| 138 | +func resourceArmLocalNetworkGatewayUpdate(d *schema.ResourceData, meta interface{}) error { |
| 139 | + // NOTE: considering the idempotency, we can safely call create again on |
| 140 | + // update. This has been written out in order to ensure clarity, |
| 141 | + return resourceArmLocalNetworkGatewayCreate(d, meta) |
| 142 | +} |
| 143 | + |
| 144 | +// resourceArmLocalNetworkGatewayDelete deletes the specified ARM local network gateway. |
| 145 | +func resourceArmLocalNetworkGatewayDelete(d *schema.ResourceData, meta interface{}) error { |
| 146 | + lnetClient := meta.(*ArmClient).localNetConnClient |
| 147 | + |
| 148 | + name := d.Get("name").(string) |
| 149 | + resGroup := d.Get("resource_group_name").(string) |
| 150 | + |
| 151 | + log.Printf("[INFO] Sending Azure ARM delete request for local network gateway '%s'.", name) |
| 152 | + _, err := lnetClient.Delete(resGroup, name) |
| 153 | + if err != nil { |
| 154 | + return fmt.Errorf("Error issuing Azure ARM delete request of local network gateway '%s': %s", name, err) |
| 155 | + } |
| 156 | + |
| 157 | + return nil |
| 158 | +} |
0 commit comments