Skip to content

Commit

Permalink
Public ip prefix (#3139)
Browse files Browse the repository at this point in the history
This should fulfil Issue #2239 

(fixes #2239)
  • Loading branch information
Steven Hawkins authored and katbyte committed Apr 15, 2019
1 parent 00d00d1 commit 13913f0
Show file tree
Hide file tree
Showing 8 changed files with 688 additions and 2 deletions.
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ type ArmClient struct {
localNetConnClient network.LocalNetworkGatewaysClient
packetCapturesClient network.PacketCapturesClient
publicIPClient network.PublicIPAddressesClient
publicIPPrefixClient network.PublicIPPrefixesClient
routesClient network.RoutesClient
routeTablesClient network.RouteTablesClient
secGroupClient network.SecurityGroupsClient
Expand Down Expand Up @@ -1124,6 +1125,10 @@ func (c *ArmClient) registerNetworkingClients(endpoint, subscriptionId string, a
c.configureClient(&publicIPAddressesClient.Client, auth)
c.publicIPClient = publicIPAddressesClient

publicIPPrefixesClient := network.NewPublicIPPrefixesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&publicIPPrefixesClient.Client, auth)
c.publicIPPrefixClient = publicIPPrefixesClient

routesClient := network.NewRoutesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&routesClient.Client, auth)
c.routesClient = routesClient
Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_postgresql_server": resourceArmPostgreSQLServer(),
"azurerm_postgresql_virtual_network_rule": resourceArmPostgreSQLVirtualNetworkRule(),
"azurerm_public_ip": resourceArmPublicIp(),
"azurerm_public_ip_prefix": resourceArmPublicIpPrefix(),
"azurerm_recovery_services_protected_vm": resourceArmRecoveryServicesProtectedVm(),
"azurerm_recovery_services_protection_policy_vm": resourceArmRecoveryServicesProtectionPolicyVm(),
"azurerm_recovery_services_vault": resourceArmRecoveryServicesVault(),
Expand Down
19 changes: 17 additions & 2 deletions azurerm/resource_arm_public_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"log"
"strings"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -126,6 +126,13 @@ func resourceArmPublicIp() *schema.Resource {
Computed: true,
},

"public_ip_prefix_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},

"zones": singleZonesSchema(),

"tags": tagsSchema(),
Expand Down Expand Up @@ -197,6 +204,14 @@ func resourceArmPublicIpCreateUpdate(d *schema.ResourceData, meta interface{}) e
Zones: zones,
}

publicIpPrefixId, publicIpPrefixIdOk := d.GetOk("public_ip_prefix_id")

if publicIpPrefixIdOk {
publicIpPrefix := network.SubResource{}
publicIpPrefix.ID = utils.String(publicIpPrefixId.(string))
publicIp.PublicIPAddressPropertiesFormat.PublicIPPrefix = &publicIpPrefix
}

dnl, dnlOk := d.GetOk("domain_name_label")
rfqdn, rfqdnOk := d.GetOk("reverse_fqdn")

Expand Down
178 changes: 178 additions & 0 deletions azurerm/resource_arm_public_ip_prefix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package azurerm

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmPublicIpPrefix() *schema.Resource {
return &schema.Resource{
Create: resourceArmPublicIpPrefixCreateUpdate,
Read: resourceArmPublicIpPrefixRead,
Update: resourceArmPublicIpPrefixCreateUpdate,
Delete: resourceArmPublicIpPrefixDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
},

"location": locationSchema(),

"resource_group_name": resourceGroupNameSchema(),

"sku": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: string(network.Standard),
ValidateFunc: validation.StringInSlice([]string{
string(network.Standard),
}, false),
},

"prefix_length": {
Type: schema.TypeInt,
Optional: true,
Default: 28,
ForceNew: true,
ValidateFunc: validation.IntBetween(24, 31),
},

"ip_prefix": {
Type: schema.TypeString,
Computed: true,
},

"zones": singleZonesSchema(),

"tags": tagsSchema(),
},
}
}

func resourceArmPublicIpPrefixCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).publicIPPrefixClient
ctx := meta.(*ArmClient).StopContext

log.Printf("[INFO] preparing arguments for AzureRM Public IP Prefix creation.")

name := d.Get("name").(string)
location := azureRMNormalizeLocation(d.Get("location").(string))
resGroup := d.Get("resource_group_name").(string)
sku := d.Get("sku").(string)
prefix_length := d.Get("prefix_length").(int)
tags := d.Get("tags").(map[string]interface{})
zones := expandZones(d.Get("zones").([]interface{}))

publicIpPrefix := network.PublicIPPrefix{
Name: &name,
Location: &location,
Sku: &network.PublicIPPrefixSku{
Name: network.PublicIPPrefixSkuName(sku),
},
PublicIPPrefixPropertiesFormat: &network.PublicIPPrefixPropertiesFormat{
PrefixLength: utils.Int32(int32(prefix_length)),
},
Tags: expandTags(tags),
Zones: zones,
}

future, err := client.CreateOrUpdate(ctx, resGroup, name, publicIpPrefix)
if err != nil {
return fmt.Errorf("Error Creating/Updating Public IP Prefix %q (Resource Group %q): %+v", name, resGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for completion of Public IP Prefix %q (Resource Group %q): %+v", name, resGroup, err)
}

read, err := client.Get(ctx, resGroup, name, "")
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read Public IP Prefix %q (resource group %q) ID", name, resGroup)
}

d.SetId(*read.ID)

return resourceArmPublicIpPrefixRead(d, meta)
}

func resourceArmPublicIpPrefixRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).publicIPPrefixClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["publicIPPrefixes"]

resp, err := client.Get(ctx, resGroup, name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on Public IP Prefix %q (Resource Group %q): %+v", name, resGroup, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resGroup)
d.Set("zones", resp.Zones)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}

if sku := resp.Sku; sku != nil {
d.Set("sku", string(sku.Name))
}

if props := resp.PublicIPPrefixPropertiesFormat; props != nil {
d.Set("prefix_length", props.PrefixLength)
d.Set("ip_prefix", props.IPPrefix)
}

flattenAndSetTags(d, resp.Tags)

return nil
}

func resourceArmPublicIpPrefixDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).publicIPPrefixClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["publicIPPrefixes"]

future, err := client.Delete(ctx, resGroup, name)
if err != nil {
return fmt.Errorf("Error deleting Public IP Prefix %q (Resource Group %q): %+v", name, resGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for deletion of Public IP Prefix %q (Resource Group %q): %+v", name, resGroup, err)
}

return nil
}
Loading

0 comments on commit 13913f0

Please sign in to comment.