Description
Library name and version
Azure.ResourceManager.Network 1.10.0
Describe the bug
Virtual Hubs - Get Outbound Routes and Get Inbound Routes don't work in Azure.ResourceManager.Network SDK because the ResourceUri property requires an Absolute Uri while the REST API itself requires a relative uri.
The given sample will throw an exception because Uri will not be able to infer a relative uri and if you manually make it a relative uri, the Json serialization of the VirtualHubOutboundRoutesContent / VirtualHubInboundRoutesContent will fail.
Fix Option 1
Issue root cause seems to be coming from the Json Serialization using AbsoluteUri instead of Uri ToString.
if (Optional.IsDefined(ResourceUri))
{
writer.WritePropertyName("resourceUri"u8);
writer.WriteStringValue(ResourceUri.AbsoluteUri);
}
Fix Option 2
An alternate solution to this would be to update the type from Uri to be of type Azure.Core.ResourceIdentifier or even just a string like is done in the other language examples.
Expected behavior
RelativeUri in VirtualHubInboundRoutesContent and VirtualHubOutboundRoutesContent should accept and send requests with a relative uri such as "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/expressRouteGateways/exrGw1/expressRouteConnections/exrConn1".
Actual behavior
Starting from example here: https://learn.microsoft.com/en-us/rest/api/virtualwan/virtual-hubs/get-inbound-routes?view=rest-virtualwan-2024-05-01&tabs=dotnet
Uri("/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/expressRouteGateways/exrGw1/expressRouteConnections/exrConn1")
Error: System.UriFormatException: 'Invalid URI: The format of the URI could not be determined.'
If we specify that this is a relative Uri, the object will successfully create but then will fail in Json Serialization for the REST call.
Uri("/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/expressRouteGateways/exrGw1/expressRouteConnections/exrConn1", UriKind.Relative)
Error: System.InvalidOperationException: This operation is not supported for a relative URI.
at System.Uri.get_AbsoluteUri()
at Azure.ResourceManager.Network.Models.VirtualHubInboundRoutesContent.System.ClientModel.Primitives.IJsonModel<Azure.ResourceManager.Network.Models.VirtualHubInboundRoutesContent>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options)
If we create the Uri using an absolute path, Json serialization works but the actual request will fail because the rest api is looking for a relative path.
Uri("https://management.azure.com/{{my actual connection uri}}")
Error: Azure.RequestFailedException: Resource https://management.azure.com/{{my actual connection uri}} not found
Status: 400 (Bad Request)
ErrorCode: ResourceNotFound
I have verified using Azure PowerShell that the REST API does return correctly if ResourceUri is a relative uri instead of the absolute.
Reproduction Steps
using Azure;
using Azure.ResourceManager;
using System;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager.Network.Models;
using Azure.ResourceManager.Network;
// Generated from example definition: specification/network/resource-manager/Microsoft.Network/stable/2024-05-01/examples/GetInboundRoutes.json
// this example is just showing the usage of "VirtualHubs_GetInboundRoutes" operation, for the dependent resources, they will have to be created separately.
// get your azure access token, for more details of how Azure SDK get your access token, please refer to https://learn.microsoft.com/en-us/dotnet/azure/sdk/authentication?tabs=command-line
TokenCredential cred = new DefaultAzureCredential();
// authenticate your client
ArmClient client = new ArmClient(cred);
// this example assumes you already have this VirtualHubResource created on azure
// for more information of creating VirtualHubResource, please refer to the document of VirtualHubResource
string subscriptionId = "subid";
string resourceGroupName = "rg1";
string virtualHubName = "virtualHub1";
ResourceIdentifier virtualHubResourceId = VirtualHubResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, virtualHubName);
VirtualHubResource virtualHub = client.GetVirtualHubResource(virtualHubResourceId);
// invoke the operation
VirtualHubInboundRoutesContent content = new VirtualHubInboundRoutesContent
{
ResourceUri = new Uri("/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/expressRouteGateways/exrGw1/expressRouteConnections/exrConn1"),
ConnectionType = "ExpressRouteConnection",
};
ArmOperation<EffectiveRouteMapRouteList> lro = await virtualHub.GetVirtualHubInboundRoutesAsync(WaitUntil.Completed, content);
EffectiveRouteMapRouteList result = lro.Value;
Console.WriteLine($"Succeeded: {result}");
Environment
.NET SDK (reflecting any global.json):
Version: 6.0.427
Commit: 93fddcf4f6
Runtime Environment:
OS Name: Windows
OS Version: 10.0.26100
OS Platform: Windows
RID: win10-x64
Visual Studio 2022 - 17.13.6