Skip to content

Commit

Permalink
Merge pull request #197 from OctopusDeploy/enh-registermachineswithproxy
Browse files Browse the repository at this point in the history
Add option to set a proxy in RegisterMachineOperation
  • Loading branch information
MichaelJCompton authored Sep 29, 2017
2 parents 91f4572 + a9a84d5 commit 67d5d99
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3595,6 +3595,7 @@ Octopus.Client.Operations
String[] EnvironmentNames { get; set; }
String MachineName { get; set; }
String MachinePolicy { get; set; }
String ProxyName { get; set; }
String[] Roles { get; set; }
Uri SubscriptionId { get; set; }
String[] Tenants { get; set; }
Expand All @@ -3616,6 +3617,7 @@ Octopus.Client.Operations
String[] EnvironmentNames { get; set; }
String MachineName { get; set; }
String MachinePolicy { get; set; }
String ProxyName { get; set; }
String[] Roles { get; set; }
Uri SubscriptionId { get; set; }
String[] Tenants { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3998,6 +3998,7 @@ Octopus.Client.Operations
String[] EnvironmentNames { get; set; }
String MachineName { get; set; }
String MachinePolicy { get; set; }
String ProxyName { get; set; }
String[] Roles { get; set; }
Uri SubscriptionId { get; set; }
String[] Tenants { get; set; }
Expand All @@ -4022,6 +4023,7 @@ Octopus.Client.Operations
String[] EnvironmentNames { get; set; }
String MachineName { get; set; }
String MachinePolicy { get; set; }
String ProxyName { get; set; }
String[] Roles { get; set; }
Uri SubscriptionId { get; set; }
String[] Tenants { get; set; }
Expand Down
5 changes: 5 additions & 0 deletions source/Octopus.Client/Operations/IRegisterMachineOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public interface IRegisterMachineOperation
/// </summary>
string TentacleThumbprint { get; set; }

/// <summary>
/// Gets or sets the name of the proxy that Octopus should use when communicating with the Tentacle.
/// </summary>
string ProxyName { get; set; }

/// <summary>
/// If a machine with the same name already exists, it won't be overwritten by default (instead, an
/// <see cref="ArgumentException" /> will be thrown).
Expand Down
38 changes: 35 additions & 3 deletions source/Octopus.Client/Operations/RegisterMachineOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public RegisterMachineOperation(IOctopusClientFactory clientFactory)
/// </summary>
public string TentacleThumbprint { get; set; }

/// <summary>
/// Gets or sets the name of the proxy that Octopus should use when communicating with the Tentacle.
/// </summary>
public string ProxyName { get; set; }

/// <summary>
/// If a machine with the same name already exists, it won't be overwritten by default (instead, an
/// <see cref="ArgumentException" /> will be thrown).
Expand Down Expand Up @@ -135,8 +140,9 @@ public void Execute(IOctopusRepository repository)
var machine = GetMachine(repository);
var tenants = GetTenants(repository);
ValidateTenantTags(repository);
var proxy = GetProxy(repository);

ApplyChanges(machine, selectedEnvironments, machinePolicy, tenants);
ApplyChanges(machine, selectedEnvironments, machinePolicy, tenants, proxy);

if (machine.Id != null)
repository.Machines.Modify(machine);
Expand Down Expand Up @@ -199,6 +205,18 @@ MachinePolicyResource GetMachinePolicy(IOctopusRepository repository)
return machinePolicy;
}

ProxyResource GetProxy(IOctopusRepository repository)
{
var proxy = default(ProxyResource);
if (!string.IsNullOrEmpty(ProxyName))
{
proxy = repository.Proxies.FindByName(ProxyName);
if (proxy == null)
throw new ArgumentException(CouldNotFindMessage("proxy name", ProxyName));
}
return proxy;
}

MachineResource GetMachine(IOctopusRepository repository)
{
var existing = default(MachineResource);
Expand Down Expand Up @@ -255,9 +273,10 @@ public async Task ExecuteAsync(IOctopusAsyncRepository repository)
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 machineTask;
ApplyChanges(machine, await selectedEnvironments, await machinePolicy, await tenants);
ApplyChanges(machine, await selectedEnvironments, await machinePolicy, await tenants, await proxy);

if (machine.Id != null)
await repository.Machines.Modify(machine).ConfigureAwait(false);
Expand Down Expand Up @@ -321,6 +340,18 @@ async Task<MachinePolicyResource> GetMachinePolicy(IOctopusAsyncRepository repos
return machinePolicy;
}

async Task<ProxyResource> GetProxy(IOctopusAsyncRepository repository)
{
var proxy = default(ProxyResource);
if (!string.IsNullOrEmpty(ProxyName))
{
proxy = await repository.Proxies.FindByName(ProxyName).ConfigureAwait(false);
if (proxy == null)
throw new ArgumentException(CouldNotFindMessage("proxy name", ProxyName));
}
return proxy;
}

async Task<MachineResource> GetMachine(IOctopusAsyncRepository repository)
{
var existing = default(MachineResource);
Expand All @@ -336,7 +367,7 @@ async Task<MachineResource> GetMachine(IOctopusAsyncRepository repository)
return existing ?? new MachineResource();
}

void ApplyChanges(MachineResource machine, IEnumerable<EnvironmentResource> environment, MachinePolicyResource machinePolicy, IEnumerable<TenantResource> tenants)
void ApplyChanges(MachineResource machine, IEnumerable<EnvironmentResource> environment, MachinePolicyResource machinePolicy, IEnumerable<TenantResource> tenants, ProxyResource proxy)
{
machine.EnvironmentIds = new ReferenceCollection(environment.Select(e => e.Id).ToArray());
machine.TenantIds = new ReferenceCollection(tenants.Select(t => t.Id).ToArray());
Expand All @@ -351,6 +382,7 @@ void ApplyChanges(MachineResource machine, IEnumerable<EnvironmentResource> envi
var listening = new ListeningTentacleEndpointResource();
listening.Uri = new Uri("https://" + TentacleHostname.ToLowerInvariant() + ":" + TentaclePort.ToString(CultureInfo.InvariantCulture) + "/").ToString();
listening.Thumbprint = TentacleThumbprint;
listening.ProxyId = proxy?.Id;
machine.Endpoint = listening;
}
else if (CommunicationStyle == CommunicationStyle.TentacleActive)
Expand Down

0 comments on commit 67d5d99

Please sign in to comment.