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

[267] Add cross-workspace support to the Terra storage provider. #272

Merged
merged 9 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
75 changes: 75 additions & 0 deletions src/TesApi.Tests/Integration/TerraWsmApiClientIntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TesApi.Web.Management;
using TesApi.Web.Management.Clients;
using TesApi.Web.Management.Configuration;

namespace TesApi.Tests.Integration
{
[TestClass, TestCategory("TerraIntegration")]
[Ignore]
public class TerraWsmApiClientIntegrationTests
{
private TerraWsmApiClient wsmApiClient = null!;
private TestTerraEnvInfo envInfo = null!;

[TestInitialize]
public void Setup()
{
envInfo = new TestTerraEnvInfo();

var terraOptions = Options.Create(new TerraOptions()
{
WsmApiHost = envInfo.WsmApiHost
});
var retryOptions = Options.Create(new RetryPolicyOptions());
var memoryCache = new MemoryCache(new MemoryCacheOptions());

wsmApiClient = new TerraWsmApiClient(new TestEnvTokenCredential(), terraOptions,
new CacheAndRetryHandler(memoryCache, retryOptions), TestLoggerFactory.Create<TerraWsmApiClient>());

}

[TestMethod]
public async Task GetContainerResourcesAsync_CallsUsingTheWSIdFromContainerName_ReturnsContainerInformation()
{
var workspaceId = Guid.Parse(envInfo.WorkspaceContainerName.Replace("sc-", ""));

var results = await wsmApiClient.GetContainerResourcesAsync(workspaceId, 0, 100, CancellationToken.None);

Assert.IsNotNull(results);
Assert.IsTrue(results.Resources.Any(i => i.ResourceAttributes.AzureStorageContainer.StorageContainerName.Equals(envInfo.WorkspaceContainerName, StringComparison.OrdinalIgnoreCase)));
}
}

public static class TestLoggerFactory
{
private static readonly ILoggerFactory SLogFactory = LoggerFactory.Create(builder =>
{
builder
.AddSystemdConsole(options =>
{
options.IncludeScopes = true;
options.TimestampFormat = "yyyy-MM-dd HH:mm:ss.fff ";
options.UseUtcTimestamp = true;
});

builder.SetMinimumLevel(LogLevel.Trace);
});


public static ILogger<T> Create<T>()
{
return SLogFactory.CreateLogger<T>();
}
}
}
41 changes: 41 additions & 0 deletions src/TesApi.Tests/Integration/TestEnvTokenCredential.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;

namespace TesApi.Tests.Integration
{
internal class TestEnvTokenCredential : TokenCredential
{
public const string TerraTokenEnvVariableName = "TERRA_AUTH_TOKEN";

public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken)
{
var token = GetTokenFromEnvVariable();

return new ValueTask<AccessToken>(new AccessToken(token, DateTimeOffset.MaxValue));
}

private static string GetTokenFromEnvVariable()
{
var token = Environment.GetEnvironmentVariable(TerraTokenEnvVariableName);

if (string.IsNullOrEmpty(token))
{
throw new InvalidOperationException($"Environment variable {TerraTokenEnvVariableName} is not set.");
}

return token;
}

public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken)
{
var token = GetTokenFromEnvVariable();

return new AccessToken(token, DateTimeOffset.MaxValue);
}
}
}
51 changes: 51 additions & 0 deletions src/TesApi.Tests/Integration/TestTerraEnvInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TesApi.Tests.Integration
{
internal class TestTerraEnvInfo
{
private const string LzStorageAccountNameEnvVarName = "TERRA_LZ_STORAGE_ACCOUNT";
private const string WorkspaceContainerNameEnvVarName = "TERRA_WS_STORAGE_CONTAINER";
private const string WsmApiHostEnvVarName = "TERRA_WSM_API_HOST";

private readonly string lzStorageAccountName;
private readonly string workspaceContainerName;
private readonly string wsmApiHost;

public string LzStorageAccountName => lzStorageAccountName;
public string WorkspaceContainerName => workspaceContainerName;
public string WsmApiHost => wsmApiHost;

public TestTerraEnvInfo()
{
lzStorageAccountName = Environment.GetEnvironmentVariable(LzStorageAccountNameEnvVarName);
workspaceContainerName = Environment.GetEnvironmentVariable(WorkspaceContainerNameEnvVarName);
wsmApiHost = Environment.GetEnvironmentVariable(WsmApiHostEnvVarName);

if (string.IsNullOrEmpty(lzStorageAccountName))
{
throw new InvalidOperationException(
$"The environment variable {LzStorageAccountNameEnvVarName} is not set");
}

if (string.IsNullOrEmpty(workspaceContainerName))
{
throw new InvalidOperationException(
$"The environment variable {WorkspaceContainerNameEnvVarName} is not set");
}

if (string.IsNullOrEmpty(wsmApiHost))
{
throw new InvalidOperationException(
$"The environment variable {WsmApiHostEnvVarName} is not set");
}
}
}
}
77 changes: 73 additions & 4 deletions src/TesApi.Tests/TerraApiStubData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Text.Json;
using TesApi.Web.Management.Configuration;
using TesApi.Web.Management.Models.Terra;
Expand All @@ -13,10 +14,10 @@ public class TerraApiStubData
public const string LandingZoneApiHost = "https://landingzone.host";
public const string WsmApiHost = "https://wsm.host";
public const string ResourceGroup = "mrg-terra-dev-previ-20191228";
public const string WorkspaceAccountName = "fooaccount";
public const string WorkspaceContainerName = "foocontainer";
public const string WorkspaceAccountName = "lzaccount1";
public const string WorkspaceStorageContainerName = "sc-ef9fed44-dba6-4825-868c-b00208522382";
public const string SasToken = "SASTOKENSTUB=";
public const string WsmGetSasResponseStorageUrl = $"https://bloburl.foo/{WorkspaceContainerName}";
public const string WsmGetSasResponseStorageUrl = $"https://{WorkspaceAccountName}.blob.core.windows.net/{WorkspaceStorageContainerName}";

public Guid LandingZoneId { get; } = Guid.NewGuid();
public Guid SubscriptionId { get; } = Guid.NewGuid();
Expand All @@ -28,6 +29,11 @@ public class TerraApiStubData
$"/subscriptions/{SubscriptionId}/resourceGroups/{ResourceGroup}/providers/Microsoft.Batch/batchAccounts/{BatchAccountName}";

public string PoolId => "poolId";

public Guid GetWorkspaceIdFromContainerName(string containerName)
{
return Guid.Parse(containerName.Replace("sc-", ""));
}
public LandingZoneResourcesApiResponse GetResourceApiResponse()
{
return JsonSerializer.Deserialize<LandingZoneResourcesApiResponse>(GetResourceApiResponseInJson());
Expand All @@ -50,7 +56,7 @@ public TerraOptions GetTerraOptions()
LandingZoneApiHost = LandingZoneApiHost,
WsmApiHost = WsmApiHost,
WorkspaceStorageAccountName = WorkspaceAccountName,
WorkspaceStorageContainerName = WorkspaceContainerName,
WorkspaceStorageContainerName = WorkspaceStorageContainerName,
WorkspaceStorageContainerResourceId = ContainerResourceId.ToString()
};
}
Expand Down Expand Up @@ -182,6 +188,44 @@ public string GetResourceApiResponseInJson()
}}";
}

public string GetContainerResourcesApiResponseInJson()
{
return $@"{{
""resources"": [
{{
""metadata"": {{
""workspaceId"": ""{WorkspaceId}"",
""resourceId"": ""{ContainerResourceId}"",
""name"": ""{WorkspaceStorageContainerName}"",
""resourceType"": ""AZURE_STORAGE_CONTAINER"",
""stewardshipType"": ""CONTROLLED"",
""cloudPlatform"": ""AZURE"",
""cloningInstructions"": ""COPY_NOTHING"",
""controlledResourceMetadata"": {{
""accessScope"": ""SHARED_ACCESS"",
""managedBy"": ""USER"",
""privateResourceUser"": {{}},
""privateResourceState"": ""NOT_APPLICABLE"",
""region"": ""southcentralus""
}},
""resourceLineage"": [],
""properties"": [],
""createdBy"": ""user@foo.com"",
""createdDate"": ""2023-02-09T01:48:46.040052Z"",
""lastUpdatedBy"": ""user@foo.com"",
""lastUpdatedDate"": ""2023-02-09T01:48:48.345442Z"",
""state"": ""READY""
}},
""resourceAttributes"": {{
""azureStorageContainer"": {{
""storageContainerName"": ""{WorkspaceStorageContainerName}""
}}
}}
}}
]
}}";
}

public string GetResourceQuotaApiResponseInJson()
{
return $@"{{
Expand Down Expand Up @@ -292,4 +336,29 @@ public ApiCreateBatchPoolResponse GetApiCreateBatchPoolResponse()
ResourceId = new Guid()
};
}

public WsmListContainerResourcesResponse GetWsmContainerResourcesApiResponse()
{
return new WsmListContainerResourcesResponse()
{
Resources = new List<Resource>()
{
new Resource()
{
Metadata = new Metadata()
{
ResourceId = ContainerResourceId.ToString(),
Name = WorkspaceStorageContainerName
},
ResourceAttributes = new ResourceAttributes()
{
AzureStorageContainer = new AzureStorageContainer()
{
StorageContainerName = WorkspaceStorageContainerName
}
}
}
}
};
}
}
Loading