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); } }
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); } }
/// <summary> /// Create a container group by creation parameters. /// </summary> public ContainerGroup CreateContainerGroup(ContainerGroupCreationParameters parameters) { var resources = new ResourceRequirements() { Requests = new ResourceRequests() { Cpu = parameters.Cpu, MemoryInGB = parameters.MemoryInGb, } }; var container = new Container() { Name = parameters.Name, Image = parameters.ContainerImage, Command = parameters.ContainerCommand, Resources = resources, EnvironmentVariables = parameters.EnvironmentVariables?.Select(e => new EnvironmentVariable(e.Key, e.Value)).ToList() }; var containerGroup = new ContainerGroup() { Location = parameters.Location, Tags = parameters.Tags, Containers = new List <Container>() { container }, OsType = parameters.OsType }; if (string.Equals(IpAddress.Type, parameters.IpAddressType, StringComparison.OrdinalIgnoreCase)) { container.Ports = new List <ContainerPort>() { new ContainerPort(parameters.Port) }; containerGroup.IpAddress = new IpAddress(new List <Port>() { new Port(parameters.Port) }); } if (!string.IsNullOrEmpty(parameters.RegistryServer)) { containerGroup.ImageRegistryCredentials = new List <ImageRegistryCredential>() { new ImageRegistryCredential() { Server = parameters.RegistryServer, Username = parameters.RegistryUsername, Password = parameters.RegistryPassword } }; } return(this.ContainerClient.ContainerGroups.CreateOrUpdate( resourceGroupName: parameters.ResourceGroupName, containerGroupName: parameters.Name, containerGroup: containerGroup)); }
/// <summary> /// Create a container group by creation parameters. /// </summary> public ContainerGroup CreateContainerGroup(ContainerGroupCreationParameters parameters) { var resources = new ResourceRequirements() { Requests = new ResourceRequests() { Cpu = parameters.Cpu, MemoryInGB = parameters.MemoryInGb, } }; var container = new Container() { Name = parameters.Name, Image = parameters.ContainerImage, Command = parameters.ContainerCommand, Resources = resources, EnvironmentVariables = parameters.EnvironmentVariables?.Select(e => new EnvironmentVariable(e.Key, e.Value)).ToList() }; if (!string.IsNullOrEmpty(parameters.AzureFileVolumeMountPath)) { container.VolumeMounts = new List <VolumeMount> { new VolumeMount { Name = ContainerInstanceCmdletBase.AzureFileVolumeName, MountPath = parameters.AzureFileVolumeMountPath } }; } var containerGroup = new ContainerGroup() { Location = parameters.Location, Tags = parameters.Tags, Containers = new List <Container>() { container }, OsType = parameters.OsType, RestartPolicy = parameters.RestartPolicy }; if (string.Equals(IpAddress.Type, parameters.IpAddressType, StringComparison.OrdinalIgnoreCase) || !string.IsNullOrEmpty(parameters.DnsNameLabel)) { container.Ports = parameters.Ports.Select(p => new ContainerPort(p)).ToList(); containerGroup.IpAddress = new IpAddress( ports: parameters.Ports.Select(p => new Port(p)).ToList(), dnsNameLabel: parameters.DnsNameLabel); } if (!string.IsNullOrEmpty(parameters.RegistryServer)) { containerGroup.ImageRegistryCredentials = new List <ImageRegistryCredential>() { new ImageRegistryCredential() { Server = parameters.RegistryServer, Username = parameters.RegistryUsername, Password = parameters.RegistryPassword } }; } if (!string.IsNullOrEmpty(parameters.AzureFileVolumeMountPath)) { var azureFileVolume = new AzureFileVolume { ShareName = parameters.AzureFileVolumeShareName, StorageAccountName = parameters.AzureFileVolumeAccountName, StorageAccountKey = parameters.AzureFileVolumeAccountKey }; containerGroup.Volumes = new List <Volume> { new Volume { Name = ContainerInstanceCmdletBase.AzureFileVolumeName, AzureFile = azureFileVolume } }; } return(this.ContainerClient.ContainerGroups.CreateOrUpdate( resourceGroupName: parameters.ResourceGroupName, containerGroupName: parameters.Name, containerGroup: containerGroup)); }