Ejemplo n.º 1
0
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(Name, $"Creating App Service Environment '{Name}'"))
            {
                AppServiceEnvironmentResource appServiceEnvironment = new AppServiceEnvironmentResource();
                appServiceEnvironment.AppServiceEnvironmentResourceName     = Name;
                appServiceEnvironment.AppServiceEnvironmentResourceLocation = Location;
                appServiceEnvironment.Location    = Location;
                appServiceEnvironment.Kind        = Kind;
                appServiceEnvironment.WorkerPools = new List <WorkerPool>()
                {
                };

                AppServiceEnvironmentResource ase = null;
                switch (ParameterSetName)
                {
                case ASEv2SubnetNameParameterSet:
                case ASEv2SubnetIdParameterSet:
                    var subnet = ParameterSetName == ASEv2SubnetNameParameterSet ? SubnetName : SubnetId;
                    //Fetch RG of given Subnet
                    var subnetResourceGroupName = NetworkClient.GetSubnetResourceGroupName(subnet, VirtualNetworkName);
                    //If unable to fetch Subnet rg from above step, use the input RG to get validation error from api call.
                    subnetResourceGroupName = !String.IsNullOrEmpty(subnetResourceGroupName) ? subnetResourceGroupName : ResourceGroupName;
                    var subnetResourceId = NetworkClient.ValidateSubnet(subnet, VirtualNetworkName, subnetResourceGroupName, DefaultContext.Subscription.Id);

                    if (!SkipRouteTable)
                    {
                        NetworkClient.EnsureASEv2RouteTable(ResourceGroupName, Name, Location, subnetResourceId);
                    }

                    if (!SkipNetworkSecurityGroup)
                    {
                        NetworkClient.EnsureASEv2NetworkSecurityGroup(ResourceGroupName, Name, Location, subnetResourceId);
                    }

                    appServiceEnvironment.VirtualNetwork            = new VirtualNetworkProfile(id: subnetResourceId);
                    appServiceEnvironment.InternalLoadBalancingMode = LoadBalancerMode == "External" ? "None" : "Web,Publishing";

                    // Create ASEv2
                    ase = WebsitesClient.CreateAppServiceEnvironment(ResourceGroupName, Name, appServiceEnvironment);
                    break;

                case ASEv3SubnetNameParameterSet:
                case ASEv3SubnetIdParameterSet:
                    var outboundSubnet = ParameterSetName == ASEv3SubnetNameParameterSet ? SubnetName : SubnetId;
                    //Fetch RG of given Subnet
                    var outboundSubnetResourceGroupName = NetworkClient.GetSubnetResourceGroupName(outboundSubnet, VirtualNetworkName);
                    //If unable to fetch Subnet rg from above step, use the input RG to get validation error from api call.
                    outboundSubnetResourceGroupName = !String.IsNullOrEmpty(outboundSubnetResourceGroupName) ? outboundSubnetResourceGroupName : ResourceGroupName;
                    var outboundSubnetResourceId = NetworkClient.ValidateSubnet(outboundSubnet, VirtualNetworkName, outboundSubnetResourceGroupName, DefaultContext.Subscription.Id);

                    appServiceEnvironment.VirtualNetwork = new VirtualNetworkProfile(id: outboundSubnetResourceId);

                    // Create ASEv3
                    NetworkClient.VerifyEmptySubnet(outboundSubnetResourceId);
                    NetworkClient.EnsureSubnetDelegation(outboundSubnetResourceId, "Microsoft.Web/hostingEnvironments");
                    ase = WebsitesClient.CreateAppServiceEnvironment(ResourceGroupName, Name, appServiceEnvironment);
                    break;
                }

                if (PassThru)
                {
                    // Refresh object to get the final state
                    var ps_ase = new PSAppServiceEnvironment(WebsitesClient.GetAppServiceEnvironment(ResourceGroupName, Name));
                    WriteObject(ps_ase);
                }
            }
        }