Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public ip prefix #3139

Merged
merged 9 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,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 @@ -1042,6 +1043,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 @@ -317,6 +317,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
16 changes: 14 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-08-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,
Required: true,
steve-hawkins marked this conversation as resolved.
Show resolved Hide resolved
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},

"zones": singleZonesSchema(),

"tags": tagsSchema(),
Expand Down Expand Up @@ -182,6 +189,8 @@ func resourceArmPublicIpCreateUpdate(d *schema.ResourceData, meta interface{}) e
}
}

publicIpPrefixId := d.Get("public_ip_prefix_id").(string)

publicIp := network.PublicIPAddress{
Name: &name,
Location: &location,
Expand All @@ -192,6 +201,9 @@ func resourceArmPublicIpCreateUpdate(d *schema.ResourceData, meta interface{}) e
PublicIPAllocationMethod: network.IPAllocationMethod(ipAllocationMethod),
PublicIPAddressVersion: ipVersion,
IdleTimeoutInMinutes: utils.Int32(int32(idleTimeout)),
PublicIPPrefix: &network.SubResource{
ID: utils.String(publicIpPrefixId),
},
},
Tags: expandTags(tags),
Zones: zones,
Expand Down
191 changes: 191 additions & 0 deletions azurerm/resource_arm_public_ip_prefix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package azurerm

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-08-01/network"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"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: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
steve-hawkins marked this conversation as resolved.
Show resolved Hide resolved
id, err := parseAzureResourceID(d.Id())
if err != nil {
return nil, err
}
name := id.Path["publicIPPrefixes"]
if name == "" {
return nil, fmt.Errorf("Error parsing supplied resource id. Please check it and rerun:\n %s", d.Id())
}
return []*schema.ResourceData{d}, nil
},
},

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),
DiffSuppressFunc: suppress.CaseDifference,
steve-hawkins marked this conversation as resolved.
Show resolved Hide resolved
ValidateFunc: validation.StringInSlice([]string{
string(network.Standard),
}, true),
steve-hawkins marked this conversation as resolved.
Show resolved Hide resolved
},

"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