Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 6cd0961

Browse files
tijmenamsingtijmenamsing-capRupengLiu
authored
Allow BackendsExtractor to pull more than 100 backends (#446)
Co-authored-by: Tijmen Amsing <tijmen.amsing@capgemini.com> Co-authored-by: RupengLiu <rliu1211@terpmail.umd.edu>
1 parent 1d72b7c commit 6cd0961

File tree

1 file changed

+53
-44
lines changed

1 file changed

+53
-44
lines changed

src/APIM_ARMTemplate/apimtemplate/Extractor/EntityExtractors/BackendExtractor.cs

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extract
1010
{
1111
public class BackendExtractor : EntityExtractor
1212
{
13-
public async Task<string> GetBackendsAsync(string ApiManagementName, string ResourceGroupName)
13+
public async Task<string> GetBackendsAsync(string ApiManagementName, string ResourceGroupName, int skipNumOfRecords)
1414
{
1515
(string azToken, string azSubId) = await auth.GetAccessToken();
1616

17-
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/backends?api-version={4}",
18-
baseUrl, azSubId, ResourceGroupName, ApiManagementName, GlobalConstants.APIVersion);
17+
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/backends?$skip={4}&api-version={5}",
18+
baseUrl, azSubId, ResourceGroupName, ApiManagementName, skipNumOfRecords, GlobalConstants.APIVersion);
1919

2020
return await CallApiManagementAsync(azToken, requestUrl);
2121
}
@@ -43,46 +43,55 @@ public async Task<Template> GenerateBackendsARMTemplateAsync(string apimname, st
4343
var namedValueResources = propertyResources.Where(resource => (resource.type == ResourceTypeConstants.Property));
4444

4545
// pull all backends for service
46-
string backends = await GetBackendsAsync(apimname, resourceGroup);
47-
JObject oBackends = JObject.Parse(backends);
46+
JObject oBackends = new JObject();
47+
int skipNumberOfBackends = 0;
4848

49-
foreach (var item in oBackends["value"])
49+
do
5050
{
51-
string backendName = ((JValue)item["name"]).Value.ToString();
52-
string backend = await GetBackendDetailsAsync(apimname, resourceGroup, backendName);
53-
54-
// convert returned backend to template resource class
55-
BackendTemplateResource backendTemplateResource = JsonConvert.DeserializeObject<BackendTemplateResource>(backend);
56-
backendTemplateResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{backendName}')]";
57-
backendTemplateResource.apiVersion = GlobalConstants.APIVersion;
58-
59-
////only extract the backend if this is a full extraction, or in the case of a single api, if it is referenced by one of the policies
60-
//if (singleApiName == null)
61-
//{
62-
// // if the user is extracting all apis, extract all the backends
63-
// Console.WriteLine("'{0}' Backend found", backendName);
64-
// templateResources.Add(backendTemplateResource);
65-
//}
66-
//else
67-
//{
68-
// bool isReferencedInPolicy = false;
69-
// foreach (PolicyTemplateResource policyTemplateResource in policyResources)
70-
// {
71-
// // the backend is used in a policy if the xml contains a set-backend-service policy, which will reference the backend's url or id
72-
// string policyContent = policyTemplateResource.properties.policyContent;
73-
// isReferencedInPolicy = DoesPolicyReferenceBackend(policyContent, namedValueResources, backendName, backendTemplateResource);
74-
// }
75-
// if (isReferencedInPolicy == true)
76-
// {
77-
// // backend was used in policy, extract it
78-
// Console.WriteLine("'{0}' Backend found", backendName);
79-
// templateResources.Add(backendTemplateResource);
80-
// }
81-
//}
82-
83-
Console.WriteLine("'{0}' Backend found", backendName);
84-
templateResources.Add(backendTemplateResource);
51+
string backends = await GetBackendsAsync(apimname, resourceGroup, skipNumberOfBackends);
52+
oBackends = JObject.Parse(backends);
53+
54+
foreach (var item in oBackends["value"])
55+
{
56+
string backendName = ((JValue)item["name"]).Value.ToString();
57+
string backend = await GetBackendDetailsAsync(apimname, resourceGroup, backendName);
58+
59+
// convert returned backend to template resource class
60+
BackendTemplateResource backendTemplateResource = JsonConvert.DeserializeObject<BackendTemplateResource>(backend);
61+
backendTemplateResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{backendName}')]";
62+
backendTemplateResource.apiVersion = GlobalConstants.APIVersion;
63+
64+
////only extract the backend if this is a full extraction, or in the case of a single api, if it is referenced by one of the policies
65+
//if (singleApiName == null)
66+
//{
67+
// // if the user is extracting all apis, extract all the backends
68+
// Console.WriteLine("'{0}' Backend found", backendName);
69+
// templateResources.Add(backendTemplateResource);
70+
//}
71+
//else
72+
//{
73+
// bool isReferencedInPolicy = false;
74+
// foreach (PolicyTemplateResource policyTemplateResource in policyResources)
75+
// {
76+
// // the backend is used in a policy if the xml contains a set-backend-service policy, which will reference the backend's url or id
77+
// string policyContent = policyTemplateResource.properties.policyContent;
78+
// isReferencedInPolicy = DoesPolicyReferenceBackend(policyContent, namedValueResources, backendName, backendTemplateResource);
79+
// }
80+
// if (isReferencedInPolicy == true)
81+
// {
82+
// // backend was used in policy, extract it
83+
// Console.WriteLine("'{0}' Backend found", backendName);
84+
// templateResources.Add(backendTemplateResource);
85+
// }
86+
//}
87+
88+
Console.WriteLine("'{0}' Backend found", backendName);
89+
templateResources.Add(backendTemplateResource);
90+
}
91+
92+
skipNumberOfBackends += GlobalConstants.NumOfRecords;
8593
}
94+
while (oBackends["nextLink"] != null);
8695

8796
armTemplate.resources = templateResources.ToArray();
8897
return armTemplate;
@@ -92,14 +101,14 @@ public bool DoesPolicyReferenceBackend(string policyContent, IEnumerable<Templat
92101
{
93102
// a policy is referenced by a backend with the set-backend-service policy, which will reference use the backends name or url, or through referencing a named value that applies to the backend
94103
var namedValueResourcesUsedByBackend = namedValueResources.Where(resource => DoesBackendReferenceNamedValue(resource, backendTemplateResource));
95-
if ((backendName != null && policyContent.Contains(backendName)) ||
96-
(backendTemplateResource.properties.url != null && policyContent.Contains(backendTemplateResource.properties.url)) ||
104+
if ((backendName != null && policyContent.Contains(backendName)) ||
105+
(backendTemplateResource.properties.url != null && policyContent.Contains(backendTemplateResource.properties.url)) ||
97106
(backendTemplateResource.properties.title != null && policyContent.Contains(backendTemplateResource.properties.title)) ||
98107
(backendTemplateResource.properties.resourceId != null && policyContent.Contains(backendTemplateResource.properties.resourceId)))
99108
{
100109
return true;
101110
}
102-
foreach(PropertyTemplateResource namedValueResource in namedValueResourcesUsedByBackend)
111+
foreach (PropertyTemplateResource namedValueResource in namedValueResourcesUsedByBackend)
103112
{
104113
if (policyContent.Contains(namedValueResource.properties.displayName) || policyContent.Contains(namedValueResource.properties.value))
105114
{
@@ -113,7 +122,7 @@ public bool DoesPolicyReferenceBackend(string policyContent, IEnumerable<Templat
113122
public bool DoesBackendReferenceNamedValue(TemplateResource namedValueResource, BackendTemplateResource backendTemplateResource)
114123
{
115124
string namedValue = (namedValueResource as PropertyTemplateResource).properties.value;
116-
return (namedValue == backendTemplateResource.properties.url
125+
return (namedValue == backendTemplateResource.properties.url
117126
|| namedValue == backendTemplateResource.properties.description
118127
|| namedValue == backendTemplateResource.properties.title);
119128
}

0 commit comments

Comments
 (0)