Skip to content
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

Add Managed Identity Test #4930

Merged
merged 14 commits into from
Jul 24, 2024
1 change: 1 addition & 0 deletions eng/testProjects.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"azure/core/model",
"azure/client-generator-core/access",
"azure/client-generator-core/usage",
"azure/resource-manager/models/common-types/managed-identity",
"azure/special-headers/client-request-id",
"encode/bytes",
"encode/datetime",
Expand Down
4 changes: 4 additions & 0 deletions src/AutoRest.CSharp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,10 @@
"commandName": "Project",
"commandLineArgs": "--standalone $(SolutionDir)\\test\\CadlRanchProjects\\azure\\core\\traits\\src\\Generated -n"
},
"typespec-azure/resource-manager/models/common-types/managed-identity": {
"commandName": "Project",
"commandLineArgs": "--standalone $(SolutionDir)\\test\\CadlRanchProjects\\azure\\resource-manager\\models\\common-types\\managed-identity\\src\\Generated -n"
},
"typespec-azure/resource-manager/models/resources": {
"commandName": "Project",
"commandLineArgs": "--standalone $(SolutionDir)\\test\\CadlRanchProjects\\azure\\resource-manager\\models\\resources\\src\\Generated -n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<Compile Include="../TestProjects/*-TypeSpec/**/*.cs" Exclude="../TestProjects/ConvenienceInitial-TypeSpec/**/*.cs" LinkBase="TestProjects" />
<Compile Include="../AutoRest.TestServer.Tests/Infrastructure/*.cs" />
<Compile Include="../AutoRest.TestServer.Tests/TestConstants.cs" />
<Compile Include="../../src/assets/Azure.Core.Shared/AzureResourceProviderNamespaceAttribute.cs"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Threading.Tasks;
using AutoRest.TestServer.Tests.Infrastructure;
using Azure.Core;
using Azure.ResourceManager;
using _Azure.ResourceManager.Models.CommonTypes.ManagedIdentity;
using _Azure.ResourceManager.Models.CommonTypes.ManagedIdentity.Models;
using Azure.ResourceManager.Resources;
using NUnit.Framework;

namespace CadlRanchProjects.Tests
{
public class AzureManagedIdentityTest : CadlRanchTestBase
{
[Test]
public Task Azure_Managed_Identity_ManagedIdentityTrackedResource_get() => Test(async (host) =>
{
ArmClient client = MgmtTestHelper.CreateArmClientWithMockAuth(host);
var resourceId = ManagedIdentityTrackedResource.CreateResourceIdentifier("00000000-0000-0000-0000-000000000000", "test-rg", "identity");
var resource = new ManagedIdentityTrackedResource(client, resourceId);
var response = await resource.GetAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual(AzureLocation.EastUS, response.Value.Data.Location);
Assert.AreEqual(resourceId, response.Value.Data.Id);
Assert.AreEqual("Succeeded", response.Value.Data.Properties.ProvisioningState);
Assert.AreEqual(ManagedServiceIdentityType.SystemAssigned, response.Value.Data.Identity.Type);
Assert.AreEqual(Guid.Empty, response.Value.Data.Identity.TenantId);
Assert.AreEqual(Guid.Empty, response.Value.Data.Identity.PrincipalId);
});

[Test]
public Task Azure_Managed_Identity_ManagedIdentityTrackedResourceCollection_createWithSystemAssigned() => Test(async (host) =>
{
ArmClient client = MgmtTestHelper.CreateArmClientWithMockAuth(host);
var resourceId = ResourceGroupResource.CreateResourceIdentifier("00000000-0000-0000-0000-000000000000", "test-rg");
var collection = new ManagedIdentityTrackedResourceCollection(client, resourceId);
var inputData = new ManagedIdentityTrackedResourceData(AzureLocation.EastUS)
{
Identity = new ManagedServiceIdentity(ManagedServiceIdentityType.SystemAssigned)
};
var response = await collection.CreateOrUpdateAsync(Azure.WaitUntil.Completed, "identity", inputData);
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual(AzureLocation.EastUS, response.Value.Data.Location);
Assert.AreEqual(new ResourceIdentifier("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Azure.ResourceManager.Models.CommonTypes.ManagedIdentity/managedIdentityTrackedResources/identity"), response.Value.Data.Id);
Assert.AreEqual("Succeeded", response.Value.Data.Properties.ProvisioningState);
Assert.AreEqual(ManagedServiceIdentityType.SystemAssigned, response.Value.Data.Identity.Type);
Assert.AreEqual(Guid.Empty, response.Value.Data.Identity.TenantId);
Assert.AreEqual(Guid.Empty, response.Value.Data.Identity.PrincipalId);
});

[Test]
public Task Azure_Managed_Identity_ManagedIdentityTrackedResource_updateWithUserAssignedAndSystemAssigned() => Test(async (host) =>
{
ArmClient client = MgmtTestHelper.CreateArmClientWithMockAuth(host);
var resourceId = ManagedIdentityTrackedResource.CreateResourceIdentifier("00000000-0000-0000-0000-000000000000", "test-rg", "identity");
var resource = new ManagedIdentityTrackedResource(client, resourceId);
var patch = new ManagedIdentityTrackedResourceData(AzureLocation.EastUS)
{
Identity = new ManagedServiceIdentity(ManagedServiceIdentityType.SystemAndUserAssignedV3)
{
UserAssignedIdentities =
{
new System.Collections.Generic.KeyValuePair<string, Azure.ResourceManager.Models.UserAssignedIdentity>("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id1", new Azure.ResourceManager.Models.UserAssignedIdentity())
}
}
};
var response = await resource.UpdateAsync(patch);
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual(AzureLocation.EastUS, response.Value.Data.Location);
Assert.AreEqual(resourceId, response.Value.Data.Id);
Assert.AreEqual("Succeeded", response.Value.Data.Properties.ProvisioningState);
Assert.AreEqual(ManagedServiceIdentityType.SystemAndUserAssignedV3, response.Value.Data.Identity.Type);
Assert.AreEqual(Guid.Empty, response.Value.Data.Identity.UserAssignedIdentities["/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id1"].ClientId);
Assert.AreEqual(Guid.Empty, response.Value.Data.Identity.UserAssignedIdentities["/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id1"].PrincipalId);
Assert.AreEqual(Guid.Empty, response.Value.Data.Identity.PrincipalId);
Assert.AreEqual(Guid.Empty, response.Value.Data.Identity.TenantId);
});
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29709.97
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "_Azure.ResourceManager.Models.CommonTypes.ManagedIdentity", "src\_Azure.ResourceManager.Models.CommonTypes.ManagedIdentity.csproj", "{28FF4005-4467-4E36-92E7-DEA27DEB1519}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "_Azure.ResourceManager.Models.CommonTypes.ManagedIdentity.Tests", "tests\_Azure.ResourceManager.Models.CommonTypes.ManagedIdentity.Tests.csproj", "{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Release|Any CPU.Build.0 = Release|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Release|Any CPU.Build.0 = Release|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Release|Any CPU.Build.0 = Release|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Release|Any CPU.Build.0 = Release|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Release|Any CPU.Build.0 = Release|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Release|Any CPU.Build.0 = Release|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A97F4B90-2591-4689-B1F8-5F21FE6D6CAE}
EndGlobalSection
EndGlobal

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

using Azure.Core;
using Azure.ResourceManager;

namespace _Azure.ResourceManager.Models.CommonTypes.ManagedIdentity.Mocking
{
/// <summary> A class to add extension methods to ArmClient. </summary>
public partial class Mockable_AzureResourceManagerModelsCommonTypesManagedIdentityArmClient : ArmResource
{
/// <summary> Initializes a new instance of the <see cref="Mockable_AzureResourceManagerModelsCommonTypesManagedIdentityArmClient"/> class for mocking. </summary>
protected Mockable_AzureResourceManagerModelsCommonTypesManagedIdentityArmClient()
{
}

/// <summary> Initializes a new instance of the <see cref="Mockable_AzureResourceManagerModelsCommonTypesManagedIdentityArmClient"/> class. </summary>
/// <param name="client"> The client parameters to use in these operations. </param>
/// <param name="id"> The identifier of the resource that is the target of operations. </param>
internal Mockable_AzureResourceManagerModelsCommonTypesManagedIdentityArmClient(ArmClient client, ResourceIdentifier id) : base(client, id)
{
}

internal Mockable_AzureResourceManagerModelsCommonTypesManagedIdentityArmClient(ArmClient client) : this(client, ResourceIdentifier.Root)
{
}

private string GetApiVersionOrNull(ResourceType resourceType)
{
TryGetApiVersion(resourceType, out string apiVersion);
return apiVersion;
}

/// <summary>
/// Gets an object representing a <see cref="ManagedIdentityTrackedResource"/> along with the instance operations that can be performed on it but with no data.
/// You can use <see cref="ManagedIdentityTrackedResource.CreateResourceIdentifier" /> to create a <see cref="ManagedIdentityTrackedResource"/> <see cref="ResourceIdentifier"/> from its components.
/// </summary>
/// <param name="id"> The resource ID of the resource to get. </param>
/// <returns> Returns a <see cref="ManagedIdentityTrackedResource"/> object. </returns>
public virtual ManagedIdentityTrackedResource GetManagedIdentityTrackedResource(ResourceIdentifier id)
{
ManagedIdentityTrackedResource.ValidateResourceId(id);
return new ManagedIdentityTrackedResource(Client, id);
}
}
}
Loading