diff --git a/internal/services/devcenter/dev_center_gallery_resource_gen.go b/internal/services/devcenter/dev_center_gallery_resource_gen.go new file mode 100644 index 000000000000..ee4729380867 --- /dev/null +++ b/internal/services/devcenter/dev_center_gallery_resource_gen.go @@ -0,0 +1,189 @@ +package devcenter + +// NOTE: this file is generated - manual changes will be overwritten. +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. +import ( + "context" + "fmt" + "time" + + "github.com/hashicorp/go-azure-helpers/lang/response" + "github.com/hashicorp/go-azure-sdk/resource-manager/devcenter/2023-04-01/galleries" + "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" +) + +var _ sdk.Resource = DevCenterGalleryResource{} + +type DevCenterGalleryResource struct{} + +func (r DevCenterGalleryResource) ModelObject() interface{} { + return &DevCenterGalleryResourceSchema{} +} + +type DevCenterGalleryResourceSchema struct { + DevCenterId string `tfschema:"dev_center_id"` + GalleryResourceId string `tfschema:"shared_gallery_id"` + Name string `tfschema:"name"` +} + +func (r DevCenterGalleryResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { + return galleries.ValidateGalleryID +} +func (r DevCenterGalleryResource) ResourceType() string { + return "azurerm_dev_center_gallery" +} +func (r DevCenterGalleryResource) Arguments() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{ + "dev_center_id": { + ForceNew: true, + Required: true, + Type: pluginsdk.TypeString, + }, + "shared_gallery_id": { + ForceNew: true, + Required: true, + Type: pluginsdk.TypeString, + }, + "name": { + ForceNew: true, + Required: true, + Type: pluginsdk.TypeString, + }, + } +} +func (r DevCenterGalleryResource) Attributes() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{} +} +func (r DevCenterGalleryResource) Create() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 30 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + client := metadata.Client.DevCenter.V20230401.Galleries + + var config DevCenterGalleryResourceSchema + if err := metadata.Decode(&config); err != nil { + return fmt.Errorf("decoding: %+v", err) + } + + subscriptionId := metadata.Client.Account.SubscriptionId + + devCenterId, err := galleries.ParseDevCenterID(config.DevCenterId) + if err != nil { + return err + } + + id := galleries.NewGalleryID(subscriptionId, devCenterId.ResourceGroupName, devCenterId.DevCenterName, config.Name) + + existing, err := client.Get(ctx, id) + if err != nil { + if !response.WasNotFound(existing.HttpResponse) { + return fmt.Errorf("checking for the presence of an existing %s: %+v", id, err) + } + } + if !response.WasNotFound(existing.HttpResponse) { + return metadata.ResourceRequiresImport(r.ResourceType(), id) + } + + var payload galleries.Gallery + if err := r.mapDevCenterGalleryResourceSchemaToGallery(config, &payload); err != nil { + return fmt.Errorf("mapping schema model to sdk model: %+v", err) + } + + if err := client.CreateOrUpdateThenPoll(ctx, id, payload); err != nil { + return fmt.Errorf("creating %s: %+v", id, err) + } + + metadata.SetID(id) + return nil + }, + } +} +func (r DevCenterGalleryResource) Read() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 5 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + client := metadata.Client.DevCenter.V20230401.Galleries + schema := DevCenterGalleryResourceSchema{} + + id, err := galleries.ParseGalleryID(metadata.ResourceData.Id()) + if err != nil { + return err + } + + devCenterId := galleries.NewDevCenterID(id.SubscriptionId, id.ResourceGroupName, id.DevCenterName) + + 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) + } + + if model := resp.Model; model != nil { + schema.DevCenterId = devCenterId.ID() + schema.Name = id.GalleryName + if err := r.mapGalleryToDevCenterGalleryResourceSchema(*model, &schema); err != nil { + return fmt.Errorf("flattening model: %+v", err) + } + } + + return metadata.Encode(&schema) + }, + } +} +func (r DevCenterGalleryResource) Delete() sdk.ResourceFunc { + return sdk.ResourceFunc{ + Timeout: 30 * time.Minute, + Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { + client := metadata.Client.DevCenter.V20230401.Galleries + + id, err := galleries.ParseGalleryID(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 (r DevCenterGalleryResource) mapDevCenterGalleryResourceSchemaToGalleryProperties(input DevCenterGalleryResourceSchema, output *galleries.GalleryProperties) error { + output.GalleryResourceId = input.GalleryResourceId + return nil +} + +func (r DevCenterGalleryResource) mapGalleryPropertiesToDevCenterGalleryResourceSchema(input galleries.GalleryProperties, output *DevCenterGalleryResourceSchema) error { + output.GalleryResourceId = input.GalleryResourceId + return nil +} + +func (r DevCenterGalleryResource) mapDevCenterGalleryResourceSchemaToGallery(input DevCenterGalleryResourceSchema, output *galleries.Gallery) error { + + if output.Properties == nil { + output.Properties = &galleries.GalleryProperties{} + } + if err := r.mapDevCenterGalleryResourceSchemaToGalleryProperties(input, output.Properties); err != nil { + return fmt.Errorf("mapping Schema to SDK Field %q / Model %q: %+v", "GalleryProperties", "Properties", err) + } + + return nil +} + +func (r DevCenterGalleryResource) mapGalleryToDevCenterGalleryResourceSchema(input galleries.Gallery, output *DevCenterGalleryResourceSchema) error { + + if input.Properties == nil { + input.Properties = &galleries.GalleryProperties{} + } + if err := r.mapGalleryPropertiesToDevCenterGalleryResourceSchema(*input.Properties, output); err != nil { + return fmt.Errorf("mapping SDK Field %q / Model %q to Schema: %+v", "GalleryProperties", "Properties", err) + } + + return nil +} diff --git a/internal/services/devcenter/dev_center_gallery_resource_gen_test.go b/internal/services/devcenter/dev_center_gallery_resource_gen_test.go new file mode 100644 index 000000000000..4d74ea97a686 --- /dev/null +++ b/internal/services/devcenter/dev_center_gallery_resource_gen_test.go @@ -0,0 +1,200 @@ +package devcenter_test + +// NOTE: this file is generated - manual changes will be overwritten. +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See NOTICE.txt in the project root for license information. +import ( + "context" + "fmt" + "testing" + + "github.com/hashicorp/go-azure-sdk/resource-manager/devcenter/2023-04-01/galleries" + "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" + "github.com/hashicorp/terraform-provider-azurerm/utils" +) + +type DevCenterGalleryTestResource struct{} + +func TestAccDevCenterGallery_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_dev_center_gallery", "test") + r := DevCenterGalleryTestResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccDevCenterGallery_requiresImport(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_dev_center_gallery", "test") + r := DevCenterGalleryTestResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.RequiresImportErrorStep(r.requiresImport), + }) +} + +func TestAccDevCenterGallery_complete(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_dev_center_gallery", "test") + r := DevCenterGalleryTestResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.complete(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + +func TestAccDevCenterGallery_update(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_dev_center_gallery", "test") + r := DevCenterGalleryTestResource{} + + 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 (r DevCenterGalleryTestResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { + id, err := galleries.ParseGalleryID(state.ID) + if err != nil { + return nil, err + } + + resp, err := clients.DevCenter.V20230401.Galleries.Get(ctx, *id) + if err != nil { + return nil, fmt.Errorf("reading %s: %+v", *id, err) + } + + return utils.Bool(resp.Model != nil), nil +} +func (r DevCenterGalleryTestResource) basic(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +provider "azurerm" { + features {} +} + +resource "azurerm_dev_center_gallery" "test" { + dev_center_id = azurerm_dev_center.test.id + shared_gallery_id = azurerm_shared_image_gallery.test.id + name = "acctestdcg${var.random_string}" +} +`, r.template(data)) +} + +func (r DevCenterGalleryTestResource) requiresImport(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_dev_center_gallery" "import" { + dev_center_id = azurerm_dev_center_gallery.test.dev_center_id + shared_gallery_id = azurerm_dev_center_gallery.test.shared_gallery_id + name = azurerm_dev_center_gallery.test.name +} +`, r.basic(data)) +} + +func (r DevCenterGalleryTestResource) complete(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +provider "azurerm" { + features {} +} + +resource "azurerm_dev_center_gallery" "test" { + dev_center_id = azurerm_dev_center.test.id + shared_gallery_id = azurerm_shared_image_gallery.test.id + name = "acctestdcg${var.random_string}" +} +`, r.template(data)) +} + +func (r DevCenterGalleryTestResource) template(data acceptance.TestData) string { + return fmt.Sprintf(` +variable "primary_location" { + default = %q +} +variable "random_integer" { + default = %d +} +variable "random_string" { + default = %q +} + +resource "azurerm_dev_center" "test" { + name = "acctestdc-${var.random_string}" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + + identity { + type = "UserAssigned" + identity_ids = [azurerm_user_assigned_identity.test.id] + } +} + + +resource "azurerm_resource_group" "test" { + name = "acctestrg-${var.random_integer}" + location = var.primary_location +} + +resource "azurerm_user_assigned_identity" "test" { + name = "acctestuami-${var.random_string}" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name +} + + +resource "azurerm_shared_image_gallery" "test" { + name = "acctestsig${var.random_string}" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name +} + + +resource "azurerm_role_assignment" "test" { + scope = azurerm_shared_image_gallery.test.id + role_definition_name = "Owner" + principal_id = azurerm_user_assigned_identity.test.principal_id +} +`, data.Locations.Primary, data.RandomInteger, data.RandomString) +} diff --git a/internal/services/devcenter/registration_gen.go b/internal/services/devcenter/registration_gen.go index 3ccec54bb72f..bcb4d09113c7 100644 --- a/internal/services/devcenter/registration_gen.go +++ b/internal/services/devcenter/registration_gen.go @@ -19,6 +19,7 @@ func (autoRegistration) DataSources() []sdk.DataSource { func (autoRegistration) Resources() []sdk.Resource { return []sdk.Resource{ + DevCenterGalleryResource{}, DevCenterProjectResource{}, DevCenterResource{}, } diff --git a/website/docs/r/dev_center_gallery.html.markdown b/website/docs/r/dev_center_gallery.html.markdown new file mode 100644 index 000000000000..5cafaf153b1c --- /dev/null +++ b/website/docs/r/dev_center_gallery.html.markdown @@ -0,0 +1,82 @@ +--- +subcategory: "Dev Center" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_dev_center_gallery" +description: |- + Manages a Dev Center Gallery. +--- + + + +# azurerm_dev_center_gallery + +Manages a Dev Center Gallery. + +## Example Usage + +```hcl +resource "azurerm_dev_center" "example" { + name = "example" + resource_group_name = azurerm_resource_group.example.name + location = azurerm_resource_group.example.location + identity { + type = "example-value" + } +} +resource "azurerm_resource_group" "example" { + name = "example-resources" + location = "West Europe" +} +resource "azurerm_shared_image_gallery" "example" { + name = "example" + location = azurerm_resource_group.example.location + resource_group_name = azurerm_resource_group.example.name +} +resource "azurerm_dev_center_gallery" "example" { + dev_center_id = azurerm_dev_center.example.id + shared_gallery_id = azurerm_shared_image_gallery.example.id + name = "example" +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `dev_center_id` - (Required) Specifies the Dev Center Id within which this Dev Center Gallery should exist. Changing this forces a new Dev Center Gallery to be created. + +* `shared_gallery_id` - (Required) The resource ID of the backing Azure Compute Gallery. Changing this forces a new Dev Center Gallery to be created. + +* `name` - (Required) Specifies the name of this Dev Center Gallery. Changing this forces a new Dev Center Gallery to be created. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the Dev Center Gallery. + +--- + + + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: + +* `create` - (Defaults to 30 minutes) Used when creating this Dev Center Gallery. +* `delete` - (Defaults to 30 minutes) Used when deleting this Dev Center Gallery. +* `read` - (Defaults to 5 minutes) Used when retrieving this Dev Center Gallery. +* `update` - (Defaults to 30 minutes) Used when updating this Dev Center Gallery. + +## Import + +An existing Dev Center Gallery can be imported into Terraform using the `resource id`, e.g. + +```shell +terraform import azurerm_dev_center_gallery.example /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DevCenter/devCenters/{devCenterName}/galleries/{galleryName} +``` + +* Where `{subscriptionId}` is the ID of the Azure Subscription where the Dev Center Gallery exists. For example `12345678-1234-9876-4563-123456789012`. +* Where `{resourceGroupName}` is the name of Resource Group where this Dev Center Gallery exists. For example `example-resource-group`. +* Where `{devCenterName}` is the name of the Dev Center. For example `devCenterValue`. +* Where `{galleryName}` is the name of the Gallery. For example `galleryValue`.