Skip to content

Commit

Permalink
New Resource: azurerm_dev_center_gallery (#23760)
Browse files Browse the repository at this point in the history
* New Resource: azurerm_dev_center_gallery

* rename shared gallery id
  • Loading branch information
jiaweitao001 authored Feb 6, 2024
1 parent f6d2f71 commit 7f840d7
Show file tree
Hide file tree
Showing 4 changed files with 472 additions and 0 deletions.
189 changes: 189 additions & 0 deletions internal/services/devcenter/dev_center_gallery_resource_gen.go
Original file line number Diff line number Diff line change
@@ -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
}
200 changes: 200 additions & 0 deletions internal/services/devcenter/dev_center_gallery_resource_gen_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions internal/services/devcenter/registration_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (autoRegistration) DataSources() []sdk.DataSource {

func (autoRegistration) Resources() []sdk.Resource {
return []sdk.Resource{
DevCenterGalleryResource{},
DevCenterProjectResource{},
DevCenterResource{},
}
Expand Down
Loading

0 comments on commit 7f840d7

Please sign in to comment.