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

adding singleton pattern in Arm.Core and Proto #20591

Merged
merged 12 commits into from
May 4, 2021
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace Azure.ResourceManager.Core
{
/// <summary>
Expand All @@ -18,14 +20,19 @@ protected SingletonOperationsBase()
/// <summary>
/// Initializes a new instance of the <see cref="SingletonOperationsBase"/> class.
/// </summary>
/// <param name="clientContext"></param>
/// <param name="id"></param>
internal SingletonOperationsBase(ClientContext clientContext, ResourceIdentifier id)
: base(clientContext, ResourceIdentifier.RootResourceIdentifier)
/// <param name="parent"></param>
internal SingletonOperationsBase(OperationsBase parent)
: base(new ClientContext(parent.ClientOptions, parent.Credential, parent.BaseUri, parent.Pipeline), ResourceIdentifier.RootResourceIdentifier)
{
ParentId = id;
Parent = parent;
ParentId = parent.Id;
}

/// <summary>
/// Gets the parent resource of this resource.
/// </summary>
protected OperationsBase Parent { get; set; }
allenjzhang marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// The typed resource identifier for the underlying resource
/// </summary>
Expand Down Expand Up @@ -56,19 +63,16 @@ protected SingletonOperationsBase()
/// Initializes a new instance of the <see cref="ContainerBase{TOperations, TIdentifier}"/> class.
/// </summary>
/// <param name="parent"> The resource representing the parent resource. </param>
/// <param name="parentId"> The identifier of the resource that is the target of operations. </param>
protected SingletonOperationsBase(OperationsBase parent, TIdentifier parentId)
: base(new ClientContext(parent.ClientOptions, parent.Credential, parent.BaseUri, parent.Pipeline), parentId)
protected SingletonOperationsBase(OperationsBase parent)
: base(parent)
{
Parent = parent;
ParentId = parentId;
ParentId = parent.Id as TIdentifier;
if (string.IsNullOrWhiteSpace(ParentId))
{
throw new InvalidOperationException();
}
}

/// <summary>
/// Gets the parent resource of this resource.
/// </summary>
protected OperationsBase Parent { get; }

/// <summary>
/// The typed resource identifier for the underlying resource
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,14 @@
using System.Linq;
using Azure.ResourceManager.Compute.Models;

namespace Proto.Compute.Convenience
namespace Proto.Compute
{
/// <summary>
/// A class representing a builder object to help create a virtual machine.
/// </summary>
public class VirtualMachineScaleSetModelBuilder : VirtualMachineScaleSetModelBuilderBase
public partial class VirtualMachineScaleSetBuilder
{
private VirtualMachineScaleSetData _model;

/// <summary>
/// Initializes a new instance of the <see cref="VirtualMachineModelBuilder"/> class.
/// </summary>
/// <param name="containerOperations"> The container to create the virtual machine in. </param>
/// <param name="model"> The data model representing the virtual machine to create. </param>
public VirtualMachineScaleSetModelBuilder(VirtualMachineScaleSetContainer containerOperations, VirtualMachineScaleSetData model)
: base(containerOperations, model)
{
// TODO: GENERATOR Update Builder after models are incorporated in generated models
_model = model;
}

public override VirtualMachineScaleSetModelBuilderBase WithUseWindowsImage(string computerNamePrefix, string adminUser, string password)
public VirtualMachineScaleSetBuilder WithUseWindowsImage(string computerNamePrefix, string adminUser, string password)
{
_model.VirtualMachineProfile.OsProfile = new VirtualMachineScaleSetOSProfile()
{
Expand All @@ -38,7 +24,7 @@ public override VirtualMachineScaleSetModelBuilderBase WithUseWindowsImage(strin
return this;
}

public override VirtualMachineScaleSetModelBuilderBase WithUseLinuxImage(string computerNamePrefix, string adminUser, string password)
public VirtualMachineScaleSetBuilder WithUseLinuxImage(string computerNamePrefix, string adminUser, string password)
{
_model.VirtualMachineProfile.OsProfile = new VirtualMachineScaleSetOSProfile()
{
Expand All @@ -55,7 +41,7 @@ public override VirtualMachineScaleSetModelBuilderBase WithUseLinuxImage(string
return this;
}

public override VirtualMachineScaleSetModelBuilderBase WithRequiredPrimaryNetworkInterface(
public VirtualMachineScaleSetBuilder WithRequiredPrimaryNetworkInterface(
string name,
ResourceIdentifier subNetResourceId,
ICollection<ResourceIdentifier> backendAddressPoolResourceIds,
Expand Down Expand Up @@ -87,7 +73,7 @@ public override VirtualMachineScaleSetModelBuilderBase WithRequiredPrimaryNetwor
return this;
}

public override VirtualMachineScaleSetModelBuilderBase WithRequiredLoadBalancer(ResourceIdentifier asetResourceId)
public VirtualMachineScaleSetBuilder WithRequiredLoadBalancer(ResourceIdentifier asetResourceId)
{
return this;
}
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion sdk/resourcemanager/Proto.Client/compute/RollingUpgrade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class RollingUpgrade : RollingUpgradeOperations
/// <param name="operations"> The client parameters to use in these operations. </param>
/// <param name="resource"> The resource that is the target of operations. </param>
internal RollingUpgrade(OperationsBase operations, RollingUpgradeStatusInfo resource)
: base(operations, operations.Id as ResourceGroupResourceIdentifier)
: base(operations)
{
Data = resource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ protected RollingUpgradeOperations()
/// </summary>
/// <param name="operation"> The client parameters to use in these operations. </param>
/// <param name="id"> The identifier of the resource that is the target of operations. </param>
protected internal RollingUpgradeOperations(OperationsBase operation, ResourceGroupResourceIdentifier id)
: base(operation, id)
protected internal RollingUpgradeOperations(OperationsBase operation)
: base(operation)
{
}

Expand All @@ -51,16 +51,27 @@ public ArmResponse<RollingUpgrade> Get(CancellationToken cancellationToken = def
v => new RollingUpgrade(Parent, new RollingUpgradeStatusInfo(v)));
}

public Task<ArmResponse<RollingUpgrade>> GetAsync(CancellationToken cancellationToken = default)
public async Task<ArmResponse<RollingUpgrade>> GetAsync(CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
return new PhArmResponse<RollingUpgrade, Azure.ResourceManager.Compute.Models.RollingUpgradeStatusInfo>(
await Operations.GetLatestAsync(ParentId.ResourceGroupName, ParentId.Name, cancellationToken),
v => new RollingUpgrade(Parent, new RollingUpgradeStatusInfo(v)));
}

// Note: Singleton may have different operations such as GET/PUT/PATCH/POST or a combination of these
// Individual methods will be generated as they are declared
public ArmResponse<Response> Cancel(CancellationToken cancellationToken = default)
{
return new ArmResponse(Operations.StartCancel(ParentId.ResourceGroupName, ParentId.Name, cancellationToken).WaitForCompletionAsync(cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult());
return new ArmResponse(Operations
.StartCancel(ParentId.ResourceGroupName, ParentId.Name, cancellationToken)
.WaitForCompletionAsync(cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult());
}

public async Task<ArmResponse<Response>> CancelAsync(CancellationToken cancellationToken = default)
{
return new ArmResponse((await Operations
.StartCancel(ParentId.ResourceGroupName, ParentId.Name, cancellationToken)
.WaitForCompletionAsync(cancellationToken)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ namespace Proto.Compute
/// <summary>
/// A class representing a builder object used to create Azure resources.
/// </summary>
public class VirtualMachineScaleSetBuilder
public partial class VirtualMachineScaleSetBuilder
allenjzhang marked this conversation as resolved.
Show resolved Hide resolved
{
private VirtualMachineScaleSetData _model;

/// <summary>
/// Initializes a new instance of the <see cref="VirtualMachineScaleSetBuilder"/> class.
/// </summary>
Expand All @@ -21,7 +23,8 @@ public class VirtualMachineScaleSetBuilder
public VirtualMachineScaleSetBuilder(VirtualMachineScaleSetContainer container, VirtualMachineScaleSetData resource)
{
Resource = resource;
UnTypedContainer = container;
VirtualMachineScaleSetContainer = container;
_model = resource;
}

/// <summary>
Expand All @@ -37,7 +40,7 @@ public VirtualMachineScaleSetBuilder(VirtualMachineScaleSetContainer container,
/// <summary>
/// Gets the container object to create the resource in.
/// </summary>
protected VirtualMachineScaleSetContainer UnTypedContainer { get; private set; }
protected VirtualMachineScaleSetContainer VirtualMachineScaleSetContainer { get; private set; }

/// <summary>
/// Creates the resource object to send to the Azure API.
Expand Down Expand Up @@ -68,7 +71,7 @@ public ArmResponse<VirtualMachineScaleSet> CreateOrUpdate(string name, Cancellat
ResourceName = name;
Resource = Build();

return UnTypedContainer.CreateOrUpdate(name, Resource, cancellationToken);
return VirtualMachineScaleSetContainer.CreateOrUpdate(name, Resource, cancellationToken);
}

/// <summary>
Expand All @@ -88,7 +91,7 @@ public async Task<ArmResponse<VirtualMachineScaleSet>> CreateOrUpdateAsync(
ResourceName = name;
Resource = Build();

return await UnTypedContainer.CreateOrUpdateAsync(name, Resource, cancellationToken).ConfigureAwait(false);
return await VirtualMachineScaleSetContainer.CreateOrUpdateAsync(name, Resource, cancellationToken).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -109,7 +112,7 @@ public ArmOperation<VirtualMachineScaleSet> StartCreateOrUpdate(string name, Can
ResourceName = name;
Resource = Build();

return UnTypedContainer.StartCreateOrUpdate(name, Resource, cancellationToken);
return VirtualMachineScaleSetContainer.StartCreateOrUpdate(name, Resource, cancellationToken);
}

/// <summary>
Expand All @@ -132,7 +135,7 @@ public async Task<ArmOperation<VirtualMachineScaleSet>> StartCreateOrUpdateAsync
ResourceName = name;
Resource = Build();

return await UnTypedContainer.StartCreateOrUpdateAsync(name, Resource, cancellationToken).ConfigureAwait(false);
return await VirtualMachineScaleSetContainer.StartCreateOrUpdateAsync(name, Resource, cancellationToken).ConfigureAwait(false);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ await Operations.StartCreateOrUpdateAsync(Id.ResourceGroupName, name, resourceDe
/// <param name="hostName"> The hostname for the virtual machine. </param>
/// <param name="location"> The location to create the Virtual Machine. </param>
/// <returns> Object used to create a <see cref="VirtualMachine"/>. </returns>
public VirtualMachineScaleSetModelBuilder Construct(string hostName, LocationData location = null)
public VirtualMachineScaleSetBuilder Construct(string hostName, LocationData location = null)
{
var parent = GetParentResource<ResourceGroup, ResourceGroupResourceIdentifier, ResourceGroupOperations>();
var vmss = new Azure.ResourceManager.Compute.Models.VirtualMachineScaleSet(location ?? parent.Data.Location)
Expand Down Expand Up @@ -249,7 +249,7 @@ public VirtualMachineScaleSetModelBuilder Construct(string hostName, LocationDat

vmss.VirtualMachineProfile.NetworkProfile.NetworkInterfaceConfigurations.Add(nicConfig);

return new VirtualMachineScaleSetModelBuilder(this, new VirtualMachineScaleSetData(vmss));
return new VirtualMachineScaleSetBuilder(this, new VirtualMachineScaleSetData(vmss));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public async Task<IEnumerable<LocationData>> ListAvailableLocationsAsync(Cancell
/// <returns> An object representing collection of subnets and their operations over a virtual network. </returns>
public RollingUpgradeOperations GetRollingUpgrade()
{
return new RollingUpgradeOperations(this, Id);
return new RollingUpgradeOperations(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ private async Task ExecuteAsync()
// NOTE: due to full test involves creating LB which have another 3-5 resources needs to be in
// proto, this test relies on pre-created VMSS.
// 1. Follow instruction here (CLI), https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/quick-create-cli
// 2. Initiate a rolling upgrade az vmss rolling-upgrade start -g [RG] -n myScaleSet --debug
// az group create --name azhang-test --location eastus
// az vmss create --resource-group azhang-test --name myScaleSet --image UbuntuLTS --upgrade-policy-mode automatic --admin-username azureuser --generate-ssh-keys
// 2. Initiate a rolling upgrade az vmss rolling-upgrade start -g azhang-test -n myScaleSet --debug
VirtualMachineScaleSet vmss1 = await client.DefaultSubscription
.GetResourceGroups().Get("azhang-test").Value
.GetVirtualMachineScaleSet().GetAsync("myScaleSet");
Expand Down