|
3 | 3 | using Flurl; |
4 | 4 | using Microsoft.Extensions.DependencyInjection; |
5 | 5 | using Microsoft.Extensions.Hosting; |
| 6 | +using OpenTelemetry.Resources; |
6 | 7 | using System; |
7 | 8 | using System.Collections.Concurrent; |
8 | 9 | using System.Collections.Generic; |
9 | 10 | using System.Linq; |
10 | 11 | using System.Net; |
11 | 12 | using System.Net.Http; |
| 13 | +using System.Runtime.InteropServices.JavaScript; |
12 | 14 | using System.Text.Json; |
13 | 15 | using System.Text.Json.Nodes; |
14 | 16 | using System.Threading; |
@@ -136,17 +138,44 @@ private static ListResourceDtosFromApim ResolveListResourceDtosFromApim(IService |
136 | 138 | { |
137 | 139 | case IPolicyResource policyResource: |
138 | 140 | return policyResource.ListDtosFromApim(parents, listNames, getDto, cancellationToken); |
| 141 | + case var _ when isWorkspaceResource(parents): |
| 142 | + return getWorkspaceResourceDtos(resource, parents, cancellationToken); |
139 | 143 | default: |
140 | | - var uri = resource.GetCollectionUri(parents, serviceUri); |
141 | | - |
142 | | - return pipeline.ListJsonObjects(uri, cancellationToken) |
143 | | - .Select(jsonObject => from name in jsonObject.GetStringProperty("name") |
144 | | - from resourceName in ResourceName.From(name) |
145 | | - from normalizedDto in resource.DeserializeToDtoJson(jsonObject) |
146 | | - select (resourceName, normalizedDto)) |
147 | | - .Select(result => result.IfErrorThrow()); |
| 144 | + return getNonWorkspaceResourceDtos(resource, parents, cancellationToken); |
148 | 145 | } |
149 | 146 | }; |
| 147 | + |
| 148 | + bool isWorkspaceResource(ParentChain parents) => |
| 149 | + parents.Any(parent => parent.Resource is WorkspaceResource); |
| 150 | + |
| 151 | + // The APIM REST API is buggy for some workspace-scoped resources. |
| 152 | + // If the resource is workspace-scoped, we fetch its DTO individually |
| 153 | + // rather than relying on the "list-all" response. |
| 154 | + IAsyncEnumerable<(ResourceName, JsonObject)> getWorkspaceResourceDtos(IResourceWithDto resource, ParentChain parents, CancellationToken cancellationToken) |
| 155 | + { |
| 156 | + var uri = resource.GetCollectionUri(parents, serviceUri); |
| 157 | + |
| 158 | + return pipeline.ListJsonObjects(uri, cancellationToken) |
| 159 | + .Select(jsonObject => from nameString in jsonObject.GetStringProperty("name") |
| 160 | + from name in ResourceName.From(nameString) |
| 161 | + select name) |
| 162 | + .Select(nameResult => nameResult.IfErrorThrow()) |
| 163 | + .Select(async (name, cancellationToken) => from dto in await resource.GetDtoFromApim(name, parents, serviceUri, pipeline, cancellationToken) |
| 164 | + select (name, dto)) |
| 165 | + .Select(result => result.IfErrorThrow()); |
| 166 | + } |
| 167 | + |
| 168 | + IAsyncEnumerable<(ResourceName, JsonObject)> getNonWorkspaceResourceDtos(IResourceWithDto resource, ParentChain parents, CancellationToken cancellationToken) |
| 169 | + { |
| 170 | + var uri = resource.GetCollectionUri(parents, serviceUri); |
| 171 | + |
| 172 | + return pipeline.ListJsonObjects(uri, cancellationToken) |
| 173 | + .Select(jsonObject => from nameString in jsonObject.GetStringProperty("name") |
| 174 | + from name in ResourceName.From(nameString) |
| 175 | + from dto in resource.DeserializeToDtoJson(jsonObject) |
| 176 | + select (name, dto)) |
| 177 | + .Select(result => result.IfErrorThrow()); |
| 178 | + } |
150 | 179 | } |
151 | 180 |
|
152 | 181 | /// <summary> |
|
0 commit comments