in packages/rulesets/src/native/utilities/arm-helper.ts [487:553]
public getCollectionApiInfo() {
const getOperationModels = this.resourcesWithGetOperations()
const allPathKeys = _.uniq(_.flattenDeep(this.resources.map((re) => re.operations.map((op) => op.apiPath))))
const getResourcePaths = (res: ResourceInfo[], name: string) =>
_.flattenDeep(res.filter((re) => re.modelName === name).map((re) => re.operations.map((op) => op.apiPath)))
const collectionApis: CollectionApiInfo[] = []
for (const re of getOperationModels) {
re.operations.forEach((op) => {
const path = op.apiPath
if (collectionApis.find((re) => re.specificGetPath[0] === path)) {
return
}
if (path.match(this.SpecificResourcePathRegEx)) {
const firstProviderIndex = path.lastIndexOf("/providers")
const lastIndex = path.lastIndexOf("/")
const possibleCollectionApiPath = path.substr(firstProviderIndex, lastIndex - firstProviderIndex)
/*
* case 1:"providers/Microsoft.Compute/virtualMachineScaleSets/{virtualMachineScaleSetName}/virtualMachines"
case 2: "providers/Microsoft.Compute/virtualMachineScaleSets/{vmScaleSetName}/virtualMachines":
case 1 and case 2 should be the same, as the difference of parameter name does not have impact
*/
const matchedPaths = allPathKeys.filter(
/**
* path may end with "/", so here we remove it
*/
(p) =>
p
.substr(p.lastIndexOf("/providers"))
.replace(/{[^/]+}/gi, "{}")
.replace(/\/$/gi, "") === possibleCollectionApiPath.replace(/{[^/]+}/gi, "{}"),
)
if (matchedPaths && matchedPaths.length >= 1 && this.getOperationIdFromPath(matchedPaths[0])) {
collectionApis.push({
specificGetPath: [path],
collectionGetPath: matchedPaths,
modelName: this.getResponseModelNameFromPath(matchedPaths[0])!,
childModelName: re.modelName,
})
}
}
})
}
/**
* if a resource definition does match a collection resource schema, we can back-stepping the corresponding operation to make sure
* we don't lost it
*/
const collectionResources = this.getCollectionResources()
for (const resource of collectionResources) {
if (getOperationModels.some((re) => re.modelName === resource[1])) {
const index = collectionApis.findIndex((e) => e.modelName === resource[1])
const collectionInfo = {
specificGetPath: getResourcePaths(getOperationModels, resource[0]) || [],
collectionGetPath: getResourcePaths(getOperationModels, resource[1]) || [],
modelName: resource[1],
childModelName: resource[0],
}
if (index === -1) {
collectionApis.push(collectionInfo)
}
}
}
return collectionApis
}