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);
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()); } }
public static bool IsAzureResourceId(this string resourceId) { if (resourceId is null) { throw new ArgumentNullException(nameof(resourceId)); } return(AzureResourceIdentifier.TryParse(resourceId, out var _)); }
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)); }
internal AzureResource(string resourceId) { if (resourceId is null) { throw new ArgumentNullException(nameof(resourceId)); } ResourceId = AzureResourceIdentifier.Parse(resourceId); AzureResourceService = null; }
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)); } }
public static bool TryParse(string resourceId, out AzureResourceIdentifier azureResourceIdentifier) => TryParse(resourceId, false, out azureResourceIdentifier);