/// <summary> /// Builds an AccountResource object using the specified parameters /// </summary> public static BatchAccount CreateAccountResource(string accountName, string resourceGroupName, string location = "location", Hashtable tags = null, string storageId = null) { string tenantUrlEnding = "batch-test.windows-int.net"; string endpoint = string.Format("{0}.{1}", accountName, tenantUrlEnding); string subscription = Guid.Empty.ToString(); string resourceGroup = resourceGroupName; string id = string.Format("id/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Batch/batchAccounts/abc", subscription, resourceGroup); BatchAccount resource = new BatchAccount( coreQuota: DefaultQuotaCount, poolQuota: DefaultQuotaCount, activeJobAndJobScheduleQuota: DefaultQuotaCount, accountEndpoint: endpoint, id: id, type: "type", location: location, provisioningState: ProvisioningState.Succeeded, autoStorage: new AutoStorageProperties() { StorageAccountId = storageId }, tags: tags == null ? null : TagsConversionHelper.CreateTagDictionary(tags, true)); return(resource); }
public void BatchAccountContextFromResourceTest() { string account = "account"; string tenantUrlEnding = "batch-test.windows-int.net"; string endpoint = string.Format("{0}.{1}", account, tenantUrlEnding); string subscription = "00000000-0000-0000-0000-000000000000"; string resourceGroup = "resourceGroup"; string id = string.Format("id/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Batch/batchAccounts/abc", subscription, resourceGroup); BatchAccount resource = new BatchAccount( coreQuota: BatchTestHelpers.DefaultQuotaCount, poolQuota: BatchTestHelpers.DefaultQuotaCount, activeJobAndJobScheduleQuota: BatchTestHelpers.DefaultQuotaCount, accountEndpoint: endpoint, id: id, type: "type") { Location = "location", ProvisioningState = ProvisioningState.Succeeded, }; BatchAccountContext context = BatchAccountContext.ConvertAccountResourceToNewAccountContext(resource); Assert.Equal <string>(context.Id, resource.Id); Assert.Equal <string>(context.AccountEndpoint, resource.AccountEndpoint); Assert.Equal <string>(context.Location, resource.Location); Assert.Equal <string>(context.State, resource.ProvisioningState.ToString()); Assert.Equal <string>(context.AccountName, account); Assert.Equal <string>(context.TaskTenantUrl, string.Format("https://{0}", endpoint)); Assert.Equal <string>(context.Subscription, subscription); Assert.Equal <string>(context.ResourceGroupName, resourceGroup); }
public void ListBatchAccountsTest() { List <BatchAccountContext> pipelineOutput = new List <BatchAccountContext>(); string accountName01 = "account01"; string resourceGroup = "resourceGroup"; BatchAccount accountResource01 = BatchTestHelpers.CreateAccountResource(accountName01, resourceGroup); string accountName02 = "account02"; BatchAccount accountResource02 = BatchTestHelpers.CreateAccountResource(accountName02, resourceGroup); BatchAccountContext expected01 = BatchAccountContext.ConvertAccountResourceToNewAccountContext(accountResource01, null); BatchAccountContext expected02 = BatchAccountContext.ConvertAccountResourceToNewAccountContext(accountResource02, null); batchClientMock.Setup(b => b.ListAccounts(null, resourceGroup)).Returns(new List <BatchAccountContext>() { expected01, expected02 }); cmdlet.AccountName = null; cmdlet.ResourceGroupName = resourceGroup; cmdlet.Tag = null; cmdlet.ExecuteCmdlet(); commandRuntimeMock.Verify(r => r.WriteObject(expected01), Times.Once()); commandRuntimeMock.Verify(r => r.WriteObject(expected02), Times.Once()); }
public void NewBatchWithAutoStorageAccountTest() { string accountName = "account01"; string resourceGroup = "resourceGroup"; string location = "location"; string storageId = "storageId"; AccountCreateParameters actualCreateParameters = null; // Setup the mock client to return a fake response and capture the account create parameters BatchAccount accountResource = BatchTestHelpers.CreateAccountResource(accountName, resourceGroup, location); BatchAccountContext fakeResponse = BatchAccountContext.ConvertAccountResourceToNewAccountContext(accountResource, null); batchClientMock.Setup(b => b.CreateAccount(It.IsAny <AccountCreateParameters>())) .Returns(fakeResponse) .Callback((AccountCreateParameters p) => actualCreateParameters = p); // Setup and run the cmdlet cmdlet.AccountName = accountName; cmdlet.ResourceGroupName = resourceGroup; cmdlet.Location = location; cmdlet.AutoStorageAccountId = storageId; cmdlet.ExecuteCmdlet(); // Verify the fake response was written to the pipeline and that the captured account create // parameters matched expectations. commandRuntimeMock.Verify(r => r.WriteObject(fakeResponse), Times.Once()); Assert.Equal(accountName, actualCreateParameters.BatchAccount); Assert.Equal(resourceGroup, actualCreateParameters.ResourceGroup); Assert.Equal(location, actualCreateParameters.Location); Assert.Equal(storageId, actualCreateParameters.AutoStorageAccountId); }
public void BatchAccountContextFromResourceTest() { string account = "account"; string tenantUrlEnding = "batch-test.windows-int.net"; string endpoint = string.Format("{0}.{1}", account, tenantUrlEnding); string subscription = "00000000-0000-0000-0000-000000000000"; string resourceGroup = "resourceGroup"; string id = string.Format("id/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Batch/batchAccounts/abc", subscription, resourceGroup); BatchAccount resource = new BatchAccount( coreQuota: BatchTestHelpers.DefaultQuotaCount, poolQuota: BatchTestHelpers.DefaultQuotaCount, activeJobAndJobScheduleQuota: BatchTestHelpers.DefaultQuotaCount, accountEndpoint: endpoint, id: id, type: "type") { Location = "location", ProvisioningState = ProvisioningState.Succeeded, }; BatchAccountContext context = BatchAccountContext.ConvertAccountResourceToNewAccountContext(resource); Assert.Equal<string>(context.Id, resource.Id); Assert.Equal<string>(context.AccountEndpoint, resource.AccountEndpoint); Assert.Equal<string>(context.Location, resource.Location); Assert.Equal<string>(context.State, resource.ProvisioningState.ToString()); Assert.Equal<string>(context.AccountName, account); Assert.Equal<string>(context.TaskTenantUrl, string.Format("https://{0}", endpoint)); Assert.Equal<string>(context.Subscription, subscription); Assert.Equal<string>(context.ResourceGroupName, resourceGroup); }
public void UpdateAccountTest() { string accountName = "account01"; string resourceGroup = "resourceGroup"; string storageId = "storageId"; Hashtable tags = new Hashtable { { "Name", "tagName" }, { "Value", "tagValue" } }; BatchAccount accountResource = BatchTestHelpers.CreateAccountResource(accountName, resourceGroup, tags: tags); BatchAccountContext expected = BatchAccountContext.ConvertAccountResourceToNewAccountContext(accountResource, null); batchClientMock.Setup(b => b.UpdateAccount(resourceGroup, accountName, tags, storageId)).Returns(expected); cmdlet.AccountName = accountName; cmdlet.ResourceGroupName = resourceGroup; cmdlet.Tag = tags; cmdlet.AutoStorageAccountId = storageId; cmdlet.ExecuteCmdlet(); commandRuntimeMock.Verify(r => r.WriteObject(expected), Times.Once()); }
/// <summary> /// Take an AccountResource and turn it into a BatchAccountContext /// </summary> /// <param name="resource">Resource info returned by RP</param> /// <returns>Void</returns> internal void ConvertAccountResourceToAccountContext(BatchAccount resource) { var accountEndpoint = resource.AccountEndpoint; if (Uri.CheckHostName(accountEndpoint) != UriHostNameType.Dns) { throw new ArgumentException(String.Format(Resources.InvalidEndpointType, accountEndpoint), "AccountEndpoint"); } this.Id = resource.Id; this.AccountEndpoint = accountEndpoint; this.Location = resource.Location; this.State = resource.ProvisioningState.ToString(); this.Tags = TagsConversionHelper.CreateTagHashtable(resource.Tags); this.DedicatedCoreQuota = resource.DedicatedCoreQuota; this.LowPriorityCoreQuota = resource.LowPriorityCoreQuota; this.DedicatedCoreQuotaPerVMFamilyEnforced = resource.DedicatedCoreQuotaPerVMFamilyEnforced; this.DedicatedCoreQuotaPerVMFamily = CreateQuotaHashTable(resource.DedicatedCoreQuotaPerVMFamily); this.PoolQuota = resource.PoolQuota; this.ActiveJobAndJobScheduleQuota = resource.ActiveJobAndJobScheduleQuota; this.PoolAllocationMode = resource.PoolAllocationMode; this.PublicNetworkAccess = resource.PublicNetworkAccess; this.Identity = resource.Identity; if (resource.AutoStorage != null) { this.AutoStorageProperties = new AutoStorageProperties() { StorageAccountId = resource.AutoStorage.StorageAccountId, LastKeySync = resource.AutoStorage.LastKeySync, }; } if (resource.KeyVaultReference != null) { this.KeyVaultReference = resource.KeyVaultReference; } this.PrivateEndpointConnections = resource.PrivateEndpointConnections; // extract the host and strip off the account name for the TaskTenantUrl and AccountName var hostParts = accountEndpoint.Split('.'); this.AccountName = hostParts[0]; this.TaskTenantUrl = Uri.UriSchemeHttps + Uri.SchemeDelimiter + accountEndpoint; // get remaining fields from Id which looks like: // /subscriptions/4a06fe24-c197-4353-adc1-058d1a51924e/resourceGroups/clwtest/providers/Microsoft.Batch/batchAccounts/clw var idParts = resource.Id.Split('/'); if (idParts.Length < 5) { throw new ArgumentException(String.Format(Resources.InvalidResourceId, resource.Id), "Id"); } this.Subscription = idParts[2]; this.ResourceGroupName = idParts[4]; }
/// <summary> /// Builds a BatchAccountContext object with the keys set for testing /// </summary> public static BatchAccountContext CreateBatchContextWithKeys() { BatchAccount resource = CreateAccountResource("account", "resourceGroup"); BatchAccountContext context = BatchAccountContext.ConvertAccountResourceToNewAccountContext(resource, null); string dummyKey = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; SetProperty(context, "PrimaryAccountKey", dummyKey); SetProperty(context, "SecondaryAccountKey", dummyKey); return(context); }
public static async Task EnableAutoStorageAsync() { using (BatchManagementClient managementClient = OpenBatchManagementClient()) { //TODO: Why do we need this...? ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { HttpWebRequest request = sender as HttpWebRequest; if (request != null) { if (request.Address.ToString().Contains(TestCommon.Configuration.BatchManagementUrl)) { return(true); } } return(sslPolicyErrors == SslPolicyErrors.None); //use the default validation for all other certificates }; //If the account doesn't already have auto storage enabled, enable it BatchAccount batchAccount = await managementClient.BatchAccount.GetAsync(TestCommon.Configuration.BatchAccountResourceGroup, TestCommon.Configuration.BatchAccountName); if (batchAccount.AutoStorage == null) { const string classicStorageAccountGroup = "Microsoft.ClassicStorage"; const string storageAccountGroup = "Microsoft.Storage"; string resourceFormatString = $"/subscriptions/{TestCommon.Configuration.BatchSubscription}/resourceGroups/{TestCommon.Configuration.StorageAccountResourceGroup}/providers/" + "{0}" + $"/storageAccounts/{TestCommon.Configuration.StorageAccountName}"; string classicStorageAccountFullResourceId = string.Format(resourceFormatString, classicStorageAccountGroup); string storageAccountFullResourceId = string.Format(resourceFormatString, storageAccountGroup); var updateParameters = new BatchAccountUpdateParameters() { AutoStorage = new AutoStorageBaseProperties { StorageAccountId = classicStorageAccountFullResourceId } }; try { await managementClient.BatchAccount.UpdateAsync(TestCommon.Configuration.BatchAccountResourceGroup, TestCommon.Configuration.BatchAccountName, updateParameters); } catch (Exception e) when(e.Message.Contains("The specified storage account could not be found")) { //If the storage account could not be found, it might be because we looked in "Classic" -- in that case swallow //the exception. } updateParameters.AutoStorage.StorageAccountId = storageAccountFullResourceId; await managementClient.BatchAccount.UpdateAsync(TestCommon.Configuration.BatchAccountResourceGroup, TestCommon.Configuration.BatchAccountName, updateParameters); } } }
public void GetBatchAccountTest() { string accountName = "account01"; string resourceGroup = "resourceGroup"; BatchAccount accountResource = BatchTestHelpers.CreateAccountResource(accountName, resourceGroup); BatchAccountContext expected = BatchAccountContext.ConvertAccountResourceToNewAccountContext(accountResource, null); batchClientMock.Setup(b => b.GetAccount(resourceGroup, accountName)).Returns(expected); cmdlet.AccountName = accountName; cmdlet.ResourceGroupName = resourceGroup; cmdlet.ExecuteCmdlet(); commandRuntimeMock.Verify(r => r.WriteObject(expected), Times.Once()); }
public void GetBatchAccount_IdentityTest() { string accountName = "account01"; string resourceGroup = "resourceGroup"; BatchAccount accountResource = BatchTestHelpers.CreateAccountResource(accountName, resourceGroup, identity: new BatchAccountIdentity(ResourceIdentityType.None, string.Empty, string.Empty, null as IDictionary <string, UserAssignedIdentities>)); BatchAccountContext expected = BatchAccountContext.ConvertAccountResourceToNewAccountContext(accountResource, null); batchClientMock.Setup(b => b.GetAccount(resourceGroup, accountName)).Returns(expected); cmdlet.AccountName = accountName; cmdlet.ResourceGroupName = resourceGroup; cmdlet.ExecuteCmdlet(); commandRuntimeMock.Verify(r => r.WriteObject(expected), Times.Once()); }
/// <summary> /// Creates an account and resource group for use with the Scenario tests /// </summary> public static BatchAccountContext CreateTestAccountAndResourceGroup(BatchController controller, string resourceGroupName, string accountName, string location) { controller.ResourceManagementClient.ResourceGroups.CreateOrUpdate(resourceGroupName, new ResourceGroup() { Location = location }); BatchAccount createResponse = controller.BatchManagementClient.BatchAccount.Create(resourceGroupName, accountName, new BatchAccountCreateParameters() { Location = location }); BatchAccountContext context = BatchAccountContext.ConvertAccountResourceToNewAccountContext(createResponse); BatchAccountKeys response = controller.BatchManagementClient.BatchAccount.GetKeys(resourceGroupName, accountName); context.PrimaryAccountKey = response.Primary; context.SecondaryAccountKey = response.Secondary; return(context); }
/// <summary> /// Builds an AccountResource object using the specified parameters /// </summary> public static BatchAccount CreateAccountResource( string accountName, string resourceGroupName, string location = "location", Hashtable tags = null, string storageId = null, bool dedicatedCoreQuotaPerVMFamilyEnforced = false, IList <VirtualMachineFamilyCoreQuota> machineFamilyQuotas = null, BatchAccountIdentity identity = null) { string tenantUrlEnding = "batch-test.windows-int.net"; string endpoint = string.Format("{0}.{1}", accountName, tenantUrlEnding); string subscription = Guid.Empty.ToString(); string resourceGroup = resourceGroupName; string id = string.Format("id/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Batch/batchAccounts/abc", subscription, resourceGroup); machineFamilyQuotas = machineFamilyQuotas ?? new List <VirtualMachineFamilyCoreQuota> { new VirtualMachineFamilyCoreQuota("foo", 55) }; BatchAccount resource = new BatchAccount( dedicatedCoreQuota: DefaultQuotaCount, lowPriorityCoreQuota: DefaultQuotaCount, poolQuota: DefaultQuotaCount, activeJobAndJobScheduleQuota: DefaultQuotaCount, accountEndpoint: endpoint, id: id, type: "type", location: location, provisioningState: ProvisioningState.Succeeded, autoStorage: new AutoStorageProperties() { StorageAccountId = storageId }, tags: tags == null ? null : TagsConversionHelper.CreateTagDictionary(tags, true), dedicatedCoreQuotaPerVMFamilyEnforced: dedicatedCoreQuotaPerVMFamilyEnforced, dedicatedCoreQuotaPerVMFamily: machineFamilyQuotas, identity: identity ?? new BatchAccountIdentity(ResourceIdentityType.None)); return(resource); }
public void NewBatchWithAutoStorageAccountTest() { string accountName = "account01"; string resourceGroup = "resourceGroup"; string location = "location"; string storageId = "storageId"; BatchAccount accountResource = BatchTestHelpers.CreateAccountResource(accountName, resourceGroup); BatchAccountContext expected = BatchAccountContext.ConvertAccountResourceToNewAccountContext(accountResource); batchClientMock.Setup(b => b.CreateAccount(resourceGroup, accountName, location, null, storageId)).Returns(expected); cmdlet.AccountName = accountName; cmdlet.ResourceGroupName = resourceGroup; cmdlet.Location = location; cmdlet.AutoStorageAccountId = storageId; cmdlet.ExecuteCmdlet(); commandRuntimeMock.Verify(r => r.WriteObject(expected), Times.Once()); }
public void BatchAccountContextFromResourceTest() { string account = "account"; string tenantUrlEnding = "batch-test.windows-int.net"; string endpoint = string.Format("{0}.{1}", account, tenantUrlEnding); string subscription = "00000000-0000-0000-0000-000000000000"; string resourceGroup = "resourceGroup"; string id = string.Format("id/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Batch/batchAccounts/abc", subscription, resourceGroup); PoolAllocationMode allocationMode = PoolAllocationMode.UserSubscription; KeyVaultReference keyVault = new KeyVaultReference( string.Format("/subscriptions{0}/resourceGroups/{1}/providers/Microsoft.KeyVault/vaults/foo", subscription, resourceGroup), "https://foo.vaults.azure.com"); BatchAccount resource = new BatchAccount( dedicatedCoreQuota: BatchTestHelpers.DefaultQuotaCount, lowPriorityCoreQuota: BatchTestHelpers.DefaultQuotaCount, poolQuota: BatchTestHelpers.DefaultQuotaCount, activeJobAndJobScheduleQuota: BatchTestHelpers.DefaultQuotaCount, accountEndpoint: endpoint, id: id, type: "type", location: "location", provisioningState: ProvisioningState.Succeeded, poolAllocationMode: allocationMode, keyVaultReference: keyVault); BatchAccountContext context = BatchAccountContext.ConvertAccountResourceToNewAccountContext(resource, null); Assert.Equal(resource.Id, context.Id); Assert.Equal(resource.AccountEndpoint, context.AccountEndpoint); Assert.Equal(resource.Location, context.Location); Assert.Equal(resource.ProvisioningState.ToString(), context.State); Assert.Equal(account, context.AccountName); Assert.Equal(string.Format("https://{0}", endpoint), context.TaskTenantUrl); Assert.Equal(subscription, context.Subscription); Assert.Equal(resourceGroup, context.ResourceGroupName); Assert.Equal(allocationMode, context.PoolAllocationMode); Assert.Equal(keyVault.Id, context.KeyVaultReference.Id); Assert.Equal(keyVault.Url, context.KeyVaultReference.Url); }
public void GetBatchAccountKeysTest() { string primaryKey = "pKey"; string secondaryKey = "sKey"; string accountName = "account01"; string resourceGroup = "resourceGroup"; BatchAccount accountResource = BatchTestHelpers.CreateAccountResource(accountName, resourceGroup); BatchAccountContext expected = BatchAccountContext.ConvertAccountResourceToNewAccountContext(accountResource); expected.PrimaryAccountKey = primaryKey; expected.SecondaryAccountKey = secondaryKey; batchClientMock.Setup(b => b.GetKeys(resourceGroup, accountName)).Returns(expected); cmdlet.AccountName = accountName; cmdlet.ResourceGroupName = resourceGroup; cmdlet.ExecuteCmdlet(); commandRuntimeMock.Verify(r => r.WriteObject(expected), Times.Once()); }
static void Main(string[] args) { Console.WriteLine("Logging in to Azure..."); AzureCredentials credentials = SdkContext.AzureCredentialsFactory .FromServicePrincipal(clientId, clientSecret, azureTenantId, AzureEnvironment.AzureGlobalCloud); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(credentials) .WithSubscription(azureSubscriptionId); Console.WriteLine("Successfully logged in to Azure"); //Create resource group Console.WriteLine("Creating resource group..."); var resourceGroup = azure.ResourceGroups .Define("DemoGroup") .WithRegion(Region.AsiaSouthEast) .Create(); Console.WriteLine($"Resource group '{resourceGroup.Name}' created"); // BatchManagementClient batchManagementClient = new BatchManagementClient(credentials); // Create a new Batch account var batchAccCreateParameters = new BatchAccountCreateParameters() { Location = "West US" }; batchManagementClient.BatchAccount.CreateAsync("DemoGroup", "sonubathaccount1", batchAccCreateParameters).GetAwaiter().GetResult(); // Get the new account from the Batch service BatchAccount account = batchManagementClient.BatchAccount.GetAsync("DemoGroup", "sonubathaccount1").GetAwaiter().GetResult(); // Delete the account batchManagementClient.BatchAccount.DeleteAsync("DemoGroup", account.Name); }
public void RegenBatchAccountKeysTest() { string newPrimaryKey = "newPrimaryKey"; string newSecondaryKey = "newSecondaryKey"; string accountName = "account01"; string resourceGroup = "resourceGroup"; AccountKeyType keyType = AccountKeyType.Primary; BatchAccount accountResource = BatchTestHelpers.CreateAccountResource(accountName, resourceGroup); BatchAccountContext expected = BatchAccountContext.ConvertAccountResourceToNewAccountContext(accountResource); expected.PrimaryAccountKey = newPrimaryKey; expected.SecondaryAccountKey = newSecondaryKey; batchClientMock.Setup(b => b.RegenerateKeys(resourceGroup, accountName, keyType)).Returns(expected); cmdlet.AccountName = accountName; cmdlet.ResourceGroupName = resourceGroup; cmdlet.KeyType = keyType.ToString(); cmdlet.ExecuteCmdlet(); commandRuntimeMock.Verify(r => r.WriteObject(expected), Times.Once()); }
/// <summary> /// Filters the subscription's account with the given tag. /// </summary> /// <param name="account">The account to filter on.</param> /// <param name="tag">The tag to filter on.</param> /// <returns>Whether or not the account's tags match with the given tag</returns> public static bool MatchesTag(BatchAccount account, Hashtable tag) { if (tag != null && tag.Count >= 1) { PSTagValuePair tagValuePair = TagsConversionHelper.Create(tag); if (tagValuePair == null) { throw new ArgumentException(Resources.InvalidTagFormat); } if (string.IsNullOrEmpty(tagValuePair.Value)) { return(ContainsTagWithName(account.Tags, tagValuePair.Name)); } else { return(ContainsTagWithName(account.Tags, tagValuePair.Name) && account.Tags[tagValuePair.Name] == tagValuePair.Value); } } return(true); }
/// <summary> /// Filters the subscription's account with the given tag. /// </summary> /// <param name="account">The account to filter on.</param> /// <param name="tag">The tag to filter on.</param> /// <returns>Whether or not the account's tags match with the given tag</returns> public static bool MatchesTag(BatchAccount account, Hashtable tag) { if (tag != null && tag.Count >= 1) { PSTagValuePair tagValuePair = TagsConversionHelper.Create(tag); if (tagValuePair == null) { throw new ArgumentException(Resources.InvalidTagFormat); } if (string.IsNullOrEmpty(tagValuePair.Value)) { return ContainsTagWithName(account.Tags, tagValuePair.Name); } else { return ContainsTagWithName(account.Tags, tagValuePair.Name) && account.Tags[tagValuePair.Name] == tagValuePair.Value; } } return true; }
public void CanCreateUserSubscriptionBatchAccount() { string accountName = "account01"; string resourceGroup = "resourceGroup"; string location = "location"; string keyVaultId = "subscriptions/0000/resourceGroups/resourceGroup/providers/Microsoft.KeyVault/vaults/myVault"; string keyVaultUrl = "https://myVault.vault.azure.com"; PoolAllocationMode allocationMode = PoolAllocationMode.UserSubscription; AccountCreateParameters actualCreateParameters = null; // Setup the mock client to return a fake response and capture the account create parameters BatchAccount accountResource = BatchTestHelpers.CreateAccountResource(accountName, resourceGroup, location); BatchAccountContext fakeResponse = BatchAccountContext.ConvertAccountResourceToNewAccountContext(accountResource, null); batchClientMock.Setup(b => b.CreateAccount(It.IsAny <AccountCreateParameters>())) .Returns(fakeResponse) .Callback((AccountCreateParameters p) => actualCreateParameters = p); // Setup and run the cmdlet cmdlet.AccountName = accountName; cmdlet.ResourceGroupName = resourceGroup; cmdlet.Location = location; cmdlet.PoolAllocationMode = allocationMode; cmdlet.KeyVaultId = keyVaultId; cmdlet.KeyVaultUrl = keyVaultUrl; cmdlet.ExecuteCmdlet(); // Verify the fake response was written to the pipeline and that the captured account create // parameters matched expectations. commandRuntimeMock.Verify(r => r.WriteObject(fakeResponse), Times.Once()); Assert.Equal(accountName, actualCreateParameters.BatchAccount); Assert.Equal(resourceGroup, actualCreateParameters.ResourceGroup); Assert.Equal(location, actualCreateParameters.Location); Assert.Equal(allocationMode, actualCreateParameters.PoolAllocationMode); Assert.Equal(keyVaultId, actualCreateParameters.KeyVaultId); Assert.Equal(keyVaultUrl, actualCreateParameters.KeyVaultUrl); }
public async Task BatchAccountEndToEndAsync() { using (MockContext context = StartMockContextAndInitializeClients(this.GetType().FullName)) { string resourceGroupName = TestUtilities.GenerateName(); string batchAccountName = TestUtilities.GenerateName(); ResourceGroup group = new ResourceGroup(this.Location); await this.ResourceManagementClient.ResourceGroups.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, group); try { // Check if the account exists var checkAvailabilityResult = await this.BatchManagementClient.Location.CheckNameAvailabilityAsync(this.Location, batchAccountName); Assert.True(checkAvailabilityResult.NameAvailable); // Create an account BatchAccountCreateParameters createParams = new BatchAccountCreateParameters(this.Location); await this.BatchManagementClient.BatchAccount.CreateAsync(resourceGroupName, batchAccountName, createParams); // Check if the account exists now checkAvailabilityResult = await this.BatchManagementClient.Location.CheckNameAvailabilityAsync(this.Location, batchAccountName); Assert.False(checkAvailabilityResult.NameAvailable); Assert.NotNull(checkAvailabilityResult.Message); Assert.NotNull(checkAvailabilityResult.Reason); // Get the account and verify some properties BatchAccount batchAccount = await this.BatchManagementClient.BatchAccount.GetAsync(resourceGroupName, batchAccountName); Assert.Equal(batchAccountName, batchAccount.Name); Assert.True(batchAccount.DedicatedCoreQuota > 0); Assert.True(batchAccount.LowPriorityCoreQuota > 0); // Rotate a key BatchAccountKeys originalKeys = await this.BatchManagementClient.BatchAccount.GetKeysAsync(resourceGroupName, batchAccountName); BatchAccountKeys newKeys = await this.BatchManagementClient.BatchAccount.RegenerateKeyAsync(resourceGroupName, batchAccountName, AccountKeyType.Primary); Assert.NotEqual(originalKeys.Primary, newKeys.Primary); Assert.Equal(originalKeys.Secondary, newKeys.Secondary); // List accounts under the resource group IPage <BatchAccount> listResponse = await this.BatchManagementClient.BatchAccount.ListByResourceGroupAsync(resourceGroupName); List <BatchAccount> accounts = new List <BatchAccount>(listResponse); string nextLink = listResponse.NextPageLink; while (nextLink != null) { listResponse = await this.BatchManagementClient.BatchAccount.ListByResourceGroupNextAsync(nextLink); accounts.AddRange(listResponse); nextLink = listResponse.NextPageLink; } Assert.Single(accounts); Assert.Equal(batchAccountName, accounts.First().Name); // Delete the account try { await this.BatchManagementClient.BatchAccount.DeleteAsync(resourceGroupName, batchAccountName); } catch (CloudException ex) { /* Account deletion is a long running operation. This .DeleteAsync() method will submit the account deletion request and * poll for the status of the long running operation until the account is deleted. Currently, querying for the operation * status after the account is deleted will return a 404 error, so we have to add this catch statement. This behavior will * be fixed in a future service release. */ if (ex.Response.StatusCode != HttpStatusCode.NotFound) { throw; } } // Verify account was deleted. A GET operation will return a 404 error and result in an exception try { await this.BatchManagementClient.BatchAccount.GetAsync(resourceGroupName, batchAccountName); } catch (CloudException ex) { Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode); } } finally { await this.ResourceManagementClient.ResourceGroups.DeleteWithHttpMessagesAsync(resourceGroupName); } } }
public Task UploadCertificateToBatchAccountAsync(Guid subscriptionId, BatchAccount batchAccount, X509Certificate2 certificate, string password) { throw new NotImplementedException(); }
/// <summary> /// Take an AccountResource and turn it into a BatchAccountContext /// </summary> /// <param name="resource">Resource info returned by RP</param> /// <returns>Void</returns> internal void ConvertAccountResourceToAccountContext(BatchAccount resource) { var accountEndpoint = resource.AccountEndpoint; if (Uri.CheckHostName(accountEndpoint) != UriHostNameType.Dns) { throw new ArgumentException(String.Format(Resources.InvalidEndpointType, accountEndpoint), "AccountEndpoint"); } this.Id = resource.Id; this.AccountEndpoint = accountEndpoint; this.Location = resource.Location; this.State = resource.ProvisioningState.ToString(); this.Tags = TagsConversionHelper.CreateTagHashtable(resource.Tags); this.CoreQuota = resource.CoreQuota; this.PoolQuota = resource.PoolQuota; this.ActiveJobAndJobScheduleQuota = resource.ActiveJobAndJobScheduleQuota; if (resource.AutoStorage != null) { this.AutoStorageProperties = new AutoStorageProperties() { StorageAccountId = resource.AutoStorage.StorageAccountId, LastKeySync = resource.AutoStorage.LastKeySync, }; } // extract the host and strip off the account name for the TaskTenantUrl and AccountName var hostParts = accountEndpoint.Split('.'); this.AccountName = hostParts[0]; this.TaskTenantUrl = Uri.UriSchemeHttps + Uri.SchemeDelimiter + accountEndpoint; // get remaining fields from Id which looks like: // /subscriptions/4a06fe24-c197-4353-adc1-058d1a51924e/resourceGroups/clwtest/providers/Microsoft.Batch/batchAccounts/clw var idParts = resource.Id.Split('/'); if (idParts.Length < 5) { throw new ArgumentException(String.Format(Resources.InvalidResourceId, resource.Id), "Id"); } this.Subscription = idParts[2]; this.ResourceGroupName = idParts[4]; }
/// <summary> /// Create a new BAC and fill it in /// </summary> /// <param name="resource">Resource info returned by RP</param> /// <returns>new instance of BatchAccountContext</returns> internal static BatchAccountContext ConvertAccountResourceToNewAccountContext(BatchAccount resource) { var baContext = new BatchAccountContext(); baContext.ConvertAccountResourceToAccountContext(resource); return baContext; }
/// <summary> /// Performs various Batch account operations using the Batch Management library. /// </summary> /// <param name="accessToken">The access token to use for authentication.</param> /// <param name="subscriptionId">The subscription id to use for creating the Batch management client</param> /// <param name="location">The location where the Batch account will be created.</param> /// <returns>A <see cref="System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</returns> private static async Task PerformBatchAccountOperationsAsync(string accessToken, string subscriptionId, string location) { using (BatchManagementClient batchManagementClient = new BatchManagementClient(new TokenCredentials(accessToken))) { batchManagementClient.SubscriptionId = subscriptionId; // Get the account quota for the subscription BatchLocationQuota quotaResponse = await batchManagementClient.Location.GetQuotasAsync(location); Console.WriteLine("Your subscription can create {0} account(s) in the {1} region.", quotaResponse.AccountQuota, location); Console.WriteLine(); // Create account string accountName = PromptUserForAccountName(); Console.WriteLine("Creating account {0}...", accountName); await batchManagementClient.BatchAccount.CreateAsync(ResourceGroupName, accountName, new BatchAccountCreateParameters() { Location = location }); Console.WriteLine("Account {0} created", accountName); Console.WriteLine(); // Get account Console.WriteLine("Getting account {0}...", accountName); BatchAccount account = await batchManagementClient.BatchAccount.GetAsync(ResourceGroupName, accountName); Console.WriteLine("Got account {0}:", account.Name); Console.WriteLine(" Account location: {0}", account.Location); Console.WriteLine(" Account resource type: {0}", account.Type); Console.WriteLine(" Account id: {0}", account.Id); Console.WriteLine(); // Print account quotas Console.WriteLine("Quotas for account {0}:", account.Name); Console.WriteLine(" Dedicated core quota: {0}", account.DedicatedCoreQuota); Console.WriteLine(" Low priority core quota: {0}", account.LowPriorityCoreQuota); Console.WriteLine(" Pool quota: {0}", account.PoolQuota); Console.WriteLine(" Active job and job schedule quota: {0}", account.ActiveJobAndJobScheduleQuota); Console.WriteLine(); // Get account keys Console.WriteLine("Getting account keys of account {0}...", account.Name); BatchAccountKeys accountKeys = await batchManagementClient.BatchAccount.GetKeysAsync(ResourceGroupName, account.Name); Console.WriteLine(" Primary key of account {0}: {1}", account.Name, accountKeys.Primary); Console.WriteLine(" Secondary key of account {0}: {1}", account.Name, accountKeys.Secondary); Console.WriteLine(); // Regenerate primary account key Console.WriteLine("Regenerating the primary key of account {0}...", account.Name); BatchAccountKeys newKeys = await batchManagementClient.BatchAccount.RegenerateKeyAsync( ResourceGroupName, account.Name, AccountKeyType.Primary); Console.WriteLine(" New primary key of account {0}: {1}", account.Name, newKeys.Primary); Console.WriteLine(" Secondary key of account {0}: {1}", account.Name, newKeys.Secondary); Console.WriteLine(); // List total number of accounts under the subscription id IPage <BatchAccount> listResponse = await batchManagementClient.BatchAccount.ListAsync(); var accounts = new List <BatchAccount>(); accounts.AddRange(listResponse); var nextLink = listResponse.NextPageLink; while (nextLink != null) { listResponse = await batchManagementClient.BatchAccount.ListNextAsync(nextLink); accounts.AddRange(listResponse); nextLink = listResponse.NextPageLink; } Console.WriteLine("Total number of Batch accounts under subscription id {0}: {1}", batchManagementClient.SubscriptionId, accounts.Count()); // Determine how many additional accounts can be created in the target region int numAccountsInRegion = accounts.Count(o => o.Location == account.Location); Console.WriteLine("Accounts in {0}: {1}", account.Location, numAccountsInRegion); Console.WriteLine("You can create {0} more accounts in the {1} region.", quotaResponse.AccountQuota - numAccountsInRegion, account.Location); Console.WriteLine(); // List accounts in the subscription Console.WriteLine("Listing all Batch accounts under subscription id {0}...", batchManagementClient.SubscriptionId); foreach (BatchAccount acct in accounts) { Console.WriteLine(" {0} - {1} | Location: {2}", accounts.IndexOf(acct) + 1, acct.Name, acct.Location); } Console.WriteLine(); // Delete account Console.Write("Hit ENTER to delete account {0}: ", account.Name); Console.ReadLine(); Console.WriteLine("Deleting account {0}...", account.Name); try { await batchManagementClient.BatchAccount.DeleteAsync(ResourceGroupName, account.Name); } catch (CloudException ex) { /* Account deletion is a long running operation. This .DeleteAsync() method will submit the account deletion request and * poll for the status of the long running operation until the account is deleted. Currently, querying for the operation * status after the account is deleted will return a 404 error, so we have to add this catch statement. This behavior will * be fixed in a future service release. */ if (ex.Response.StatusCode != HttpStatusCode.NotFound) { throw; } } Console.WriteLine("Account {0} deleted", account.Name); Console.WriteLine(); } }
public async Task <bool> DeployAsync() { var isDeploymentSuccessful = false; var mainTimer = Stopwatch.StartNew(); RefreshableConsole.WriteLine("Running..."); await ValidateTokenProviderAsync(); tokenCredentials = new TokenCredentials(new RefreshableAzureServiceTokenProvider("https://management.azure.com/")); azureCredentials = new AzureCredentials(tokenCredentials, null, null, AzureEnvironment.AzureGlobalCloud); azureClient = GetAzureClient(azureCredentials); await ValidateConfigurationAsync(); try { RefreshableConsole.WriteLine(); RefreshableConsole.WriteLine($"VM host: {configuration.VmName}.{configuration.RegionName}.cloudapp.azure.com"); RefreshableConsole.WriteLine($"VM username: {configuration.VmUsername}"); RefreshableConsole.WriteLine($"VM password: {configuration.VmPassword}"); RefreshableConsole.WriteLine(); await CreateResourceGroupAsync(); BatchAccount batchAccount = null; IGenericResource appInsights = null; ICosmosDBAccount cosmosDb = null; IStorageAccount storageAccount = null; IVirtualMachine linuxVm = null; ConnectionInfo sshConnectionInfo = null; await Task.WhenAll(new Task[] { Task.Run(async() => batchAccount = await CreateBatchAccountAsync(), cts.Token), Task.Run(async() => appInsights = await CreateAppInsightsResourceAsync(), cts.Token), Task.Run(async() => cosmosDb = await CreateCosmosDbAsync(), cts.Token), Task.Run(async() => storageAccount = await CreateStorageAccountAsync(), cts.Token), Task.Run(async() => { linuxVm = await CreateVirtualMachineAsync(); sshConnectionInfo = new ConnectionInfo(linuxVm.GetPrimaryPublicIPAddress().Fqdn, configuration.VmUsername, new PasswordAuthenticationMethod(configuration.VmUsername, configuration.VmPassword)); await WaitForSshConnectivityAsync(sshConnectionInfo); await ConfigureVmAsync(sshConnectionInfo); }, cts.Token) }); var vmManagedIdentity = linuxVm.SystemAssignedManagedServiceIdentityPrincipalId; await AssignVmAsBillingReaderToSubscriptionAsync(vmManagedIdentity); await AssignVmAsContributorToAppInsightsAsync(vmManagedIdentity, appInsights); await AssignVmAsContributorToCosmosDb(vmManagedIdentity, cosmosDb); await AssignVmAsContributorToBatchAccountAsync(vmManagedIdentity, batchAccount); await AssignVmAsContributorToStorageAccountAsync(vmManagedIdentity, storageAccount); await AssignVmAsDataReaderToStorageAccountAsync(vmManagedIdentity, storageAccount); await RestartVmAsync(linuxVm); await WaitForSshConnectivityAsync(sshConnectionInfo); await WaitForDockerComposeAsync(sshConnectionInfo); await WaitForCromwellAsync(sshConnectionInfo); isDeploymentSuccessful = await VerifyInstallationAsync(storageAccount); if (!isDeploymentSuccessful) { await DeleteResourceGroupIfUserConsentsAsync(); } } catch (Microsoft.Rest.Azure.CloudException cloudException) { var json = cloudException.Response.Content; RefreshableConsole.WriteLine(json); Debugger.Break(); WriteGeneralRetryMessageToConsole(); await DeleteResourceGroupIfUserConsentsAsync(); } catch (Exception exc) { RefreshableConsole.WriteLine(exc.ToString()); Debugger.Break(); WriteGeneralRetryMessageToConsole(); await DeleteResourceGroupIfUserConsentsAsync(); } RefreshableConsole.WriteLine($"Completed in {mainTimer.Elapsed.TotalMinutes:n1} minutes."); return(isDeploymentSuccessful); }
public async Task BatchAccountCanCreateWithBYOSEnabled() { using (MockContext context = StartMockContextAndInitializeClients(this.GetType().FullName)) { string resourceGroupName = TestUtilities.GenerateName(); string batchAccountName = TestUtilities.GenerateName(); string keyvaultName = TestUtilities.GenerateName(); ResourceGroup group = new ResourceGroup(this.Location); await this.ResourceManagementClient.ResourceGroups.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, group); try { //Register with keyvault just in case we haven't already await this.ResourceManagementClient.Providers.RegisterWithHttpMessagesAsync("Microsoft.KeyVault"); var result = await this.ResourceManagementClient.Resources.CreateOrUpdateWithHttpMessagesAsync( resourceGroupName : resourceGroupName, resourceProviderNamespace : "Microsoft.KeyVault", parentResourcePath : "", resourceType : "vaults", resourceName : keyvaultName, apiVersion : "2016-10-01", parameters : new GenericResource() { Location = this.Location, Properties = new Dictionary <string, object> { { "tenantId", "72f988bf-86f1-41af-91ab-2d7cd011db47" }, { "sku", new Dictionary <string, object> { { "family", "A" }, { "name", "standard" } } }, { "accessPolicies", new [] { new Dictionary <string, object> { { "objectId", "f520d84c-3fd3-4cc8-88d4-2ed25b00d27a" }, { "tenantId", "72f988bf-86f1-41af-91ab-2d7cd011db47" }, { "permissions", new Dictionary <string, object> { { "secrets", new [] { "All" } }, { "keys", new [] { "All" } }, } } } } }, { "enabledForDeployment", true }, { "enabledForTemplateDeployment", true }, { "enabledForDiskEncryption", true } } }); var keyVaultReferenceId = $"/subscriptions/{this.BatchManagementClient.SubscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.KeyVault/vaults/{keyvaultName}"; var keyVaultReferenceUrl = ((Newtonsoft.Json.Linq.JObject)result.Body.Properties)["vaultUri"].ToString(); // Create an account BatchAccountCreateParameters createParams = new BatchAccountCreateParameters( this.Location, poolAllocationMode: PoolAllocationMode.UserSubscription, keyVaultReference: new KeyVaultReference( keyVaultReferenceId, keyVaultReferenceUrl)); await this.BatchManagementClient.BatchAccount.CreateAsync(resourceGroupName, batchAccountName, createParams); // Get the account and verify some properties BatchAccount batchAccount = await this.BatchManagementClient.BatchAccount.GetAsync(resourceGroupName, batchAccountName); Assert.Equal(batchAccountName, batchAccount.Name); Assert.Null(batchAccount.DedicatedCoreQuota); Assert.Null(batchAccount.DedicatedCoreQuotaPerVMFamily); Assert.False(batchAccount.DedicatedCoreQuotaPerVMFamilyEnforced); // TODO: change this when pfq enforcement happens Assert.Null(batchAccount.LowPriorityCoreQuota); Assert.Equal(PoolAllocationMode.UserSubscription, batchAccount.PoolAllocationMode); Assert.Equal(keyVaultReferenceId, batchAccount.KeyVaultReference.Id); Assert.Equal(keyVaultReferenceUrl, batchAccount.KeyVaultReference.Url); } finally { await this.ResourceManagementClient.ResourceGroups.DeleteWithHttpMessagesAsync(resourceGroupName); } } }
/// <summary> /// Create a new BAC and fill it in /// </summary> /// <param name="resource">Resource info returned by RP</param> /// <param name="azureContext">The Azure Context</param> /// <returns>new instance of BatchAccountContext</returns> internal static BatchAccountContext ConvertAccountResourceToNewAccountContext(BatchAccount resource, IAzureContext azureContext) { var baContext = new BatchAccountContext(azureContext); baContext.ConvertAccountResourceToAccountContext(resource); return(baContext); }