// this function will generate master template for each API within this version set and an extra master template to link these apis public static async Task GenerateAPIVersionSetTemplates(ExtractorConfig exc, FileNameGenerator fileNameGenerator, FileNames fileNames, FileWriter fileWriter) { // get api dictionary and check api version set var apiDictionary = await GetAllAPIsDictionary(exc.sourceApimName, exc.resourceGroup, fileWriter); if (!apiDictionary.ContainsKey(exc.apiVersionSetName)) { throw new Exception("API Version Set with this name doesn't exist"); } else { Console.WriteLine("Start extracting the API version set {0}", exc.apiVersionSetName); foreach (string apiName in apiDictionary[exc.apiVersionSetName]) { // generate seperate folder for each API string apiFileFolder = String.Concat(@exc.fileFolder, $@"/{apiName}"); System.IO.Directory.CreateDirectory(apiFileFolder); await GenerateTemplates(new Extractor(exc, apiFileFolder), apiName, null, fileNameGenerator, fileNames, fileWriter, null); } // create master templates for this apiVersionSet string versionSetFolder = String.Concat(@exc.fileFolder, fileNames.versionSetMasterFolder); System.IO.Directory.CreateDirectory(versionSetFolder); await GenerateTemplates(new Extractor(exc, versionSetFolder), null, apiDictionary[exc.apiVersionSetName], fileNameGenerator, fileNames, fileWriter, null); Console.WriteLine($@"Finish extracting APIVersionSet {exc.apiVersionSetName}"); } }
// this function will generate templates for multiple specified APIs public static async Task GenerateMultipleAPIsTemplates(ExtractorConfig exc, FileNameGenerator fileNameGenerator, FileWriter fileWriter, FileNames fileNames) { if (exc.mutipleAPIs == null && exc.mutipleAPIs.Equals("")) { throw new Exception("mutipleAPIs parameter doesn't have any data"); } string[] apis = exc.mutipleAPIs.Split(','); for (int i = 0; i < apis.Length; i++) { apis[i] = apis[i].Trim(); } Console.WriteLine("Start extracting these {0} APIs", apis.Length); foreach (string apiName in apis) { // generate seperate folder for each API string apiFileFolder = String.Concat(@exc.fileFolder, $@"/{apiName}"); System.IO.Directory.CreateDirectory(apiFileFolder); await GenerateTemplates(new Extractor(exc, apiFileFolder), apiName, null, fileNameGenerator, fileNames, fileWriter, null); } // create master templates for these apis string groupApiFolder = String.Concat(@exc.fileFolder, fileNames.groupAPIsMasterFolder); System.IO.Directory.CreateDirectory(groupApiFolder); await GenerateTemplates(new Extractor(exc, groupApiFolder), null, apis.ToList(), fileNameGenerator, fileNames, fileWriter, null); Console.WriteLine($@"Finish extracting mutiple APIs"); }
private void UpdateExtractorConfigFromAdditionalArguments(ExtractorConfig extractorConfig) { var extractorConfigType = typeof(ExtractorConfig); foreach (var option in this.Options.Where(o => o.HasValue())) { extractorConfigType.GetProperty(option.LongName)?.SetValue(extractorConfig, option.Value()); } }
// validation check, some parameter can't be used at the same time, and check required parameters are passed in public static void validationCheck(ExtractorConfig exc) { if (exc.sourceApimName == null) { throw new Exception("Missing parameter <sourceApimName>."); } if (exc.destinationApimName == null) { throw new Exception("Missing parameter <destinationApimName>."); } if (exc.resourceGroup == null) { throw new Exception("Missing parameter <resourceGroup>."); } if (exc.fileFolder == null) { throw new Exception("Missing parameter <filefolder>."); } bool splitAPIs = exc.splitAPIs != null && exc.splitAPIs.Equals("true"); bool hasVersionSetName = exc.apiVersionSetName != null; bool hasSingleApi = exc.apiName != null; bool includeRevisions = exc.includeAllRevisions != null && exc.includeAllRevisions.Equals("true"); bool hasMultipleAPIs = exc.mutipleAPIs != null; if (splitAPIs && hasSingleApi) { throw new Exception("Can't use splitAPIs and apiName at same time"); } if (splitAPIs && hasVersionSetName) { throw new Exception("Can't use splitAPIs and apiVersionSetName at same time"); } if ((hasVersionSetName || hasSingleApi) && hasMultipleAPIs) { throw new Exception("Can't use mutipleAPIs with apiName or apiVersionSetName at the same time"); } if (hasSingleApi && hasVersionSetName) { throw new Exception("Can't use apiName and apiVersionSetName at same time"); } if (!hasSingleApi && includeRevisions) { throw new Exception("\"includeAllRevisions\" can be used when you specify the API you want to extract with \"apiName\""); } }
public Extractor(ExtractorConfig exc, string dirName) { this.sourceApimName = exc.sourceApimName; this.destinationApimName = exc.destinationApimName; this.resourceGroup = exc.resourceGroup; this.fileFolder = dirName; this.linkedTemplatesBaseUrl = exc.linkedTemplatesBaseUrl; this.linkedTemplatesSasToken = exc.linkedTemplatesSasToken; this.linkedTemplatesUrlQueryString = exc.linkedTemplatesUrlQueryString; this.policyXMLBaseUrl = exc.policyXMLBaseUrl; this.policyXMLSasToken = exc.policyXMLSasToken; this.apiVersionSetName = exc.apiVersionSetName; this.includeAllRevisions = exc.includeAllRevisions != null && exc.includeAllRevisions.Equals("true"); }
public Extractor(ExtractorConfig exc, string singleApiName, string dirName) { this.sourceApimName = exc.sourceApimName; this.destinationApimName = exc.destinationApimName; this.resourceGroup = exc.resourceGroup; this.fileFolder = dirName; this.apiName = singleApiName; this.linkedTemplatesBaseUrl = exc.linkedTemplatesBaseUrl; this.linkedTemplatesUrlQueryString = exc.linkedTemplatesUrlQueryString; this.policyXMLBaseUrl = exc.policyXMLBaseUrl; this.apiVersionSetName = exc.apiVersionSetName; this.includeAllRevisions = exc.includeAllRevisions; this.multipleAPINames = null; }
public Extractor(ExtractorConfig exc) { this.sourceApimName = exc.sourceApimName; this.destinationApimName = exc.destinationApimName; this.resourceGroup = exc.resourceGroup; this.fileFolder = exc.fileFolder; this.apiName = exc.apiName; this.linkedTemplatesBaseUrl = exc.linkedTemplatesBaseUrl; this.linkedTemplatesUrlQueryString = exc.linkedTemplatesUrlQueryString; this.policyXMLBaseUrl = exc.policyXMLBaseUrl; this.apiVersionSetName = exc.apiVersionSetName; this.includeAllRevisions = exc.includeAllRevisions; if (exc.multipleAPINames != null) { this.multipleAPINames = new List <string>(exc.multipleAPINames); } }
// this function will generate split api templates / folders for each api in this sourceApim public async Task GenerateSplitAPITemplates(ExtractorConfig exc, FileNameGenerator fileNameGenerator, FileWriter fileWriter, FileNames fileNames) { // Generate folders based on all apiversionset var apiDictionary = await this.GetAllAPIsDictionary(exc.sourceApimName, exc.resourceGroup, fileWriter); // Generate templates based on each API/APIversionSet foreach (KeyValuePair <string, List <string> > versionSetEntry in apiDictionary) { string apiFileFolder = exc.fileFolder; // if it's APIVersionSet, generate the versionsetfolder for templates if (versionSetEntry.Value.Count > 1) { // this API has VersionSet string apiDisplayName = versionSetEntry.Key; // create apiVersionSet folder apiFileFolder = String.Concat(@apiFileFolder, $@"/{apiDisplayName}"); System.IO.Directory.CreateDirectory(apiFileFolder); // create master templates for each apiVersionSet string versionSetFolder = String.Concat(@apiFileFolder, fileNames.versionSetMasterFolder); System.IO.Directory.CreateDirectory(versionSetFolder); Extractor masterConfig = new Extractor(exc, versionSetEntry.Value, versionSetFolder); await this.GenerateTemplates(masterConfig, fileNameGenerator, fileNames, fileWriter); Console.WriteLine($@"Finish extracting APIVersionSet {versionSetEntry.Key}"); } // Generate templates for each api foreach (string apiName in versionSetEntry.Value) { // create folder for each API string tempFileFolder = String.Concat(@apiFileFolder, $@"/{apiName}"); System.IO.Directory.CreateDirectory(tempFileFolder); Extractor excConfig = new Extractor(exc, apiName, tempFileFolder); // generate templates for each API await this.GenerateTemplates(excConfig, fileNameGenerator, fileNames, fileWriter); Console.WriteLine($@"Finish extracting API {apiName}"); } } }
public Extractor(ExtractorConfig exc, string dirName) { this.sourceApimName = exc.sourceApimName; this.destinationApimName = exc.destinationApimName; this.resourceGroup = exc.resourceGroup; this.fileFolder = dirName; this.linkedTemplatesBaseUrl = exc.linkedTemplatesBaseUrl; this.linkedTemplatesSasToken = exc.linkedTemplatesSasToken; this.linkedTemplatesUrlQueryString = exc.linkedTemplatesUrlQueryString; this.policyXMLBaseUrl = exc.policyXMLBaseUrl; this.policyXMLSasToken = exc.policyXMLSasToken; this.apiVersionSetName = exc.apiVersionSetName; this.includeAllRevisions = exc.includeAllRevisions != null && exc.includeAllRevisions.Equals("true"); this.serviceUrlParameters = exc.serviceUrlParameters; this.paramServiceUrl = (exc.paramServiceUrl != null && exc.paramServiceUrl.Equals("true")) || exc.serviceUrlParameters != null; this.paramNamedValue = exc.paramNamedValue != null && exc.paramNamedValue.Equals("true"); this.paramApiLoggerId = exc.paramApiLoggerId != null && exc.paramApiLoggerId.Equals("true"); this.paramLogResourceId = exc.paramLogResourceId != null && exc.paramLogResourceId.Equals("true"); }
public static async Task GenerateSingleAPIWithRevisionsTemplates(ExtractorConfig exc, string apiName, FileNameGenerator fileNameGenerator, FileWriter fileWriter, FileNames fileNames) { Console.WriteLine("Extracting singleAPI {0} with revisions", apiName); APIExtractor apiExtractor = new APIExtractor(fileWriter); // Get all revisions for this api string revisions = await apiExtractor.GetAPIRevisionsAsync(exc.sourceApimName, exc.resourceGroup, apiName); JObject revs = JObject.Parse(revisions); string currentRevision = null; List <string> revList = new List <string>(); // Generate seperate folder for each API revision for (int i = 0; i < ((JContainer)revs["value"]).Count; i++) { string apiID = ((JValue)revs["value"][i]["apiId"]).Value.ToString(); string singleApiName = apiID.Split("/")[2]; if (((JValue)revs["value"][i]["isCurrent"]).Value.ToString().Equals("True")) { currentRevision = singleApiName; } string revFileFolder = String.Concat(@exc.fileFolder, $@"/{singleApiName}"); System.IO.Directory.CreateDirectory(revFileFolder); await GenerateTemplates(new Extractor(exc, revFileFolder), singleApiName, null, fileNameGenerator, fileNames, fileWriter, null); revList.Add(singleApiName); } if (currentRevision == null) { throw new Exception($"Revision {apiName} doesn't exist, something went wrong!"); } // generate revisions master folder string revMasterFolder = String.Concat(@exc.fileFolder, fileNames.revisionMasterFolder); System.IO.Directory.CreateDirectory(revMasterFolder); Extractor revExc = new Extractor(exc, revMasterFolder); Template apiRevisionTemplate = await apiExtractor.GenerateAPIRevisionTemplateAsync(currentRevision, revList, apiName, revExc); await GenerateTemplates(revExc, null, null, fileNameGenerator, fileNames, fileWriter, apiRevisionTemplate); }
public ExtractCommand() { this.Name = GlobalConstants.ExtractName; this.Description = GlobalConstants.ExtractDescription; var extractorConfigFilePathOption = this.Option("--extractorConfig <extractorConfig>", "Config file of the extractor", CommandOptionType.SingleValue); AddExtractorConfigPropertiesToCommandLineOptions(); this.HelpOption(); this.OnExecute(async() => { ExtractorConfig extractorConfig = new ExtractorConfig(); if (extractorConfigFilePathOption.HasValue()) { var fileReader = new FileReader(); extractorConfig = fileReader.ConvertConfigJsonToExtractorConfig(extractorConfigFilePathOption.Value()); } UpdateExtractorConfigFromAdditionalArguments(extractorConfig); try { extractorConfig.Validate(); string singleApiName = extractorConfig.apiName; bool splitAPIs = extractorConfig.splitAPIs != null && extractorConfig.splitAPIs.Equals("true"); bool hasVersionSetName = extractorConfig.apiVersionSetName != null; bool hasSingleApi = singleApiName != null; bool includeRevisions = extractorConfig.includeAllRevisions != null && extractorConfig.includeAllRevisions.Equals("true"); bool hasMultipleAPIs = extractorConfig.multipleAPIs != null; EntityExtractor.baseUrl = (extractorConfig.serviceBaseUrl == null) ? EntityExtractor.baseUrl : extractorConfig.serviceBaseUrl; // start running extractor Console.WriteLine("API Management Template"); Console.WriteLine("Connecting to {0} API Management Service on {1} Resource Group ...", extractorConfig.sourceApimName, extractorConfig.resourceGroup); // initialize file helper classes FileWriter fileWriter = new FileWriter(); FileNameGenerator fileNameGenerator = new FileNameGenerator(); FileNames fileNames = extractorConfig.baseFileName == null ? fileNameGenerator.GenerateFileNames(extractorConfig.sourceApimName) : fileNameGenerator.GenerateFileNames(extractorConfig.baseFileName); if (splitAPIs) { // create split api templates for all apis in the sourceApim await ExtractorUtils.GenerateSplitAPITemplates(extractorConfig, fileNameGenerator, fileWriter, fileNames); await ExtractorUtils.GenerateTemplates(new Extractor(extractorConfig), null, null, fileNameGenerator, fileNames, fileWriter, null); } else if (hasVersionSetName) { // create split api templates and aggregated api templates for this apiversionset await ExtractorUtils.GenerateAPIVersionSetTemplates(extractorConfig, fileNameGenerator, fileNames, fileWriter); } else if (hasMultipleAPIs) { // generate templates for multiple APIs await ExtractorUtils.GenerateMultipleAPIsTemplates(extractorConfig, fileNameGenerator, fileWriter, fileNames); } else if (hasSingleApi && includeRevisions) { // handle singel API include Revision extraction await ExtractorUtils.GenerateSingleAPIWithRevisionsTemplates(extractorConfig, singleApiName, fileNameGenerator, fileWriter, fileNames); } else { // create single api template or create aggregated api templates for all apis within the sourceApim if (hasSingleApi) { Console.WriteLine("Executing extraction for {0} API ...", singleApiName); } else { Console.WriteLine("Executing full extraction ..."); } await ExtractorUtils.GenerateTemplates(new Extractor(extractorConfig), singleApiName, null, fileNameGenerator, fileNames, fileWriter, null); } Console.WriteLine("Templates written to output location"); Console.WriteLine("Press any key to exit process:"); #if DEBUG Console.ReadKey(); #endif } catch (Exception ex) { Console.WriteLine("Error occured: " + ex.Message); throw; } }); }
public Extractor(ExtractorConfig exc) : this(exc, exc.fileFolder) { }
public ExtractCommand() { this.Name = GlobalConstants.ExtractName; this.Description = GlobalConstants.ExtractDescription; this.HelpOption(); this.OnExecute(async() => { // convert config file to extractorConfig class FileReader fileReader = new FileReader(); ExtractorConfig extractorConfig = fileReader.ConvertConfigJsonToExtractorConfig(); try { if (extractorConfig.sourceApimName == null) { throw new Exception("Missing parameter <sourceApimName>."); } if (extractorConfig.destinationApimName == null) { throw new Exception("Missing parameter <destinationApimName>."); } if (extractorConfig.resourceGroup == null) { throw new Exception("Missing parameter <resourceGroup>."); } if (extractorConfig.fileFolder == null) { throw new Exception("Missing parameter <filefolder>."); } bool splitAPIs = extractorConfig.splitAPIs != null && extractorConfig.splitAPIs.Equals("true"); string apiVersionSetName = extractorConfig.apiVersionSetName; string singleApiName = extractorConfig.apiName; // validaion check if (splitAPIs && singleApiName != null) { throw new Exception("Can't use --splitAPIs and --apiName at same time"); } if (splitAPIs && apiVersionSetName != null) { throw new Exception("Can't use --splitAPIs and --apiVersionSetName at same time"); } if (singleApiName != null && apiVersionSetName != null) { throw new Exception("Can't use --apiName and --apiVersionSetName at same time"); } Console.WriteLine("API Management Template"); Console.WriteLine(); Console.WriteLine("Connecting to {0} API Management Service on {1} Resource Group ...", extractorConfig.sourceApimName, extractorConfig.resourceGroup); if (singleApiName != null) { Console.WriteLine("Executing extraction for {0} API ...", singleApiName); } else { Console.WriteLine("Executing full extraction ..."); } // initialize file helper classes FileWriter fileWriter = new FileWriter(); FileNameGenerator fileNameGenerator = new FileNameGenerator(); FileNames fileNames = fileNameGenerator.GenerateFileNames(extractorConfig.sourceApimName); // create template folder with all apis and split api templates if (splitAPIs) { // create split api templates for all apis in the sourceApim await this.GenerateSplitAPITemplates(extractorConfig, fileNameGenerator, fileWriter, fileNames); } else if (apiVersionSetName != null) { // create split api templates and aggregated api templates for this apiversionset await this.GenerateAPIVersionSetTemplates(extractorConfig, fileNameGenerator, fileNames, fileWriter); } else { // create single api template or create aggregated api templates for all apis within the sourceApim await this.GenerateTemplates(new Extractor(extractorConfig), fileNameGenerator, fileNames, fileWriter); } Console.WriteLine("Templates written to output location"); Console.WriteLine("Press any key to exit process:"); #if DEBUG Console.ReadKey(); #endif } catch (Exception ex) { Console.WriteLine("Error occured: " + ex.Message); throw; } }); }