Skip to content

Commit

Permalink
New Data Source: `azurerm_system_center_virtual_machine_manager_inven…
Browse files Browse the repository at this point in the history
…tory_items` (#25110)

* New Resource: azurerm_system_center_virtual_machine_manager_server_inventory_item

* update code

* New Data Source: azurerm_system_center_virtual_machine_manager_inventory_items

* update code

* update code

* update code

* update code

* update code

* update code

* update code

* update code
  • Loading branch information
neil-yechenwei authored Mar 26, 2024
1 parent 23a3c28 commit a7f43da
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ func (r Registration) WebsiteCategories() []string {
}

func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{}
return []sdk.DataSource{
SystemCenterVirtualMachineManagerInventoryItemsDataSource{},
}
}

func (r Registration) Resources() []sdk.Resource {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package systemcentervirtualmachinemanager

import (
"context"
"fmt"
"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-sdk/resource-manager/systemcentervirtualmachinemanager/2023-10-07/inventoryitems"
"github.com/hashicorp/go-azure-sdk/resource-manager/systemcentervirtualmachinemanager/2023-10-07/vmmservers"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"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"
)

type SystemCenterVirtualMachineManagerInventoryItemsDataSource struct{}

var _ sdk.DataSource = SystemCenterVirtualMachineManagerInventoryItemsDataSource{}

type SystemCenterVirtualMachineManagerInventoryItemsDataSourceModel struct {
SystemCenterVirtualMachineManagerServerId string `tfschema:"system_center_virtual_machine_manager_server_id"`
InventoryItems []InventoryItem `tfschema:"inventory_items"`
InventoryType string `tfschema:"inventory_type"`
}

type InventoryItem struct {
id string `tfschema:"id"`
name string `tfschema:"name"`
Uuid string `tfschema:"uuid"`
}

func (l SystemCenterVirtualMachineManagerInventoryItemsDataSource) ResourceType() string {
return "azurerm_system_center_virtual_machine_manager_inventory_items"
}

func (l SystemCenterVirtualMachineManagerInventoryItemsDataSource) ModelObject() interface{} {
return &SystemCenterVirtualMachineManagerInventoryItemsDataSourceModel{}
}

func (l SystemCenterVirtualMachineManagerInventoryItemsDataSource) Arguments() map[string]*schema.Schema {
return map[string]*pluginsdk.Schema{
"inventory_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(inventoryitems.PossibleValuesForInventoryType(), false),
},

"system_center_virtual_machine_manager_server_id": commonschema.ResourceIDReferenceRequired(&vmmservers.VMmServerId{}),
}
}

func (l SystemCenterVirtualMachineManagerInventoryItemsDataSource) Attributes() map[string]*schema.Schema {
return map[string]*pluginsdk.Schema{
"inventory_items": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"id": {
Type: pluginsdk.TypeString,
Computed: true,
},

"name": {
Type: pluginsdk.TypeString,
Computed: true,
},

"uuid": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},
}
}

func (l SystemCenterVirtualMachineManagerInventoryItemsDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.SystemCenterVirtualMachineManager.InventoryItems

var state SystemCenterVirtualMachineManagerInventoryItemsDataSourceModel
if err := metadata.Decode(&state); err != nil {
return err
}

scvmmServerId, err := inventoryitems.ParseVMmServerID(state.SystemCenterVirtualMachineManagerServerId)
if err != nil {
return err
}

resp, err := client.ListByVMMServer(ctx, *scvmmServerId)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", scvmmServerId)
}
return fmt.Errorf("reading %s: %+v", scvmmServerId, err)
}

if model := resp.Model; model != nil {
inventoryItems := flattenInventoryItems(model, state.InventoryType)
if len(inventoryItems) == 0 {
return fmt.Errorf("no inventory items were found for %s", scvmmServerId)
}
state.InventoryItems = inventoryItems
}

metadata.ResourceData.SetId(scvmmServerId.ID())

return metadata.Encode(&state)
},
}
}

func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType string) []InventoryItem {
results := make([]InventoryItem, 0)
if input == nil {
return results
}

for _, item := range *input {
if props := item.Properties; props != nil {
inventoryItem := InventoryItem{}

if v, ok := props.(inventoryitems.CloudInventoryItem); ok && inventoryType == string(inventoryitems.InventoryTypeCloud) {
inventoryItem.id = pointer.From(item.Id)
inventoryItem.name = pointer.From(v.InventoryItemName)
inventoryItem.Uuid = pointer.From(v.Uuid)
results = append(results, inventoryItem)
} else if v, ok := props.(inventoryitems.VirtualMachineInventoryItem); ok && inventoryType == string(inventoryitems.InventoryTypeVirtualMachine) {
inventoryItem.id = pointer.From(item.Id)
inventoryItem.name = pointer.From(v.InventoryItemName)
inventoryItem.Uuid = pointer.From(v.Uuid)
results = append(results, inventoryItem)
} else if v, ok := props.(inventoryitems.VirtualMachineTemplateInventoryItem); ok && inventoryType == string(inventoryitems.InventoryTypeVirtualMachineTemplate) {
inventoryItem.id = pointer.From(item.Id)
inventoryItem.name = pointer.From(v.InventoryItemName)
inventoryItem.Uuid = pointer.From(v.Uuid)
results = append(results, inventoryItem)
} else if v, ok := props.(inventoryitems.VirtualNetworkInventoryItem); ok && inventoryType == string(inventoryitems.InventoryTypeVirtualNetwork) {
inventoryItem.id = pointer.From(item.Id)
inventoryItem.name = pointer.From(v.InventoryItemName)
inventoryItem.Uuid = pointer.From(v.Uuid)
results = append(results, inventoryItem)
}
}
}

return results
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package systemcentervirtualmachinemanager_test

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type SystemCenterVirtualMachineManagerInventoryItemsDataSource struct{}

func TestAccDataSourceSystemCenterVirtualMachineManagerInventoryItems_basic(t *testing.T) {
if os.Getenv("ARM_TEST_CUSTOM_LOCATION_ID") == "" || os.Getenv("ARM_TEST_FQDN") == "" || os.Getenv("ARM_TEST_USERNAME") == "" || os.Getenv("ARM_TEST_PASSWORD") == "" {
t.Skip("Skipping as one of `ARM_TEST_CUSTOM_LOCATION_ID`, `ARM_TEST_FQDN`, `ARM_TEST_USERNAME`, `ARM_TEST_PASSWORD` was not specified")
}

data := acceptance.BuildTestData(t, "data.azurerm_system_center_virtual_machine_manager_inventory_items", "test")
r := SystemCenterVirtualMachineManagerInventoryItemsDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("inventory_items.#").Exists(),
),
},
})
}

func (d SystemCenterVirtualMachineManagerInventoryItemsDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
// Service team confirmed that waiting to sync Inventory Items in System Center Virtual Machine Manager Server is expected behaviour since the backend operator creates CRD (Custom Resource Definitions) for all the existing resources from onPrem and create InventoryItem resources which takes some time depending upon the number of resources after PUT System Center Virtual Machine Manager Server operation
resource "time_sleep" "wait_1_minute" {
depends_on = [azurerm_system_center_virtual_machine_manager_server.test]
create_duration = "1m"
}
data "azurerm_system_center_virtual_machine_manager_inventory_items" "test" {
inventory_type = "Cloud"
system_center_virtual_machine_manager_server_id = azurerm_system_center_virtual_machine_manager_server.test.id
depends_on = [time_sleep.wait_1_minute]
}
`, SystemCenterVirtualMachineManagerServerResource{}.basic(data))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
subcategory: "System Center Virtual Machine Manager"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_system_center_virtual_machine_manager_inventory_items"
description: |-
Gets information about existing System Center Virtual Machine Manager Inventory Items.
---

# Data Source: azurerm_system_center_virtual_machine_manager_inventory_items

Use this data source to access information about existing System Center Virtual Machine Manager Inventory Items.

## Example Usage

```hcl
data "azurerm_system_center_virtual_machine_manager_inventory_items" "example" {
inventory_type = "Cloud"
system_center_virtual_machine_manager_server_id = azurerm_system_center_virtual_machine_manager_server.example.id
}
```

## Argument Reference

* `inventory_type` - (Required) The inventory type of the System Center Virtual Machine Manager Inventory Item. Possible values are `Cloud`, `VirtualMachine`, `VirtualMachineTemplate` and `VirtualNetwork`.

* `system_center_virtual_machine_manager_server_id` - (Required) The ID of the System Center Virtual Machine Manager Server.

## Attributes Reference

* `id` - The ID of the System Center Virtual Machine Manager Inventory Items.

* `inventory_items` - One or more `inventory_items` blocks as defined below.

---

A `inventory_items` block exports the following:

* `id` - The ID of the System Center Virtual Machine Manager Inventory Item.

* `name` - The name of the System Center Virtual Machine Manager Inventory Item.

* `uuid` - The UUID of the System Center Virtual Machine Manager Inventory Item that is assigned by System Center Virtual Machine Manager.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when retrieving the System Center Virtual Machine Manager Inventory Items.

0 comments on commit a7f43da

Please sign in to comment.