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

New Resource: azurerm_dev_center_gallery #23760

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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:"gallery_resource_id"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like we did for the generated resource let's rename this to shared_gallery_id

Suggested change
GalleryResourceId string `tfschema:"gallery_resource_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,
},
"gallery_resource_id": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"gallery_resource_id": {
"shared_gallery_id": {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed.

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
gallery_resource_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
gallery_resource_id = azurerm_dev_center_gallery.test.gallery_resource_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
gallery_resource_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