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

Commit

Permalink
Allows TagExtractor to pull more than 100 tags. (#359)
Browse files Browse the repository at this point in the history
* Allows TagExtractor to pull more than 100 tags.

* Refactored changes as recommended by RupengLiu.
  • Loading branch information
KevinHale-Bentley authored Mar 4, 2020
1 parent 008aef1 commit 2e68fb4
Showing 1 changed file with 38 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extract
{
public class TagExtractor: EntityExtractor
public class TagExtractor : EntityExtractor
{
public async Task<string> GetTagsAsync(string ApiManagementName, string ResourceGroupName)
public async Task<string> GetTagsAsync(string ApiManagementName, string ResourceGroupName, int skipNumOfRecords )
{
(string azToken, string azSubId) = await auth.GetAccessToken();

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

return await CallApiManagementAsync(azToken, requestUrl);
}
Expand All @@ -28,7 +28,7 @@ public async Task<Template> GenerateTagsTemplateAsync(string apimname, string re

// isolate tag and api operation associations in the case of a single api extraction
var apiOperationTagResources = apiTemplateResources.Where(resource => resource.type == ResourceTypeConstants.APIOperationTag);

// isolate tag and api associations in the case of a single api extraction
var apiTagResources = apiTemplateResources.Where(resource => resource.type == ResourceTypeConstants.APITag);

Expand All @@ -41,34 +41,44 @@ public async Task<Template> GenerateTagsTemplateAsync(string apimname, string re
List<TemplateResource> templateResources = new List<TemplateResource>();

// pull all named values (Tags) for service
string Tags = await GetTagsAsync(apimname, resourceGroup);
JObject oTags = JObject.Parse(Tags);
JObject oTags = new JObject();
int skipNumOfTags = 0;

foreach (var extractedTag in oTags["value"])
do
{
string TagName = ((JValue)extractedTag["name"]).Value.ToString();

// convert returned named value to template resource class
TagTemplateResource TagTemplateResource = JsonConvert.DeserializeObject<TagTemplateResource>(extractedTag.ToString());
TagTemplateResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{TagName}')]";
TagTemplateResource.type = ResourceTypeConstants.Tag;
TagTemplateResource.apiVersion = GlobalConstants.APIVersion;
TagTemplateResource.scale = null;

// only extract the tag if this is a full extraction,
// or in the case of a single api, if it is found in tags associated with the api operations
// or if it is found in tags associated with the api
// or if it is found in tags associated with the products associated with the api
if (singleApiName == null
|| apiOperationTagResources.Any(t => t.name.Contains($"/{TagName}'"))
|| apiTagResources.Any(t => t.name.Contains($"/{TagName}'"))
|| (productAPIResources.Any(t => t.name.Contains($"/{singleApiName}"))
&& productTagResources.Any(t => t.name.Contains($"/{TagName}'"))))
string Tags = await GetTagsAsync(apimname, resourceGroup, skipNumOfTags);
oTags = JObject.Parse(Tags);

foreach (var extractedTag in oTags["value"])
{
Console.WriteLine("'{0}' Tag found", TagName);
templateResources.Add(TagTemplateResource);
string TagName = ((JValue)extractedTag["name"]).Value.ToString();

// convert returned named value to template resource class
TagTemplateResource TagTemplateResource = JsonConvert.DeserializeObject<TagTemplateResource>(extractedTag.ToString());
TagTemplateResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{TagName}')]";
TagTemplateResource.type = ResourceTypeConstants.Tag;
TagTemplateResource.apiVersion = GlobalConstants.APIVersion;
TagTemplateResource.scale = null;

// only extract the tag if this is a full extraction,
// or in the case of a single api, if it is found in tags associated with the api operations
// or if it is found in tags associated with the api
// or if it is found in tags associated with the products associated with the api
if (singleApiName == null
|| apiOperationTagResources.Any(t => t.name.Contains($"/{TagName}'"))
|| apiTagResources.Any(t => t.name.Contains($"/{TagName}'"))
|| (productAPIResources.Any(t => t.name.Contains($"/{singleApiName}"))
&& productTagResources.Any(t => t.name.Contains($"/{TagName}'"))))
{
Console.WriteLine("'{0}' Tag found", TagName);
templateResources.Add(TagTemplateResource);
}
}

skipNumOfTags += GlobalConstants.NumOfRecords;
}
while (oTags["nextLink"] != null);


armTemplate.resources = templateResources.ToArray();
return armTemplate;
Expand Down

0 comments on commit 2e68fb4

Please sign in to comment.