Skip to content

Commit

Permalink
add implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
scme0 committed Nov 15, 2023
1 parent 8abd2cc commit dd09776
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using Octopus.Client.Model;
using Octopus.Client.Model.Endpoints;

Expand All @@ -13,6 +14,11 @@ public RegisterKubernetesClusterOperation(IOctopusClientFactory clientFactory) :
{
}

protected override void PrepareMachineForReRegistration(MachineResource machine, string proxyId)
{
machine.Endpoint = GenerateEndpoint(proxyId);
}

protected override EndpointResource GenerateEndpoint(string proxyId)
{
return CommunicationStyle switch
Expand Down
66 changes: 44 additions & 22 deletions source/Octopus.Server.Client/Operations/RegisterMachineOperation.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Octopus.Client.Exceptions;
using Octopus.Client.Model;
using Octopus.Client.Model.Endpoints;

namespace Octopus.Client.Operations
{
Expand Down Expand Up @@ -65,16 +63,26 @@ public RegisterMachineOperation(IOctopusClientFactory clientFactory) : base(clie
/// </exception>
public override void Execute(IOctopusSpaceRepository repository)
{
var selectedEnvironments = GetEnvironments(repository);
var machinePolicy = GetMachinePolicy(repository);
var machine = GetMachine(repository);
var tenants = GetTenants(repository);
ValidateTenantTags(repository);
var proxy = GetProxy(repository);

ApplyBaseChanges(machine, machinePolicy, proxy);
ApplyDeploymentTargetChanges(machine, selectedEnvironments, tenants);
if (machine.Id == null || AllowOverwrite)
{
var machinePolicy = GetMachinePolicy(repository);
ValidateTenantTags(repository);
ApplyBaseChanges(machine, machinePolicy, proxy);
ApplyDeploymentTargetChanges(machine, GetEnvironments(repository), GetTenants(repository));
}
else
{
PrepareMachineForReRegistration(machine, proxy?.Id);
}

ModifyOrCreateMachine(repository, machine);
}

static void ModifyOrCreateMachine(IOctopusSpaceRepository repository, MachineResource machine)
{
if (machine.Id != null)
repository.Machines.Modify(machine);
else
Expand Down Expand Up @@ -130,12 +138,11 @@ MachineResource GetMachine(IOctopusSpaceRepository repository)
try
{
existing = repository.Machines.FindByName(MachineName);
if (!AllowOverwrite && existing?.Id != null)
throw new InvalidRegistrationArgumentsException($"A machine named '{MachineName}' already exists on the Octopus Server in the target space. Use the 'force' parameter if you intended to update the existing machine.");
}
catch (OctopusDeserializationException) // eat it, probably caused by resource incompatability between versions
catch (OctopusDeserializationException) // eat it, probably caused by resource incompatibility between versions
{
}

return existing ?? new MachineResource();
}

Expand All @@ -147,17 +154,34 @@ MachineResource GetMachine(IOctopusSpaceRepository repository)
/// </exception>
public override async Task ExecuteAsync(IOctopusSpaceAsyncRepository repository)
{
var selectedEnvironments = GetEnvironments(repository).ConfigureAwait(false);
var machinePolicy = GetMachinePolicy(repository).ConfigureAwait(false);
var machineTask = GetMachine(repository).ConfigureAwait(false);
var tenants = GetTenants(repository).ConfigureAwait(false);
await ValidateTenantTags(repository).ConfigureAwait(false);
var proxy = GetProxy(repository).ConfigureAwait(false);
var machine = await GetMachine(repository).ConfigureAwait(false);
var proxy = await GetProxy(repository).ConfigureAwait(false);

var machine = await machineTask;
ApplyBaseChanges(machine, await machinePolicy, await proxy);
ApplyDeploymentTargetChanges(machine, await selectedEnvironments, await tenants);
if (machine.Id == null || AllowOverwrite)
{
var machinePolicy = GetMachinePolicy(repository).ConfigureAwait(false);
await ValidateTenantTags(repository).ConfigureAwait(false);
ApplyBaseChanges(machine, await machinePolicy, proxy);
var selectedEnvironments = await GetEnvironments(repository).ConfigureAwait(false);
var tenants = await GetTenants(repository).ConfigureAwait(false);
ApplyDeploymentTargetChanges(machine, selectedEnvironments, tenants);
}
else
{
PrepareMachineForReRegistration(machine, proxy?.Id);
}

await ModifyOrCreateMachine(repository, machine).ConfigureAwait(false);
}

protected virtual void PrepareMachineForReRegistration(MachineResource machineResource, string proxyId)
{
throw new InvalidRegistrationArgumentsException(
$"A machine named '{MachineName}' already exists in the environment. Use the 'force' parameter if you intended to update the existing machine.");
}

static async Task ModifyOrCreateMachine(IOctopusSpaceAsyncRepository repository, MachineResource machine)
{
if (machine.Id != null)
await repository.Machines.Modify(machine).ConfigureAwait(false);
else
Expand Down Expand Up @@ -226,8 +250,6 @@ async Task<MachineResource> GetMachine(IOctopusSpaceAsyncRepository repository)
try
{
existing = await repository.Machines.FindByName(MachineName).ConfigureAwait(false);
if (!AllowOverwrite && existing?.Id != null)
throw new InvalidRegistrationArgumentsException($"A machine named '{MachineName}' already exists in the environment. Use the 'force' parameter if you intended to update the existing machine.");
}
catch (OctopusDeserializationException) // eat it, probably caused by resource incompatability between versions
{
Expand Down

0 comments on commit dd09776

Please sign in to comment.