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";

            AccountResource resource = new AccountResource() 
            { 
                Id = string.Format("id/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Batch/batchAccounts/abc", subscription, resourceGroup), 
                Location = "location", 
                Properties = new AccountProperties() { AccountEndpoint = endpoint, ProvisioningState = AccountProvisioningState.Succeeded },
                Type = "type"
            };
            BatchAccountContext context = BatchAccountContext.ConvertAccountResourceToNewAccountContext(resource);

            Assert.Equal<string>(context.Id, resource.Id);
            Assert.Equal<string>(context.AccountEndpoint, resource.Properties.AccountEndpoint);
            Assert.Equal<string>(context.Location, resource.Location);
            Assert.Equal<string>(context.State, resource.Properties.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);
        }
        /// <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(AccountResource resource)
        {
            var accountEndpoint = resource.Properties.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.Properties.ProvisioningState.ToString();
            this.Tags = Helpers.CreateTagHashtable(resource.Tags);

            // 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 + String.Join(".", hostParts, 1, hostParts.Length - 1);

            // 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];
        }
        public static AccountResource CreateAccountResource(string accountName, string resourceGroupName, Hashtable[] tags = null)
        {
            string tenantUrlEnding = "batch-test.windows-int.net";
            string endpoint = string.Format("{0}.{1}", accountName, tenantUrlEnding);
            string subscription = "00000000-0000-0000-0000-000000000000";
            string resourceGroup = resourceGroupName;

            AccountResource resource = new AccountResource()
            {
                Id = string.Format("id/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Batch/batchAccounts/abc", subscription, resourceGroup),
                Location = "location",
                Properties = new AccountProperties() { AccountEndpoint = endpoint, ProvisioningState = AccountProvisioningState.Succeeded },
                Type = "type"
            };
            if (tags != null)
            {
                resource.Tags = Microsoft.Azure.Commands.Batch.Helpers.CreateTagDictionary(tags, true);
            }
            return resource;
        }
Example #4
0
        /// <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(AccountResource 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>
 /// 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(AccountResource resource)
 {
     var baContext = new BatchAccountContext();
     baContext.ConvertAccountResourceToAccountContext(resource);
     return baContext;
 }