Ejemplo n.º 1
0
        public async Task <SiteInner> CreateSiteAsync(
            IResourceGroup resourceGroup,
            AppServicePlanInner appServicePlan,
            string azureWebsiteName,
            string remoteEndpoint,
            X509Certificate2 webAppX509Certificate,
            IDictionary <string, string> tags   = null,
            CancellationToken cancellationToken = default
            )
        {
            try {
                tags ??= new Dictionary <string, string>();

                Log.Information($"Creating Azure AppService: {azureWebsiteName} ...");

                var webSiteParameters = new SiteInner {
                    Location = resourceGroup.RegionName,
                    Tags     = tags,

                    Enabled               = true,
                    HttpsOnly             = true, // Will redirect HTTP traffic to HTTPS.
                    ClientAffinityEnabled = false,
                    ServerFarmId          = appServicePlan.Id,
                    SiteConfig            = new SiteConfig {
                        AppSettings = new List <NameValuePair> {
                            new NameValuePair {
                                Name = PROXY_ENV_REMOTE_ENDPOINT,
                                // NOTE: This should be Public IP address exposed by Ingress.
                                Value = remoteEndpoint
                            },
                            new NameValuePair {
                                Name = PROXY_ENV_REMOTE_ENDPOINT_SSL_THUMBPRINT,
                                // NOTE: this certificate should be added to Ingress as default certificate.
                                Value = webAppX509Certificate.Thumbprint
                            }
                        },

                        // Coming from Microsoft.Web/sites/config resource
                        NumberOfWorkers             = 1,
                        RequestTracingEnabled       = true,
                        HttpLoggingEnabled          = true,
                        DetailedErrorLoggingEnabled = true,
                        AlwaysOn      = true,
                        MinTlsVersion = SupportedTlsVersions.OneFullStopTwo
                    }
                };

                webSiteParameters.Validate();

                var webSite = await _webSiteManagementClient
                              .WebApps
                              .CreateOrUpdateAsync(
                    resourceGroup.Name,
                    azureWebsiteName,
                    webSiteParameters,
                    cancellationToken
                    );

                Log.Information($"Created Azure AppService: {azureWebsiteName}");

                return(webSite);
            }
            catch (Exception ex) {
                Log.Error(ex, $"Failed to create Azure AppService: {azureWebsiteName}");
                throw;
            }
        }