Skip to content

Commit

Permalink
backup restore using msi (Azure#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
solankisamir authored Jan 5, 2022
1 parent 7fa89f6 commit ed36686
Show file tree
Hide file tree
Showing 3 changed files with 8,153 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class ApiManagementTestBase : TestBase
private const string TestCertificateKey = "TestCertificate";
private const string TestCertificatePasswordKey = "TestCertificatePassword";
private const string TestKeyVaultSecretKey = "testKeyVaultSecretUrl";
private const string TestBackupStorageAccount = "TestBackupStorageAccount";
private const string TestBackupUserMsiClientId = "TestBackupUserMsiClientId";
private const string TestBackupUserMsiResourceId = "TestBackupUserMsiResourceId";

public string location { get; set; }
public string subscriptionId { get; set; }
Expand All @@ -45,6 +48,9 @@ public class ApiManagementTestBase : TestBase
public string base64EncodedTestCertificateData { get; internal set; }
public string testCertificatePassword { get; internal set; }
public string testKeyVaultSecretUrl { get; internal set; }
public string testBackupStorageAccountName { get; internal set; }
public string testBackupUserMsiClientId { get; internal set; }
public string testBackupUserMsiId { get; internal set; }

public ApiManagementTestBase(MockContext context)
{
Expand Down Expand Up @@ -110,6 +116,24 @@ private void Initialize()
HttpMockServer.Variables[TestKeyVaultSecretKey] = testKeyVaultSecretUrl;
}

if (testEnv.ConnectionString.KeyValuePairs.TryGetValue(TestBackupStorageAccount, out string testBackupStorageAccount))
{
this.testBackupStorageAccountName = testBackupStorageAccount;
HttpMockServer.Variables[TestBackupStorageAccount] = testBackupStorageAccount;
}

if (testEnv.ConnectionString.KeyValuePairs.TryGetValue(TestBackupUserMsiClientId, out string backupUserMsiClientId))
{
this.testBackupUserMsiClientId = backupUserMsiClientId;
HttpMockServer.Variables[TestBackupUserMsiClientId] = backupUserMsiClientId;
}

if (testEnv.ConnectionString.KeyValuePairs.TryGetValue(TestBackupUserMsiResourceId, out string backupUserMsiId))
{
this.testBackupUserMsiId = backupUserMsiId;
HttpMockServer.Variables[TestBackupUserMsiResourceId] = backupUserMsiId;
}

this.subscriptionId = testEnv.SubscriptionId;
HttpMockServer.Variables[SubIdKey] = subscriptionId;
HttpMockServer.Variables[ServiceNameKey] = this.serviceName;
Expand Down Expand Up @@ -138,6 +162,21 @@ private void Initialize()
{
this.testKeyVaultSecretUrl = testKVSecretUrl;
}
HttpMockServer.Variables.TryGetValue(TestBackupStorageAccount, out string testBackupStorageAccount);
if (!string.IsNullOrEmpty(testBackupStorageAccount))
{
this.testBackupStorageAccountName = testBackupStorageAccount;
}
HttpMockServer.Variables.TryGetValue(TestBackupUserMsiClientId, out string backupUserMsiClientId);
if(!string.IsNullOrEmpty(backupUserMsiClientId))
{
this.testBackupUserMsiClientId = backupUserMsiClientId;
}
HttpMockServer.Variables.TryGetValue(TestBackupUserMsiResourceId, out string backupUserMsiId);
if (!string.IsNullOrEmpty(backupUserMsiId))
{
this.testBackupUserMsiId = backupUserMsiId;
}
}

tags = new Dictionary<string, string> { { "tag1", "value1" }, { "tag2", "value2" }, { "tag3", "value3" } };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Linq;
using Xunit;
using System;
using System.Collections.Generic;

namespace ApiManagement.Tests.ResourceProviderTests
{
Expand Down Expand Up @@ -97,5 +98,82 @@ public void BackupAndRestoreService()
PlatformVersion.Stv2);
}
}

[Fact]
[Trait("owner", "sasolank")]
public void BackupAndRestoreServiceUsingManagedIdentity()
{
Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback");
using (MockContext context = MockContext.Start(this.GetType()))
{
var testBase = new ApiManagementTestBase(context);

// assign user assigned identity
testBase.serviceProperties.Identity = new ApiManagementServiceIdentity("UserAssigned")
{
UserAssignedIdentities = new Dictionary<string, UserIdentityProperties>()
{
{ testBase.testBackupUserMsiId, new UserIdentityProperties() }
}
};
var createdService = testBase.client.ApiManagementService.CreateOrUpdate(
resourceGroupName: testBase.rgName,
serviceName: testBase.serviceName,
parameters: testBase.serviceProperties);

ValidateService(createdService,
testBase.serviceName,
testBase.rgName,
testBase.subscriptionId,
testBase.location,
testBase.serviceProperties.PublisherEmail,
testBase.serviceProperties.PublisherName,
testBase.serviceProperties.Sku.Name,
testBase.tags,
PlatformVersion.Stv2);
Assert.NotNull(createdService.Identity);
Assert.NotNull(createdService.Identity.Type);
Assert.Equal("UserAssigned", createdService.Identity.Type);
Assert.NotNull(createdService.Identity.UserAssignedIdentities);
Assert.Equal(1, createdService.Identity.UserAssignedIdentities.Count);

const string apimBackupContainerName = "apimbackupcontainer";
const string apimBackupName = "apimbackup.zip";
var parameters = new ApiManagementServiceBackupRestoreParameters()
{
StorageAccount = testBase.testBackupStorageAccountName,
BackupName = apimBackupName,
ContainerName = apimBackupContainerName,
AccessType = AccessType.UserAssignedManagedIdentity,
ClientId = testBase.testBackupUserMsiClientId
};

var backupServiceResponse = testBase.client.ApiManagementService.Backup(testBase.rgName, testBase.serviceName, parameters);
Assert.NotNull(backupServiceResponse);
ValidateService(backupServiceResponse,
testBase.serviceName,
testBase.rgName,
testBase.subscriptionId,
testBase.location,
testBase.serviceProperties.PublisherEmail,
testBase.serviceProperties.PublisherName,
testBase.serviceProperties.Sku.Name,
testBase.tags,
PlatformVersion.Stv2);

var restoreServiceResponse = testBase.client.ApiManagementService.Restore(testBase.rgName, testBase.serviceName, parameters);
Assert.NotNull(restoreServiceResponse);
ValidateService(restoreServiceResponse,
testBase.serviceName,
testBase.rgName,
testBase.subscriptionId,
testBase.location,
testBase.serviceProperties.PublisherEmail,
testBase.serviceProperties.PublisherName,
testBase.serviceProperties.Sku.Name,
testBase.tags,
PlatformVersion.Stv2);
}
}
}
}
Loading

0 comments on commit ed36686

Please sign in to comment.