-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
688 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.