Пример #1
0
        private static ResourceOptions MakeResourceOptions(ResourceOptions?options, Input <string>?id)
        {
            var defaultOptions = new ResourceOptions
            {
                Version = Utilities.Version,
            };
            var merged = ResourceOptions.Merge(defaultOptions, options);

            // Override the ID if one was specified for consistency with other language SDKs.
            merged.Id = id ?? merged.Id;
            return(merged);
        }
Пример #2
0
    public CosmosApp(string name, CosmosAppArgs args, ComponentResourceOptions?options = null)
        : base("examples:azure:CosmosApp", name, options)
    {
        var resourceGroup   = args.ResourceGroup;
        var locations       = args.Locations;
        var primaryLocation = locations[0];
        var parentOptions   = (CustomResourceOptions)ResourceOptions.Merge(new CustomResourceOptions {
            Parent = this
        }, options);

        // Cosmos DB Account with multiple replicas
        var cosmosAccount = new Account($"cosmos-{name}",
                                        new AccountArgs
        {
            ResourceGroupName = resourceGroup.Name,
            Location          = primaryLocation,
            GeoLocations      = locations.Select((l, i) => new AccountGeoLocationsArgs {
                Location = l, FailoverPriority = i
            }).ToArray(),
            OfferType         = "Standard",
            ConsistencyPolicy = new AccountConsistencyPolicyArgs {
                ConsistencyLevel = "Session"
            },
            EnableMultipleWriteLocations = args.EnableMultiMaster,
        },
                                        parentOptions);

        var database = new SqlDatabase($"db-{name}",
                                       new SqlDatabaseArgs
        {
            ResourceGroupName = resourceGroup.Name,
            AccountName       = cosmosAccount.Name,
            Name = args.DatabaseName,
        },
                                       parentOptions);

        var container = new SqlContainer($"sql-{name}",
                                         new SqlContainerArgs
        {
            ResourceGroupName = resourceGroup.Name,
            AccountName       = cosmosAccount.Name,
            DatabaseName      = database.Name,
            Name = args.ContainerName,
        },
                                         parentOptions);

        // Traffic Manager as a global HTTP endpoint
        var profile = new TrafficManagerProfile($"tm{name}",
                                                new TrafficManagerProfileArgs
        {
            ResourceGroupName    = resourceGroup.Name,
            TrafficRoutingMethod = "Performance",
            DnsConfigs           =
            {
                new TrafficManagerProfileDnsConfigsArgs
                {
                    // Subdomain must be globally unique, so we default it with the full resource group name
                    RelativeName = Output.Format($"{name}{resourceGroup.Name}"),
                    Ttl          = 60,
                }
            },
            MonitorConfigs =
            {
                new TrafficManagerProfileMonitorConfigsArgs
                {
                    Protocol = "HTTP",
                    Port     = 80,
                    Path     = "/api/ping",
                }
            },
        },
                                                parentOptions);

        var globalContext   = new GlobalContext(resourceGroup, cosmosAccount, database, container, parentOptions);
        var buildLocation   = args.Factory(globalContext);
        var endpointOptions = (CustomResourceOptions)ResourceOptions.Merge(options, new CustomResourceOptions {
            Parent = profile, DeleteBeforeReplace = true
        });

        var endpoints = locations.Select(location =>
        {
            var app = buildLocation(new RegionalContext(location));

            return(new TrafficManagerEndpoint($"tm{name}{location}".Truncate(16),
                                              new TrafficManagerEndpointArgs
            {
                ResourceGroupName = resourceGroup.Name,
                ProfileName = profile.Name,
                Type = app.Type,
                TargetResourceId = app.Id,
                Target = app.Url,
                EndpointLocation = location,
            },
                                              endpointOptions));
        }).ToList();

        this.Endpoint = Output.Format($"http://{profile.Fqdn}");
        this.RegisterOutputs();
    }
Пример #3
0
        public Func <RegionalContext, IRegionalEndpoint> BuildVMScaleSetApp(GlobalContext context)
        {
            var options = (CustomResourceOptions)ResourceOptions.Merge(context.Options, new CustomResourceOptions {
                DeleteBeforeReplace = true
            });
            var file = File.ReadAllText("./vm/vmCustomData.yaml");

            return((RegionalContext region) =>
            {
                var location = region.Location;
                var domainName = $"rnddnplm{location}"; //TODO: random

                var publicIp = new PublicIp($"pip-{location}", new PublicIpArgs
                {
                    ResourceGroupName = resourceGroup.Name,
                    Location = location,
                    AllocationMethod = "Static",
                    DomainNameLabel = domainName,
                },
                                            options);

                var loadBalancer = new LoadBalancer($"lb-{location}", new LoadBalancerArgs
                {
                    ResourceGroupName = resourceGroup.Name,
                    Location = location,
                    FrontendIpConfigurations =
                    {
                        new LoadBalancerFrontendIpConfigurationsArgs
                        {
                            Name = "PublicIPAddress",
                            PublicIpAddressId = publicIp.Id,
                        }
                    }
                },
                                                    options);

                var bpepool = new BackendAddressPool($"bap-{location}", new BackendAddressPoolArgs
                {
                    ResourceGroupName = resourceGroup.Name,
                    LoadbalancerId = loadBalancer.Id,
                },
                                                     options);

                var probe = new Probe($"ssh-probe-{location}".Truncate(16), new ProbeArgs
                {
                    ResourceGroupName = resourceGroup.Name,
                    LoadbalancerId = loadBalancer.Id,
                    Port = 80,
                },
                                      options);

                var rule = new Rule($"rule-{location}", new RuleArgs
                {
                    ResourceGroupName = resourceGroup.Name,
                    BackendAddressPoolId = bpepool.Id,
                    BackendPort = 80,
                    FrontendIpConfigurationName = "PublicIPAddress",
                    FrontendPort = 80,
                    LoadbalancerId = loadBalancer.Id,
                    ProbeId = probe.Id,
                    Protocol = "Tcp",
                },
                                    options);

                var vnet = new VirtualNetwork($"vnet-{location}", new VirtualNetworkArgs
                {
                    ResourceGroupName = resourceGroup.Name,
                    Location = location,
                    AddressSpaces = { "10.0.0.0/16" },
                },
                                              options);

                var subnet = new Subnet($"subnet-{location}", new SubnetArgs
                {
                    ResourceGroupName = resourceGroup.Name,
                    AddressPrefix = "10.0.2.0/24",
                    VirtualNetworkName = vnet.Name,
                },
                                        options);

                var customData = Output.All <string>(context.CosmosAccount.Endpoint, context.CosmosAccount.PrimaryMasterKey, context.Database.Name, context.Container.Name)
                                 .Apply(values =>
                {
                    return file.Replace("${ENDPOINT}", values[0])
                    .Replace("${MASTER_KEY}", values[1])
                    .Replace("${DATABASE}", values[2])
                    .Replace("${COLLECTION}", values[3])
                    .Replace("${LOCATION}", location);
                });

                var scaleSet = new ScaleSet($"vmss-{location}", new ScaleSetArgs
                {
                    ResourceGroupName = resourceGroup.Name,
                    Location = location,
                    NetworkProfiles =
                    {
                        new ScaleSetNetworkProfilesArgs
                        {
                            IpConfigurations =
                            {
                                new ScaleSetNetworkProfilesIpConfigurationsArgs
                                {
                                    LoadBalancerBackendAddressPoolIds ={ bpepool.Id                                         },
                                    Name = "IPConfiguration",
                                    Primary = true,
                                    SubnetId = subnet.Id,
                                }
                            },
                            Name = "networkprofile",
                            Primary = true,
                        }
                    },
                    OsProfile = new ScaleSetOsProfileArgs
                    {
                        AdminUsername = "******",
                        AdminPassword = "******",
                        ComputerNamePrefix = "lab",
                        CustomData = customData,
                    },
                    OsProfileLinuxConfig = new ScaleSetOsProfileLinuxConfigArgs {
                        DisablePasswordAuthentication = false
                    },
                    Sku = new ScaleSetSkuArgs
                    {
                        Capacity = 1,
                        Name = "Standard_DS1_v2",
                        Tier = "Standard",
                    },
                    StorageProfileDataDisks =
                    {
                        new ScaleSetStorageProfileDataDisksArgs
                        {
                            Caching = "ReadWrite",
                            CreateOption = "Empty",
                            DiskSizeGb = 10,
                            Lun = 0,
                        }
                    },
                    StorageProfileImageReference = new ScaleSetStorageProfileImageReferenceArgs
                    {
                        Offer = "UbuntuServer",
                        Publisher = "Canonical",
                        Sku = "18.04-LTS",
                        Version = "latest",
                    },
                    StorageProfileOsDisk = new ScaleSetStorageProfileOsDiskArgs
                    {
                        Caching = "ReadWrite",
                        CreateOption = "FromImage",
                        ManagedDiskType = "Standard_LRS",
                        Name = "",
                    },
                    UpgradePolicyMode = "Automatic",
                },
                                            (CustomResourceOptions)ResourceOptions.Merge(options, new ResourceOptions {
                    DependsOn = { bpepool, rule }
                }));

                var autoscale = new AutoscaleSetting($"as-{location}", new AutoscaleSettingArgs
                {
                    ResourceGroupName = resourceGroup.Name,
                    Location = location,
                    Notification = new AutoscaleSettingNotificationArgs
                    {
                        Email = new AutoscaleSettingNotificationEmailArgs
                        {
                            CustomEmails = { "*****@*****.**" },
                            SendToSubscriptionAdministrator = true,
                            SendToSubscriptionCoAdministrator = true,
                        },
                    },
                    Profiles =
                    {
                        new AutoscaleSettingProfilesArgs
                        {
                            Capacity = new AutoscaleSettingProfilesCapacityArgs
                            {
                                Default = 1,
                                Maximum = 10,
                                Minimum = 1,
                            },
                            Name = "defaultProfile",
                            Rules =
                            {
                                new AutoscaleSettingProfilesRulesArgs
                                {
                                    MetricTrigger = new AutoscaleSettingProfilesRulesMetricTriggerArgs
                                    {
                                        MetricName = "Percentage CPU",
                                        MetricResourceId = scaleSet.Id,
                                        Operator = "GreaterThan",
                                        Statistic = "Average",
                                        Threshold = 75,
                                        TimeAggregation = "Average",
                                        TimeGrain = "PT1M",
                                        TimeWindow = "PT5M",
                                    },
                                    ScaleAction = new AutoscaleSettingProfilesRulesScaleActionArgs
                                    {
                                        Cooldown = "PT1M",
                                        Direction = "Increase",
                                        Type = "ChangeCount",
                                        Value = 1,
                                    },
                                },
                                new AutoscaleSettingProfilesRulesArgs
                                {
                                    MetricTrigger = new AutoscaleSettingProfilesRulesMetricTriggerArgs
                                    {
                                        MetricName = "Percentage CPU",
                                        MetricResourceId = scaleSet.Id,
                                        Operator = "LessThan",
                                        Statistic = "Average",
                                        Threshold = 25,
                                        TimeAggregation = "Average",
                                        TimeGrain = "PT1M",
                                        TimeWindow = "PT5M",
                                    },
                                    ScaleAction = new AutoscaleSettingProfilesRulesScaleActionArgs
                                    {
                                        Cooldown = "PT1M",
                                        Direction = "Decrease",
                                        Type = "ChangeCount",
                                        Value = 1,
                                    },
                                },
                            }
                        }
                    },
                    TargetResourceId = scaleSet.Id,
                },
                                                     options);

                return new AzureEndpoint(publicIp.Id);
            });
        }