protected override bool CheckExistence()
        {
            if (Parameters.Properties.ResourceGroupExists)
            {
                using (var client = new WebSiteManagementClient(GetCredentials()))
                {
                    var listResult = client.WebHostingPlans.ListAsync(Parameters.Tenant.SiteName).Result;
                    return listResult.WebHostingPlans.Any(p => p.Name.Equals(Parameters.FarmName(Position)));
                }
            }

            return false;
        }
示例#2
0
        static async Task MainAsync()
        {
            // Get the credentials
            SubscriptionCloudCredentials cloudCreds = GetCredsFromServicePrincipal();

            // Create our own HttpClient so we can do logging
            HttpClient httpClient = new HttpClient(new LoggingHandler(new HttpClientHandler()));

            // Use the creds to create the clients we need
            _resourceGroupClient = new ResourceManagementClient(cloudCreds, httpClient);
            _websiteClient = new WebSiteManagementClient(cloudCreds, httpClient);

            await ListResourceGroupsAndSites();

            // Note: site names are globally unique, so you may need to change it to avoid conflicts
            await CreateSite("MyResourceGroup", "MyWebHostingPlan", "SampleSiteFromAPI", "West US");
        }
        private string CreateWebsite()
        {
            using (var client = new WebSiteManagementClient(GetCredentials()))
            {
                var wsResult = client.WebSites.CreateOrUpdateAsync(
                    Parameters.Tenant.SiteName,
                    Parameters.GetSiteName(Position),
                    null,
                    new WebSiteCreateOrUpdateParameters()
                    {
                        WebSite = new WebSiteBase(Parameters.Location(Position))
                        {
                            Properties = new WebSiteBaseProperties()
                            {
                                ServerFarm = Parameters.FarmName(Position)
                            }
                        }
                    }).Result;

                return wsResult.WebSite.Id;
            }
        }
        protected override bool CreateOrUpdate()
        {
            var created = true;

            try
            {
                using (var client = new WebSiteManagementClient(GetCredentials()))
                {
                    // Skip if exists
                    if (!CheckExistence())
                    {
                        var createResult = client.WebHostingPlans.CreateOrUpdateAsync(
                            Parameters.Tenant.SiteName,
                            new WebHostingPlanCreateOrUpdateParameters()
                            {
                                WebHostingPlan =
                                    new WebHostingPlan()
                                    {
                                        Name = Parameters.FarmName(Position),
                                        Location = Parameters.Location(Position),
                                        Properties = new WebHostingPlanProperties()
                                        {
                                            Sku = SkuOptions.Standard
                                        }
                                    }
                            }).Result;
                    }
                }
            }
            catch (Exception ex)
            {
                created = false;
                Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            }

            return created;
        }
        private void UpdateRecommendationsSiteApplicationSettings()
        {
            using (var client = new WebSiteManagementClient(GetCredentials()))
            {
                // Build up the Application Setting list
                var properties = new List<NameValuePair>()
                {
                    new NameValuePair()
                    {
                        Name = "SqlServer",
                        Value = string.Format("{0}.database.windows.net", Parameters.GetSiteName("primary"))
                    },
                    new NameValuePair()
                    {
                        Name = "SqlDB",
                        Value = Parameters.Tenant.DatabaseName
                    },
                    new NameValuePair()
                    {
                        Name = "SqlUserID",
                        Value = Parameters.Tenant.UserName
                    },
                    new NameValuePair()
                    {
                        Name = "SqlPassword",
                        Value = Parameters.Tenant.Password
                    }
                };

                // Update the Settings
                var updateResult = client.WebSites.UpdateAppSettingsAsync(
                    Parameters.Tenant.SiteName, 
                    Parameters.GetSiteName(Position),
                    null, new WebSiteNameValueParameters()
                    {
                        Location = Parameters.Location(Position),
                        Properties = properties,
                    }).Result;
            }
        }
示例#6
0
 public static IAppServiceEnvironmentsOperations AppServiceEnvironments(this WebSiteManagementClient client)
 {
     return(client.AppServiceEnvironments);
 }
        public static string RequestAndInstallInternal(Target target)
        {
            BaseURI = target.BaseUri ?? "https://acme-staging.api.letsencrypt.org/";
            configPath = ConfigPath(BaseURI);
            try
            {
                webSiteClient = ArmHelper.GetWebSiteManagementClient(target);
            }
            catch (Exception ex)
            {
                Trace.TraceError("Unabled to create Azure Web Site Management client " + ex.ToString());
                throw;
            }

            if (!Directory.Exists(configPath))
            {
                Directory.CreateDirectory(configPath);
            }
            var email = target.Email;
            try
            {
                using (var signer = new RS256Signer())
                {
                    signer.Init();

                    var signerPath = Path.Combine(configPath, "Signer");
                    if (File.Exists(signerPath))
                    {
                        Trace.TraceInformation($"Loading Signer from {signerPath}");
                        using (var signerStream = File.OpenRead(signerPath))
                            signer.Load(signerStream);
                    }

                    using (client = new AcmeClient(new Uri(BaseURI), new AcmeServerDirectory(), signer))
                    {
                        client.Init();
                        Trace.TraceInformation("\nGetting AcmeServerDirectory");
                        client.GetDirectory(true);

                        var registrationPath = Path.Combine(configPath, "Registration");
                        if (File.Exists(registrationPath))
                        {
                            Trace.TraceInformation($"Loading Registration from {registrationPath}");
                            using (var registrationStream = File.OpenRead(registrationPath))
                                client.Registration = AcmeRegistration.Load(registrationStream);
                        }
                        else
                        {


                            var contacts = new string[] { };
                            if (!String.IsNullOrEmpty(email))
                            {
                                email = "mailto:" + email;
                                contacts = new string[] { email };
                            }

                            Trace.TraceInformation("Calling Register");
                            var registration = client.Register(contacts);


                            Trace.TraceInformation($"Do you agree to {registration.TosLinkUri}? (Y/N) ");


                            Trace.TraceInformation("Updating Registration");
                            client.UpdateRegistration(true, true);

                            Trace.TraceInformation("Saving Registration");
                            using (var registrationStream = File.OpenWrite(registrationPath))
                                client.Registration.Save(registrationStream);

                            Trace.TraceInformation("Saving Signer");
                            using (var signerStream = File.OpenWrite(signerPath))
                                signer.Save(signerStream);
                        }

                        //                        if (Options.Renew)
                        //                        {
                        //                            CheckRenewals();
                        //#if DEBUG
                        //                            Trace.TraceInformation("Press enter to continue.");
                        //                            Trace.ReadLine();
                        //#endif
                        //                            return;
                        //                        }
                        return Auto(target);
                    }
                }
            }
            catch (Exception e)
            {
                var acmeWebException = e as AcmeClient.AcmeWebException;
                if (acmeWebException != null)
                {
                    Trace.TraceError(acmeWebException.Message);
                    Trace.TraceError("ACME Server Returned:");
                    Trace.TraceError(acmeWebException.Response.ContentAsString);
                }
                else
                {
                    Trace.TraceError(e.ToString());
                }
                throw;
            }
            return null;
        }
        private void UpdateApplicationSettings()
        {
            using (var client = new WebSiteManagementClient(GetCredentials()))
            {
                // Build up the Application Setting list
                var properties = new List<NameValuePair>()
                {
                    new NameValuePair()
                    {
                        Name = "TenantEventTypeGenre",
                        Value = Parameters.Tenant.Theme
                    },
                    new NameValuePair()
                    {
                        Name = "TenantName",
                        Value = Parameters.Tenant.SiteName
                    },
                    new NameValuePair()
                    {
                        Name = "DatabaseUserName",
                        Value = Parameters.Tenant.UserName
                    },
                    new NameValuePair()
                    {
                        Name = "DatabaseUserPassword",
                        Value = Parameters.Tenant.Password
                    },
                    new NameValuePair()
                    {
                        Name = "PrimaryDatabaseServer",
                        Value = Parameters.GetSiteName("primary")
                    },
                    new NameValuePair()
                    {
                        Name = "SecondaryDatabaseServer",
                        Value = Parameters.GetSiteName("secondary")
                    },
                    new NameValuePair()
                    {
                        Name = "TenantDbName",
                        Value = Parameters.Tenant.DatabaseName
                    },
                    new NameValuePair()
                    {
                        Name = "EnableAuditing",
                        Value = "false"
                    },
                    new NameValuePair()
                    {
                        Name = "SearchServiceKey",
                        Value = Parameters.Tenant.SearchServiceKey
                    },
                    new NameValuePair()
                    {
                        Name = "SearchServiceName",
                        Value = Parameters.Tenant.SearchServiceName
                    },
                    new NameValuePair()
                    {
                        Name = "DocumentDbServiceEndpointUri",
                        Value = string.Format("https://{0}.documents.azure.com:443/", Parameters.Tenant.SiteName)
                    },
                    new NameValuePair()
                    {
                        Name = "DocumentDbServiceAuthorizationKey",
                        Value = Parameters.Tenant.DocumentDbKey
                    },
                    new NameValuePair()
                    {
                        Name = "RecommendationSiteUrl",
                        Value = Parameters.Tenant.SiteName + "prprimary.azurewebsites.net"
                    }
                };

                // Update the Settings
                var updateResult = client.WebSites.UpdateAppSettingsAsync(
                    Parameters.Tenant.SiteName, 
                    Parameters.GetSiteName(Position),
                    null, new WebSiteNameValueParameters()
                    {
                        Location = Parameters.Location(Position),
                        Properties = properties,
                    }).Result;
            }
        }
 public AzureActiveDirectoryHelper(AzureActiveDirectoryConfig configuration)
 {
     var token = GetAuthorizationHeader(configuration);
     var cred = new TokenCloudCredentials(configuration.azureSubscriptionID, token);
     client = new WebSiteManagementClient(cred, APIENDPOINT);
 }
 public static IWebAppsOperations WebApps(this WebSiteManagementClient client)
 {
     return(client.WebApps);
 }
 public static IDeletedWebAppsOperations DeletedWebApps(this WebSiteManagementClient client)
 {
     return(client.DeletedWebApps);
 }
 public static string ApiVersion(this WebSiteManagementClient client)
 {
     return("2018-02-01");
 }
 public static ICertificatesOperations Certificates(this WebSiteManagementClient client)
 {
     return(client.Certificates);
 }
 public static string ApiVersion(this WebSiteManagementClient client)
 {
     return(client.ApiVersion());
 }
 public static IAppServicePlansOperations AppServicePlans(this WebSiteManagementClient client)
 {
     return(client.AppServicePlans);
 }
示例#16
-2
        static async Task MainAsync()
        {
            // Set Environment - Choose between Azure public cloud, china cloud and US govt. cloud
            _environment = AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud];

            // Get the credentials
            TokenCloudCredentials cloudCreds = GetCredsFromServicePrincipal();
            
            var tokenCreds = new TokenCredentials(cloudCreds.Token);

            var loggingHandler = new LoggingHandler(new HttpClientHandler());

            // Create our own HttpClient so we can do logging
            var httpClient = new HttpClient(loggingHandler);

            // Use the creds to create the clients we need
            _resourceGroupClient = new ResourceManagementClient(cloudCreds, _environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager), httpClient);
            _websiteClient = new WebSiteManagementClient(_environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager), tokenCreds, loggingHandler);
            _websiteClient.SubscriptionId = cloudCreds.SubscriptionId;

            await ListResourceGroupsAndSites();

            // Note: site names are globally unique, so you may need to change it to avoid conflicts
            await CreateSite("MyResourceGroup", "MyAppServicePlan", "SampleSiteFromAPI", "West US");

            // Upload certificate to resource group
            await UpdateLoadCertificate("MyResourceGroup", "CertificateName", "West US", "PathToPfxFile", "CertificatePassword");
        }
        public static WebSiteManagementClient GetWebSiteManagementClient(IAuthSettings model)
        {
            var settings = ActiveDirectoryServiceSettings.Azure;
            var authContext = new AuthenticationContext(settings.AuthenticationEndpoint + model.Tenant);

            var token = authContext.AcquireToken(settings.TokenAudience.ToString(), new ClientCredential(model.ClientId.ToString(), model.ClientSecret));
            var creds = new TokenCredentials(token.AccessToken);

            var websiteClient = new WebSiteManagementClient(creds);
            websiteClient.SubscriptionId = model.SubscriptionId.ToString();
            return websiteClient;
        }