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

Move generic resource from resources to core #20927

Merged
merged 15 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions sdk/core/Azure.Core/tests/TestClients/ArmOperationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.ResourceManager.Core;

namespace Azure.Core.Tests
{
public class ArmOperationTest : ArmOperation<TestResource>, IOperationSource<TestResource>
public class ArmOperationTest : Operation<TestResource>, IOperationSource<TestResource>
{
private TestResource _value;
private bool _exceptionOnWait;
Expand Down
3 changes: 1 addition & 2 deletions sdk/core/Azure.Core/tests/TestClients/PhArmOperationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.TestFramework;
using Azure.ResourceManager.Core;

namespace Azure.Core.Tests
{
public class PhArmOperationTest<T> : ArmOperation<T>
public class PhArmOperationTest<T> : Operation<T>
where T : class
{
private OperationOrResponseInternals<T> _operationHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.Pipeline;
using Azure.ResourceManager.Core;

namespace Azure.Core.Tests
{
Expand Down Expand Up @@ -82,7 +81,7 @@ public virtual Task<ArmResponseTest<TestResource>> GetArmResponseAsync(Cancellat
}
}

public virtual ArmOperation<TestResource> GetPhArmOperation(CancellationToken cancellationToken = default)
public virtual Operation<TestResource> GetPhArmOperation(CancellationToken cancellationToken = default)
{
using var scope = _diagnostic.CreateScope("TestResourceOperations.GetPhArmOperation");
scope.Start();
Expand All @@ -98,14 +97,14 @@ public virtual ArmOperation<TestResource> GetPhArmOperation(CancellationToken ca
}
}

public virtual Task<ArmOperation<TestResource>> GetPhArmOperationAsync(CancellationToken cancellationToken = default)
public virtual Task<Operation<TestResource>> GetPhArmOperationAsync(CancellationToken cancellationToken = default)
{
using var scope = _diagnostic.CreateScope("TestResourceOperations.GetPhArmOperation");
scope.Start();

try
{
return Task.FromResult<ArmOperation<TestResource>>(new PhArmOperationTest<TestResource>(new TestResource()));
return Task.FromResult<Operation<TestResource>>(new PhArmOperationTest<TestResource>(new TestResource()));
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.ResourceManager.Resources.Models;

namespace Azure.ResourceManager.Core.Adapters
{
/// <summary>
Expand All @@ -21,7 +19,7 @@ public abstract class TrackedResource<TIdentifier, TModel> : TrackedResource<TId
/// <param name="location"> The location of the resource. </param>
/// <param name="data"> The model to copy from. </param>
protected TrackedResource(TIdentifier id, LocationData location, TModel data)
:base(id, id?.Name, id?.ResourceType, null, location)
:base(id, id?.Name, id?.ResourceType, location, null)
{
Model = data;
}
Expand Down
31 changes: 11 additions & 20 deletions sdk/resourcemanager/Azure.ResourceManager.Core/src/ApiVersions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// Licensed under the MIT License.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Azure.ResourceManager.Resources;
using System.Reflection;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Resources.Models;

namespace Azure.ResourceManager.Core
Expand Down Expand Up @@ -44,8 +45,8 @@ internal void SetProviderClient(TokenCredential credential, Uri baseUri, string
_clientOptions.Convert<ResourcesManagementClientOptions>()).Providers;
}

private Dictionary<string, PropertyWrapper> _loadedResourceToApiVersions = new Dictionary<string, PropertyWrapper>();
private Dictionary<string, string> _nonLoadedResourceToApiVersion = new Dictionary<string, string>();
private ConcurrentDictionary<string, PropertyWrapper> _loadedResourceToApiVersions = new ConcurrentDictionary<string, PropertyWrapper>();
private ConcurrentDictionary<string, string> _nonLoadedResourceToApiVersion = new ConcurrentDictionary<string, string>();

private void BuildApiTable(ArmClientOptions clientOptions)
{
Expand All @@ -61,7 +62,7 @@ private void BuildApiTable(ArmClientOptions clientOptions)
if (prop.GetValue(apiObject) is ApiVersionsBase propVal)
{
var key = propVal.ResourceType;
_loadedResourceToApiVersions.Add(key.ToString(), new PropertyWrapper(prop, apiObject));
_loadedResourceToApiVersions.TryAdd(key.ToString(), new PropertyWrapper(prop, apiObject));
}
}
}
Expand Down Expand Up @@ -98,7 +99,7 @@ private string LoadApiVersion(ResourceType resourceType, CancellationToken cance
{
if (type.ResourceType.Equals(resourceType.Type))
{
_nonLoadedResourceToApiVersion.Add(resourceType.ToString(), type.ApiVersions[0]);
_nonLoadedResourceToApiVersion.TryAdd(resourceType.ToString(), type.ApiVersions[0]);
return type.ApiVersions[0];
}
}
Expand All @@ -120,7 +121,7 @@ private async Task<string> LoadApiVersionAsync(ResourceType resourceType, Cancel
{
if (type.ResourceType.Equals(resourceType.Type))
{
_nonLoadedResourceToApiVersion.Add(resourceType.ToString(), type.ApiVersions[0]);
_nonLoadedResourceToApiVersion.TryAdd(resourceType.ToString(), type.ApiVersions[0]);
return type.ApiVersions[0];
}
}
Expand Down Expand Up @@ -190,18 +191,8 @@ internal ApiVersions Clone()
{
ApiVersions copy = new ApiVersions(_clientOptions);
copy.ProviderOperations = ProviderOperations;

copy._loadedResourceToApiVersions = new Dictionary<string, PropertyWrapper>();
foreach (var property in _loadedResourceToApiVersions)
{
copy._loadedResourceToApiVersions.Add(property.Key, property.Value);
}

copy._nonLoadedResourceToApiVersion = new Dictionary<string, string>();
foreach (var property in _nonLoadedResourceToApiVersion)
{
copy._nonLoadedResourceToApiVersion.Add(property.Key, property.Value);
}
copy._loadedResourceToApiVersions = new ConcurrentDictionary<string, PropertyWrapper>(_loadedResourceToApiVersions);
copy._nonLoadedResourceToApiVersion = new ConcurrentDictionary<string, string>(_nonLoadedResourceToApiVersion);
return copy;
}
}
Expand Down
34 changes: 11 additions & 23 deletions sdk/resourcemanager/Azure.ResourceManager.Core/src/ArmClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected ArmClient()
/// <param name="options"> The client parameters to use in these operations. </param>
/// <exception cref="ArgumentNullException"> If <see cref="TokenCredential"/> is null. </exception>
public ArmClient(TokenCredential credential, ArmClientOptions options = default)
: this(null, null, credential, options)
: this(null, new Uri(DefaultUri), credential, options)
{
}

Expand All @@ -54,7 +54,7 @@ public ArmClient(
string defaultSubscriptionId,
TokenCredential credential,
ArmClientOptions options = default)
: this(defaultSubscriptionId, null, credential, options)
: this(defaultSubscriptionId, new Uri(DefaultUri), credential, options)
{
}

Expand Down Expand Up @@ -144,33 +144,21 @@ public ResourceGroupOperations GetResourceGroupOperations(ResourceGroupResourceI
return new ResourceGroupOperations(new SubscriptionOperations(new ClientContext(ClientOptions, Credential, BaseUri, Pipeline), id.SubscriptionId), id.ResourceGroupName);
}

/// <summary>
/// Gets resource operations base.
/// </summary>
/// <typeparam name="T"> The type of the underlying model this class wraps. </typeparam>
/// <param name="subscription"> The id of the Azure subscription. </param>
/// <param name="resourceGroup"> The resource group name. </param>
/// <param name="name"> The resource type name. </param>
/// <returns> Resource operations of the resource. </returns>
public virtual T GetResourceOperations<T>(string subscription, string resourceGroup, string name)
where T : OperationsBase
{
var subOp = new SubscriptionOperations(new ClientContext(ClientOptions, Credential, BaseUri, Pipeline), subscription);
var rgOp = subOp.GetResourceGroups().Get(resourceGroup);
return Activator.CreateInstance(
typeof(T),
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance,
null,
new object[] { rgOp, name },
CultureInfo.InvariantCulture) as T;
}

private Subscription GetDefaultSubscription()
{
var sub = GetSubscriptions().List().FirstOrDefault();
if (sub is null)
throw new Exception("No subscriptions found for the given credentials");
return sub;
}

/// <summary>
/// Gets a container representing all resources as generic objects in the current tenant.
/// </summary>
/// <returns> GenericResource container. </returns>
public GenericResourceOperations GetGenericResourcesOperations(TenantResourceIdentifier id)
{
return new GenericResourceOperations(new ClientContext(ClientOptions, Credential, BaseUri, Pipeline), id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,24 @@ public async virtual Task<TOperations> TryGetAsync(string resourceName, Cancella
/// Determines whether or not the azure resource exists in this container
/// </summary>
/// <param name="resourceName"> The name of the resource you want to check. </param>
/// <param name="cancellationToken"> A token to allow the caller to cancel the call to the service.
/// The default value is <see cref="CancellationToken.None" />. </param>
/// <returns> Whether or not the resource existed. </returns>
public virtual bool DoesExist(string resourceName, CancellationToken cancellationToken = default)
{
return TryGet(resourceName, cancellationToken) == null ? false : true;
}

/// <summary>
/// Determines whether or not the azure resource exists in this container
/// </summary>
/// <param name="resourceName"> The name of the resource you want to check. </param>
/// <param name="cancellationToken"> A token to allow the caller to cancel the call to the service.
/// The default value is <see cref="CancellationToken.None" />. </param>
/// <returns> Whether or not the resource existed. </returns>
public virtual bool DoesExist(string resourceName)
public virtual async Task<bool> DoesExistAsync(string resourceName, CancellationToken cancellationToken = default)
{
return TryGet(resourceName) == null ? false : true;
return await TryGetAsync(resourceName, cancellationToken).ConfigureAwait(false) == null ? false : true;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected override void Validate(ResourceIdentifier identifier)
/// <param name="resourceDetails"> The properties of the extension resource. </param>
/// <param name="cancellationToken"> A token to allow the caller to cancel the call to the service. The default value is <see cref="CancellationToken.None" />. </param>
/// <returns> An Http envelope containing the operations for the given extension. </returns>
public abstract ArmResponse<TOperations> Create(string name, TInput resourceDetails, CancellationToken cancellationToken = default);
public abstract Response<TOperations> Create(string name, TInput resourceDetails, CancellationToken cancellationToken = default);

/// <summary>
/// Create a new extension resource at the given scope without blocking the current thread.
Expand All @@ -60,7 +60,7 @@ protected override void Validate(ResourceIdentifier identifier)
/// <param name="resourceDetails">The properties of the extension resource. </param>
/// <param name="cancellationToken"> A token to allow the caller to cancel the call to the service. The default value is <see cref="CancellationToken.None" />. </param>
/// <returns>A Task that creates the extension resource. </returns>
public abstract Task<ArmResponse<TOperations>> CreateAsync(string name, TInput resourceDetails, CancellationToken cancellationToken = default);
public abstract Task<Response<TOperations>> CreateAsync(string name, TInput resourceDetails, CancellationToken cancellationToken = default);

/// <summary>
/// Begin Creation of a new extension resource. Block until the creation is accepted by the service.
Expand All @@ -69,8 +69,8 @@ protected override void Validate(ResourceIdentifier identifier)
/// <param name="name"> The name of the created extension resource. </param>
/// <param name="resourceDetails"> The properties of the extension resource. </param>
/// <param name="cancellationToken"> A token to allow the caller to cancel the call to the service. The default value is <see cref="CancellationToken.None" />. </param>
/// <returns> An instance of <see cref="ArmOperation{TOperation}"/>, allowing fine grained control over waiting for creation to complete. </returns>
public abstract ArmOperation<TOperations> StartCreate(string name, TInput resourceDetails, CancellationToken cancellationToken = default);
/// <returns> An instance of <see cref="Operation{TOperation}"/>, allowing fine grained control over waiting for creation to complete. </returns>
public abstract Operation<TOperations> StartCreate(string name, TInput resourceDetails, CancellationToken cancellationToken = default);

/// <summary>
/// Begin Creation of a new extension resource in a background task.
Expand All @@ -79,8 +79,8 @@ protected override void Validate(ResourceIdentifier identifier)
/// <param name="name"> The name of the created extension resource. </param>
/// <param name="resourceDetails"> The properties of the extension resource. </param>
/// <param name="cancellationToken"> A token to allow the caller to cancel the call to the service. The default value is <see cref="CancellationToken.None" />. </param>
/// <returns> A <see cref="Task"/> that on completion returns an <see cref="ArmOperation{TOperations}"/> that allows polling for completion of the operation. </returns>
public abstract Task<ArmOperation<TOperations>> StartCreateAsync(string name, TInput resourceDetails, CancellationToken cancellationToken = default);
/// <returns> A <see cref="Task"/> that on completion returns an <see cref="Operation{TOperations}"/> that allows polling for completion of the operation. </returns>
public abstract Task<Operation<TOperations>> StartCreateAsync(string name, TInput resourceDetails, CancellationToken cancellationToken = default);

/// <summary>
/// Lists the extension resources at the current scope. Blocks until the first page of results is returned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ protected ExtensionResourceOperationsBase(OperationsBase genericOperations, Reso
/// </summary>
/// <param name="cancellationToken"> A token allowing cancellation of the Http call in the task. </param>
/// <returns> An Http Response containing details and operations for the extension resource. </returns>
public abstract ArmResponse<TOperations> Get(CancellationToken cancellationToken = default);
public abstract Response<TOperations> Get(CancellationToken cancellationToken = default);

/// <summary>
/// Get details and operations for this extension resource. This call returns a Task that completes when the details are returned from the service.
/// </summary>
/// <param name="cancellationToken"> A token allowing cancellation of the Http call in the task. </param>
/// <returns> A Task that retrieves the resource details. When complete, the task will yield an Http Response
/// containing details and operations for the extension resource. </returns>
public abstract Task<ArmResponse<TOperations>> GetAsync(CancellationToken cancellationToken = default);
public abstract Task<Response<TOperations>> GetAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Get details for this resource from the service or can be overriden to provide a cached instance.
/// </summary>
/// <returns> A <see cref="ArmResponse{TOperations}"/> operation for this resource. </returns>
/// <returns> A <see cref="Response{TOperations}"/> operation for this resource. </returns>
protected virtual TOperations GetResource(CancellationToken cancellationToken = default)
{
return Get(cancellationToken).Value;
Expand All @@ -86,7 +86,7 @@ protected virtual TOperations GetResource(CancellationToken cancellationToken =
/// Get details for this resource from the service or can be overriden to provide a cached instance.
/// </summary>
/// <param name="cancellationToken"> A token allowing cancellation of the Http call in the task. </param>
/// <returns> A <see cref="Task"/> that on completion returns a <see cref="ArmResponse{TOperations}"/> operation for this resource. </returns>
/// <returns> A <see cref="Task"/> that on completion returns a <see cref="Response{TOperations}"/> operation for this resource. </returns>
protected virtual async Task<TOperations> GetResourceAsync(CancellationToken cancellationToken = default)
{
return (await GetAsync(cancellationToken).ConfigureAwait(false)).Value;
Expand Down
Loading