Esempio n. 1
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");
        }
Esempio n. 2
0
        public void ShouldCreateBackendTemplateFromCreatorConfig()
        {
            // arrange
            BackendTemplateCreator backendTemplateCreator = new BackendTemplateCreator(new TemplateBuilder());
            CreatorConfig          creatorConfig          = new CreatorConfig()
            {
                backends = new List <BackendTemplateProperties>()
            };
            BackendTemplateProperties backend = new BackendTemplateProperties()
            {
                title       = "title",
                description = "description",
                resourceId  = "resourceId",
                url         = "url",
                protocol    = "protocol",
                proxy       = new BackendProxy()
                {
                    url      = "url",
                    username = "******",
                    password = "******"
                },
                tls = new BackendTLS()
                {
                    validateCertificateChain = true,
                    validateCertificateName  = true
                },
                credentials = new BackendCredentials()
                {
                    certificate   = new string[] { "cert1" },
                    query         = new object(),
                    header        = new object(),
                    authorization = new BackendCredentialsAuthorization()
                    {
                        scheme    = "scheme",
                        parameter = "parameter"
                    }
                },
                properties = new BackendSubProperties()
                {
                    serviceFabricCluster = new BackendServiceFabricCluster()
                    {
                        clientCertificatethumbprint   = "",
                        managementEndpoints           = new string[] { "endpoint" },
                        maxPartitionResolutionRetries = 1,
                        serverCertificateThumbprints  = new string[] { "thumbprint" },
                        serverX509Names = new ServerX509Names[] {
                            new ServerX509Names()
                            {
                                name = "name",
                                issuerCertificateThumbprint = "thumbprint"
                            }
                        }
                    }
                }
            };

            creatorConfig.backends.Add(backend);

            // act
            Template backendTemplate = backendTemplateCreator.CreateBackendTemplate(creatorConfig);
            BackendTemplateResource backendTemplateResource = (BackendTemplateResource)backendTemplate.Resources[0];

            // assert
            Assert.Equal($"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{backend.title}')]", backendTemplateResource.Name);
            Assert.Equal(backend.title, backendTemplateResource.Properties.title);
            Assert.Equal(backend.description, backendTemplateResource.Properties.description);
            Assert.Equal(backend.resourceId, backendTemplateResource.Properties.resourceId);
            Assert.Equal(backend.url, backendTemplateResource.Properties.url);
            Assert.Equal(backend.protocol, backendTemplateResource.Properties.protocol);
            Assert.Equal(backend.proxy.url, backendTemplateResource.Properties.proxy.url);
            Assert.Equal(backend.proxy.username, backendTemplateResource.Properties.proxy.username);
            Assert.Equal(backend.proxy.password, backendTemplateResource.Properties.proxy.password);
            Assert.Equal(backend.tls.validateCertificateChain, backendTemplateResource.Properties.tls.validateCertificateChain);
            Assert.Equal(backend.tls.validateCertificateName, backendTemplateResource.Properties.tls.validateCertificateName);
            Assert.Equal(backend.credentials.certificate, backendTemplateResource.Properties.credentials.certificate);
            Assert.Equal(backend.credentials.query, backendTemplateResource.Properties.credentials.query);
            Assert.Equal(backend.credentials.header, backendTemplateResource.Properties.credentials.header);
            Assert.Equal(backend.credentials.authorization.scheme, backendTemplateResource.Properties.credentials.authorization.scheme);
            Assert.Equal(backend.credentials.authorization.parameter, backendTemplateResource.Properties.credentials.authorization.parameter);
            Assert.Equal(backend.properties.serviceFabricCluster.clientCertificatethumbprint, backendTemplateResource.Properties.properties.serviceFabricCluster.clientCertificatethumbprint);
            Assert.Equal(backend.properties.serviceFabricCluster.managementEndpoints, backendTemplateResource.Properties.properties.serviceFabricCluster.managementEndpoints);
            Assert.Equal(backend.properties.serviceFabricCluster.maxPartitionResolutionRetries, backendTemplateResource.Properties.properties.serviceFabricCluster.maxPartitionResolutionRetries);
            Assert.Equal(backend.properties.serviceFabricCluster.serverCertificateThumbprints, backendTemplateResource.Properties.properties.serviceFabricCluster.serverCertificateThumbprints);
            Assert.Equal(backend.properties.serviceFabricCluster.serverX509Names[0].issuerCertificateThumbprint, backendTemplateResource.Properties.properties.serviceFabricCluster.serverX509Names[0].issuerCertificateThumbprint);
            Assert.Equal(backend.properties.serviceFabricCluster.serverX509Names[0].name, backendTemplateResource.Properties.properties.serviceFabricCluster.serverX509Names[0].name);
        }
        public void ShouldCreateBackendTemplateFromCreatorConfig()
        {
            // arrange
            BackendTemplateCreator backendTemplateCreator = new BackendTemplateCreator(new TemplateBuilder());
            CreatorConfig          creatorConfig          = new CreatorConfig()
            {
                backends = new List <BackendTemplateProperties>()
            };
            BackendTemplateProperties backend = new BackendTemplateProperties()
            {
                Title       = "title",
                Description = "description",
                ResourceId  = "resourceId",
                Url         = "url",
                Protocol    = "protocol",
                Proxy       = new BackendProxy()
                {
                    Url      = "url",
                    Username = "******",
                    Password = "******"
                },
                Tls = new BackendTls()
                {
                    ValidateCertificateChain = true,
                    ValidateCertificateName  = true
                },
                Credentials = new BackendCredentials()
                {
                    Certificate   = new string[] { "cert1" },
                    Query         = new object(),
                    Header        = new object(),
                    Authorization = new BackendCredentialsAuthorization()
                    {
                        Scheme    = "scheme",
                        Parameter = "parameter"
                    }
                },
                Properties = new BackendServiceFabricProperties()
                {
                    ServiceFabricCluster = new BackendServiceFabricCluster()
                    {
                        ClientCertificatethumbprint   = "",
                        ManagementEndpoints           = new string[] { "endpoint" },
                        MaxPartitionResolutionRetries = 1,
                        ServerCertificateThumbprints  = new string[] { "thumbprint" },
                        ServerX509Names = new ServerX509Names[] {
                            new ServerX509Names()
                            {
                                Name = "name",
                                IssuerCertificateThumbprint = "thumbprint"
                            }
                        }
                    }
                }
            };

            creatorConfig.backends.Add(backend);

            // act
            Template backendTemplate = backendTemplateCreator.CreateBackendTemplate(creatorConfig);
            BackendTemplateResource backendTemplateResource = (BackendTemplateResource)backendTemplate.Resources[0];

            // assert
            Assert.Equal($"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{backend.Title}')]", backendTemplateResource.Name);
            Assert.Equal(backend.Title, backendTemplateResource.Properties.Title);
            Assert.Equal(backend.Description, backendTemplateResource.Properties.Description);
            Assert.Equal(backend.ResourceId, backendTemplateResource.Properties.ResourceId);
            Assert.Equal(backend.Url, backendTemplateResource.Properties.Url);
            Assert.Equal(backend.Protocol, backendTemplateResource.Properties.Protocol);
            Assert.Equal(backend.Proxy.Url, backendTemplateResource.Properties.Proxy.Url);
            Assert.Equal(backend.Proxy.Username, backendTemplateResource.Properties.Proxy.Username);
            Assert.Equal(backend.Proxy.Password, backendTemplateResource.Properties.Proxy.Password);
            Assert.Equal(backend.Tls.ValidateCertificateChain, backendTemplateResource.Properties.Tls.ValidateCertificateChain);
            Assert.Equal(backend.Tls.ValidateCertificateName, backendTemplateResource.Properties.Tls.ValidateCertificateName);
            Assert.Equal(backend.Credentials.Certificate, backendTemplateResource.Properties.Credentials.Certificate);
            Assert.Equal(backend.Credentials.Query, backendTemplateResource.Properties.Credentials.Query);
            Assert.Equal(backend.Credentials.Header, backendTemplateResource.Properties.Credentials.Header);
            Assert.Equal(backend.Credentials.Authorization.Scheme, backendTemplateResource.Properties.Credentials.Authorization.Scheme);
            Assert.Equal(backend.Credentials.Authorization.Parameter, backendTemplateResource.Properties.Credentials.Authorization.Parameter);
            Assert.Equal(backend.Properties.ServiceFabricCluster.ClientCertificatethumbprint, backendTemplateResource.Properties.Properties.ServiceFabricCluster.ClientCertificatethumbprint);
            Assert.Equal(backend.Properties.ServiceFabricCluster.ManagementEndpoints, backendTemplateResource.Properties.Properties.ServiceFabricCluster.ManagementEndpoints);
            Assert.Equal(backend.Properties.ServiceFabricCluster.MaxPartitionResolutionRetries, backendTemplateResource.Properties.Properties.ServiceFabricCluster.MaxPartitionResolutionRetries);
            Assert.Equal(backend.Properties.ServiceFabricCluster.ServerCertificateThumbprints, backendTemplateResource.Properties.Properties.ServiceFabricCluster.ServerCertificateThumbprints);
            Assert.Equal(backend.Properties.ServiceFabricCluster.ServerX509Names[0].IssuerCertificateThumbprint, backendTemplateResource.Properties.Properties.ServiceFabricCluster.ServerX509Names[0].IssuerCertificateThumbprint);
            Assert.Equal(backend.Properties.ServiceFabricCluster.ServerX509Names[0].Name, backendTemplateResource.Properties.Properties.ServiceFabricCluster.ServerX509Names[0].Name);
        }