Example #1
0
 public static void ValidateCsmSite(Site site)
 {
     NotNull(site, "site");
     NotNullOrEmpty(site.SubscriptionId, "SubscriptionId");
     NotNullOrEmpty(site.ResourceGroupName, "resourceGroupName");
     NotNullOrEmpty(site.SiteName, "Name");
 }
        public static async Task<ResourceGroup> PutInDesiredState(this ResourceGroup resourceGroup)
        {
            // If the resourceGroup is assigned, don't mess with it
            if (!string.IsNullOrEmpty(resourceGroup.UserId)) return resourceGroup;

            //TODO: move to config
            var neededSites = 1 - resourceGroup.Sites.Where(s => s.IsSimpleWAWSOriginalSite).Count();
            var createdSites = Enumerable.Empty<Site>();

            if (neededSites > 0)
            {
                createdSites = await Enumerable.Range(0, neededSites).Select(async n =>
                {
                    var site = new Site(resourceGroup.SubscriptionId, resourceGroup.ResourceGroupName, SiteNameGenerator.GenerateName());
                    var csmSiteResponse = await csmClient.HttpInvoke(HttpMethod.Put, CsmTemplates.Site.Bind(site), new { properties = new { }, location = resourceGroup.GeoRegion });
                    csmSiteResponse.EnsureSuccessStatusCode();

                    var csmSite = await csmSiteResponse.Content.ReadAsAsync<CsmWrapper<CsmSite>>();

                    return await Load(site, csmSite);
                }).WhenAll();
            }

            resourceGroup.Sites = resourceGroup.Sites.Union(createdSites);

            if (!resourceGroup.Tags.ContainsKey(Constants.CommonApiAppsDeployed) ||
                !resourceGroup.Tags[Constants.CommonApiAppsDeployed].Equals(Constants.CommonApiAppsDeployedVersion))
            {
                var csmTemplateString = string.Empty;

                using (var reader = new StreamReader(SimpleSettings.CommonApiAppsCsmTemplatePath))
                {
                    csmTemplateString = await reader.ReadToEndAsync();
                }

                var gatewayName = resourceGroup.Gateways.Count() != 0
                    ? resourceGroup.Gateways.Select(s => s.GatewayName).First()
                    : Guid.NewGuid().ToString().Replace("-", "");
                csmTemplateString = csmTemplateString.Replace("{{gatewayName}}", gatewayName);

                var deployment = new CsmDeployment
                {
                    DeploymentName = resourceGroup.ResourceUniqueId,
                    SubscriptionId = resourceGroup.SubscriptionId,
                    ResourceGroupName = resourceGroup.ResourceGroupName,
                    CsmTemplate = JsonConvert.DeserializeObject<JToken>(csmTemplateString),
                };

                await deployment.Deploy(block: true);
                resourceGroup.Tags[Constants.CommonApiAppsDeployed] = Constants.CommonApiAppsDeployedVersion;
                await resourceGroup.Update();
                await resourceGroup.Load();
            }

            return resourceGroup;
        }