示例#1
0
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(Name, "Create Container Group"))
            {
                var creationParameter = new ContainerGroupCreationParameters
                {
                    Name = Name,
                    ResourceGroupName = ResourceGroupName,
                    Location          = Location ?? GetResourceGroupLocation(ResourceGroupName),
                    Tags                 = TagsConversionHelper.CreateTagDictionary(Tag, validate: true),
                    OsType               = OsType ?? ContainerGroupCreationParameters.DefaultOsType,
                    RestartPolicy        = RestartPolicy ?? ContainerGroupRestartPolicy.Always,
                    IpAddressType        = IpAddressType,
                    DnsNameLabel         = DnsNameLabel,
                    Ports                = Port ?? ContainerGroupCreationParameters.DefaultPorts,
                    ContainerImage       = Image,
                    EnvironmentVariables = ConvertHashtableToDictionary(EnvironmentVariable),
                    Cpu                        = Cpu ?? ContainerGroupCreationParameters.DefaultCpu,
                    MemoryInGb                 = MemoryInGB ?? ContainerGroupCreationParameters.DefaultMemory,
                    RegistryServer             = RegistryServerDomain,
                    RegistryUsername           = RegistryCredential?.UserName,
                    RegistryPassword           = ContainerGroupCreationParameters.ConvertToString(RegistryCredential?.Password),
                    AzureFileVolumeShareName   = AzureFileVolumeShareName,
                    AzureFileVolumeAccountName = AzureFileVolumeAccountCredential?.UserName,
                    AzureFileVolumeAccountKey  = ContainerGroupCreationParameters.ConvertToString(AzureFileVolumeAccountCredential?.Password),
                    AzureFileVolumeMountPath   = AzureFileVolumeMountPath,
                    Identity                   = ParseIdentity()
                };

                if (!string.IsNullOrWhiteSpace(this.Command))
                {
                    ParseError[] errors;
                    Token[]      tokens;
                    Parser.ParseInput(this.Command, out tokens, out errors);
                    if (errors.Any())
                    {
                        throw new ArgumentException($"Invalid 'Command' parameter: {string.Join("; ", errors.Select(err => err.Message))}");
                    }
                    creationParameter.ContainerCommand = tokens.Select(token => token.Text.Trim('\'', '"')).Where(token => !string.IsNullOrEmpty(token)).ToList();
                }

                creationParameter.Validate();

                var psContainerGroup = PSContainerGroup.FromContainerGroup(
                    CreateContainerGroup(creationParameter));

                WriteObject(psContainerGroup);
            }
        }
示例#2
0
        public override void ExecuteCmdlet()
        {
            if (this.ShouldProcess(this.Name, "Create Container Group"))
            {
                var creationParameter = new ContainerGroupCreationParameters()
                {
                    Name = this.Name,
                    ResourceGroupName = this.ResourceGroupName,
                    Location          = this.Location ?? this.GetResourceGroupLocation(this.ResourceGroupName),
                    Tags                 = TagsConversionHelper.CreateTagDictionary(this.Tag, validate: true),
                    OsType               = this.OsType ?? ContainerGroupCreationParameters.DefaultOsType,
                    RestartPolicy        = this.RestartPolicy ?? ContainerGroupRestartPolicy.Always,
                    IpAddressType        = this.IpAddressType,
                    DnsNameLabel         = this.DnsNameLabel,
                    Ports                = this.Port ?? ContainerGroupCreationParameters.DefaultPorts,
                    ContainerImage       = this.Image,
                    EnvironmentVariables = this.ConvertHashtableToDictionary(this.EnvironmentVariable),
                    Cpu                        = this.Cpu ?? ContainerGroupCreationParameters.DefaultCpu,
                    MemoryInGb                 = this.MemoryInGB ?? ContainerGroupCreationParameters.DefaultMemory,
                    RegistryServer             = this.RegistryServerDomain,
                    RegistryUsername           = this.RegistryCredential?.UserName,
                    RegistryPassword           = ContainerGroupCreationParameters.ConvertToString(this.RegistryCredential?.Password),
                    AzureFileVolumeShareName   = this.AzureFileVolumeShareName,
                    AzureFileVolumeAccountName = this.AzureFileVolumeAccountCredential?.UserName,
                    AzureFileVolumeAccountKey  = ContainerGroupCreationParameters.ConvertToString(this.AzureFileVolumeAccountCredential?.Password),
                    AzureFileVolumeMountPath   = this.AzureFileVolumeMountPath
                };
#if !NETSTANDARD
                if (!string.IsNullOrWhiteSpace(this.Command))
                {
                    Collection <PSParseError> errors;
                    var commandTokens = PSParser.Tokenize(this.Command, out errors);
                    if (errors.Any())
                    {
                        throw new ArgumentException($"Invalid 'Command' parameter: {string.Join("; ", errors.Select(err => err.Message))}");
                    }
                    creationParameter.ContainerCommand = commandTokens.Select(token => token.Content).ToList();
                }
#endif
                creationParameter.Validate();

                var psContainerGroup = PSContainerGroup.FromContainerGroup(
                    this.CreateContainerGroup(creationParameter));

                this.WriteObject(psContainerGroup);
            }
        }
        protected override void ExecuteCmdletInternal()
        {
            ContainerInstanceCmdletBase.InitializeAutoMapper();

            if (!string.IsNullOrEmpty(this.ResourceGroupName) && !string.IsNullOrEmpty(this.Name))
            {
                var psContainerGroup = PSContainerGroup.FromContainerGroup(
                    this.ContainerClient.ContainerGroups.Get(this.ResourceGroupName, this.Name));
                this.WriteObject(psContainerGroup);
            }
            else if (!string.IsNullOrEmpty(this.ResourceId))
            {
                var resource = this.ResourceClient.Resources.GetById(this.ResourceId, this.ContainerClient.ApiVersion);
                if (resource != null)
                {
                    var psContainerGroup = PSContainerGroup.FromContainerGroup(
                        this.ContainerClient.ContainerGroups.Get(this.ParseResourceGroupFromResourceId(this.ResourceId), resource.Name));
                    this.WriteObject(psContainerGroup);
                }
            }
            else
            {
                var psContainerGroups = new List <PSContainerGroupList>();
                var containerGroups   = this.ListContainerGroups();
                foreach (var containerGroup in containerGroups)
                {
                    psContainerGroups.Add(Mapper.Map <PSContainerGroupList>(PSContainerGroup.FromContainerGroup(containerGroup)));
                }

                while (!string.IsNullOrEmpty(containerGroups.NextPageLink))
                {
                    containerGroups = this.ListContainerGroupsNext(containerGroups.NextPageLink);
                    foreach (var containerGroup in containerGroups)
                    {
                        psContainerGroups.Add(Mapper.Map <PSContainerGroupList>(PSContainerGroup.FromContainerGroup(containerGroup)));
                    }
                }

                this.WriteObject(psContainerGroups, true);
            }
        }