From c0dfc7fb76d127d192ee7af8c30ce5265aef9790 Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Mon, 26 Aug 2024 01:10:25 +0000 Subject: [PATCH 1/6] new resource stack hci marketplace gallery image --- .../services/azurestackhci/registration.go | 1 + ..._hci_marketplace_gallery_image_resource.go | 357 ++++++++++++++++++ ...marketplace_gallery_image_resource_test.go | 234 ++++++++++++ 3 files changed, 592 insertions(+) create mode 100644 internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go create mode 100644 internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go diff --git a/internal/services/azurestackhci/registration.go b/internal/services/azurestackhci/registration.go index 49f65474ad48..806368509a4a 100644 --- a/internal/services/azurestackhci/registration.go +++ b/internal/services/azurestackhci/registration.go @@ -52,5 +52,6 @@ func (r Registration) DataSources() []sdk.DataSource { func (r Registration) Resources() []sdk.Resource { return []sdk.Resource{ StackHCILogicalNetworkResource{}, + StackHCIMarketplaceGalleryImageResource{}, } } diff --git a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go new file mode 100644 index 000000000000..0237d4b7b251 --- /dev/null +++ b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go @@ -0,0 +1,357 @@ +package azurestackhci + +import ( + "context" + "fmt" + "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/azurestackhci/2024-01-01/marketplacegalleryimages" + "github.com/hashicorp/go-azure-sdk/resource-manager/azurestackhci/2024-01-01/storagecontainers" + "github.com/hashicorp/go-azure-sdk/resource-manager/extendedlocation/2021-08-15/customlocations" + "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 = StackHCIMarketplaceGalleryImageResource{} + _ sdk.ResourceWithUpdate = StackHCIMarketplaceGalleryImageResource{} +) + +type StackHCIMarketplaceGalleryImageResource struct{} + +func (StackHCIMarketplaceGalleryImageResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { + return marketplacegalleryimages.ValidateMarketplaceGalleryImageID +} + +func (StackHCIMarketplaceGalleryImageResource) ResourceType() string { + return "azurerm_stack_hci_marketplace_gallery_image" +} + +func (StackHCIMarketplaceGalleryImageResource) ModelObject() interface{} { + return &StackHCIMarketplaceGalleryImageResourceModel{} +} + +type StackHCIMarketplaceGalleryImageResourceModel struct { + Name string `tfschema:"name"` + ResourceGroupName string `tfschema:"resource_group_name"` + Location string `tfschema:"location"` + CustomLocationId string `tfschema:"custom_location_id"` + CloudInitDataSource string `tfschema:"cloud_init_data_source"` + HypervGeneration string `tfschema:"hyperv_generation"` + Identifier []StackHCIMarketplaceGalleryImageIdentifier `tfschema:"identifier"` + OsType string `tfschema:"os_type"` + Version string `tfschema:"version"` + StoragePathId string `tfschema:"storage_path_id"` + Tags map[string]interface{} `tfschema:"tags"` +} + +type StackHCIMarketplaceGalleryImageIdentifier struct { + Offer string `tfschema:"offer"` + Publisher string `tfschema:"publisher"` + Sku string `tfschema:"sku"` +} + +func (StackHCIMarketplaceGalleryImageResource) 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][\-\.\_a-zA-Z0-9]{0,78}[a-zA-Z0-9]$`), + "name must be between 2 and 80 characters and can only contain alphanumberic characters, hyphen, dot and underline", + ), + }, + + "resource_group_name": commonschema.ResourceGroupName(), + + "location": commonschema.Location(), + + "custom_location_id": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: customlocations.ValidateCustomLocationID, + }, + + "identifier": { + Type: pluginsdk.TypeList, + Required: true, + ForceNew: true, + Elem: &pluginsdk.Resource{ + Schema: map[string]*pluginsdk.Schema{ + "publisher": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "offer": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "sku": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + }, + }, + }, + + "os_type": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + string(marketplacegalleryimages.OperatingSystemTypesLinux), + string(marketplacegalleryimages.OperatingSystemTypesWindows), + }, false), + }, + + "version": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "cloud_init_data_source": { + Type: pluginsdk.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + string(marketplacegalleryimages.CloudInitDataSourceAzure), + string(marketplacegalleryimages.CloudInitDataSourceNoCloud), + }, false), + }, + + "hyperv_generation": { + Type: pluginsdk.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + string(marketplacegalleryimages.HyperVGenerationVOne), + string(marketplacegalleryimages.HyperVGenerationVTwo), + }, false), + }, + + "storage_path_id": { + Type: pluginsdk.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: storagecontainers.ValidateStorageContainerID, + }, + + "tags": commonschema.Tags(), + } +} + +func (StackHCIMarketplaceGalleryImageResource) Attributes() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{} +} + +func (r StackHCIMarketplaceGalleryImageResource) Create() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 30 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + client := metadata.Client.AzureStackHCI.MarketplaceGalleryImages + + var config StackHCIMarketplaceGalleryImageResourceModel + if err := metadata.Decode(&config); err != nil { + return fmt.Errorf("decoding: %+v", err) + } + + subscriptionId := metadata.Client.Account.SubscriptionId + id := marketplacegalleryimages.NewMarketplaceGalleryImageID(subscriptionId, config.ResourceGroupName, 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 := marketplacegalleryimages.MarketplaceGalleryImages{ + Name: pointer.To(config.Name), + Location: location.Normalize(config.Location), + Tags: tags.Expand(config.Tags), + ExtendedLocation: &marketplacegalleryimages.ExtendedLocation{ + Name: pointer.To(config.CustomLocationId), + Type: pointer.To(marketplacegalleryimages.ExtendedLocationTypesCustomLocation), + }, + Properties: &marketplacegalleryimages.MarketplaceGalleryImageProperties{ + Identifier: expandStackHCIMarketplaceGalleryImageIdentifier(config.Identifier), + OsType: marketplacegalleryimages.OperatingSystemTypes(config.OsType), + }, + } + + if config.CloudInitDataSource != "" { + payload.Properties.CloudInitDataSource = pointer.To(marketplacegalleryimages.CloudInitDataSource(config.CloudInitDataSource)) + } + + if config.StoragePathId != "" { + payload.Properties.ContainerId = pointer.To(config.StoragePathId) + } + + if config.HypervGeneration != "" { + payload.Properties.HyperVGeneration = pointer.To(marketplacegalleryimages.HyperVGeneration(config.HypervGeneration)) + } + + if config.Version != "" { + payload.Properties.Version = &marketplacegalleryimages.GalleryImageVersion{ + Name: pointer.To(config.Version), + } + } + + if err := client.CreateOrUpdateThenPoll(ctx, id, payload); err != nil { + return fmt.Errorf("performing create %s: %+v", id, err) + } + + metadata.SetID(id) + + return nil + }, + } +} + +func (r StackHCIMarketplaceGalleryImageResource) Read() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 5 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + client := metadata.Client.AzureStackHCI.MarketplaceGalleryImages + + id, err := marketplacegalleryimages.ParseMarketplaceGalleryImageID(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) + } + + schema := StackHCIMarketplaceGalleryImageResourceModel{ + Name: id.MarketplaceGalleryImageName, + ResourceGroupName: id.ResourceGroupName, + } + + if model := resp.Model; model != nil { + schema.Location = location.Normalize(model.Location) + schema.Tags = tags.Flatten(model.Tags) + + if model.ExtendedLocation != nil && model.ExtendedLocation.Name != nil { + customLocationId, err := customlocations.ParseCustomLocationIDInsensitively(*model.ExtendedLocation.Name) + if err != nil { + return err + } + + schema.CustomLocationId = customLocationId.ID() + } + + if props := model.Properties; props != nil { + schema.CloudInitDataSource = string(pointer.From(props.CloudInitDataSource)) + schema.StoragePathId = string(pointer.From(props.ContainerId)) + schema.OsType = string(props.OsType) + schema.HypervGeneration = string(pointer.From(props.HyperVGeneration)) + } + + } + return metadata.Encode(&schema) + }, + } +} + +func (r StackHCIMarketplaceGalleryImageResource) Update() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 30 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + client := metadata.Client.AzureStackHCI.MarketplaceGalleryImages + + id, err := marketplacegalleryimages.ParseMarketplaceGalleryImageID(metadata.ResourceData.Id()) + if err != nil { + return err + } + + var model StackHCIMarketplaceGalleryImageResourceModel + if err := metadata.Decode(&model); err != nil { + return fmt.Errorf("decoding: %+v", err) + } + + parameters := &marketplacegalleryimages.MarketplaceGalleryImagesUpdateRequest{} + if metadata.ResourceData.HasChange("tags") { + parameters.Tags = tags.Expand(model.Tags) + } + + if err := client.UpdateThenPoll(ctx, *id, *parameters); err != nil { + return fmt.Errorf("updating %s: %+v", id, err) + } + return nil + }, + } +} + +func (r StackHCIMarketplaceGalleryImageResource) Delete() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 30 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + client := metadata.Client.AzureStackHCI.MarketplaceGalleryImages + + id, err := marketplacegalleryimages.ParseMarketplaceGalleryImageID(metadata.ResourceData.Id()) + if err != nil { + return err + } + + if err := client.DeleteThenPoll(ctx, *id); err != nil { + return fmt.Errorf("deleting %s: %+v", id, err) + } + + return nil + }, + } +} + +func expandStackHCIMarketplaceGalleryImageIdentifier(input []StackHCIMarketplaceGalleryImageIdentifier) *marketplacegalleryimages.GalleryImageIdentifier { + if len(input) == 0 { + return nil + } + + v := input[0] + + return &marketplacegalleryimages.GalleryImageIdentifier{ + Offer: v.Offer, + Publisher: v.Publisher, + Sku: v.Sku, + } +} + +func flattenStackHCIMarketplaceGalleryImageIdentifier(input *marketplacegalleryimages.GalleryImageIdentifier) []StackHCIMarketplaceGalleryImageIdentifier { + if input == nil { + return make([]StackHCIMarketplaceGalleryImageIdentifier, 0) + } + + return []StackHCIMarketplaceGalleryImageIdentifier{ + { + Offer: input.Offer, + Publisher: input.Publisher, + Sku: input.Sku, + }, + } +} diff --git a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go new file mode 100644 index 000000000000..d64c7d16f1af --- /dev/null +++ b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go @@ -0,0 +1,234 @@ +package azurestackhci_test + +import ( + "context" + "fmt" + "os" + "testing" + + "github.com/hashicorp/go-azure-helpers/lang/pointer" + "github.com/hashicorp/go-azure-sdk/resource-manager/azurestackhci/2024-01-01/marketplacegalleryimages" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance" + "github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check" + "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" +) + +type StackHCIMarketplaceGalleryImageResource struct{} + +func TestAccStackHCIMarketplaceGalleryImage_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_stack_hci_marketplace_gallery_image", "test") + r := StackHCIMarketplaceGalleryImageResource{} + + if os.Getenv(customLocationIdEnv) == "" { + t.Skipf("skipping since %q has not been specified", customLocationIdEnv) + } + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccStackHCIMarketplaceGalleryImage_complete(t *testing.T) { + if os.Getenv(customLocationIdEnv) == "" { + t.Skipf("skipping since %q has not been specified", customLocationIdEnv) + } + + data := acceptance.BuildTestData(t, "azurerm_stack_hci_marketplace_gallery_image", "test") + r := StackHCIMarketplaceGalleryImageResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.complete(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccStackHCIMarketplaceGalleryImage_update(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_stack_hci_marketplace_gallery_image", "test") + r := StackHCIMarketplaceGalleryImageResource{} + + if os.Getenv(customLocationIdEnv) == "" { + t.Skipf("skipping since %q has not been specified", customLocationIdEnv) + } + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.complete(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccStackHCIMarketplaceGalleryImage_requiresImport(t *testing.T) { + if os.Getenv(customLocationIdEnv) == "" { + t.Skipf("skipping since %q has not been specified", customLocationIdEnv) + } + + data := acceptance.BuildTestData(t, "azurerm_stack_hci_marketplace_gallery_image", "test") + r := StackHCIMarketplaceGalleryImageResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.RequiresImportErrorStep(r.requiresImport), + }) +} + +func (r StackHCIMarketplaceGalleryImageResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { + clusterClient := client.AzureStackHCI.MarketplaceGalleryImages + id, err := marketplacegalleryimages.ParseMarketplaceGalleryImageID(state.ID) + if err != nil { + return nil, err + } + + resp, err := clusterClient.Get(ctx, *id) + if err != nil { + return nil, fmt.Errorf("retrieving %s: %+v", *id, err) + } + + return pointer.To(resp.Model != nil), nil +} + +func (r StackHCIMarketplaceGalleryImageResource) basic(data acceptance.TestData) string { + template := r.template(data) + return fmt.Sprintf(` +%s + +provider "azurerm" { + features {} +} + +resource "azurerm_stack_hci_marketplace_gallery_image" "test" { + name = "acctest-mgi-${var.random_string}" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + custom_location_id = %q + os_type = "Windows" + version = "20348.2113.231109" + identifier { + publisher = "MicrosoftWindowsServer" + offer = "WindowsServer" + sku = "2022-datacenter-azure-edition" + } + + depends_on = [azurerm_role_assignment.test] +} +`, template, os.Getenv(customLocationIdEnv)) +} + +func (r StackHCIMarketplaceGalleryImageResource) requiresImport(data acceptance.TestData) string { + config := r.basic(data) + + return fmt.Sprintf(` +%s + +resource "azurerm_stack_hci_marketplace_gallery_image" "import" { + name = azurerm_stack_hci_marketplace_gallery_image.test.name + resource_group_name = azurerm_stack_hci_marketplace_gallery_image.test.resource_group_name + location = azurerm_stack_hci_marketplace_gallery_image.test.location + custom_location_id = azurerm_stack_hci_marketplace_gallery_image.test.custom_location_id +} +`, config) +} + +func (r StackHCIMarketplaceGalleryImageResource) update(data acceptance.TestData) string { + template := r.template(data) + return fmt.Sprintf(` +%s + +provider "azurerm" { + features {} +} + +resource "azurerm_stack_hci_marketplace_gallery_image" "test" { + name = "acctest-ln-${var.random_string}" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + custom_location_id = %q + + tags = { + foo = "bar" + } +} +`, template, os.Getenv(customLocationIdEnv)) +} + +func (r StackHCIMarketplaceGalleryImageResource) complete(data acceptance.TestData) string { + template := r.template(data) + return fmt.Sprintf(` +%s + +provider "azurerm" { + features {} +} + +resource "azurerm_stack_hci_marketplace_gallery_image" "test" { + name = "acctest-ln-${var.random_string}" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + custom_location_id = %q + + tags = { + foo = "bar" + env = "test" + } +} +`, template, os.Getenv(customLocationIdEnv)) +} + +func (r StackHCIMarketplaceGalleryImageResource) template(data acceptance.TestData) string { + return fmt.Sprintf(` +variable "primary_location" { + default = %q +} + +variable "random_string" { + default = %q +} + +resource "azurerm_resource_group" "test" { + name = "acctest-hci-mgi-${var.random_string}" + location = var.primary_location +} + +data "azurerm_client_config" "test" {} + +resource "azurerm_role_assignment" "test" { + scope = azurerm_resource_group.test.id + role_definition_name = "Azure Connected Machine Resource Manager" + principal_id = data.azurerm_client_config.test.object_id +} +`, data.Locations.Primary, data.RandomString) +} From fc463b00384a1756edd99304f971b33c3d344ac7 Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Tue, 27 Aug 2024 07:55:09 +0000 Subject: [PATCH 2/6] fix tests --- ..._hci_marketplace_gallery_image_resource.go | 22 +++--- ...marketplace_gallery_image_resource_test.go | 68 ++++++++++++------- 2 files changed, 55 insertions(+), 35 deletions(-) diff --git a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go index 0237d4b7b251..1b36bb9f64dd 100644 --- a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go +++ b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go @@ -81,6 +81,16 @@ func (StackHCIMarketplaceGalleryImageResource) Arguments() map[string]*pluginsdk ValidateFunc: customlocations.ValidateCustomLocationID, }, + "hyperv_generation": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + string(marketplacegalleryimages.HyperVGenerationVOne), + string(marketplacegalleryimages.HyperVGenerationVTwo), + }, false), + }, + "identifier": { Type: pluginsdk.TypeList, Required: true, @@ -138,16 +148,6 @@ func (StackHCIMarketplaceGalleryImageResource) Arguments() map[string]*pluginsdk }, false), }, - "hyperv_generation": { - Type: pluginsdk.TypeString, - Optional: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - string(marketplacegalleryimages.HyperVGenerationVOne), - string(marketplacegalleryimages.HyperVGenerationVTwo), - }, false), - }, - "storage_path_id": { Type: pluginsdk.TypeString, Optional: true, @@ -165,7 +165,7 @@ func (StackHCIMarketplaceGalleryImageResource) Attributes() map[string]*pluginsd func (r StackHCIMarketplaceGalleryImageResource) Create() sdk.ResourceFunc { return sdk.ResourceFunc{ - Timeout: 30 * time.Minute, + Timeout: 2 * time.Hour, Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { client := metadata.Client.AzureStackHCI.MarketplaceGalleryImages diff --git a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go index d64c7d16f1af..0a0e209e3116 100644 --- a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go +++ b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go @@ -64,7 +64,7 @@ func TestAccStackHCIMarketplaceGalleryImage_update(t *testing.T) { data.ResourceTest(t, r, []acceptance.TestStep{ { - Config: r.basic(data), + Config: r.update(data), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), ), @@ -78,7 +78,7 @@ func TestAccStackHCIMarketplaceGalleryImage_update(t *testing.T) { }, data.ImportStep(), { - Config: r.basic(data), + Config: r.update(data), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), ), @@ -135,8 +135,9 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "test" { resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location custom_location_id = %q - os_type = "Windows" - version = "20348.2113.231109" + hyperv_generation = "V2" + os_type = "Windows" + version = "20348.2655.240810" identifier { publisher = "MicrosoftWindowsServer" offer = "WindowsServer" @@ -159,6 +160,14 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "import" { resource_group_name = azurerm_stack_hci_marketplace_gallery_image.test.resource_group_name location = azurerm_stack_hci_marketplace_gallery_image.test.location custom_location_id = azurerm_stack_hci_marketplace_gallery_image.test.custom_location_id + hyperv_generation = azurerm_stack_hci_marketplace_gallery_image.test.hyperv_generation + os_type = azurerm_stack_hci_marketplace_gallery_image.test.os_type + version = azurerm_stack_hci_marketplace_gallery_image.test.version + identifier { + publisher = azurerm_stack_hci_marketplace_gallery_image.test.identifier.0.publisher + offer = azurerm_stack_hci_marketplace_gallery_image.test.identifier.0.offer + sku = azurerm_stack_hci_marketplace_gallery_image.test.identifier.0.sku + } } `, config) } @@ -166,7 +175,7 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "import" { func (r StackHCIMarketplaceGalleryImageResource) update(data acceptance.TestData) string { template := r.template(data) return fmt.Sprintf(` -%s +%[1]s provider "azurerm" { features {} @@ -177,58 +186,69 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "test" { resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location custom_location_id = %q - + hyperv_generation = "V2" + os_type = "Windows" + version = "20348.2655.240810" + identifier { + publisher = "MicrosoftWindowsServer" + offer = "WindowsServer" + sku = "2022-datacenter-azure-edition" + } tags = { foo = "bar" } } -`, template, os.Getenv(customLocationIdEnv)) +`, template, data.RandomString, os.Getenv(customLocationIdEnv)) } func (r StackHCIMarketplaceGalleryImageResource) complete(data acceptance.TestData) string { template := r.template(data) return fmt.Sprintf(` -%s +%[1]s provider "azurerm" { features {} } resource "azurerm_stack_hci_marketplace_gallery_image" "test" { - name = "acctest-ln-${var.random_string}" + name = "acctest-mgi-%[2]s" resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location - custom_location_id = %q - + custom_location_id = %[3]q + hyperv_generation = "V2" + os_type = "Windows" + version = "20348.2655.240810" + identifier { + publisher = "MicrosoftWindowsServer" + offer = "WindowsServer" + sku = "2022-datacenter-azure-edition" + } tags = { foo = "bar" env = "test" } } -`, template, os.Getenv(customLocationIdEnv)) +`, template, data.RandomString, os.Getenv(customLocationIdEnv)) } func (r StackHCIMarketplaceGalleryImageResource) template(data acceptance.TestData) string { return fmt.Sprintf(` -variable "primary_location" { - default = %q -} - -variable "random_string" { - default = %q -} - resource "azurerm_resource_group" "test" { - name = "acctest-hci-mgi-${var.random_string}" - location = var.primary_location + name = "acctest-hci-mgi-%s" + location = "%s" } data "azurerm_client_config" "test" {} +// service principal of 'Microsoft.AzureStackHCI Resource Provider' +data "azuread_service_principal" "hciRp" { + client_id = "1412d89f-b8a8-4111-b4fd-e82905cbd85d" +} + resource "azurerm_role_assignment" "test" { scope = azurerm_resource_group.test.id role_definition_name = "Azure Connected Machine Resource Manager" - principal_id = data.azurerm_client_config.test.object_id + principal_id = data.azuread_service_principal.hciRp.object_id } -`, data.Locations.Primary, data.RandomString) +`, data.RandomString, data.Locations.Primary) } From 77d184e5e4f5041c20ac07b69ee92cf9e11e7434 Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Mon, 23 Sep 2024 08:36:10 +0000 Subject: [PATCH 3/6] fix test --- ...marketplace_gallery_image_resource_test.go | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go index 0a0e209e3116..4b1403efe305 100644 --- a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go +++ b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go @@ -131,7 +131,7 @@ provider "azurerm" { } resource "azurerm_stack_hci_marketplace_gallery_image" "test" { - name = "acctest-mgi-${var.random_string}" + name = "acctest-mgi-%s" resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location custom_location_id = %q @@ -146,7 +146,7 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "test" { depends_on = [azurerm_role_assignment.test] } -`, template, os.Getenv(customLocationIdEnv)) +`, template, data.RandomString, os.Getenv(customLocationIdEnv)) } func (r StackHCIMarketplaceGalleryImageResource) requiresImport(data acceptance.TestData) string { @@ -181,14 +181,23 @@ provider "azurerm" { features {} } +resource "azurerm_stack_hci_storage_path" "test" { + name = "acctest-sp-%[2]s" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + custom_location_id = %[3]q + path = "C:\\ClusterStorage\\UserStorage_2\\sp-${var.random_string}" +} + resource "azurerm_stack_hci_marketplace_gallery_image" "test" { - name = "acctest-ln-${var.random_string}" + name = "acctest-mgi-%[2]s" resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location - custom_location_id = %q + custom_location_id = %[3]q hyperv_generation = "V2" os_type = "Windows" version = "20348.2655.240810" + storage_path_id = azurerm_stack_hci_storage_path.test.id identifier { publisher = "MicrosoftWindowsServer" offer = "WindowsServer" @@ -210,6 +219,14 @@ provider "azurerm" { features {} } +resource "azurerm_stack_hci_storage_path" "test" { + name = "acctest-sp-%[2]s" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + custom_location_id = %[3]q + path = "C:\\ClusterStorage\\UserStorage_2\\sp-${var.random_string}" +} + resource "azurerm_stack_hci_marketplace_gallery_image" "test" { name = "acctest-mgi-%[2]s" resource_group_name = azurerm_resource_group.test.name @@ -218,6 +235,7 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "test" { hyperv_generation = "V2" os_type = "Windows" version = "20348.2655.240810" + storage_path_id = azurerm_stack_hci_storage_path.test.id identifier { publisher = "MicrosoftWindowsServer" offer = "WindowsServer" From 21d66831a4f960db407d2ed209fc4b7d2b6ef172 Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Sun, 29 Sep 2024 09:15:51 +0000 Subject: [PATCH 4/6] fix test;add doc --- ..._hci_marketplace_gallery_image_resource.go | 45 +++---- ...marketplace_gallery_image_resource_test.go | 51 ++++---- ...ci_marketplace_gallery_image.html.markdown | 111 ++++++++++++++++++ 3 files changed, 159 insertions(+), 48 deletions(-) create mode 100644 website/docs/r/stack_hci_marketplace_gallery_image.html.markdown diff --git a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go index 1b36bb9f64dd..01d444657d82 100644 --- a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go +++ b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go @@ -39,17 +39,16 @@ func (StackHCIMarketplaceGalleryImageResource) ModelObject() interface{} { } type StackHCIMarketplaceGalleryImageResourceModel struct { - Name string `tfschema:"name"` - ResourceGroupName string `tfschema:"resource_group_name"` - Location string `tfschema:"location"` - CustomLocationId string `tfschema:"custom_location_id"` - CloudInitDataSource string `tfschema:"cloud_init_data_source"` - HypervGeneration string `tfschema:"hyperv_generation"` - Identifier []StackHCIMarketplaceGalleryImageIdentifier `tfschema:"identifier"` - OsType string `tfschema:"os_type"` - Version string `tfschema:"version"` - StoragePathId string `tfschema:"storage_path_id"` - Tags map[string]interface{} `tfschema:"tags"` + Name string `tfschema:"name"` + ResourceGroupName string `tfschema:"resource_group_name"` + Location string `tfschema:"location"` + CustomLocationId string `tfschema:"custom_location_id"` + HypervGeneration string `tfschema:"hyperv_generation"` + Identifier []StackHCIMarketplaceGalleryImageIdentifier `tfschema:"identifier"` + OsType string `tfschema:"os_type"` + Version string `tfschema:"version"` + StoragePathId string `tfschema:"storage_path_id"` + Tags map[string]interface{} `tfschema:"tags"` } type StackHCIMarketplaceGalleryImageIdentifier struct { @@ -138,16 +137,6 @@ func (StackHCIMarketplaceGalleryImageResource) Arguments() map[string]*pluginsdk ValidateFunc: validation.StringIsNotEmpty, }, - "cloud_init_data_source": { - Type: pluginsdk.TypeString, - Optional: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - string(marketplacegalleryimages.CloudInitDataSourceAzure), - string(marketplacegalleryimages.CloudInitDataSourceNoCloud), - }, false), - }, - "storage_path_id": { Type: pluginsdk.TypeString, Optional: true, @@ -165,7 +154,7 @@ func (StackHCIMarketplaceGalleryImageResource) Attributes() map[string]*pluginsd func (r StackHCIMarketplaceGalleryImageResource) Create() sdk.ResourceFunc { return sdk.ResourceFunc{ - Timeout: 2 * time.Hour, + Timeout: 3 * time.Hour, Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { client := metadata.Client.AzureStackHCI.MarketplaceGalleryImages @@ -199,10 +188,6 @@ func (r StackHCIMarketplaceGalleryImageResource) Create() sdk.ResourceFunc { }, } - if config.CloudInitDataSource != "" { - payload.Properties.CloudInitDataSource = pointer.To(marketplacegalleryimages.CloudInitDataSource(config.CloudInitDataSource)) - } - if config.StoragePathId != "" { payload.Properties.ContainerId = pointer.To(config.StoragePathId) } @@ -267,10 +252,14 @@ func (r StackHCIMarketplaceGalleryImageResource) Read() sdk.ResourceFunc { } if props := model.Properties; props != nil { - schema.CloudInitDataSource = string(pointer.From(props.CloudInitDataSource)) schema.StoragePathId = string(pointer.From(props.ContainerId)) schema.OsType = string(props.OsType) schema.HypervGeneration = string(pointer.From(props.HyperVGeneration)) + schema.Identifier = flattenStackHCIMarketplaceGalleryImageIdentifier(props.Identifier) + + if props.Version != nil { + schema.Version = pointer.From(props.Version.Name) + } } } @@ -310,7 +299,7 @@ func (r StackHCIMarketplaceGalleryImageResource) Update() sdk.ResourceFunc { func (r StackHCIMarketplaceGalleryImageResource) Delete() sdk.ResourceFunc { return sdk.ResourceFunc{ - Timeout: 30 * time.Minute, + Timeout: 1 * time.Hour, Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { client := metadata.Client.AzureStackHCI.MarketplaceGalleryImages diff --git a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go index 4b1403efe305..5b608cab0ad0 100644 --- a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go +++ b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go @@ -14,16 +14,21 @@ import ( "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" ) -type StackHCIMarketplaceGalleryImageResource struct{} +type StackHCIMarketplaceGalleryImageResource struct { + // az vm image list --all --output table --sku 2022-datacenter-azure-edition-core + imageVersion string +} func TestAccStackHCIMarketplaceGalleryImage_basic(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_stack_hci_marketplace_gallery_image", "test") - r := StackHCIMarketplaceGalleryImageResource{} - if os.Getenv(customLocationIdEnv) == "" { t.Skipf("skipping since %q has not been specified", customLocationIdEnv) } + data := acceptance.BuildTestData(t, "azurerm_stack_hci_marketplace_gallery_image", "test") + r := StackHCIMarketplaceGalleryImageResource{ + imageVersion: "20348.2402.240607", + } + data.ResourceTest(t, r, []acceptance.TestStep{ { Config: r.basic(data), @@ -41,7 +46,9 @@ func TestAccStackHCIMarketplaceGalleryImage_complete(t *testing.T) { } data := acceptance.BuildTestData(t, "azurerm_stack_hci_marketplace_gallery_image", "test") - r := StackHCIMarketplaceGalleryImageResource{} + r := StackHCIMarketplaceGalleryImageResource{ + imageVersion: "20348.2582.240703", + } data.ResourceTest(t, r, []acceptance.TestStep{ { @@ -55,13 +62,15 @@ func TestAccStackHCIMarketplaceGalleryImage_complete(t *testing.T) { } func TestAccStackHCIMarketplaceGalleryImage_update(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_stack_hci_marketplace_gallery_image", "test") - r := StackHCIMarketplaceGalleryImageResource{} - if os.Getenv(customLocationIdEnv) == "" { t.Skipf("skipping since %q has not been specified", customLocationIdEnv) } + data := acceptance.BuildTestData(t, "azurerm_stack_hci_marketplace_gallery_image", "test") + r := StackHCIMarketplaceGalleryImageResource{ + imageVersion: "20348.2655.240810", + } + data.ResourceTest(t, r, []acceptance.TestStep{ { Config: r.update(data), @@ -93,7 +102,9 @@ func TestAccStackHCIMarketplaceGalleryImage_requiresImport(t *testing.T) { } data := acceptance.BuildTestData(t, "azurerm_stack_hci_marketplace_gallery_image", "test") - r := StackHCIMarketplaceGalleryImageResource{} + r := StackHCIMarketplaceGalleryImageResource{ + imageVersion: "20348.2655.240905", + } data.ResourceTest(t, r, []acceptance.TestStep{ { @@ -137,16 +148,16 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "test" { custom_location_id = %q hyperv_generation = "V2" os_type = "Windows" - version = "20348.2655.240810" + version = "%s" identifier { publisher = "MicrosoftWindowsServer" offer = "WindowsServer" - sku = "2022-datacenter-azure-edition" + sku = "2022-datacenter-azure-edition-core" } depends_on = [azurerm_role_assignment.test] } -`, template, data.RandomString, os.Getenv(customLocationIdEnv)) +`, template, data.RandomString, os.Getenv(customLocationIdEnv), r.imageVersion) } func (r StackHCIMarketplaceGalleryImageResource) requiresImport(data acceptance.TestData) string { @@ -186,7 +197,7 @@ resource "azurerm_stack_hci_storage_path" "test" { resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location custom_location_id = %[3]q - path = "C:\\ClusterStorage\\UserStorage_2\\sp-${var.random_string}" + path = "C:\\ClusterStorage\\UserStorage_2\\sp-mgi-%[2]s" } resource "azurerm_stack_hci_marketplace_gallery_image" "test" { @@ -196,18 +207,18 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "test" { custom_location_id = %[3]q hyperv_generation = "V2" os_type = "Windows" - version = "20348.2655.240810" + version = "%s" storage_path_id = azurerm_stack_hci_storage_path.test.id identifier { publisher = "MicrosoftWindowsServer" offer = "WindowsServer" - sku = "2022-datacenter-azure-edition" + sku = "2022-datacenter-azure-edition-core" } tags = { foo = "bar" } } -`, template, data.RandomString, os.Getenv(customLocationIdEnv)) +`, template, data.RandomString, os.Getenv(customLocationIdEnv), r.imageVersion) } func (r StackHCIMarketplaceGalleryImageResource) complete(data acceptance.TestData) string { @@ -224,7 +235,7 @@ resource "azurerm_stack_hci_storage_path" "test" { resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location custom_location_id = %[3]q - path = "C:\\ClusterStorage\\UserStorage_2\\sp-${var.random_string}" + path = "C:\\ClusterStorage\\UserStorage_2\\sp-mgi-%[2]s" } resource "azurerm_stack_hci_marketplace_gallery_image" "test" { @@ -234,19 +245,19 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "test" { custom_location_id = %[3]q hyperv_generation = "V2" os_type = "Windows" - version = "20348.2655.240810" + version = "%s" storage_path_id = azurerm_stack_hci_storage_path.test.id identifier { publisher = "MicrosoftWindowsServer" offer = "WindowsServer" - sku = "2022-datacenter-azure-edition" + sku = "2022-datacenter-azure-edition-core" } tags = { foo = "bar" env = "test" } } -`, template, data.RandomString, os.Getenv(customLocationIdEnv)) +`, template, data.RandomString, os.Getenv(customLocationIdEnv), r.imageVersion) } func (r StackHCIMarketplaceGalleryImageResource) template(data acceptance.TestData) string { diff --git a/website/docs/r/stack_hci_marketplace_gallery_image.html.markdown b/website/docs/r/stack_hci_marketplace_gallery_image.html.markdown new file mode 100644 index 000000000000..fc89aa80d560 --- /dev/null +++ b/website/docs/r/stack_hci_marketplace_gallery_image.html.markdown @@ -0,0 +1,111 @@ +--- +subcategory: "Azure Stack HCI" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_stack_hci_marketplace_gallery_image" +description: |- + Manages a Azure Stack HCI Marketplace Gallery Image. +--- + +# azurerm_stack_hci_marketplace_gallery_image + +Manages a Azure Stack HCI Marketplace Gallery Image. + +## Example Usage + +```hcl +resource "azurerm_resource_group" "example" { + name = "examples" + location = "West Europe" +} + +data "azurerm_client_config" "example" {} + +// service principal of 'Microsoft.AzureStackHCI Resource Provider' +data "azuread_service_principal" "hciRp" { + client_id = "1412d89f-b8a8-4111-b4fd-e82905cbd85d" +} + +resource "azurerm_role_assignment" "example" { + scope = azurerm_resource_group.example.id + role_definition_name = "Azure Connected Machine Resource Manager" + principal_id = data.azuread_service_principal.hciRp.object_id +} + +resource "azurerm_stack_hci_marketplace_gallery_image" "example" { + name = "example-mgi" + resource_group_name = azurerm_resource_group.example.name + location = azurerm_resource_group.example.location + custom_location_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.ExtendedLocation/customLocations/cl1" + hyperv_generation = "V2" + os_type = "Windows" + version = "20348.2655.240905" + identifier { + publisher = "MicrosoftWindowsServer" + offer = "WindowsServer" + sku = "2022-datacenter-azure-edition-core" + } + tags = { + foo = "bar" + env = "example" + } +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `name` - (Required) The name which should be used for this Azure Stack HCI Marketplace Gallery Image. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created. + +* `resource_group_name` - (Required) The name of the Resource Group where the Azure Stack HCI Marketplace Gallery Image should exist. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created. + +* `location` - (Required) The Azure Region where the Azure Stack HCI Marketplace Gallery Image should exist. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created. + +* `custom_location_id` - (Required) The ID of Custom Location where the Azure Stack HCI Marketplace Gallery Image should exist. Changing this forces a new resource to be created. + +* `hyperv_generation` - (Required) The hypervisor generation of the Azure Stack HCI Marketplace Gallery Image. Possible values are `V1` and `V2`. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created. + +* `identifier` - (Required) One or more `identifier` blocks as defined below. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created. + +* `os_type` - (Required) The Operating System type of the Azure Stack HCI Marketplace Gallery Image. Possible values are `Windows` and `Linux`. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created. + +* `version` - (Required) The version of the Azure Stack HCI Marketplace Gallery Image. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created. + +--- + +* `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. + +* `tags` - (Optional) A mapping of tags which should be assigned to the Azure Stack HCI Marketplace Gallery Image. + +--- + +A `identifier` block supports the following: + +* `offer` - (Required) The offer of the Azure Stack HCI Marketplace Gallery Image. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created. + +* `publisher` - (Required) The publisher of the Azure Stack HCI Marketplace Gallery Image. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created. + +* `sku` - (Required) The sku of the Azure Stack HCI Marketplace Gallery Image. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the Azure Stack HCI Marketplace Gallery Image. + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions: + +* `create` - (Defaults to 3 hours) Used when creating the Azure Stack HCI Marketplace Gallery Image. +* `read` - (Defaults to 5 minutes) Used when retrieving the Azure Stack HCI Marketplace Gallery Image. +* `update` - (Defaults to 30 minutes) Used when updating the Azure Stack HCI Marketplace Gallery Image. +* `delete` - (Defaults to 1 hour) Used when deleting the Azure Stack HCI Marketplace Gallery Image. + +## Import + +Azure Stack HCI Marketplace Gallery Images can be imported using the `resource id`, e.g. + +```shell +terraform import azurerm_stack_hci_marketplace_gallery_image.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg1/providers/Microsoft.AzureStackHCI/marketplaceGalleryImages/image1 +``` From 1cce5166952506817e6b014c6b3a69809a361e6d Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Sun, 29 Sep 2024 09:37:06 +0000 Subject: [PATCH 5/6] fix golint --- .../stack_hci_marketplace_gallery_image_resource.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go index 01d444657d82..33cf6d6dbded 100644 --- a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go +++ b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go @@ -252,7 +252,7 @@ func (r StackHCIMarketplaceGalleryImageResource) Read() sdk.ResourceFunc { } if props := model.Properties; props != nil { - schema.StoragePathId = string(pointer.From(props.ContainerId)) + schema.StoragePathId = pointer.From(props.ContainerId) schema.OsType = string(props.OsType) schema.HypervGeneration = string(pointer.From(props.HyperVGeneration)) schema.Identifier = flattenStackHCIMarketplaceGalleryImageIdentifier(props.Identifier) From 1d34de33ff5679b2158bf7fbb709301f76798e18 Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Fri, 11 Oct 2024 07:22:19 +0000 Subject: [PATCH 6/6] fix schema, test, doc --- ..._hci_marketplace_gallery_image_resource.go | 46 ++++++------------ ...marketplace_gallery_image_resource_test.go | 48 +++++++++++++++++-- ...ci_marketplace_gallery_image.html.markdown | 10 ++-- 3 files changed, 65 insertions(+), 39 deletions(-) diff --git a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go index 33cf6d6dbded..cbd2226f5cfe 100644 --- a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go +++ b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource.go @@ -73,27 +73,20 @@ func (StackHCIMarketplaceGalleryImageResource) Arguments() map[string]*pluginsdk "location": commonschema.Location(), - "custom_location_id": { + "custom_location_id": commonschema.ResourceIDReferenceRequiredForceNew(&customlocations.CustomLocationId{}), + + "hyperv_generation": { Type: pluginsdk.TypeString, Required: true, ForceNew: true, - ValidateFunc: customlocations.ValidateCustomLocationID, - }, - - "hyperv_generation": { - Type: pluginsdk.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - string(marketplacegalleryimages.HyperVGenerationVOne), - string(marketplacegalleryimages.HyperVGenerationVTwo), - }, false), + ValidateFunc: validation.StringInSlice(marketplacegalleryimages.PossibleValuesForHyperVGeneration(), false), }, "identifier": { Type: pluginsdk.TypeList, Required: true, ForceNew: true, + MaxItems: 1, Elem: &pluginsdk.Resource{ Schema: map[string]*pluginsdk.Schema{ "publisher": { @@ -121,13 +114,10 @@ func (StackHCIMarketplaceGalleryImageResource) Arguments() map[string]*pluginsdk }, "os_type": { - Type: pluginsdk.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - string(marketplacegalleryimages.OperatingSystemTypesLinux), - string(marketplacegalleryimages.OperatingSystemTypesWindows), - }, false), + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice(marketplacegalleryimages.PossibleValuesForOperatingSystemTypes(), false), }, "version": { @@ -183,8 +173,12 @@ func (r StackHCIMarketplaceGalleryImageResource) Create() sdk.ResourceFunc { Type: pointer.To(marketplacegalleryimages.ExtendedLocationTypesCustomLocation), }, Properties: &marketplacegalleryimages.MarketplaceGalleryImageProperties{ - Identifier: expandStackHCIMarketplaceGalleryImageIdentifier(config.Identifier), - OsType: marketplacegalleryimages.OperatingSystemTypes(config.OsType), + Identifier: expandStackHCIMarketplaceGalleryImageIdentifier(config.Identifier), + OsType: marketplacegalleryimages.OperatingSystemTypes(config.OsType), + HyperVGeneration: pointer.To(marketplacegalleryimages.HyperVGeneration(config.HypervGeneration)), + Version: &marketplacegalleryimages.GalleryImageVersion{ + Name: pointer.To(config.Version), + }, }, } @@ -192,16 +186,6 @@ func (r StackHCIMarketplaceGalleryImageResource) Create() sdk.ResourceFunc { payload.Properties.ContainerId = pointer.To(config.StoragePathId) } - if config.HypervGeneration != "" { - payload.Properties.HyperVGeneration = pointer.To(marketplacegalleryimages.HyperVGeneration(config.HypervGeneration)) - } - - if config.Version != "" { - payload.Properties.Version = &marketplacegalleryimages.GalleryImageVersion{ - Name: pointer.To(config.Version), - } - } - if err := client.CreateOrUpdateThenPoll(ctx, id, payload); err != nil { return fmt.Errorf("performing create %s: %+v", id, err) } diff --git a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go index 5b608cab0ad0..0b42275f6f51 100644 --- a/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go +++ b/internal/services/azurestackhci/stack_hci_marketplace_gallery_image_resource_test.go @@ -73,7 +73,14 @@ func TestAccStackHCIMarketplaceGalleryImage_update(t *testing.T) { data.ResourceTest(t, r, []acceptance.TestStep{ { - Config: r.update(data), + Config: r.updateNoTag(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.updateTag(data), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), ), @@ -87,7 +94,7 @@ func TestAccStackHCIMarketplaceGalleryImage_update(t *testing.T) { }, data.ImportStep(), { - Config: r.update(data), + Config: r.updateNoTag(data), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), ), @@ -183,7 +190,42 @@ resource "azurerm_stack_hci_marketplace_gallery_image" "import" { `, config) } -func (r StackHCIMarketplaceGalleryImageResource) update(data acceptance.TestData) string { +func (r StackHCIMarketplaceGalleryImageResource) updateNoTag(data acceptance.TestData) string { + template := r.template(data) + return fmt.Sprintf(` +%[1]s + +provider "azurerm" { + features {} +} + +resource "azurerm_stack_hci_storage_path" "test" { + name = "acctest-sp-%[2]s" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + custom_location_id = %[3]q + path = "C:\\ClusterStorage\\UserStorage_2\\sp-mgi-%[2]s" +} + +resource "azurerm_stack_hci_marketplace_gallery_image" "test" { + name = "acctest-mgi-%[2]s" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + custom_location_id = %[3]q + hyperv_generation = "V2" + os_type = "Windows" + version = "%s" + storage_path_id = azurerm_stack_hci_storage_path.test.id + identifier { + publisher = "MicrosoftWindowsServer" + offer = "WindowsServer" + sku = "2022-datacenter-azure-edition-core" + } +} +`, template, data.RandomString, os.Getenv(customLocationIdEnv), r.imageVersion) +} + +func (r StackHCIMarketplaceGalleryImageResource) updateTag(data acceptance.TestData) string { template := r.template(data) return fmt.Sprintf(` %[1]s diff --git a/website/docs/r/stack_hci_marketplace_gallery_image.html.markdown b/website/docs/r/stack_hci_marketplace_gallery_image.html.markdown index fc89aa80d560..f620edf9279c 100644 --- a/website/docs/r/stack_hci_marketplace_gallery_image.html.markdown +++ b/website/docs/r/stack_hci_marketplace_gallery_image.html.markdown @@ -3,12 +3,12 @@ subcategory: "Azure Stack HCI" layout: "azurerm" page_title: "Azure Resource Manager: azurerm_stack_hci_marketplace_gallery_image" description: |- - Manages a Azure Stack HCI Marketplace Gallery Image. + Manages an Azure Stack HCI Marketplace Gallery Image. --- # azurerm_stack_hci_marketplace_gallery_image -Manages a Azure Stack HCI Marketplace Gallery Image. +Manages an Azure Stack HCI Marketplace Gallery Image. ## Example Usage @@ -61,11 +61,11 @@ The following arguments are supported: * `location` - (Required) The Azure Region where the Azure Stack HCI Marketplace Gallery Image should exist. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created. -* `custom_location_id` - (Required) The ID of Custom Location where the Azure Stack HCI Marketplace Gallery Image should exist. Changing this forces a new resource to be created. +* `custom_location_id` - (Required) The ID of the Custom Location where the Azure Stack HCI Marketplace Gallery Image should exist. Changing this forces a new resource to be created. * `hyperv_generation` - (Required) The hypervisor generation of the Azure Stack HCI Marketplace Gallery Image. Possible values are `V1` and `V2`. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created. -* `identifier` - (Required) One or more `identifier` blocks as defined below. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created. +* `identifier` - (Required) An `identifier` block as defined below. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created. * `os_type` - (Required) The Operating System type of the Azure Stack HCI Marketplace Gallery Image. Possible values are `Windows` and `Linux`. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created. @@ -79,7 +79,7 @@ The following arguments are supported: --- -A `identifier` block supports the following: +An `identifier` block supports the following: * `offer` - (Required) The offer of the Azure Stack HCI Marketplace Gallery Image. Changing this forces a new Azure Stack HCI Marketplace Gallery Image to be created.