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

Data Source: azurerm_system_center_virtual_machine_manager_inventory_items - normalize the resource ID #25955

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ func (l SystemCenterVirtualMachineManagerInventoryItemsDataSource) Read() sdk.Re
}

if model := resp.Model; model != nil {
inventoryItems := flattenInventoryItems(model, state.InventoryType)
inventoryItems, err := flattenInventoryItems(model, state.InventoryType)
if err != nil {
return err
}
if len(inventoryItems) == 0 {
return fmt.Errorf("no inventory items were found for %s", scvmmServerId)
}
Expand All @@ -121,39 +124,67 @@ func (l SystemCenterVirtualMachineManagerInventoryItemsDataSource) Read() sdk.Re
}
}

func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType string) []InventoryItem {
func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType string) ([]InventoryItem, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We intentionally return a pointer and an error, to ensure that we return nil when an error occurs (so that we have to handle the error) - can we update this to match everywhere else in the codebase:

Suggested change
func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType string) ([]InventoryItem, error) {
func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType string) (*[]InventoryItem, error) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

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 API starts with uppercase. So it has to use ParseInventoryItemIDInsensitively() in Read() to normalize the 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.

Can we update this description to explain that all instances of this will use the case insensitive parser, then we don't need to repeat this comment multiple times

Suggested change
// But the static segment `InventoryItems` in the resource ID of the Inventory Item returned by API starts with uppercase. So it has to use ParseInventoryItemIDInsensitively() in Read() to normalize the resource ID
// 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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

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)
// 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 API starts with uppercase. So it has to use ParseInventoryItemIDInsensitively() in Read() to normalize the 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
// 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 API starts with uppercase. So it has to use ParseInventoryItemIDInsensitively() in Read() to normalize the resource 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.

Updated

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)
// 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 API starts with uppercase. So it has to use ParseInventoryItemIDInsensitively() in Read() to normalize the 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
// 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 API starts with uppercase. So it has to use ParseInventoryItemIDInsensitively() in Read() to normalize the resource 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.

Updated

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)
// 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 API starts with uppercase. So it has to use ParseInventoryItemIDInsensitively() in Read() to normalize the 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
// 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 API starts with uppercase. So it has to use ParseInventoryItemIDInsensitively() in Read() to normalize the resource 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.

Updated

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
}
Loading