// // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. // using System; using Microsoft.Azure.Management.Sql.Models; using Microsoft.SqlTools.ResourceProvider.Core; namespace Microsoft.SqlTools.ResourceProvider.DefaultImpl { /// /// Implementation for using VS services. /// Provides information about an Azure resource /// public class AzureResourceWrapper : IAzureResource { public const string ResourceGroupsPart = "resourceGroups"; private string resourceGroupName; /// /// Initializes the resource /// public AzureResourceWrapper(TrackedResource azureResource) { CommonUtil.CheckForNull(azureResource, nameof(azureResource)); AzureResource = azureResource; } /// /// Resource name /// public string Name { get { return AzureResource != null ? AzureResource.Name : string.Empty; } set { throw new NotSupportedException(); } } /// /// Resource type /// public string Type { get { return AzureResource != null ? AzureResource.Type : string.Empty; } set { throw new NotSupportedException(); } } /// /// Resource id /// public string Id { get { return AzureResource != null ? AzureResource.Id : string.Empty; } set { throw new NotSupportedException(); } } /// /// Resource Group Name /// public string ResourceGroupName { get { this.resourceGroupName ??= ParseResourceGroupNameFromId(); return this.resourceGroupName ?? string.Empty; } set { this.resourceGroupName = value; } } private string ParseResourceGroupNameFromId() { if (!string.IsNullOrEmpty(Id)) { string[] idParts = Id.Split('/'); // Look for the "resourceGroups" section and return the section after this, hence // always stop before idParts.Length - 1 for (int i = 0; i < idParts.Length - 1; i++) { if (string.Compare(idParts[i], ResourceGroupsPart, StringComparison.OrdinalIgnoreCase) == 0) { return idParts[i + 1]; } } } return null; } /// /// Resource Location /// public string Location { get { return AzureResource != null ? AzureResource.Location : string.Empty; } set { if (AzureResource != null) { AzureResource.Location = value; } } } /// /// The resource wrapped by this class /// protected TrackedResource AzureResource { get; set; } } }