Skip to content

Commit cc86bc7

Browse files
Add ListBySubscription operations related emitter changes and some refactor (#51854)
1 parent 6ab5c81 commit cc86bc7

23 files changed

+2608
-1358
lines changed

eng/packages/http-client-csharp-mgmt/emitter/src/resource-detection.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ export async function updateClients(
9393
if (modelId && kind) {
9494
const entry = resourceModelToMetadataMap.get(modelId);
9595
entry?.methods.push({
96-
id: method.crossLanguageDefinitionId,
97-
kind
96+
methodId: method.crossLanguageDefinitionId,
97+
kind,
98+
operationPath: method.operation.path,
99+
operationScope: getOperationScope(method.operation.path)
98100
});
99101
if (entry && !entry.resourceType) {
100102
entry.resourceType = calculateResourceTypeFromPath(
@@ -115,8 +117,9 @@ export async function updateClients(
115117
}
116118
}
117119

118-
// after the resourceIdPattern has been populated, we can set the parentResourceId
120+
// after the resourceIdPattern has been populated, we can set the parentResourceId and the resource scope of each resource method
119121
for (const [modelId, metadata] of resourceModelToMetadataMap) {
122+
// get parent resource model id
120123
const parentResourceModelId = getParentResourceModelId(
121124
sdkContext,
122125
models.get(modelId)
@@ -126,6 +129,14 @@ export async function updateClients(
126129
parentResourceModelId
127130
)?.resourceIdPattern;
128131
}
132+
133+
// figure out the resourceScope of all resource methods
134+
for (const method of metadata.methods) {
135+
method.resourceScope = getResourceScopeOfMethod(
136+
method.operationPath,
137+
resourceModelToMetadataMap.values()
138+
);
139+
}
129140
}
130141

131142
// the last step, add the decorator to the resource model
@@ -291,6 +302,27 @@ function getResourceScope(model: InputModelType): ResourceScope {
291302
return ResourceScope.ResourceGroup; // all the templates work as if there is a resource group decorator when there is no such decorator
292303
}
293304

305+
function getResourceScopeOfMethod(
306+
path: string,
307+
resources: MapIterator<ResourceMetadata>
308+
): string | undefined {
309+
// loop all possible resource metadata and see if some of them match the operation path of this method as a prefix
310+
const candidates: string[] = [];
311+
for (const otherMetadata of resources) {
312+
if (
313+
otherMetadata.resourceIdPattern &&
314+
path.startsWith(otherMetadata.resourceIdPattern)
315+
) {
316+
candidates.push(otherMetadata.resourceIdPattern);
317+
}
318+
}
319+
// finds the longest resource path id in candidates as the resource scope
320+
if (candidates.length > 0) {
321+
return candidates.reduce((a, b) => (a.length > b.length ? a : b));
322+
}
323+
return undefined;
324+
}
325+
294326
// TODO -- this logic needs to be refined in the near future.
295327
function getOperationScope(path: string): ResourceScope {
296328
if (

eng/packages/http-client-csharp-mgmt/emitter/src/resource-metadata.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,28 @@ export function convertMethodMetadataToArguments(
7979
}
8080

8181
export interface ResourceMethod {
82-
id: string;
82+
/**
83+
* the crossLanguageDefinitionId of the corresponding input method
84+
*/
85+
methodId: string;
86+
/**
87+
* the kind of this resource method
88+
*/
8389
kind: ResourceOperationKind;
90+
/**
91+
* the path of this resource method
92+
*/
93+
operationPath: string;
94+
/**
95+
* the scope of this resource method, it could be tenant/resource group/subscription/management group
96+
*/
97+
operationScope: ResourceScope;
98+
/**
99+
* The maximum scope of this resource method.
100+
* The value of this could be a resource path pattern of an existing resource
101+
* or undefined
102+
*/
103+
resourceScope?: string;
84104
}
85105

86106
export enum ResourceOperationKind {
@@ -90,5 +110,4 @@ export enum ResourceOperationKind {
90110
Get = "Get",
91111
List = "List",
92112
Update = "Update"
93-
// ListBySubscription = "ListBySubscription",
94113
}

0 commit comments

Comments
 (0)