/// <summary> /// Executes the operation against the specified Octopus Deploy server. /// </summary> /// <param name="repository">The Octopus Deploy server repository.</param> /// <exception cref="System.ArgumentException"> /// </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 machineTask; ApplyBaseChanges(machine, await machinePolicy, await proxy); ApplyDeploymentTargetChanges(machine, await selectedEnvironments, await tenants); if (machine.Id != null) { await repository.Machines.Modify(machine).ConfigureAwait(false); } else { await repository.Machines.Create(machine).ConfigureAwait(false); } }
async Task <List <TenantResource> > GetTenants(IOctopusSpaceAsyncRepository repository) { if (Tenants == null || !Tenants.Any()) { return(new List <TenantResource>()); } var tenantsByName = new List <TenantResource>(); foreach (var tenantName in Tenants) { var tenant = await repository.Tenants.FindByName(tenantName).ConfigureAwait(false); if (tenant != null) { tenantsByName.Add(tenant); } } var missing = Tenants.Except(tenantsByName.Select(e => e.Name), StringComparer.OrdinalIgnoreCase).ToArray(); var tenantsById = await repository.Tenants.Get(missing).ConfigureAwait(false); missing = missing.Except(tenantsById.Select(e => e.Id), StringComparer.OrdinalIgnoreCase).ToArray(); if (missing.Any()) { throw new ArgumentException(CouldNotFindMessage("tenant", missing)); } return(tenantsById.Concat(tenantsByName).ToList()); }
async Task <List <WorkerPoolResource> > GetWorkerPools(IOctopusSpaceAsyncRepository repository) { var selectedPools = await repository.WorkerPools.FindByNames(WorkerPoolNames).ConfigureAwait(false); var missing = WorkerPoolNames.Except(selectedPools.Select(p => p.Name), StringComparer.OrdinalIgnoreCase).ToList(); if (missing.Any()) { throw new InvalidRegistrationArgumentsException(CouldNotFindMessage("worker pool", missing.ToArray())); } return(selectedPools); }
protected async Task <ProxyResource> GetProxy(IOctopusSpaceAsyncRepository 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); }
protected async Task <MachinePolicyResource> GetMachinePolicy(IOctopusSpaceAsyncRepository repository) { var machinePolicy = default(MachinePolicyResource); if (!string.IsNullOrEmpty(MachinePolicy)) { machinePolicy = await repository.MachinePolicies.FindByName(MachinePolicy).ConfigureAwait(false); if (machinePolicy == null) { throw new ArgumentException(CouldNotFindMessage("machine policy", MachinePolicy)); } } return(machinePolicy); }
async Task ValidateTenantTags(IOctopusSpaceAsyncRepository repository) { if (TenantTags == null || !TenantTags.Any()) { return; } var tagSets = await repository.TagSets.FindAll().ConfigureAwait(false); var missingTags = TenantTags.Where(tt => !tagSets.Any(ts => ts.Tags.Any(t => t.CanonicalTagName.Equals(tt, StringComparison.OrdinalIgnoreCase)))).ToList(); if (missingTags.Any()) { throw new ArgumentException(CouldNotFindMessage("tag", missingTags.ToArray())); } }
async Task <MachineResource> GetMachine(IOctopusSpaceAsyncRepository repository) { var existing = default(MachineResource); try { existing = await repository.Machines.FindByName(MachineName).ConfigureAwait(false); if (!AllowOverwrite && existing?.Id != null) { throw new ArgumentException($"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 { } return(existing ?? new MachineResource()); }
/// <summary> /// Executes the operation against the specified Octopus Deploy server. /// </summary> /// <param name="repository">The Octopus Deploy server repository.</param> /// <exception cref="InvalidRegistrationArgumentsException"> /// </exception> public override async Task ExecuteAsync(IOctopusSpaceAsyncRepository repository) { var selectedPools = GetWorkerPools(repository).ConfigureAwait(false); var machinePolicy = GetMachinePolicy(repository).ConfigureAwait(false); var machineTask = GetWorker(repository).ConfigureAwait(false); var proxy = GetProxy(repository).ConfigureAwait(false); var machine = await machineTask; ApplyBaseChanges(machine, await machinePolicy, await proxy); machine.WorkerPoolIds = new ReferenceCollection((await selectedPools).Select(p => p.Id).ToArray()); if (machine.Id != null) { await repository.Workers.Modify(machine).ConfigureAwait(false); } else { await repository.Workers.Create(machine).ConfigureAwait(false); } }
async Task <List <EnvironmentResource> > GetEnvironments(IOctopusSpaceAsyncRepository repository) { var selectedEnvironments = new List <EnvironmentResource>(); foreach (var environmentName in EnvironmentNames) { var environment = await repository.Environments.FindByName(environmentName).ConfigureAwait(false); if (environment != null) { selectedEnvironments.Add(environment); } } var missing = EnvironmentNames.Except(selectedEnvironments.Select(e => e.Name), StringComparer.OrdinalIgnoreCase).ToList(); if (missing.Any()) { throw new ArgumentException(CouldNotFindMessage("environment", missing.ToArray())); } return(selectedEnvironments); }
/// <summary> /// Executes the operation against the specified Octopus Deploy server. /// </summary> /// <param name="repository">The Octopus Deploy server repository.</param> /// <exception cref="System.ArgumentException"> /// </exception> public abstract Task ExecuteAsync(IOctopusSpaceAsyncRepository repository);