public static APIVersionSetTemplateCreator GenerateAPIVersionSetTemplateCreator()
        {
            TemplateCreator templateCreator = new TemplateCreator();
            APIVersionSetTemplateCreator apiVersionSetTemplateCreator = new APIVersionSetTemplateCreator(templateCreator);

            return(apiVersionSetTemplateCreator);
        }
        public void ShouldCreateAPIVersionSetTemplateFromCreatorConfig()
        {
            // arrange
            APIVersionSetTemplateCreator apiVersionSetTemplateCreator = new APIVersionSetTemplateCreator(new TemplateBuilder());
            CreatorConfig creatorConfig = new CreatorConfig()
            {
                apiVersionSets = new List <APIVersionSetConfig>()
            };
            APIVersionSetConfig apiVersionSet = new APIVersionSetConfig()
            {
                id                = "id",
                Description       = "description",
                DisplayName       = "displayName",
                VersionHeaderName = "versionHeaderName",
                VersioningScheme  = "versioningScheme",
                VersionQueryName  = "versionQueryName"
            };

            creatorConfig.apiVersionSets.Add(apiVersionSet);

            // act
            Template versionSetTemplate            = apiVersionSetTemplateCreator.CreateAPIVersionSetTemplate(creatorConfig);
            var      apiVersionSetTemplateResource = (ApiVersionSetTemplateResource)versionSetTemplate.Resources[0];

            // assert
            Assert.Equal(apiVersionSet.Description, apiVersionSetTemplateResource.Properties.Description);
            Assert.Equal(apiVersionSet.DisplayName, apiVersionSetTemplateResource.Properties.DisplayName);
            Assert.Equal(apiVersionSet.VersionHeaderName, apiVersionSetTemplateResource.Properties.VersionHeaderName);
            Assert.Equal(apiVersionSet.VersioningScheme, apiVersionSetTemplateResource.Properties.VersioningScheme);
            Assert.Equal(apiVersionSet.VersionQueryName, apiVersionSetTemplateResource.Properties.VersionQueryName);
        }
        public void ShouldCreateAPIVersionSetTemplateFromCreatorConfig()
        {
            // arrange
            APIVersionSetTemplateCreator apiVersionSetTemplateCreator = APIVersionSetTemplateCreatorFactory.GenerateAPIVersionSetTemplateCreator();
            CreatorConfig creatorConfig = new CreatorConfig()
            {
                apiVersionSet = new APIVersionSetConfig()
                {
                    id                = "id",
                    description       = "description",
                    displayName       = "displayName",
                    versionHeaderName = "versionHeaderName",
                    versioningScheme  = "versioningScheme",
                    versionQueryName  = "versionQueryName"
                }
            };

            // act
            Template versionSetTemplate = apiVersionSetTemplateCreator.CreateAPIVersionSetTemplate(creatorConfig);
            APIVersionSetTemplateResource apiVersionSetTemplateResource = (APIVersionSetTemplateResource)versionSetTemplate.resources[0];

            // assert
            Assert.Equal(creatorConfig.apiVersionSet.description, apiVersionSetTemplateResource.properties.description);
            Assert.Equal(creatorConfig.apiVersionSet.displayName, apiVersionSetTemplateResource.properties.displayName);
            Assert.Equal(creatorConfig.apiVersionSet.versionHeaderName, apiVersionSetTemplateResource.properties.versionHeaderName);
            Assert.Equal(creatorConfig.apiVersionSet.versioningScheme, apiVersionSetTemplateResource.properties.versioningScheme);
            Assert.Equal(creatorConfig.apiVersionSet.versionQueryName, apiVersionSetTemplateResource.properties.versionQueryName);
        }
        public void ShouldUseDefaultResourceNameWithoutProvidedId()
        {
            // arrange
            APIVersionSetTemplateCreator apiVersionSetTemplateCreator = APIVersionSetTemplateCreatorFactory.GenerateAPIVersionSetTemplateCreator();
            CreatorConfig creatorConfig = new CreatorConfig()
            {
                apiVersionSet = new APIVersionSetConfig()
            };

            // act
            Template versionSetTemplate = apiVersionSetTemplateCreator.CreateAPIVersionSetTemplate(creatorConfig);
            APIVersionSetTemplateResource apiVersionSetTemplateResource = (APIVersionSetTemplateResource)versionSetTemplate.resources[0];

            // assert
            Assert.Equal("[concat(parameters('ApimServiceName'), '/versionset')]", apiVersionSetTemplateResource.name);
        }
        public void ShouldUseDefaultResourceNameWithoutProvidedId()
        {
            // arrange
            APIVersionSetTemplateCreator apiVersionSetTemplateCreator = new APIVersionSetTemplateCreator(new TemplateBuilder());
            CreatorConfig creatorConfig = new CreatorConfig()
            {
                apiVersionSets = new List <APIVersionSetConfig>()
            };
            APIVersionSetConfig apiVersionSet = new APIVersionSetConfig();

            creatorConfig.apiVersionSets.Add(apiVersionSet);

            // act
            Template versionSetTemplate            = apiVersionSetTemplateCreator.CreateAPIVersionSetTemplate(creatorConfig);
            var      apiVersionSetTemplateResource = (ApiVersionSetTemplateResource)versionSetTemplate.Resources[0];

            // assert
            Assert.Equal($"[concat(parameters('{ParameterNames.ApimServiceName}'), '/versionset')]", apiVersionSetTemplateResource.Name);
        }
        public void ShouldUseProvidedIdInResourceName()
        {
            // arrange
            APIVersionSetTemplateCreator apiVersionSetTemplateCreator = APIVersionSetTemplateCreatorFactory.GenerateAPIVersionSetTemplateCreator();
            CreatorConfig creatorConfig = new CreatorConfig()
            {
                apiVersionSet = new APIVersionSetConfig()
                {
                    id = "id"
                }
            };

            // act
            Template versionSetTemplate = apiVersionSetTemplateCreator.CreateAPIVersionSetTemplate(creatorConfig);
            APIVersionSetTemplateResource apiVersionSetTemplateResource = (APIVersionSetTemplateResource)versionSetTemplate.resources[0];

            // assert
            Assert.Equal($"[concat(parameters('ApimServiceName'), '/{creatorConfig.apiVersionSet.id}')]", apiVersionSetTemplateResource.name);
        }
Exemplo n.º 7
0
        public async Task ExecuteCommandAsync(CreatorConfig creatorConfig)
        {
            if (!Directory.Exists(creatorConfig.outputLocation))
            {
                Directory.CreateDirectory(creatorConfig.outputLocation);
            }

            FileReader fileReader = new FileReader();

            // initialize file helper classes
            FileNames fileNames = creatorConfig.baseFileName == null
                ? FileNameGenerator.GenerateFileNames(creatorConfig.apimServiceName)
                : FileNameGenerator.GenerateFileNames(creatorConfig.baseFileName);

            // initialize template creator classes
            APIVersionSetTemplateCreator       apiVersionSetTemplateCreator        = new APIVersionSetTemplateCreator(this.templateBuilder);
            LoggerTemplateCreator              loggerTemplateCreator               = new LoggerTemplateCreator(this.templateBuilder);
            BackendTemplateCreator             backendTemplateCreator              = new BackendTemplateCreator(this.templateBuilder);
            AuthorizationServerTemplateCreator authorizationServerTemplateCreator  = new AuthorizationServerTemplateCreator(this.templateBuilder);
            ProductAPITemplateCreator          productAPITemplateCreator           = new ProductAPITemplateCreator(this.templateBuilder);
            TagAPITemplateCreator              tagAPITemplateCreator               = new TagAPITemplateCreator();
            PolicyTemplateCreator              policyTemplateCreator               = new PolicyTemplateCreator(fileReader, this.templateBuilder);
            ProductGroupTemplateCreator        productGroupTemplateCreator         = new ProductGroupTemplateCreator();
            SubscriptionTemplateCreator        productSubscriptionsTemplateCreator = new SubscriptionTemplateCreator();
            DiagnosticTemplateCreator          diagnosticTemplateCreator           = new DiagnosticTemplateCreator();
            ReleaseTemplateCreator             releaseTemplateCreator              = new ReleaseTemplateCreator();
            ProductTemplateCreator             productTemplateCreator              = new ProductTemplateCreator(policyTemplateCreator, productGroupTemplateCreator, productSubscriptionsTemplateCreator, this.templateBuilder);
            PropertyTemplateCreator            propertyTemplateCreator             = new PropertyTemplateCreator(this.templateBuilder);
            TagTemplateCreator    tagTemplateCreator    = new TagTemplateCreator(this.templateBuilder);
            APITemplateCreator    apiTemplateCreator    = new APITemplateCreator(fileReader, policyTemplateCreator, productAPITemplateCreator, tagAPITemplateCreator, diagnosticTemplateCreator, releaseTemplateCreator, this.templateBuilder);
            MasterTemplateCreator masterTemplateCreator = new MasterTemplateCreator(this.templateBuilder);

            // create templates from provided configuration
            Console.WriteLine("Creating global service policy template");
            Console.WriteLine("------------------------------------------");
            Template globalServicePolicyTemplate = creatorConfig.policy != null?policyTemplateCreator.CreateGlobalServicePolicyTemplate(creatorConfig) : null;

            Console.WriteLine("Creating API version set template");
            Console.WriteLine("------------------------------------------");
            Template apiVersionSetsTemplate = creatorConfig.apiVersionSets != null?apiVersionSetTemplateCreator.CreateAPIVersionSetTemplate(creatorConfig) : null;

            Console.WriteLine("Creating product template");
            Console.WriteLine("------------------------------------------");
            Template productsTemplate = creatorConfig.products != null?productTemplateCreator.CreateProductTemplate(creatorConfig) : null;

            Console.WriteLine("Creating product/APIs template");
            Console.WriteLine("------------------------------------------");
            Template productAPIsTemplate = creatorConfig.products != null && creatorConfig.apis != null?productAPITemplateCreator.CreateProductAPITemplate(creatorConfig) : null;

            Console.WriteLine("Creating named values template");
            Console.WriteLine("------------------------------------------");
            Template propertyTemplate = creatorConfig.namedValues != null?propertyTemplateCreator.CreatePropertyTemplate(creatorConfig) : null;

            Console.WriteLine("Creating logger template");
            Console.WriteLine("------------------------------------------");
            Template loggersTemplate = creatorConfig.loggers != null?loggerTemplateCreator.CreateLoggerTemplate(creatorConfig) : null;

            Console.WriteLine("Creating backend template");
            Console.WriteLine("------------------------------------------");
            Template backendsTemplate = creatorConfig.backends != null?backendTemplateCreator.CreateBackendTemplate(creatorConfig) : null;

            Console.WriteLine("Creating authorization server template");
            Console.WriteLine("------------------------------------------");
            Template authorizationServersTemplate = creatorConfig.authorizationServers != null?authorizationServerTemplateCreator.CreateAuthorizationServerTemplate(creatorConfig) : null;

            // store name and whether the api will depend on the version set template each api necessary to build linked templates
            List <LinkedMasterTemplateAPIInformation> apiInformation = new List <LinkedMasterTemplateAPIInformation>();
            List <Template> apiTemplates = new List <Template>();

            Console.WriteLine("Creating API templates");
            Console.WriteLine("------------------------------------------");

            IDictionary <string, string[]> apiVersions = APITemplateCreator.GetApiVersionSets(creatorConfig);

            foreach (APIConfig api in creatorConfig.apis)
            {
                if (creatorConfig.ConsiderAllApiForDeployments || creatorConfig.PreferredApis.Contains(api.name))
                {
                    bool isServiceUrlParameterizeInYml = false;
                    if (creatorConfig.serviceUrlParameters != null && creatorConfig.serviceUrlParameters.Count > 0)
                    {
                        isServiceUrlParameterizeInYml = creatorConfig.serviceUrlParameters.Any(s => s.ApiName.Equals(api.name));
                        api.serviceUrl = isServiceUrlParameterizeInYml ?
                                         creatorConfig.serviceUrlParameters.Where(s => s.ApiName.Equals(api.name)).FirstOrDefault().ServiceUrl : api.serviceUrl;
                    }
                    // create api templates from provided api config - if the api config contains a supplied apiVersion, split the templates into 2 for metadata and swagger content, otherwise create a unified template
                    List <Template> apiTemplateSet = await apiTemplateCreator.CreateAPITemplatesAsync(api);

                    apiTemplates.AddRange(apiTemplateSet);
                    // create the relevant info that will be needed to properly link to the api template(s) from the master template
                    apiInformation.Add(new LinkedMasterTemplateAPIInformation()
                    {
                        name    = api.name,
                        isSplit = apiTemplateCreator.IsSplitAPI(api),
                        dependsOnGlobalServicePolicies = creatorConfig.policy != null,
                        dependsOnVersionSets           = api.apiVersionSetId != null,
                        dependsOnVersion              = masterTemplateCreator.GetDependsOnPreviousApiVersion(api, apiVersions),
                        dependsOnProducts             = api.products != null,
                        dependsOnTags                 = api.tags != null,
                        dependsOnLoggers              = await masterTemplateCreator.DetermineIfAPIDependsOnLoggerAsync(api, fileReader),
                        dependsOnAuthorizationServers = api.authenticationSettings != null && api.authenticationSettings.OAuth2 != null && api.authenticationSettings.OAuth2.AuthorizationServerId != null,
                        dependsOnBackends             = await masterTemplateCreator.DetermineIfAPIDependsOnBackendAsync(api, fileReader),
                        isServiceUrlParameterize      = isServiceUrlParameterizeInYml
                    });
                }
            }

            Console.WriteLine("Creating tag template");
            Console.WriteLine("------------------------------------------");
            Template tagTemplate = creatorConfig.tags != null?tagTemplateCreator.CreateTagTemplate(creatorConfig) : null;

            // create parameters file
            Template templateParameters = masterTemplateCreator.CreateMasterTemplateParameterValues(creatorConfig);

            // write templates to outputLocation
            if (creatorConfig.linked == true)
            {
                // create linked master template
                Template masterTemplate = masterTemplateCreator.CreateLinkedMasterTemplate(creatorConfig, globalServicePolicyTemplate, apiVersionSetsTemplate, productsTemplate, productAPIsTemplate, propertyTemplate, loggersTemplate, backendsTemplate, authorizationServersTemplate, tagTemplate, apiInformation, fileNames, creatorConfig.apimServiceName);
                FileWriter.WriteJSONToFile(masterTemplate, string.Concat(creatorConfig.outputLocation, fileNames.LinkedMaster));
            }
            foreach (Template apiTemplate in apiTemplates)
            {
                APITemplateResource apiResource = apiTemplate.Resources.FirstOrDefault(resource => resource.Type == ResourceTypeConstants.API) as APITemplateResource;
                APIConfig           providedAPIConfiguration = creatorConfig.apis.FirstOrDefault(api => string.Compare(apiResource.Name, APITemplateCreator.MakeResourceName(api), true) == 0);
                // if the api version is not null the api is split into multiple templates. If the template is split and the content value has been set, then the template is for a subsequent api
                string apiFileName = FileNameGenerator.GenerateCreatorAPIFileName(providedAPIConfiguration.name, apiTemplateCreator.IsSplitAPI(providedAPIConfiguration), apiResource.Properties.Value != null);
                FileWriter.WriteJSONToFile(apiTemplate, string.Concat(creatorConfig.outputLocation, apiFileName));
            }
            if (globalServicePolicyTemplate != null)
            {
                FileWriter.WriteJSONToFile(globalServicePolicyTemplate, string.Concat(creatorConfig.outputLocation, fileNames.GlobalServicePolicy));
            }
            if (apiVersionSetsTemplate != null)
            {
                FileWriter.WriteJSONToFile(apiVersionSetsTemplate, string.Concat(creatorConfig.outputLocation, fileNames.ApiVersionSets));
            }
            if (productsTemplate != null)
            {
                FileWriter.WriteJSONToFile(productsTemplate, string.Concat(creatorConfig.outputLocation, fileNames.Products));
            }
            if (productAPIsTemplate != null)
            {
                FileWriter.WriteJSONToFile(productAPIsTemplate, string.Concat(creatorConfig.outputLocation, fileNames.ProductAPIs));
            }
            if (propertyTemplate != null)
            {
                FileWriter.WriteJSONToFile(propertyTemplate, string.Concat(creatorConfig.outputLocation, fileNames.NamedValues));
            }
            if (loggersTemplate != null)
            {
                FileWriter.WriteJSONToFile(loggersTemplate, string.Concat(creatorConfig.outputLocation, fileNames.Loggers));
            }
            if (backendsTemplate != null)
            {
                FileWriter.WriteJSONToFile(backendsTemplate, string.Concat(creatorConfig.outputLocation, fileNames.Backends));
            }
            if (authorizationServersTemplate != null)
            {
                FileWriter.WriteJSONToFile(authorizationServersTemplate, string.Concat(creatorConfig.outputLocation, fileNames.AuthorizationServers));
            }
            if (tagTemplate != null)
            {
                FileWriter.WriteJSONToFile(tagTemplate, string.Concat(creatorConfig.outputLocation, fileNames.Tags));
            }

            // write parameters to outputLocation
            FileWriter.WriteJSONToFile(templateParameters, string.Concat(creatorConfig.outputLocation, fileNames.Parameters));
            Console.WriteLine("Templates written to output location");
        }
Exemplo n.º 8
0
        public CreateCommand()
        {
            this.Name        = Constants.CreateName;
            this.Description = Constants.CreateDescription;

            // list command options
            CommandOption configFile = this.Option("--configFile <configFile>", "Config YAML file location", CommandOptionType.SingleValue).IsRequired();

            this.HelpOption();

            this.OnExecute(async() =>
            {
                // convert config file to CreatorConfig class
                FileReader fileReader       = new FileReader();
                CreatorConfig creatorConfig = await fileReader.ConvertConfigYAMLToCreatorConfigAsync(configFile.Value());

                // ensure required parameters have been passed in
                if (creatorConfig.outputLocation == null)
                {
                    throw new CommandParsingException(this, "Output location is required");
                }
                else if (creatorConfig.version == null)
                {
                    throw new CommandParsingException(this, "Version is required");
                }
                else if (creatorConfig.apimServiceName == null)
                {
                    throw new CommandParsingException(this, "APIM service name is required");
                }
                else if (creatorConfig.api == null)
                {
                    throw new CommandParsingException(this, "API configuration is required");
                }
                else if (creatorConfig.api.openApiSpec == null)
                {
                    throw new CommandParsingException(this, "Open API Spec is required");
                }
                else if (creatorConfig.api.suffix == null)
                {
                    throw new CommandParsingException(this, "API suffix is required");
                }
                else if (creatorConfig.api.name == null)
                {
                    throw new CommandParsingException(this, "API name is required");
                }
                else if (creatorConfig.linked == true && creatorConfig.linkedTemplatesBaseUrl == null)
                {
                    throw new CommandParsingException(this, "LinkTemplatesBaseUrl is required for linked templates");
                }
                else
                {
                    // required parameters have been supplied

                    // initialize helper classes
                    FileWriter fileWriter           = new FileWriter();
                    TemplateCreator templateCreator = new TemplateCreator();
                    APIVersionSetTemplateCreator apiVersionSetTemplateCreator = new APIVersionSetTemplateCreator(templateCreator);
                    ProductAPITemplateCreator productAPITemplateCreator       = new ProductAPITemplateCreator();
                    PolicyTemplateCreator policyTemplateCreator         = new PolicyTemplateCreator(fileReader);
                    DiagnosticTemplateCreator diagnosticTemplateCreator = new DiagnosticTemplateCreator();
                    APITemplateCreator apiTemplateCreator       = new APITemplateCreator(fileReader, templateCreator, policyTemplateCreator, productAPITemplateCreator, diagnosticTemplateCreator);
                    MasterTemplateCreator masterTemplateCreator = new MasterTemplateCreator(templateCreator);

                    // create templates from provided configuration
                    Template apiVersionSetTemplate = creatorConfig.apiVersionSet != null ? apiVersionSetTemplateCreator.CreateAPIVersionSetTemplate(creatorConfig) : null;
                    Template initialAPITemplate    = await apiTemplateCreator.CreateInitialAPITemplateAsync(creatorConfig);
                    Template subsequentAPITemplate = apiTemplateCreator.CreateSubsequentAPITemplate(creatorConfig);
                    if (creatorConfig.linked == true)
                    {
                        CreatorFileNames creatorFileNames = fileWriter.GenerateCreatorLinkedFileNames();

                        // create linked master template
                        Template masterTemplate           = masterTemplateCreator.CreateLinkedMasterTemplate(apiVersionSetTemplate, initialAPITemplate, subsequentAPITemplate, creatorFileNames);
                        Template masterTemplateParameters = masterTemplateCreator.CreateMasterTemplateParameterValues(creatorConfig);

                        // write templates to outputLocation
                        if (apiVersionSetTemplate != null)
                        {
                            fileWriter.WriteJSONToFile(apiVersionSetTemplate, String.Concat(creatorConfig.outputLocation, creatorFileNames.apiVersionSet));
                        }
                        fileWriter.WriteJSONToFile(initialAPITemplate, String.Concat(creatorConfig.outputLocation, creatorFileNames.initialAPI));
                        fileWriter.WriteJSONToFile(subsequentAPITemplate, String.Concat(creatorConfig.outputLocation, creatorFileNames.subsequentAPI));
                        fileWriter.WriteJSONToFile(masterTemplate, String.Concat(creatorConfig.outputLocation, "/master.template.json"));
                        fileWriter.WriteJSONToFile(masterTemplateParameters, String.Concat(creatorConfig.outputLocation, "/master.parameters.json"));
                    }
                    else
                    {
                        // create unlinked master template
                        Template initialMasterTemplate    = masterTemplateCreator.CreateInitialUnlinkedMasterTemplate(apiVersionSetTemplate, initialAPITemplate);
                        Template subsequentMasterTemplate = masterTemplateCreator.CreateSubsequentUnlinkedMasterTemplate(subsequentAPITemplate);
                        Template masterTemplateParameters = masterTemplateCreator.CreateMasterTemplateParameterValues(creatorConfig);

                        // write templates to outputLocation
                        fileWriter.WriteJSONToFile(initialMasterTemplate, String.Concat(creatorConfig.outputLocation, "/master1.template.json"));
                        fileWriter.WriteJSONToFile(subsequentMasterTemplate, String.Concat(creatorConfig.outputLocation, "/master2.template.json"));
                        fileWriter.WriteJSONToFile(masterTemplateParameters, String.Concat(creatorConfig.outputLocation, "/master.parameters.json"));
                    }

                    ColoredConsole.WriteLine("Templates written to output location");
                }
                return(0);
            });
        }