-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
New Resource: azurerm_network_manager_ipam_pool
#28695
Open
teowa
wants to merge
10
commits into
hashicorp:main
Choose a base branch
from
teowa:nm_ipam_pool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+640
−1
Open
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d6e6021
network manager ipam pool
teowa 4b517e9
Merge branch 'main' of github.com:hashicorp/terraform-provider-azurer…
teowa bad2f7c
fix doc
teowa 7cd763b
fix test
teowa d3f0d58
fix test
teowa 3d0c4e5
fix test
teowa 7b193ef
fix name validation
teowa c7c0898
fix error message
teowa 2049bcb
fix code
teowa 7f6b19f
fix
teowa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
303 changes: 303 additions & 0 deletions
303
internal/services/network/network_manager_ipam_pool_resource.go
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,303 @@ | ||
package network | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/hashicorp/go-azure-helpers/lang/pointer" | ||
"github.com/hashicorp/go-azure-helpers/lang/response" | ||
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" | ||
"github.com/hashicorp/go-azure-helpers/resourcemanager/location" | ||
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags" | ||
"github.com/hashicorp/go-azure-sdk/resource-manager/network/2024-05-01/ipampools" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" | ||
) | ||
|
||
var ( | ||
_ sdk.Resource = ManagerIpamPoolResource{} | ||
_ sdk.ResourceWithUpdate = ManagerIpamPoolResource{} | ||
) | ||
|
||
type ManagerIpamPoolResource struct{} | ||
|
||
func (ManagerIpamPoolResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||
return ipampools.ValidateIPamPoolID | ||
} | ||
|
||
func (ManagerIpamPoolResource) ResourceType() string { | ||
return "azurerm_network_manager_ipam_pool" | ||
} | ||
|
||
func (ManagerIpamPoolResource) ModelObject() interface{} { | ||
return &ManagerIpamPoolResourceModel{} | ||
} | ||
|
||
type ManagerIpamPoolResourceModel struct { | ||
AddressPrefixes []string `tfschema:"address_prefixes"` | ||
Description string `tfschema:"description"` | ||
DisplayName string `tfschema:"display_name"` | ||
Location string `tfschema:"location"` | ||
Name string `tfschema:"name"` | ||
NetworkManagerId string `tfschema:"network_manager_id"` | ||
ParentPoolName string `tfschema:"parent_pool_name"` | ||
Tags map[string]interface{} `tfschema:"tags"` | ||
} | ||
|
||
func (ManagerIpamPoolResource) Arguments() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{ | ||
"name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringMatch( | ||
regexp.MustCompile(`^[a-zA-Z0-9\_\.\-]{1,64}$`), | ||
"name must be between 1 and 64 characters long and can only contain letters, numbers, underscores(_), periods(.), and hyphens(-).", | ||
), | ||
}, | ||
|
||
"network_manager_id": commonschema.ResourceIDReferenceRequiredForceNew(&ipampools.NetworkManagerId{}), | ||
|
||
"location": commonschema.Location(), | ||
|
||
"display_name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringMatch( | ||
regexp.MustCompile(`^[a-zA-Z0-9\_\.\-]{1,64}$`), | ||
"display_name must be between 1 and 64 characters long and can only contain letters, numbers, underscores(_), periods(.), and hyphens(-).", | ||
), | ||
}, | ||
|
||
"address_prefixes": { | ||
Type: pluginsdk.TypeList, | ||
Required: true, | ||
ForceNew: true, | ||
Elem: &pluginsdk.Schema{ | ||
Type: pluginsdk.TypeString, | ||
ValidateFunc: validation.IsCIDR, | ||
}, | ||
}, | ||
|
||
"parent_pool_name": { | ||
Type: pluginsdk.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringMatch( | ||
regexp.MustCompile(`^[a-zA-Z0-9\_\.\-]{1,64}$`), | ||
"parent_pool_name must be between 1 and 64 characters long and can only contain letters, numbers, underscores(_), periods(.), and hyphens(-).", | ||
), | ||
}, | ||
|
||
"description": { | ||
Type: pluginsdk.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.StringIsNotEmpty, | ||
}, | ||
|
||
"tags": commonschema.Tags(), | ||
} | ||
} | ||
|
||
func (ManagerIpamPoolResource) Attributes() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{} | ||
} | ||
|
||
func (r ManagerIpamPoolResource) Create() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Network.IPamPools | ||
|
||
var config ManagerIpamPoolResourceModel | ||
if err := metadata.Decode(&config); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
networkManagerId, err := ipampools.ParseNetworkManagerID(config.NetworkManagerId) | ||
if err != nil { | ||
return fmt.Errorf("parsing `network_manager_id`: %+v", err) | ||
} | ||
|
||
subscriptionId := metadata.Client.Account.SubscriptionId | ||
id := ipampools.NewIPamPoolID(subscriptionId, networkManagerId.ResourceGroupName, networkManagerId.NetworkManagerName, config.Name) | ||
|
||
existing, err := client.Get(ctx, id) | ||
if err != nil && !response.WasNotFound(existing.HttpResponse) { | ||
return fmt.Errorf("checking for presence of existing %s: %+v", id, err) | ||
} | ||
if !response.WasNotFound(existing.HttpResponse) { | ||
return metadata.ResourceRequiresImport(r.ResourceType(), id) | ||
} | ||
|
||
payload := ipampools.IPamPool{ | ||
Name: pointer.To(config.Name), | ||
Location: location.Normalize(config.Location), | ||
Tags: tags.Expand(config.Tags), | ||
Properties: ipampools.IPamPoolProperties{ | ||
AddressPrefixes: config.AddressPrefixes, | ||
Description: pointer.To(config.Description), | ||
DisplayName: pointer.To(config.DisplayName), | ||
ParentPoolName: pointer.To(config.ParentPoolName), | ||
}, | ||
} | ||
|
||
if err := client.CreateThenPoll(ctx, id, payload); err != nil { | ||
return fmt.Errorf("performing create %s: %+v", id, err) | ||
} | ||
|
||
metadata.SetID(id) | ||
|
||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (r ManagerIpamPoolResource) Read() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 5 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Network.IPamPools | ||
|
||
id, err := ipampools.ParseIPamPoolID(metadata.ResourceData.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := client.Get(ctx, *id) | ||
if err != nil { | ||
if response.WasNotFound(resp.HttpResponse) { | ||
return metadata.MarkAsGone(id) | ||
} | ||
|
||
return fmt.Errorf("retrieving %s: %+v", id, err) | ||
} | ||
|
||
networkManagerId := ipampools.NewNetworkManagerID(id.SubscriptionId, id.ResourceGroupName, id.NetworkManagerName).ID() | ||
schema := ManagerIpamPoolResourceModel{ | ||
Name: id.IpamPoolName, | ||
NetworkManagerId: networkManagerId, | ||
} | ||
|
||
if model := resp.Model; model != nil { | ||
schema.Location = location.Normalize(model.Location) | ||
schema.Tags = tags.Flatten(model.Tags) | ||
|
||
props := model.Properties | ||
schema.AddressPrefixes = props.AddressPrefixes | ||
schema.Description = pointer.From(props.Description) | ||
schema.DisplayName = pointer.From(props.DisplayName) | ||
schema.ParentPoolName = pointer.From(props.ParentPoolName) | ||
} | ||
|
||
return metadata.Encode(&schema) | ||
}, | ||
} | ||
} | ||
|
||
func (r ManagerIpamPoolResource) Update() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Network.IPamPools | ||
|
||
id, err := ipampools.ParseIPamPoolID(metadata.ResourceData.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var model ManagerIpamPoolResourceModel | ||
if err := metadata.Decode(&model); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
parameters := ipampools.IPamPoolUpdate{ | ||
Properties: &ipampools.IPamPoolUpdateProperties{}, | ||
} | ||
|
||
if metadata.ResourceData.HasChange("tags") { | ||
parameters.Tags = tags.Expand(model.Tags) | ||
} | ||
|
||
if metadata.ResourceData.HasChange("description") { | ||
parameters.Properties.Description = pointer.To(model.Description) | ||
} | ||
|
||
if metadata.ResourceData.HasChange("display_name") { | ||
parameters.Properties.DisplayName = pointer.To(model.DisplayName) | ||
} | ||
|
||
if _, err := client.Update(ctx, *id, parameters); err != nil { | ||
return fmt.Errorf("updating %s: %+v", id, err) | ||
} | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (r ManagerIpamPoolResource) Delete() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.Network.IPamPools | ||
|
||
id, err := ipampools.ParseIPamPoolID(metadata.ResourceData.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := client.DeleteThenPoll(ctx, *id); err != nil { | ||
return fmt.Errorf("deleting %s: %+v", id, err) | ||
} | ||
|
||
// https://github.com/Azure/azure-rest-api-specs/issues/31688 | ||
if err := resourceIpamPoolWaitForDeleted(ctx, *client, *id); err != nil { | ||
return fmt.Errorf("waiting for %s to be deleted: %+v", id, err) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func resourceIpamPoolWaitForDeleted(ctx context.Context, client ipampools.IPamPoolsClient, id ipampools.IPamPoolId) error { | ||
deadline, ok := ctx.Deadline() | ||
if !ok { | ||
return fmt.Errorf("internal error: context had no deadline") | ||
} | ||
state := &pluginsdk.StateChangeConf{ | ||
MinTimeout: 5 * time.Second, | ||
ContinuousTargetOccurence: 3, | ||
Pending: []string{"Exists"}, | ||
Target: []string{"NotFound"}, | ||
Refresh: resourceIpamPoolRefreshFunc(ctx, client, id), | ||
Timeout: time.Until(deadline), | ||
} | ||
|
||
if _, err := state.WaitForStateContext(ctx); err != nil { | ||
return fmt.Errorf("waiting for %s to be deleted: %+v", id, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceIpamPoolRefreshFunc(ctx context.Context, client ipampools.IPamPoolsClient, id ipampools.IPamPoolId) pluginsdk.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
log.Printf("[DEBUG] Checking %s status ..", id) | ||
|
||
resp, err := client.Get(ctx, id) | ||
if err != nil { | ||
if response.WasNotFound(resp.HttpResponse) { | ||
return resp, "NotFound", nil | ||
} | ||
|
||
return resp, "Error", fmt.Errorf("retrieving %s: %+v", id, err) | ||
} | ||
|
||
return resp, "Exists", nil | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed