Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource Identifier changes #19636

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ namespace Azure.ResourceManager.Core
/// A class representing a generic proxy resource in Azure
/// </summary>
/// <typeparam name="TModel"> The type of the underlying model this class wraps </typeparam>
public abstract class ProxyResource<TModel> : Resource
where TModel : class
/// <typeparam name="TIdentifier"> The type of the underlying modelresource Id </typeparam>
public abstract class ProxyResource<TIdentifier, TModel> : Resource<TIdentifier>
where TModel : class
where TIdentifier : TenantResourceIdentifier
{
/// <summary>
/// Initializes a new instance of the <see cref="ProxyResource{TModel}"/> class.
/// Initializes a new instance of the <see cref="ProxyResource{TModel, TIdentifier}"/> class.
/// </summary>
/// <param name="id"> The identifier of the resource that is the target of operations. </param>
/// <param name="data"> The model to copy from. </param>
protected ProxyResource(ResourceIdentifier id, TModel data)
protected ProxyResource(TIdentifier id, TModel data)
{
Id = id;
Model = data;
}

/// <summary>
/// Initializes a new instance of the <see cref="ProxyResource{TModel}"/> class.
/// Initializes a new instance of the <see cref="ProxyResource{TModel, TIdentifier}"/> class.
/// </summary>
/// <param name="id"> The identifier of the resource that is the target of operations. </param>
/// <param name="data"> The model to copy from. </param>
Expand All @@ -34,7 +36,7 @@ protected ProxyResource(string id, TModel data)
}
else
{
Id = id;
Id = ResourceIdentifier.Create(id) as TIdentifier;
}

Model = data;
Expand All @@ -43,18 +45,18 @@ protected ProxyResource(string id, TModel data)
/// <summary>
/// Gets or sets the identifier for the resource.
/// </summary>
public override ResourceIdentifier Id { get; protected set; }
public override TIdentifier Id { get; protected set; }

/// <summary>
/// Gets or sets the Model this resource is based off.
/// </summary>
public virtual TModel Model { get; set; }

/// <summary>
/// Converts from a <see cref="ProxyResource{TModel}"/> into the TModel.
/// Converts from a <see cref="ProxyResource{TModel, TIdentifier}"/> into the TModel.
/// </summary>
/// <param name="other"> The tracked resource convert from. </param>
public static implicit operator TModel(ProxyResource<TModel> other)
public static implicit operator TModel(ProxyResource<TIdentifier, TModel> other)
{
return other.Model;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,27 @@ namespace Azure.ResourceManager.Core
/// <summary>
/// A class representing a generic tracked resource in Azure.
/// </summary>
/// <typeparam name="TIdentifier"> The type of the underlying resource id </typeparam>
/// <typeparam name="TModel"> The type of the underlying model this class wraps </typeparam>
public abstract class TrackedResource<TModel> : TrackedResource
public abstract class TrackedResource<TIdentifier, TModel> : TrackedResource<TIdentifier>
where TIdentifier : TenantResourceIdentifier
where TModel : class
{
/// <summary>
/// Initializes a new instance of the <see cref="TrackedResource{TModel}"/> class.
/// Initializes a new instance of the <see cref="TrackedResource{TModel, TIdentifier}"/> class.
/// </summary>
/// <param name="id"> The identifier of the resource that is the target of operations. </param>
/// <param name="location"> The location of the resource. </param>
/// <param name="data"> The model to copy from. </param>
protected TrackedResource(ResourceIdentifier id, LocationData location, TModel data)
protected TrackedResource(TIdentifier id, LocationData location, TModel data)
{
Id = id;
Location = location;
Model = data;
}

/// <summary>
/// Initializes a new instance of the <see cref="TrackedResource{TModel}"/> class.
/// Initializes a new instance of the <see cref="TrackedResource{TModel, TIdentifier}"/> class.
/// </summary>
/// <param name="id"> The identifier of the resource that is the target of operations. </param>
/// <param name="location"> The location of the resource. </param>
Expand All @@ -39,7 +41,7 @@ protected TrackedResource(string id, LocationData location, TModel data)
}
else
{
Id = id;
Id = ResourceIdentifier.Create(id) as TIdentifier;
}

Location = location;
Expand All @@ -55,7 +57,7 @@ protected TrackedResource(string id, LocationData location, TModel data)
/// Converts from a <see cref="TrackedResource{TModel}"/> into the TModel.
/// </summary>
/// <param name="other"> The tracked resource convert from. </param>
public static implicit operator TModel(TrackedResource<TModel> other)
public static implicit operator TModel(TrackedResource<TIdentifier, TModel> other)
{
return other.Model;
}
Expand Down
14 changes: 8 additions & 6 deletions sdk/resourcemanager/Azure.ResourceManager.Core/src/ArmBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ namespace Azure.ResourceManager.Core
/// <summary>
/// A class representing a builder object used to create Azure resources.
/// </summary>
/// <typeparam name="TIdentifier"> The type of the Identifier class for a specific resource. </typeparam>
/// <typeparam name="TOperations"> The type of the operations class for a specific resource. </typeparam>
/// <typeparam name="TResource"> The type of the class containing properties for the underlying resource. </typeparam>
public class ArmBuilder<TOperations, TResource>
where TResource : Resource
where TOperations : ResourceOperationsBase<TOperations>
public class ArmBuilder<TIdentifier, TOperations, TResource>
where TIdentifier: TenantResourceIdentifier
where TResource : Resource<TIdentifier>
where TOperations : ResourceOperationsBase<TIdentifier, TOperations>
{
/// <summary>
/// Initializes a new instance of the <see cref="ArmBuilder{TOperations, TResource}"/> class.
/// Initializes a new instance of the <see cref="ArmBuilder{TIdentifier, TOperations, TResource}"/> class.
/// </summary>
/// <param name="container"> The container object to create the resource in. </param>
/// <param name="resource"> The resource to create. </param>
public ArmBuilder(ResourceContainerBase<TOperations, TResource> container, TResource resource)
public ArmBuilder(ResourceContainerBase<TIdentifier, TOperations, TResource> container, TResource resource)
{
Resource = resource;
UnTypedContainer = container;
Expand All @@ -40,7 +42,7 @@ public ArmBuilder(ResourceContainerBase<TOperations, TResource> container, TReso
/// <summary>
/// Gets the container object to create the resource in.
/// </summary>
protected ResourceContainerBase<TOperations, TResource> UnTypedContainer { get; private set; }
protected ResourceContainerBase<TIdentifier, TOperations, TResource> UnTypedContainer { get; private set; }

/// <summary>
/// Creates the resource object to send to the Azure API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private AzureResourceManagerClient(
DefaultSubscription = string.IsNullOrWhiteSpace(defaultSubscriptionId)
? GetDefaultSubscription()
: GetSubscriptionOperations(defaultSubscriptionId).Get().Value;
_clientOptions.ApiVersions.SetProviderClient(credential, baseUri, DefaultSubscription.Id.Subscription);
_clientOptions.ApiVersions.SetProviderClient(credential, baseUri, DefaultSubscription.Id.SubscriptionId);
}

/// <summary>
Expand Down Expand Up @@ -145,9 +145,9 @@ public virtual ResourceGroupOperations GetResourceGroupOperations(string subscri
/// </summary>
/// <param name="resourceGroupId"> The resource identifier of the resource group. </param>
/// <returns> Resource group operations. </returns>
public virtual ResourceGroupOperations GetResourceGroupOperations(ResourceIdentifier resourceGroupId)
public virtual ResourceGroupOperations GetResourceGroupOperations(ResourceGroupResourceIdentifier resourceGroupId)
{
return GetSubscriptionOperations(resourceGroupId.Subscription).GetResourceGroupOperations(resourceGroupId.ResourceGroup);
return GetSubscriptionOperations(resourceGroupId.SubscriptionId).GetResourceGroupOperations(resourceGroupId.ResourceGroupName);
markcowl marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,31 @@ namespace Azure.ResourceManager.Core
/// Base class representing collection of resources.
/// </summary>
/// <typeparam name="TOperations"> The type of the class containing operations for the underlying resource. </typeparam>
public abstract class ContainerBase<TOperations> : OperationsBase
where TOperations : ResourceOperationsBase<TOperations>
/// <typeparam name="TIdentifier"> The type of the resource identifier. </typeparam>
public abstract class ContainerBase<TIdentifier, TOperations> : OperationsBase
where TOperations : ResourceOperationsBase<TIdentifier, TOperations> where TIdentifier : ResourceIdentifier
{
/// <summary>
/// Initializes a new instance of the <see cref="ContainerBase{TOperations}"/> class for mocking.
/// Initializes a new instance of the <see cref="ContainerBase{TIdentifier, TOperations}"/> class for mocking.
/// </summary>
protected ContainerBase()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ContainerBase{TOperations}"/> class.
/// Initializes a new instance of the <see cref="ContainerBase{TIdentifier, TOperations}"/> class.
/// </summary>
/// <param name="options"> The client parameters to use in these operations. </param>
/// <param name="id"> The identifier of the resource that is the target of operations. </param>
/// <param name="credential"> A credential used to authenticate to an Azure Service. </param>
/// <param name="baseUri"> The base URI of the service. </param>
protected ContainerBase(AzureResourceManagerClientOptions options, ResourceIdentifier id, TokenCredential credential, Uri baseUri)
protected ContainerBase(AzureResourceManagerClientOptions options, TIdentifier id, TokenCredential credential, Uri baseUri)
: base(options, id, credential, baseUri)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ContainerBase{TOperations}"/> class.
/// Initializes a new instance of the <see cref="ContainerBase{TOperations, TIdentifier}"/> class.
/// </summary>
/// <param name="parent"> The resource representing the parent resource. </param>
protected ContainerBase(ResourceOperationsBase parent)
Expand Down Expand Up @@ -104,15 +105,15 @@ public virtual bool DoesExist(string resourceName)
/// Get an instance of the operations this container holds.
/// </summary>
/// <param name="resourceName"> The name of the resource to scope the operations to. </param>
/// <returns> An instance of <see cref="ResourceContainerBase{TOperations, TResource}"/>. </returns>
protected virtual ResourceOperationsBase<TOperations> GetOperation(string resourceName)
/// <returns> An instance of <see cref="ResourceContainerBase{TIdentifier, TOperations, TResource}"/>. </returns>
protected virtual ResourceOperationsBase<TIdentifier, TOperations> GetOperation(string resourceName)
{
return Activator.CreateInstance(
typeof(TOperations).BaseType,
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance,
null,
new object[] { Parent, resourceName },
CultureInfo.InvariantCulture) as ResourceOperationsBase<TOperations>;
CultureInfo.InvariantCulture) as ResourceOperationsBase<TIdentifier, TOperations>;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Azure.ResourceManager.Core
/// <summary>
/// A class representing the generic azure resource data model.
/// </summary>
public class GenericResourceData : TrackedResource<ResourceManager.Resources.Models.GenericResource>
public class GenericResourceData : TrackedResource<TenantResourceIdentifier, ResourceManager.Resources.Models.GenericResource>
markcowl marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Initializes a new instance of the <see cref="GenericResourceData"/> class.
Expand Down Expand Up @@ -41,7 +41,7 @@ public GenericResourceData(ResourceManager.Resources.Models.GenericResource gene
/// Initializes a new instance of the <see cref="GenericResourceData"/> class.
/// </summary>
/// <param name="id"> The identifier of the resource that is the target of operations. </param>
public GenericResourceData(ResourceIdentifier id)
public GenericResourceData(TenantResourceIdentifier id)
: base(id, LocationData.Default, null)
{
}
Expand All @@ -51,13 +51,13 @@ public GenericResourceData(ResourceIdentifier id)
/// </summary>
/// <param name="id"> The identifier of the resource that is the target of operations. </param>
/// <param name="location"> The location of the resource. </param>
public GenericResourceData(ResourceIdentifier id, LocationData location)
public GenericResourceData(TenantResourceIdentifier id, LocationData location)
: base(id, location, null)
{
}

/// <inheritdoc/>
public override ResourceIdentifier Id { get; protected set; }
public override TenantResourceIdentifier Id { get; protected set; }

/// <summary>
/// Gets or sets who this resource is managed by.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,38 @@ namespace Azure.ResourceManager.Core
/// <summary>
/// A class representing the operations that can be performed over a specific ArmResource.
/// </summary>
public class GenericResourceOperations : ResourceOperationsBase<GenericResource>, ITaggableResource<GenericResource>, IDeletableResource
public class GenericResourceOperations : ResourceOperationsBase<TenantResourceIdentifier, GenericResource>, ITaggableResource<TenantResourceIdentifier, GenericResource>, IDeletableResource
{
/// <summary>
/// Initializes a new instance of the <see cref="GenericResourceOperations"/> class.
/// </summary>
/// <param name="operations"> The resource operations to copy the options from. </param>
/// <param name="id"> The identifier of the resource that is the target of operations. </param>
internal GenericResourceOperations(ResourceOperationsBase operations, ResourceIdentifier id)
internal GenericResourceOperations(ResourceOperationsBase operations, TenantResourceIdentifier id)
: base(operations, id)
{
}

/// <inheritdoc/>
protected override ResourceType ValidResourceType => ResourceGroupOperations.ResourceType;

private ResourcesOperations Operations => new ResourcesManagementClient(
BaseUri,
Id.Subscription,
Credential,
ClientOptions.Convert<ResourcesManagementClientOptions>()).Resources;
private ResourcesOperations Operations
{
get
{
string subscription;
if (!Id.TryGetSubscriptionId(out subscription))
{
subscription = Guid.Empty.ToString();
}

return new ResourcesManagementClient(
BaseUri,
subscription,
markcowl marked this conversation as resolved.
Show resolved Hide resolved
Credential,
ClientOptions.Convert<ResourcesManagementClientOptions>()).Resources;
}
}

/// <summary>
/// Delete the resource.
Expand Down Expand Up @@ -242,7 +254,7 @@ await op.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false),

private string GetApiVersion(CancellationToken cancellationToken)
{
string version = ClientOptions.ApiVersions.TryGetApiVersion(Id.Type, cancellationToken);
string version = ClientOptions.ApiVersions.TryGetApiVersion(Id.ResourceType, cancellationToken);
if (version is null)
{
throw new InvalidOperationException($"An invalid resouce id was given {Id}");
Expand All @@ -251,7 +263,7 @@ private string GetApiVersion(CancellationToken cancellationToken)
}
private async Task<string> GetApiVersionAsync(CancellationToken cancellationToken)
{
string version = await ClientOptions.ApiVersions.TryGetApiVersionAsync(Id.Type, cancellationToken).ConfigureAwait(false);
string version = await ClientOptions.ApiVersions.TryGetApiVersionAsync(Id.ResourceType, cancellationToken).ConfigureAwait(false);
if (version is null)
{
throw new InvalidOperationException($"An invalid resouce id was given {Id}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ namespace Azure.ResourceManager.Core
/// <summary>
/// Interface for operations that allow manipulating resource tags.
/// </summary>
/// <typeparam name="TIdentifier"> The identifier type for this resource. </typeparam>
/// <typeparam name="TOperations"> The typed operations for a specific resource. </typeparam>
public interface ITaggableResource<TOperations>
where TOperations : ResourceOperationsBase<TOperations>
public interface ITaggableResource<TIdentifier, TOperations>
where TOperations : ResourceOperationsBase<TIdentifier, TOperations>
where TIdentifier : TenantResourceIdentifier
{
/// <summary>
/// Add a tag to the resource.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,28 @@ protected OperationsBase(AzureResourceManagerClientOptions options, ResourceIden
/// <summary>
/// Gets the resource client.
/// </summary>
protected ResourcesManagementClient ResourcesClient => new ResourcesManagementClient(
BaseUri,
Id.Subscription,
Credential,
ClientOptions.Convert<ResourcesManagementClientOptions>());
protected ResourcesManagementClient ResourcesClient
{
get
{
string subscription;
markcowl marked this conversation as resolved.
Show resolved Hide resolved
if (!Id.TryGetSubscriptionId(out subscription))
{
subscription = Guid.Empty.ToString();
}

return new ResourcesManagementClient(BaseUri, subscription, Credential, ClientOptions.Convert<ResourcesManagementClientOptions>());
}
}

/// <summary>
/// Validate the resource identifier against current operations.
/// </summary>
/// <param name="identifier"> The resource identifier. </param>
protected virtual void Validate(ResourceIdentifier identifier)
{
if (identifier?.Type != ValidResourceType)
throw new ArgumentException($"Invalid resource type {identifier?.Type} expected {ValidResourceType}");
if (identifier?.ResourceType != ValidResourceType)
throw new ArgumentException($"Invalid resource type {identifier?.ResourceType} expected {ValidResourceType}");
}
}
}
Loading