Skip to content

Apply azure container instance SDK 2017-10-01 #5020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ResourceManager/ContainerInstance/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
- Additional information about change #1
-->
## Current Release
* Apply Azure Container Instance SDK 2017-10-01
- Support container run-to-completion
- Support Azure File volume mount
- Support opening multiple ports for public IP

## Version 0.1.0
* Add support for online help
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<HintPath>..\..\..\packages\Microsoft.Azure.Common.2.1.0\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Management.ContainerInstance, Version=0.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.ContainerInstance.1.0.0-preview\lib\net452\Microsoft.Azure.Management.ContainerInstance.dll</HintPath>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.ContainerInstance.1.0.1-preview\lib\net452\Microsoft.Azure.Management.ContainerInstance.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Azure.Management.ResourceManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
Expand Down Expand Up @@ -187,10 +187,13 @@
<None Include="ScenarioTests\ContainerInstanceTests.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.ContainerInstance.Test.ScenarioTests.ContainerInstanceTests\TestContainerGroupCommands.json">
<None Include="SessionRecords\Microsoft.Azure.Commands.ContainerInstance.Test.ScenarioTests.ContainerInstanceTests\TestContainerInstanceLog.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.ContainerInstance.Test.ScenarioTests.ContainerInstanceTests\TestContainerInstanceLog.json">
<None Include="SessionRecords\Microsoft.Azure.Commands.ContainerInstance.Test.ScenarioTests.ContainerInstanceTests\TestCreateContainerGroup.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Commands.ContainerInstance.Test.ScenarioTests.ContainerInstanceTests\TestCreateContainerGroupWithVolume.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ContainerInstanceTests(ITestOutputHelper output)

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestContainerGroupCommands()
public void TestCreateContainerGroup()
{
TestController.NewInstance.RunPowerShellTest("Test-AzureRmContainerGroup");
}
Expand All @@ -42,5 +42,12 @@ public void TestContainerInstanceLog()
{
TestController.NewInstance.RunPowerShellTest("Test-AzureRmContainerInstanceLog");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestCreateContainerGroupWithVolume()
{
TestController.NewInstance.RunPowerShellTest("Test-AzureRmContainerGroupWithVolumeMount");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@ function Test-AzureRmContainerGroup
$location = Get-ProviderLocation "Microsoft.ContainerInstance/ContainerGroups"
$image = "nginx"
$osType = "Linux"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for the new parameter set

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the session recording expose storage account key used for volume?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - Can you create a test storage account and delete it at the end of the test? This should prevent any security issues caused by exposing the account key.

$restartPolicy = "Never"
$port1 = 8000
$port2 = 8001

try
{
New-AzureRmResourceGroup -Name $resourceGroupName -Location $location
$containerGroupCreated = New-AzureRmContainerGroup -ResourceGroupName $resourceGroupName -Name $containerGroupName -Image $image -OsType $osType -IpAddressType "public" -Cpu 1 -Memory 1.5
$containerGroupCreated = New-AzureRmContainerGroup -ResourceGroupName $resourceGroupName -Name $containerGroupName -Image $image -OsType $osType -RestartPolicy $restartPolicy -IpAddressType "public" -Port @($port1, $port2) -Cpu 1 -Memory 1.5

Assert-AreEqual $containerGroupCreated.ResourceGroupName $resourceGroupName
Assert-AreEqual $containerGroupCreated.Name $containerGroupName
Assert-AreEqual $containerGroupCreated.Location $location
Assert-AreEqual $containerGroupCreated.OsType $osType
Assert-AreEqual $containerGroupCreated.RestartPolicy $restartPolicy
Assert-NotNull $containerGroupCreated.IpAddress
Assert-AreEqual $containerGroupCreated.Ports.Count 2
Assert-NotNull $containerGroupCreated.Containers
Assert-AreEqual $containerGroupCreated.Containers[0].Image $image
Assert-AreEqual $containerGroupCreated.Containers[0].Cpu 1
Expand Down Expand Up @@ -64,13 +69,13 @@ function Test-AzureRmContainerInstanceLog
$resourceGroupName = Get-RandomResourceGroupName
$containerGroupName = Get-RandomContainerGroupName
$location = Get-ProviderLocation "Microsoft.ContainerInstance/ContainerGroups"
$image = "nginx"
$image = "alpine"
$osType = "Linux"

try
{
New-AzureRmResourceGroup -Name $resourceGroupName -Location $location
$containerGroupCreated = New-AzureRmContainerGroup -ResourceGroupName $resourceGroupName -Name $containerGroupName -Image $image -OsType $osType -IpAddressType "public"
$containerGroupCreated = New-AzureRmContainerGroup -ResourceGroupName $resourceGroupName -Name $containerGroupName -Image $image -OsType $osType -IpAddressType "Public" -RestartPolicy "Never" -Command "echo hello"
$containerInstanceName = $containerGroupName

$log = $containerGroupCreated | Get-AzureRmContainerInstanceLog -Name $containerInstanceName
Expand All @@ -85,6 +90,44 @@ function Test-AzureRmContainerInstanceLog
}
}

<#
.SYNOPSIS
Test New-AzureRmContainerGroup with Azure File volume mount.
#>
function Test-AzureRmContainerGroupWithVolumeMount
{
$resourceGroupName = Get-RandomResourceGroupName
$containerGroupName = Get-RandomContainerGroupName
$location = Get-ProviderLocation "Microsoft.ContainerInstance/ContainerGroups"
$image = "alpine"
$shareName = "acipstestshare"
$accountName = "acipstest"
$accountKey = "password"
$secureAccountKey = ConvertTo-SecureString $accountKey -AsPlainText -Force
$accountCredential = New-Object System.Management.Automation.PSCredential ($accountName, $secureAccountKey)
$mountPath = "/mnt/azfile"

try
{
New-AzureRmResourceGroup -Name $resourceGroupName -Location $location
$containerGroupCreated = New-AzureRmContainerGroup -ResourceGroupName $resourceGroupName -Name $containerGroupName -Image $image -RestartPolicy "Never" -Command "ls $mountPath" -AzureFileVolumeShareName $shareName -AzureFileVolumeAccountCredential $accountCredential -AzureFileVolumeMountPath $mountPath

Assert-NotNull $containerGroupCreated.Volumes
Assert-NotNull $containerGroupCreated.Volumes[0].AzureFile
Assert-AreEqual $containerGroupCreated.Volumes[0].AzureFile.ShareName $shareName
Assert-AreEqual $containerGroupCreated.Volumes[0].AzureFile.StorageAccountName $accountName
Assert-NotNull $containerGroupCreated.Containers[0].VolumeMounts
Assert-AreEqual $containerGroupCreated.Containers[0].VolumeMounts[0].MountPath $mountPath

Remove-AzureRmContainerGroup -ResourceGroupName $resourceGroupName -Name $containerGroupName
}
finally
{
# Cleanup
Clean-ResourceGroup $resourceGroupName
}
}

<#
.SYNOPSIS
Assert a container group object.
Expand All @@ -109,7 +152,9 @@ function Assert-ContainerGroup
Assert-AreEqual $Actual.Name $Expected.Name
Assert-AreEqual $Actual.Location $Expected.Location
Assert-AreEqual $Actual.OsType $Expected.OsType
Assert-AreEqual $Actual.RestartPolicy $Expected.RestartPolicy
Assert-NotNull $Actual.IpAddress
Assert-AreEqual $Actual.Ports.Count $Expected.Ports.Count
Assert-NotNull $Actual.Containers
Assert-AreEqual $Actual.Containers[0].Image $Expected.Containers[0].Image
Assert-AreEqual $Actual.Containers[0].Cpu $Expected.Containers[0].Cpu
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<package id="Hyak.Common" version="1.0.3" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Management.ContainerInstance" version="1.0.0-preview" targetFramework="net452" />
<package id="Microsoft.Azure.Management.ContainerInstance" version="1.0.1-preview" targetFramework="net452" />
<package id="Microsoft.Azure.Management.ResourceManager" version="1.6.0-preview" targetFramework="net452" />
<package id="Microsoft.Azure.Test.Framework" version="1.0.6179.26854-prerelease" targetFramework="net45" />
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.7.0" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<Compile Include="Commands\ContainerInstanceCmdletBase.cs" />
<Compile Include="Models\ContainerInstanceAutoMapperProfile.cs" />
<Compile Include="Models\PSContainer.cs" />
<Compile Include="Models\PSContainerEvent.cs" />
<Compile Include="Models\PSEvent.cs" />
<Compile Include="Models\PSContainerGroup.cs" />
<Compile Include="Models\PSContainerGroupList.cs" />
<Compile Include="Models\PSContainerState.cs" />
Expand All @@ -65,7 +65,7 @@
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.Azure.Management.ContainerInstance, Version=0.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.Azure.Management.ContainerInstance.1.0.0-preview\lib\net452\Microsoft.Azure.Management.ContainerInstance.dll</HintPath>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.ContainerInstance.1.0.1-preview\lib\net452\Microsoft.Azure.Management.ContainerInstance.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public abstract class ContainerInstanceCmdletBase : AzureRMCmdlet
/// </summary>
protected const string ContainerInstanceLogNoun = "AzureRmContainerInstanceLog";

/// <summary>
/// Azure File volume name.
/// </summary>
private const string AzureFileVolumeName = "azurefile";

/// <summary>
/// Container instance management client.
/// </summary>
Expand Down Expand Up @@ -117,18 +122,31 @@ public ContainerGroup CreateContainerGroup(ContainerGroupCreationParameters para
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
OsType = parameters.OsType,
RestartPolicy = parameters.RestartPolicy
};

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) });
container.Ports = parameters.Ports.Select(p => new ContainerPort(p)).ToList();
containerGroup.IpAddress = new IpAddress(parameters.Ports.Select(p => new Port(p)).ToList());
}

if (!string.IsNullOrEmpty(parameters.RegistryServer))
Expand All @@ -144,6 +162,25 @@ public ContainerGroup CreateContainerGroup(ContainerGroupCreationParameters para
};
}

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,11 @@ public class NewAzureContainerGroupCommand : ContainerInstanceCmdletBase
{
protected const string CreateContainerGroupBaseParamSet = "CreateContainerGroupBaseParamSet";
protected const string CreateContainerGroupWithRegistryParamSet = "CreateContainerGroupWithRegistryParamSet";
protected const string CreateContainerGroupWithAzureFileVolumeParamSet = "CreateContainerGroupWithAzureFileMountParamSet";

[Parameter(
Mandatory = true,
Position = 0,
ParameterSetName = CreateContainerGroupBaseParamSet,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The resource group name.")]
[Parameter(
Mandatory = true,
Position = 0,
ParameterSetName = CreateContainerGroupWithRegistryParamSet,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The resource group name.")]
[ResourceGroupCompleter()]
Expand All @@ -53,28 +47,45 @@ public class NewAzureContainerGroupCommand : ContainerInstanceCmdletBase
[Parameter(
Mandatory = true,
Position = 1,
ParameterSetName = CreateContainerGroupBaseParamSet,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The container group name.")]
[Parameter(
Mandatory = true,
Position = 1,
ParameterSetName = CreateContainerGroupWithRegistryParamSet,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The container group name.")]
[ValidateNotNullOrEmpty]
public string Name { get; set; }

[Parameter(
Mandatory = true,
ParameterSetName = CreateContainerGroupBaseParamSet,
Position = 2,
HelpMessage = "The container image.")]
[ValidateNotNullOrEmpty]
public string Image { get; set; }

[Parameter(
Mandatory = true,
ParameterSetName = CreateContainerGroupWithRegistryParamSet,
HelpMessage = "The container image.")]
HelpMessage = "The custom container registry credential.")]
[ValidateNotNullOrEmpty]
public string Image { get; set; }
public PSCredential RegistryCredential { get; set; }

[Parameter(
Mandatory = true,
ParameterSetName = CreateContainerGroupWithAzureFileVolumeParamSet,
HelpMessage = "The name of the Azure File share to mount.")]
[ValidateNotNullOrEmpty]
public string AzureFileVolumeShareName { get; set; }

[Parameter(
Mandatory = true,
ParameterSetName = CreateContainerGroupWithAzureFileVolumeParamSet,
HelpMessage = "The storage account credential of the Azure File share to mount where the username is the storage account name and the key is the storage account key.")]
[ValidateNotNullOrEmpty]
public PSCredential AzureFileVolumeAccountCredential { get; set; }

[Parameter(
Mandatory = true,
ParameterSetName = CreateContainerGroupWithAzureFileVolumeParamSet,
HelpMessage = "The mount path for the Azure File volume.")]
[ValidateNotNullOrEmpty]
public string AzureFileVolumeMountPath { get; set; }

[Parameter(
Mandatory = false,
Expand All @@ -93,6 +104,17 @@ public class NewAzureContainerGroupCommand : ContainerInstanceCmdletBase
IgnoreCase = true)]
public string OsType { get; set; }

[Parameter(
Mandatory = false,
HelpMessage = "The container restart policy. Default: Always")]
[ValidateNotNullOrEmpty]
[ValidateSet(
ContainerGroupRestartPolicy.Always,
ContainerGroupRestartPolicy.Never,
ContainerGroupRestartPolicy.OnFailure,
IgnoreCase = true)]
public string RestartPolicy { get; set; }

[Parameter(
Mandatory = false,
HelpMessage = "The required CPU cores. Default: 1")]
Expand All @@ -119,9 +141,9 @@ public class NewAzureContainerGroupCommand : ContainerInstanceCmdletBase

[Parameter(
Mandatory = false,
HelpMessage = "The port to open. Default: 80")]
HelpMessage = "The port(s) to open. Default: [80]")]
[ValidateNotNullOrEmpty]
public int? Port { get; set; }
public int[] Port { get; set; }

[Parameter(
Mandatory = false,
Expand All @@ -143,13 +165,6 @@ public class NewAzureContainerGroupCommand : ContainerInstanceCmdletBase
[Alias("RegistryServer")]
public string RegistryServerDomain { get; set; }

[Parameter(
Mandatory = true,
ParameterSetName = CreateContainerGroupWithRegistryParamSet,
HelpMessage = "The custom container registry credential.")]
[ValidateNotNullOrEmpty]
public PSCredential RegistryCredential { get; set; }

[Parameter(
Mandatory = false,
ValueFromPipelineByPropertyName = true)]
Expand All @@ -166,15 +181,20 @@ public override void ExecuteCmdlet()
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,
Port = this.Port ?? ContainerGroupCreationParameters.DefaultPort,
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)
RegistryPassword = ContainerGroupCreationParameters.ConvertToString(this.RegistryCredential?.Password),
AzureFileVolumeShareName = this.AzureFileVolumeShareName,
AzureFileVolumeAccountName = this.AzureFileVolumeAccountCredential?.UserName,
AzureFileVolumeAccountKey = ContainerGroupCreationParameters.ConvertToString(this.AzureFileVolumeAccountCredential?.Password),
AzureFileVolumeMountPath = this.AzureFileVolumeMountPath
};

if (!string.IsNullOrWhiteSpace(this.Command))
Expand Down
Loading