public async Task <IEnumerable <ApiOperationPolicyTemplateResource> > Create(ApiDeploymentDefinition api, string[] dependsOn)
        {
            var fileReader = new FileReader();
            var apiOperationPolicyTemplateResources = new List <ApiOperationPolicyTemplateResource>();

            foreach (var pair in api.Operations)
            {
                var operationPolicy = pair.Value.Policy;
                var operationName   = pair.Key;

                var isUrl = operationPolicy.IsUri(out _);

                var apiOperationPolicyTemplateResource = new ApiOperationPolicyTemplateResource()
                {
                    Name       = $"[concat(parameters('ApimServiceName'), '/{api.Name}/{operationName}/policy')]",
                    Properties = new ApiOperationPolicyProperties()
                    {
                        Format = isUrl ? "rawxml-link" : "rawxml",
                        Value  = isUrl ? api.Policy : await fileReader.RetrieveFileContentsAsync(api.Policy)
                    },
                    DependsOn = dependsOn
                };

                apiOperationPolicyTemplateResources.Add(apiOperationPolicyTemplateResource);
            }

            return(apiOperationPolicyTemplateResources);
        }
Ejemplo n.º 2
0
        private async Task <ApiTemplateResource> CreateApiTemplateResourceAsync(ApiDeploymentDefinition api, bool isInitial)
        {
            ApiTemplateResource apiTemplateResource = new ApiTemplateResource()
            {
                Name       = $"[concat(parameters('ApimServiceName'), '/{api.Name}')]",
                Properties = new ApiProperties(),
                DependsOn  = new string[] { }
            };

            if (isInitial)
            {
                var openAPISpecReader = new OpenAPISpecReader(api.OpenApiSpec);

                apiTemplateResource.Properties.Format = await openAPISpecReader.GetOpenApiFormat();

                apiTemplateResource.Properties.Value = await openAPISpecReader.GetValue();;
                apiTemplateResource.Properties.Path  = api.Path;
            }
            else
            {
                var path = api.Path.Replace("//", "/");
                if (path.StartsWith("/"))
                {
                    path = path.Substring(1);
                }

                apiTemplateResource.Properties.ApiVersion                    = api.ApiVersion;
                apiTemplateResource.Properties.ServiceUrl                    = api.ServiceUrl;
                apiTemplateResource.Properties.Type                          = api.Type;
                apiTemplateResource.Properties.ApiType                       = api.ApiType;
                apiTemplateResource.Properties.Description                   = api.Description;
                apiTemplateResource.Properties.SubscriptionRequired          = api.SubscriptionRequired;
                apiTemplateResource.Properties.ApiRevision                   = api.ApiRevision;
                apiTemplateResource.Properties.ApiRevisionDescription        = api.ApiRevisionDescription;
                apiTemplateResource.Properties.ApiVersionDescription         = api.ApiVersionDescription;
                apiTemplateResource.Properties.AuthenticationSettings        = api.AuthenticationSettings;
                apiTemplateResource.Properties.Path                          = path;
                apiTemplateResource.Properties.IsCurrent                     = api.IsCurrent;
                apiTemplateResource.Properties.DisplayName                   = api.DisplayName;
                apiTemplateResource.Properties.SubscriptionKeyParameterNames = api.SubscriptionKeyParameterNames;
                apiTemplateResource.Properties.Protocols                     = api.Protocols.GetItems(new[] { "https" });

                if (api.ApiVersionSetId != null)
                {
                    apiTemplateResource.Properties.ApiVersionSetId = $"[resourceId('{ResourceType.ApiVersionSet}', parameters('ApimServiceName'), '{api.ApiVersionSetId}')]";
                }

                if (api.AuthenticationSettings != null && api.AuthenticationSettings.OAuth2 != null && api.AuthenticationSettings.OAuth2.AuthorizationServerId != null &&
                    apiTemplateResource.Properties.AuthenticationSettings != null && apiTemplateResource.Properties.AuthenticationSettings.OAuth2 != null && apiTemplateResource.Properties.AuthenticationSettings.OAuth2.AuthorizationServerId != null)
                {
                    apiTemplateResource.Properties.AuthenticationSettings.OAuth2.AuthorizationServerId = api.AuthenticationSettings.OAuth2.AuthorizationServerId;
                }
            }

            return(apiTemplateResource);
        }
        public ReleaseTemplateResource CreateAPIReleaseTemplateResource(ApiDeploymentDefinition api, string[] dependsOn)
        {
            string releaseName = $"release-revision-{api.ApiRevision}";
            // create release resource with properties
            ReleaseTemplateResource releaseTemplateResource = new ReleaseTemplateResource()
            {
                Name       = $"[concat(parameters('ApimServiceName'), '/{api.Name}/{releaseName}')]",
                Properties = new ReleaseTemplateProperties()
                {
                    Notes = $"Release created to make revision {api.ApiRevision} current.",
                    ApiId = $"[resourceId('{ResourceType.Api}', parameters('ApimServiceName'), '{api.Name}')]"
                },
                DependsOn = dependsOn
            };

            return(releaseTemplateResource);
        }
Ejemplo n.º 4
0
        public async Task <ApiPolicyTemplateResource> Create(ApiDeploymentDefinition api, string[] dependsOn)
        {
            var fileReader = new FileReader();

            var isUrl = api.Policy.IsUri(out _);

            var policyTemplateResource = new ApiPolicyTemplateResource()
            {
                Name       = $"[concat(parameters('ApimServiceName'), '/{api.Name}/policy')]",
                Properties = new ApiPolicyProperties()
                {
                    Format = isUrl ? "rawxml-link" : "rawxml",
                    Value  = isUrl ? api.Policy : await fileReader.RetrieveFileContentsAsync(api.Policy)
                },
                DependsOn = dependsOn
            };

            return(policyTemplateResource);
        }
Ejemplo n.º 5
0
        private async Task <Template> CreateApiTemplateAsync(ApiDeploymentDefinition api, bool isInitial)
        {
            var template = EmptyTemplate;

            template.Parameters.Add(ApiServiceNameParameter.Key, ApiServiceNameParameter.Value);

            var resources           = new List <TemplateResource>();
            var apiTemplateResource = await CreateApiTemplateResourceAsync(api, isInitial);

            resources.Add(apiTemplateResource);

            if (!isInitial)
            {
                resources.AddRange(await CreateChildResourceTemplates(api));
            }

            template.Resources = resources.ToArray();

            return(await Task.FromResult(template));
        }
Ejemplo n.º 6
0
        public List <TagApiTemplateResource> CreateTagApiTemplateResources(ApiDeploymentDefinition api, string[] dependsOn)
        {
            var tagApiTemplates = new List <TagApiTemplateResource>();

            var tagDisplayNames = api.TagList;

            foreach (string tagDisplayName in tagDisplayNames)
            {
                var tagName = GetTagName(tagDisplayName);

                var tagApiTemplate = new TagApiTemplateResource()
                {
                    Name       = $"[concat(parameters('ApimServiceName'), '/{api.Name}/{tagName}')]",
                    Properties = new TagApiTemplateProperties(),
                    DependsOn  = dependsOn
                };

                tagApiTemplates.Add(tagApiTemplate);
            }
            return(tagApiTemplates);
        }
Ejemplo n.º 7
0
        public List <ProductApiTemplateResource> CreateProductApiTemplateResources(ApiDeploymentDefinition api, string[] dependsOn)
        {
            var productApiTemplates = new List <ProductApiTemplateResource>();

            var productDisplayNames = api.ProductList;

            foreach (string productDisplayName in productDisplayNames)
            {
                var productName = GetProductName(productDisplayName);

                var productApiTemplate = new ProductApiTemplateResource()
                {
                    Name       = $"[concat(parameters('ApimServiceName'), '/{productName}/{api.Name}')]",
                    Properties = new ProductApiTemplateProperties(),
                    DependsOn  = dependsOn
                };

                productApiTemplates.Add(productApiTemplate);
            }
            return(productApiTemplates);
        }
Ejemplo n.º 8
0
        private async Task <List <TemplateResource> > CreateChildResourceTemplates(ApiDeploymentDefinition api)
        {
            var resources = new List <TemplateResource>();

            var dependsOn = new string[] { $"[resourceId('{ResourceType.Api}', parameters('ApimServiceName'), '{api.Name}')]" };

            if (api.Policy != null)
            {
                resources.Add(await _apiPolicyTemplateCreator.Create(api, dependsOn));
            }

            if (api.Operations != null)
            {
                resources.AddRange(await _apiOperationPolicyTemplateCreator.Create(api, dependsOn));
            }

            if (api.IsDependOnProducts())
            {
                resources.AddRange(this._productApiTemplateCreator.CreateProductApiTemplateResources(api, dependsOn));
            }

            if (api.IsDependOnTags())
            {
                resources.AddRange(this._tagApiTemplateCreator.CreateTagApiTemplateResources(api, dependsOn));
            }

            if (api.Diagnostic != null)
            {
                resources.Add(this._diagnosticTemplateCreator.CreateAPIDiagnosticTemplateResource(api, dependsOn));
            }

            if (api.Name.Contains(";rev"))
            {
                resources.Add(this._releaseTemplateCreator.CreateAPIReleaseTemplateResource(api, dependsOn));
            }

            return(resources);
        }
        public DiagnosticTemplateResource CreateAPIDiagnosticTemplateResource(ApiDeploymentDefinition api, string[] dependsOn)
        {
            // create diagnostic resource with properties
            DiagnosticTemplateResource diagnosticTemplateResource = new DiagnosticTemplateResource()
            {
                Name       = $"[concat(parameters('ApimServiceName'), '/{api.Name}/{api.Diagnostic.Name}')]",
                Properties = new DiagnosticTemplateProperties()
                {
                    AlwaysLog = api.Diagnostic.AlwaysLog,
                    Sampling  = api.Diagnostic.Sampling,
                    Frontend  = api.Diagnostic.Frontend,
                    Backend   = api.Diagnostic.Backend,
                    EnableHttpCorrelationHeaders = api.Diagnostic.EnableHttpCorrelationHeaders
                },
                DependsOn = dependsOn
            };

            // reference the provided logger if loggerId is provided
            if (api.Diagnostic.LoggerId != null)
            {
                diagnosticTemplateResource.Properties.LoggerId = $"[resourceId('{ResourceType.Logger}', parameters('ApimServiceName'), '{api.Diagnostic.LoggerId}')]";
            }
            return(diagnosticTemplateResource);
        }
Ejemplo n.º 10
0
        public async Task <(Template InitialApiTemplate, Template SubsequentApiTemplate)> CreateApiTemplatesAsync(ApiDeploymentDefinition api)
        {
            // update api name if necessary (apiRevision > 1 and isCurrent = true)
            if (int.TryParse(api.ApiRevision, out var revisionNumber))
            {
                if (revisionNumber > 1 && api.IsCurrent == true)
                {
                    api.Name += $";rev={revisionNumber}";
                }
            }

            var initial = await CreateApiTemplateAsync(api, true);

            var subsequent = await CreateApiTemplateAsync(api, false);

            return(initial, subsequent);
        }
Ejemplo n.º 11
0
        private async Task <string[]> CreateApiResourceDependencies(DeploymentDefinition creatorConfig, ApiDeploymentDefinition api)
        {
            var fileReader = new FileReader();

            List <string> apiDependsOn = new List <string>();

            if (api.IsDependOnGlobalServicePolicies(creatorConfig))
            {
                apiDependsOn.Add(DependsOn(_nestedTemplateName.GlobalServicePolicy()));
            }
            if (api.IsDependOnNamedValues(creatorConfig))
            {
                apiDependsOn.Add(DependsOn(_nestedTemplateName.NamedValues()));
            }
            if (api.IsDependOnApiVersionSet())
            {
                apiDependsOn.Add(DependsOn(_nestedTemplateName.ApiVersionSets()));
            }

            if (api.IsDependOnProducts() && creatorConfig.Products?.Count > 0)
            {
                apiDependsOn.Add(DependsOn(_nestedTemplateName.Products()));
            }

            if (api.IsDependOnTags() && creatorConfig.Tags?.Count > 0)
            {
                apiDependsOn.Add(DependsOn(_nestedTemplateName.Tags()));
            }

            if (await api.IsDependOnLogger(fileReader) && creatorConfig.Loggers != null)
            {
                apiDependsOn.Add(DependsOn(_nestedTemplateName.Loggers()));
            }

            if (await api.IsDependOnBackend(fileReader) && creatorConfig.Backends != null)
            {
                apiDependsOn.Add(DependsOn(_nestedTemplateName.Backends()));
            }

            if (api.IsDependOnAuthorizationServers())
            {
                apiDependsOn.Add(DependsOn(_nestedTemplateName.AuthorizationServers()));
            }

            if (creatorConfig.Certificates != null)
            {
                apiDependsOn.Add(DependsOn(_nestedTemplateName.Certificates()));
            }

            return(apiDependsOn.ToArray());
        }