Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Net;
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.ManagedServiceIdentity;
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.Storage;
Expand Down Expand Up @@ -49,6 +50,13 @@ public static StorageManagementClient GetStorageManagementClient(MockContext con
return client;
}

public static ManagedServiceIdentityClient GetManagedServiceIdentityClient(MockContext context, RecordedDelegatingHandler handler)
{
handler.IsPassThrough = true;
var client = context.GetServiceClient<ManagedServiceIdentityClient>(handlers: handler);
return client;
}

public static void WaitSeconds(double seconds)
{
if (HttpMockServer.Mode != HttpRecorderMode.Playback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="Microsoft.Azure.Management.Storage" Version="4.2.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Network" Version="19.17.1-preview" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="1.6.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.ManagedServiceIdentity" Version="1.0.0" />
<PackageReference Include="WindowsAzure.Storage" Version="8.7.0" />
<ProjectReference Include="..\src\Microsoft.Azure.Management.Compute.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.Azure.Management.ManagedServiceIdentity;
using Microsoft.Azure.Management.ManagedServiceIdentity.Models;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System;
Expand All @@ -16,27 +18,27 @@ public class VMIdentityTests : VMTestBase
[Fact]
public void TestVMIdentitySystemAssignedUserAssigned()
{
//
// Prerequisite: in order to record this test, first create a user identity in resource group 'identitytest' and set the value of identity here.
//
string rgName = "StaticRGforIdentityTest";
const string identity = "/subscriptions/e37510d7-33b6-4676-886f-ee75bcc01871/resourcegroups/RGforSDKtestResources/providers/Microsoft.ManagedIdentity/userAssignedIdentities/UserIdentityforTest";

using (MockContext context = MockContext.Start(this.GetType()))
{
EnsureClientsInitialized(context);

ImageReference imgageRef = GetPlatformVMImage(useWindowsImage: true);
ImageReference imageRef = GetPlatformVMImage(useWindowsImage: true);

// Create resource group
var rgName = ComputeManagementTestUtilities.GenerateName(TestPrefix);
string storageAccountName = ComputeManagementTestUtilities.GenerateName(TestPrefix);
string asName = ComputeManagementTestUtilities.GenerateName("as");
string userIdentityName = ComputeManagementTestUtilities.GenerateName("userid");
VirtualMachine inputVM;

try
{
var storageAccountOutput = CreateStorageAccount(rgName, storageAccountName);

// Creating User Assigned Managed Identity
Identity identityResponse = m_MsiClient.UserAssignedIdentities.CreateOrUpdate(rgName, userIdentityName, new Identity(location: ComputeManagementTestUtilities.DefaultLocation));
string identity = identityResponse.Id;

Action<VirtualMachine> addUserIdentity = vm =>
{
vm.Identity = new VirtualMachineIdentity();
Expand All @@ -47,7 +49,7 @@ public void TestVMIdentitySystemAssignedUserAssigned()
};
};

var vmResult = CreateVM(rgName, asName, storageAccountOutput, imgageRef, out inputVM, addUserIdentity);
var vmResult = CreateVM(rgName, asName, storageAccountOutput, imageRef, out inputVM, addUserIdentity);
Assert.Equal(ResourceIdentityType.SystemAssignedUserAssigned, vmResult.Identity.Type);
Assert.NotNull(vmResult.Identity.PrincipalId);
Assert.NotNull(vmResult.Identity.TenantId);
Expand All @@ -62,7 +64,6 @@ public void TestVMIdentitySystemAssignedUserAssigned()
Assert.True(getVM.Identity.UserAssignedIdentities.Keys.Contains(identity));
Assert.NotNull(getVM.Identity.UserAssignedIdentities[identity].PrincipalId);
Assert.NotNull(getVM.Identity.UserAssignedIdentities[identity].ClientId);

}
finally
{
Expand All @@ -71,5 +72,4 @@ public void TestVMIdentitySystemAssignedUserAssigned()
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.Azure.Management.ManagedServiceIdentity;
using Microsoft.Azure.Management.Network;
using Microsoft.Azure.Management.Network.Models;
using Microsoft.Azure.Management.ResourceManager;
Expand Down Expand Up @@ -32,6 +33,7 @@ public class VMTestBase
protected ComputeManagementClient m_CrpClient;
protected StorageManagementClient m_SrpClient;
protected NetworkManagementClient m_NrpClient;
protected ManagedServiceIdentityClient m_MsiClient;

protected bool m_initialized = false;
protected object m_lock = new object();
Expand All @@ -51,6 +53,7 @@ protected void EnsureClientsInitialized(MockContext context)
m_CrpClient = ComputeManagementTestUtilities.GetComputeManagementClient(context, new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK });
m_SrpClient = ComputeManagementTestUtilities.GetStorageManagementClient(context, new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK });
m_NrpClient = ComputeManagementTestUtilities.GetNetworkManagementClient(context, new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK });
m_MsiClient = ComputeManagementTestUtilities.GetManagedServiceIdentityClient(context, new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK });

m_subId = m_CrpClient.SubscriptionId;
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("AZURE_VM_TEST_LOCATION")))
Expand Down
Loading