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

azurerm_stack_hci_marketplace_gallery_image, azurerm_stack_hci_network_interface, azurerm_stack_hci_virtual_hard_disk - add wait for create #28394

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package azurestackhci
import (
"context"
"fmt"
"log"
"regexp"
"time"

Expand Down Expand Up @@ -193,6 +194,11 @@ func (r StackHCIMarketplaceGalleryImageResource) Create() sdk.ResourceFunc {
return fmt.Errorf("performing create %s: %+v", id, err)
}

// https://github.com/Azure/azure-rest-api-specs/issues/31876
if err := resourceMarketplaceGalleryImageWaitForCreated(ctx, *client, id); err != nil {
return fmt.Errorf("waiting for %s to be created: %+v", id, err)
}

metadata.SetID(id)

return nil
Expand Down Expand Up @@ -330,3 +336,42 @@ func flattenStackHCIMarketplaceGalleryImageIdentifier(input *marketplacegalleryi
},
}
}

func resourceMarketplaceGalleryImageWaitForCreated(ctx context.Context, client marketplacegalleryimages.MarketplaceGalleryImagesClient, id marketplacegalleryimages.MarketplaceGalleryImageId) error {
deadline, ok := ctx.Deadline()
if !ok {
return fmt.Errorf("internal error: context had no deadline")
}

state := &pluginsdk.StateChangeConf{
MinTimeout: 10 * time.Second,
ContinuousTargetOccurence: 3,
Pending: []string{"NotFound"},
Target: []string{"Found"},
Refresh: resourceMarketplaceGalleryImageRefreshFunc(ctx, client, id),
Timeout: time.Until(deadline),
}

if _, err := state.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for %s to be created: %+v", id, err)
}

return nil
}

func resourceMarketplaceGalleryImageRefreshFunc(ctx context.Context, client marketplacegalleryimages.MarketplaceGalleryImagesClient, id marketplacegalleryimages.MarketplaceGalleryImageId) pluginsdk.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Printf("[DEBUG] Checking status for %s ..", 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, "Found", nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "test" {
sku = "2022-datacenter-azure-edition-core"
}

lifecycle {
ignore_changes = [storage_path_id]
}

depends_on = [azurerm_role_assignment.test]
}
`, template, data.RandomString, os.Getenv(customLocationIdEnv), r.imageVersion)
Expand All @@ -189,6 +193,10 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "import" {
offer = azurerm_stack_hci_marketplace_gallery_image.test.identifier.0.offer
sku = azurerm_stack_hci_marketplace_gallery_image.test.identifier.0.sku
}

lifecycle {
ignore_changes = [storage_path_id]
}
}
`, config)
}
Expand Down Expand Up @@ -224,6 +232,8 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "test" {
offer = "WindowsServer"
sku = "2022-datacenter-azure-edition-core"
}

depends_on = [azurerm_role_assignment.test]
}
`, template, data.RandomString, os.Getenv(customLocationIdEnv), r.imageVersion)
}
Expand Down Expand Up @@ -262,6 +272,8 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "test" {
tags = {
foo = "bar"
}

depends_on = [azurerm_role_assignment.test]
}
`, template, data.RandomString, os.Getenv(customLocationIdEnv), r.imageVersion)
}
Expand Down Expand Up @@ -301,6 +313,8 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "test" {
foo = "bar"
env = "test"
}

depends_on = [azurerm_role_assignment.test]
}
`, template, data.RandomString, os.Getenv(customLocationIdEnv), r.imageVersion)
}
Expand All @@ -312,9 +326,7 @@ resource "azurerm_resource_group" "test" {
location = "%s"
}

data "azurerm_client_config" "test" {}

// service principal of 'Microsoft.AzureStackHCI Resource Provider'
// service principal of 'Microsoft.AzureStackHCI' Resource Provider
data "azuread_service_principal" "hciRp" {
client_id = "1412d89f-b8a8-4111-b4fd-e82905cbd85d"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package azurestackhci
import (
"context"
"fmt"
"log"
"regexp"
"time"

Expand Down Expand Up @@ -183,6 +184,11 @@ func (r StackHCINetworkInterfaceResource) Create() sdk.ResourceFunc {

metadata.SetID(id)

// https://github.com/Azure/azure-rest-api-specs/issues/31876
if err := resourceNetworkInterfaceWaitForCreated(ctx, *client, id); err != nil {
return fmt.Errorf("waiting for %s to be created: %+v", id, err)
}

return nil
},
}
Expand Down Expand Up @@ -352,3 +358,42 @@ func flattenStackHCINetworkInterfaceIPConfiguration(input *[]networkinterfaces.I

return results, nil
}

func resourceNetworkInterfaceWaitForCreated(ctx context.Context, client networkinterfaces.NetworkInterfacesClient, id networkinterfaces.NetworkInterfaceId) error {
deadline, ok := ctx.Deadline()
if !ok {
return fmt.Errorf("internal error: context had no deadline")
}

state := &pluginsdk.StateChangeConf{
MinTimeout: 10 * time.Second,
ContinuousTargetOccurence: 4,
Pending: []string{"NotFound"},
Target: []string{"Found"},
Refresh: resourceNetworkInterfaceRefreshFunc(ctx, client, id),
Timeout: time.Until(deadline),
}

if _, err := state.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for %s to be created: %+v", id, err)
}

return nil
}

func resourceNetworkInterfaceRefreshFunc(ctx context.Context, client networkinterfaces.NetworkInterfacesClient, id networkinterfaces.NetworkInterfaceId) pluginsdk.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Printf("[DEBUG] Checking status for %s ..", 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, "Found", nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ resource "azurerm_stack_hci_network_interface" "test" {
}

lifecycle {
ignore_changes = [mac_address]
ignore_changes = [mac_address, ip_configuration.0.private_ip_address]
}
}
`, template, data.RandomString, os.Getenv(customLocationIdEnv))
Expand All @@ -184,6 +184,10 @@ resource "azurerm_stack_hci_network_interface" "import" {
ip_configuration {
subnet_id = azurerm_stack_hci_network_interface.test.ip_configuration.0.subnet_id
}

lifecycle {
ignore_changes = [mac_address, ip_configuration.0.private_ip_address]
}
}
`, config)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package azurestackhci
import (
"context"
"fmt"
"log"
"regexp"
"time"

Expand Down Expand Up @@ -209,6 +210,11 @@ func (r StackHCIVirtualHardDiskResource) Create() sdk.ResourceFunc {
return fmt.Errorf("performing create %s: %+v", id, err)
}

// https://github.com/Azure/azure-rest-api-specs/issues/31876
if err := resourceVirtualHardDiskWaitForCreated(ctx, *client, id); err != nil {
return fmt.Errorf("waiting for %s to be created: %+v", id, err)
}

metadata.SetID(id)

return nil
Expand Down Expand Up @@ -319,3 +325,42 @@ func (r StackHCIVirtualHardDiskResource) Delete() sdk.ResourceFunc {
},
}
}

func resourceVirtualHardDiskWaitForCreated(ctx context.Context, client virtualharddisks.VirtualHardDisksClient, id virtualharddisks.VirtualHardDiskId) error {
deadline, ok := ctx.Deadline()
if !ok {
return fmt.Errorf("internal error: context had no deadline")
}

state := &pluginsdk.StateChangeConf{
MinTimeout: 10 * time.Second,
ContinuousTargetOccurence: 4,
Pending: []string{"NotFound"},
Target: []string{"Found"},
Refresh: resourceVirtualHardDiskRefreshFunc(ctx, client, id),
Timeout: time.Until(deadline),
}

if _, err := state.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for %s to be created: %+v", id, err)
}

return nil
}

func resourceVirtualHardDiskRefreshFunc(ctx context.Context, client virtualharddisks.VirtualHardDisksClient, id virtualharddisks.VirtualHardDiskId) pluginsdk.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Printf("[DEBUG] Checking status for %s ..", 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, "Found", nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ resource "azurerm_resource_group" "example" {

data "azurerm_client_config" "example" {}

// service principal of 'Microsoft.AzureStackHCI Resource Provider'
// service principal of 'Microsoft.AzureStackHCI' Resource Provider
data "azuread_service_principal" "hciRp" {
client_id = "1412d89f-b8a8-4111-b4fd-e82905cbd85d"
}
Expand Down Expand Up @@ -48,6 +48,8 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "example" {
foo = "bar"
env = "example"
}

depends_on = [azurerm_role_assignment.example]
}
```

Expand Down Expand Up @@ -75,6 +77,8 @@ The following arguments are supported:

* `storage_path_id` - (Optional) The ID of the Azure Stack HCI Storage Path used for this Marketplace Gallery Image. Changing this forces a new Azure Stack HCI Virtual Hard Disk to be created.

-> **Note:** If `storage_path_id` is not specified, it will be assigned by the server. If you experience a diff you may need to add this to `ignore_changes`.

* `tags` - (Optional) A mapping of tags which should be assigned to the Azure Stack HCI Marketplace Gallery Image.

---
Expand All @@ -89,7 +93,7 @@ An `identifier` block supports the following:

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:
In addition to the Arguments listed above - the following Attributes are exported:

* `id` - The ID of the Azure Stack HCI Marketplace Gallery Image.

Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/stack_hci_network_interface.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ An `ip_configuration` block supports the following:

* `private_ip_address` - (Optional) The IPv4 address of the IP configuration. Changing this forces a new resource to be created.

-> **Note:** If `private_ip_address` is not specified, it will be assigned by the server. If you experience a diff you may need to add this to `ignore_changes`.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:
Expand Down
Loading