Esempio n. 1
0
        public async Task CreateWithSimpleParameters(ICmdletAdapter adapter)
        {
            ResourceGroupName = ResourceGroupName ?? Name;
            AppServicePlan    = AppServicePlan ?? Name;
            string planResourceGroup = ResourceGroupName;
            string planName          = AppServicePlan;
            var    rgStrategy        = ResourceGroupStrategy.CreateResourceGroupConfig(ResourceGroupName);
            ResourceConfig <ResourceGroup> planRG = rgStrategy;

            if (MyInvocation.BoundParameters.ContainsKey(nameof(AppServicePlan)))
            {
                if (!TryGetServerFarmFromResourceId(AppServicePlan, out planResourceGroup, out planName))
                {
                    planResourceGroup = ResourceGroupName;
                    planName          = AppServicePlan;
                }

                planRG = ResourceGroupStrategy.CreateResourceGroupConfig(planResourceGroup);
            }
            else
            {
                var farm = await GetDefaultServerFarm(Location);

                if (farm != null)
                {
                    planResourceGroup = farm.ResourceGroup;
                    planName          = farm.Name;
                    planRG            = ResourceGroupStrategy.CreateResourceGroupConfig(planResourceGroup);
                }
            }


            var farmStrategy = planRG.CreateServerFarmConfig(planResourceGroup, planName);
            var siteStrategy = rgStrategy.CreateSiteConfig(farmStrategy, Name);
            var client       = new WebClient(DefaultContext);

            var current = await siteStrategy.GetStateAsync(client, default(CancellationToken));

            if (!MyInvocation.BoundParameters.ContainsKey(nameof(Location)))
            {
                Location = current.GetLocation(siteStrategy) ?? "East US";
            }

            var engine   = new SdkEngine(DefaultContext.Subscription.Id);
            var target   = siteStrategy.GetTargetState(current, engine, Location);
            var endState = await siteStrategy.UpdateStateAsync(client, target, default(CancellationToken), adapter, adapter.ReportTaskProgress);

            var output = endState.Get(siteStrategy) ?? current.Get(siteStrategy);

            output.SiteConfig = WebsitesClient.WrappedWebsitesClient.WebApps().GetConfiguration(output.ResourceGroup, output.Name).ConvertToSiteConfig();

            try
            {
                var appSettings = WebsitesClient.WrappedWebsitesClient.WebApps().ListApplicationSettings(output.ResourceGroup, output.Name);
                output.SiteConfig.AppSettings = appSettings.Properties.Select(s => new NameValuePair {
                    Name = s.Key, Value = s.Value
                }).ToList();
                var connectionStrings = WebsitesClient.WrappedWebsitesClient.WebApps().ListConnectionStrings(output.ResourceGroup, output.Name);
                output.SiteConfig.ConnectionStrings = connectionStrings
                                                      .Properties
                                                      .Select(s => new ConnStringInfo()
                {
                    Name             = s.Key,
                    ConnectionString = s.Value.Value,
                    Type             = s.Value.Type
                }).ToList();
            }
            catch
            {
                //ignore if this call fails as it will for reader RBAC
            }

            string userName = null, password = null;

            try
            {
                var scmHostName = output.EnabledHostNames.FirstOrDefault(s => s.Contains(".scm."));
                if (!string.IsNullOrWhiteSpace(scmHostName))
                {
                    var profile = await WebsitesClient.WrappedWebsitesClient.WebApps.ListPublishingProfileXmlWithSecretsAsync(output.ResourceGroup, output.Name, new CsmPublishingProfileOptions { Format = "WebDeploy" });

                    var doc = new XmlDocument();
                    doc.Load(profile);
                    userName = doc.SelectSingleNode("//publishProfile[@publishMethod=\"MSDeploy\"]/@userName").Value;
                    password = doc.SelectSingleNode("//publishProfile[@publishMethod=\"MSDeploy\"]/@userPWD").Value;
                    var newOutput = new PSSite(output)
                    {
                        GitRemoteUri      = $"https://{scmHostName}",
                        GitRemoteUsername = userName,
                        GitRemotePassword = SecureStringExtensions.ConvertToSecureString(password)
                    };
                    output = newOutput;
                    var git        = new GitCommand(adapter.SessionState.Path, GitRepositoryPath);
                    var repository = await git.VerifyGitRepository();

                    if (repository != null)
                    {
                        if (!await git.CheckExistence())
                        {
                            adapter.WriteWarningAsync(git.InstallationInstructions);
                        }
                        else if (!string.IsNullOrWhiteSpace(userName) && !string.IsNullOrWhiteSpace(password))
                        {
                            await git.AddRemoteRepository("azure", $"https://{userName}:{password}@{scmHostName}");

                            adapter.WriteVerboseAsync(Microsoft.Azure.Commands.WebApps.Properties.Resources.GitRemoteMessage);
                            newOutput.GitRemoteName = "azure";
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                // do not write errors for problems with adding git repository
                var repoPath = GitRepositoryPath ?? adapter?.SessionState?.Path?.CurrentFileSystemLocation?.Path;
                adapter.WriteWarningAsync(String.Format(Microsoft.Azure.Commands.WebApps.Properties.Resources.GitRemoteAddFailure, repoPath, exception.Message));
            }
            adapter.WriteObjectAsync(output);
        }