Skip to content

Commit

Permalink
Data Source: `azurerm_system_center_virtual_machine_manager_inventory…
Browse files Browse the repository at this point in the history
…_items` - normalize the resource ID (#25955)

* Data Source: azurerm_system_center_virtual_machine_manager_inventory_items - normalize the resource id

* use pointer and error

* update description

* remove duplicate message
  • Loading branch information
neil-yechenwei authored May 28, 2024
1 parent b560eed commit c18bce7
Showing 1 changed file with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,14 @@ func (l SystemCenterVirtualMachineManagerInventoryItemsDataSource) Read() sdk.Re
}

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

metadata.ResourceData.SetId(scvmmServerId.ID())
Expand All @@ -121,39 +124,61 @@ func (l SystemCenterVirtualMachineManagerInventoryItemsDataSource) Read() sdk.Re
}
}

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

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)
// Service API indicates that the static segment `inventoryItems` in the resource ID of the Inventory Item should start with lowercase. See more details from https://github.com/Azure/azure-rest-api-specs/blob/92c409d93f895a30d51603b2fda78a49b3a2cd60/specification/scvmm/resource-manager/Microsoft.ScVmm/stable/2023-10-07/scvmm.json#L1785
// But the static segment `InventoryItems` in the resource ID of the Inventory Item returned by the API starts with uppercase. So all instances of setting the inventory item ID must use ParseInventoryItemIDInsensitively() in Read() to normalize the resource ID
scvmmServerInventoryItemId, err := inventoryitems.ParseInventoryItemIDInsensitively(pointer.From(item.Id))
if err != nil {
return nil, err
}
inventoryItem.id = scvmmServerInventoryItemId.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)
scvmmServerInventoryItemId, err := inventoryitems.ParseInventoryItemIDInsensitively(pointer.From(item.Id))
if err != nil {
return nil, err
}
inventoryItem.id = scvmmServerInventoryItemId.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)
scvmmServerInventoryItemId, err := inventoryitems.ParseInventoryItemIDInsensitively(pointer.From(item.Id))
if err != nil {
return nil, err
}
inventoryItem.id = scvmmServerInventoryItemId.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)
scvmmServerInventoryItemId, err := inventoryitems.ParseInventoryItemIDInsensitively(pointer.From(item.Id))
if err != nil {
return nil, err
}
inventoryItem.id = scvmmServerInventoryItemId.ID()

inventoryItem.name = pointer.From(v.InventoryItemName)
inventoryItem.Uuid = pointer.From(v.Uuid)
results = append(results, inventoryItem)
}
}
}

return results
return &results, nil
}

0 comments on commit c18bce7

Please sign in to comment.