public static bool TryParse(string resourceId, out AzureResourceIdentifier azureResourceIdentifier)
        {
            azureResourceIdentifier = null;

            if (string.IsNullOrEmpty(resourceId))
            {
                return(false);
            }

            resourceId = SanitizeResourceId(resourceId, out bool addedTrailingSlash);

            foreach (var expression in new Regex[] { ResourceExpression, ResourceGroupExpression, SubscriptionExpression })
            {
                var match = expression.Match(resourceId);

                if (match.Success && Guid.TryParse(match.Groups[1].Value, out Guid subscriptionId))
                {
                    if (expression == ResourceExpression)
                    {
                        azureResourceIdentifier = new AzureResourceIdentifier(subscriptionId, match.Groups[2].Value, match.Groups[3].Value, ParseResourceSegment(match.Groups[4].Value));
                    }
                    else if (expression == ResourceGroupExpression)
                    {
                        azureResourceIdentifier = new AzureResourceIdentifier(subscriptionId, match.Groups[2].Value);
                    }
                    else if (expression == SubscriptionExpression)
                    {
                        azureResourceIdentifier = new AzureResourceIdentifier(subscriptionId);
                    }

                    break;
                }
            }

            return(azureResourceIdentifier != null);
示例#2
0
        public static bool TryParse(string resourceId, bool allowUnnamedResource, out AzureResourceIdentifier azureResourceIdentifier)
        {
            azureResourceIdentifier = null;

            if (string.IsNullOrEmpty(resourceId))
            {
                return(false);
            }

            resourceId = SanitizeResourceId(resourceId, out bool addedTrailingSlash);

            foreach (var expression in new Regex[] { ResourceExpression, ResourceGroupExpression, SubscriptionExpression })
            {
                var match = expression.Match(resourceId);

                if (match.Success && Guid.TryParse(match.Groups[1].Value, out Guid subscriptionId))
                {
                    if (expression == ResourceExpression)
                    {
                        azureResourceIdentifier = new AzureResourceIdentifier(subscriptionId, match.Groups[2].Value, match.Groups[3].Value, ParseResourceSegment(match.Groups[4].Value));
                    }
                    else if (expression == ResourceGroupExpression)
                    {
                        azureResourceIdentifier = new AzureResourceIdentifier(subscriptionId, match.Groups[2].Value);
                    }
                    else if (expression == SubscriptionExpression)
                    {
                        azureResourceIdentifier = new AzureResourceIdentifier(subscriptionId);
                    }

                    break;
                }
            }

            return(azureResourceIdentifier != null);

            KeyValuePair <string, string>[] ParseResourceSegment(string resourceSegment)
            {
                var tokens = resourceSegment.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

                var segments = tokens
                               .Select((segment, index) => new { segment, index })
                               .GroupBy(x => x.index / 2, x => x.segment)
                               .Select(group => new KeyValuePair <string, string>(group.First(), group.Skip(1).LastOrDefault()));

                if (!allowUnnamedResource)
                {
                    var emptySegment = segments.FirstOrDefault(segment => string.IsNullOrEmpty(segment.Value));

                    if (!string.IsNullOrEmpty(emptySegment.Key))
                    {
                        throw new ArgumentException($"Missing name for resource '{emptySegment.Key}'", nameof(resourceId));
                    }
                }

                return(segments.ToArray());
            }
        }
示例#3
0
        public static bool IsAzureResourceId(this string resourceId)
        {
            if (resourceId is null)
            {
                throw new ArgumentNullException(nameof(resourceId));
            }

            return(AzureResourceIdentifier.TryParse(resourceId, out var _));
        }
示例#4
0
        protected AzureResource(string resourceId, IAzureResourceService azureResourceService)
        {
            if (resourceId is null)
            {
                throw new ArgumentNullException(nameof(resourceId));
            }

            ResourceId           = AzureResourceIdentifier.Parse(resourceId);
            AzureResourceService = azureResourceService ?? throw new ArgumentNullException(nameof(azureResourceService));
        }
示例#5
0
        internal AzureResource(string resourceId)
        {
            if (resourceId is null)
            {
                throw new ArgumentNullException(nameof(resourceId));
            }

            ResourceId           = AzureResourceIdentifier.Parse(resourceId);
            AzureResourceService = null;
        }
示例#6
0
        public static Task <IEnumerable <string> > GetApiVersionsAsync(this IAzureResourceService azureResourceService, AzureResourceIdentifier azureResourceIdentifier, bool includePreviewVersions = false)
        {
            if (azureResourceService is null)
            {
                throw new ArgumentNullException(nameof(azureResourceService));
            }

            if (azureResourceIdentifier is null)
            {
                throw new ArgumentNullException(nameof(azureResourceIdentifier));
            }

            if (string.IsNullOrEmpty(azureResourceIdentifier.ResourceNamespace))
            {
                return(azureResourceService.GetApiVersionsAsync(azureResourceIdentifier.SubscriptionId, "Microsoft.Resources", "resourceGroups", includePreviewVersions));
            }
            else
            {
                return(azureResourceService.GetApiVersionsAsync(azureResourceIdentifier.SubscriptionId, azureResourceIdentifier.ResourceNamespace, azureResourceIdentifier.ResourceTypeName, includePreviewVersions));
            }
        }
示例#7
0
 public static bool TryParse(string resourceId, out AzureResourceIdentifier azureResourceIdentifier)
 => TryParse(resourceId, false, out azureResourceIdentifier);