From 8b3d6b97167e0aa2c263a7c8a2fa451903af57be Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 14 May 2024 08:45:40 +0800 Subject: [PATCH 1/4] Data Source: azurerm_system_center_virtual_machine_manager_inventory_items - normalize the resource id --- ...ine_manager_inventory_items_data_source.go | 47 +++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go b/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go index 020fbe2ed866..ef1ced3f441b 100644 --- a/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go +++ b/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go @@ -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) } @@ -121,10 +124,10 @@ 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 { @@ -132,22 +135,50 @@ func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType 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 + 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 + 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 + 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 + 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) @@ -155,5 +186,5 @@ func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType } } - return results + return results, nil } From d159ee50ada3c5cc318b55431e54ffde7c77ffa8 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 14 May 2024 19:26:12 +0800 Subject: [PATCH 2/4] use pointer and error --- ...tual_machine_manager_inventory_items_data_source.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go b/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go index ef1ced3f441b..ebb11c88c515 100644 --- a/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go +++ b/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go @@ -111,10 +111,10 @@ func (l SystemCenterVirtualMachineManagerInventoryItemsDataSource) Read() sdk.Re if err != nil { return err } - if len(inventoryItems) == 0 { + 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()) @@ -124,10 +124,10 @@ func (l SystemCenterVirtualMachineManagerInventoryItemsDataSource) Read() sdk.Re } } -func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType string) ([]InventoryItem, error) { +func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType string) (*[]InventoryItem, error) { results := make([]InventoryItem, 0) if input == nil { - return results, nil + return pointer.To(results), nil } for _, item := range *input { @@ -186,5 +186,5 @@ func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType } } - return results, nil + return pointer.To(results), nil } From 48d6170c0173b9d04f148569cb9272ddb82811cc Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 28 May 2024 09:20:33 +0800 Subject: [PATCH 3/4] update description --- ...al_machine_manager_inventory_items_data_source.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go b/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go index ebb11c88c515..65b54b18ae04 100644 --- a/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go +++ b/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go @@ -127,7 +127,7 @@ func (l SystemCenterVirtualMachineManagerInventoryItemsDataSource) Read() sdk.Re func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType string) (*[]InventoryItem, error) { results := make([]InventoryItem, 0) if input == nil { - return pointer.To(results), nil + return &results, nil } for _, item := range *input { @@ -136,7 +136,7 @@ func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType if v, ok := props.(inventoryitems.CloudInventoryItem); ok && inventoryType == string(inventoryitems.InventoryTypeCloud) { // 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 + // 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 @@ -148,7 +148,7 @@ func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType results = append(results, inventoryItem) } else if v, ok := props.(inventoryitems.VirtualMachineInventoryItem); ok && inventoryType == string(inventoryitems.InventoryTypeVirtualMachine) { // 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 + // 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 @@ -160,7 +160,7 @@ func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType results = append(results, inventoryItem) } else if v, ok := props.(inventoryitems.VirtualMachineTemplateInventoryItem); ok && inventoryType == string(inventoryitems.InventoryTypeVirtualMachineTemplate) { // 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 + // 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 @@ -172,7 +172,7 @@ func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType results = append(results, inventoryItem) } else if v, ok := props.(inventoryitems.VirtualNetworkInventoryItem); ok && inventoryType == string(inventoryitems.InventoryTypeVirtualNetwork) { // 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 + // 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 @@ -186,5 +186,5 @@ func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType } } - return pointer.To(results), nil + return &results, nil } From c74ed7587bb18f05697601d271f7de55cd75aafb Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Tue, 28 May 2024 16:44:19 +0800 Subject: [PATCH 4/4] remove duplicate message --- ...r_virtual_machine_manager_inventory_items_data_source.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go b/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go index 65b54b18ae04..742133c1efdd 100644 --- a/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go +++ b/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_inventory_items_data_source.go @@ -147,8 +147,6 @@ func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType inventoryItem.Uuid = pointer.From(v.Uuid) results = append(results, inventoryItem) } else if v, ok := props.(inventoryitems.VirtualMachineInventoryItem); ok && inventoryType == string(inventoryitems.InventoryTypeVirtualMachine) { - // 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 @@ -159,8 +157,6 @@ func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType inventoryItem.Uuid = pointer.From(v.Uuid) results = append(results, inventoryItem) } else if v, ok := props.(inventoryitems.VirtualMachineTemplateInventoryItem); ok && inventoryType == string(inventoryitems.InventoryTypeVirtualMachineTemplate) { - // 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 @@ -171,8 +167,6 @@ func flattenInventoryItems(input *[]inventoryitems.InventoryItem, inventoryType inventoryItem.Uuid = pointer.From(v.Uuid) results = append(results, inventoryItem) } else if v, ok := props.(inventoryitems.VirtualNetworkInventoryItem); ok && inventoryType == string(inventoryitems.InventoryTypeVirtualNetwork) { - // 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