Skip to content

Commit

Permalink
Merge pull request #18 from vojtech-vit-massive/feature/#16-certificates
Browse files Browse the repository at this point in the history
Feature/#16 certificates
  • Loading branch information
Swoogan authored Apr 16, 2019
2 parents a1ae6d3 + 94767b3 commit 4370b3e
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 24 deletions.
64 changes: 44 additions & 20 deletions Octopus-Cmdlets/GetCertificate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,20 @@ public class GetCertificate : PSCmdlet
public string[] CertificateId { get; set; }

/// <summary>
/// <para type="description">The environments of the certificates to retrieve.</para>
/// <para type="description">The thumbprint of the certificate to retrieve.</para>
/// </summary>
[Parameter(
ParameterSetName = "ByEnvironment",
ParameterSetName = "ByThumbprint",
Mandatory = true,
ValueFromPipelineByPropertyName = true)]
public string[] Thumbprint { get; set; }

/// <summary>
/// <para type="description">The environments of the certificates to retrieve.</para>
/// </summary>
[Parameter(
Mandatory = false,
ValueFromPipelineByPropertyName = true)]
public string[] Environment { get; set; }

/// <summary>
Expand Down Expand Up @@ -106,50 +114,66 @@ protected override void ProcessRecord()
case "ById":
ProcessById();
break;
case "ByEnvironment":
ProcessByEnvironment();
case "ByThumbprint":
ProcessByThumbprint();
break;
default:
throw new Exception("Unknown ParameterSetName: " + ParameterSetName);
}
}

private void ProcessByEnvironment()
private void ProcessByName()
{
var certs = Environment == null
var certs = Name == null
? _certificates
: (from c in _certificates
from cenv in c.EnvironmentIds
from env in Environment
where cenv == env
from n in Name
where c.Name.Equals(n, StringComparison.InvariantCultureIgnoreCase)
select c);

certs = FilterEnvironment(certs);

foreach (var cert in certs)
WriteObject(cert);
}

private void ProcessByName()
private void ProcessById()
{
var certs = Name == null
? _certificates
: (from c in _certificates
from n in Name
where c.Name.Equals(n, StringComparison.InvariantCultureIgnoreCase)
select c);
var certs = from c in _certificates
from id in CertificateId
where id == c.Id
select c;

certs = FilterEnvironment(certs);

foreach (var cert in certs)
WriteObject(cert);
}

private void ProcessById()
private void ProcessByThumbprint()
{
var certs = from c in _certificates
from id in CertificateId
where id == c.Id
select c;
from thumb in Thumbprint
where thumb == c.Thumbprint
select c;

certs = FilterEnvironment(certs);

foreach (var cert in certs)
WriteObject(cert);
}

private IEnumerable<CertificateResource> FilterEnvironment(IEnumerable<CertificateResource> certs)
{
if (Environment == null || Environment.Length == 0)
{
return certs;
}

return from c in certs
from e in Environment
where c.EnvironmentIds.Contains(e, StringComparer.OrdinalIgnoreCase)
select c;
}
}
}
Binary file modified Octopus-Cmdlets/Octopus-Cmdlets.psd1
Binary file not shown.
128 changes: 128 additions & 0 deletions Octopus-Cmdlets/RemoveLibraryVariable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#region License
// Copyright 2014 Colin Svingen

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

using System;
using System.Linq;
using System.Management.Automation;
using Octopus.Client;
using Octopus.Client.Model;

namespace Octopus_Cmdlets
{
/// <summary>
/// <para type="synopsis">Remove a variable from the Octopus Deploy library variable set.</para>
/// <para type="description">The Remove-OctoLibraryVariable cmdlet removes a variable from the Octopus Deploy library variable set.</para>
/// </summary>
/// <example>
/// <code>PS C:\>remove-octolibraryvariable -VariableSet Database -Name ConnectionString</code>
/// <para>
/// Remove the variable 'ConnectionString' from the variable set 'Database'.
/// </para>
/// </example>
[Cmdlet(VerbsCommon.Remove, "LibraryVariable", DefaultParameterSetName = "ByName")]
public class RemoveLibraryVariable : PSCmdlet
{
/// <summary>
/// <para type="description">The name of the library variable set to remove the variable from.</para>
/// </summary>
[Parameter(
Position = 0,
Mandatory = true,
ValueFromPipelineByPropertyName = true)]
public string VariableSet { get; set; }

/// <summary>
/// <para type="description">The name of the variable to remove.</para>
/// </summary>
[Parameter(
ParameterSetName = "ByName",
Position = 1,
Mandatory = true,
ValueFromPipelineByPropertyName = true)]
public string[] Name { get; set; }

private IOctopusRepository _octopus;
private VariableSetResource _variableSet;

/// <summary>
/// BeginProcessing
/// </summary>
protected override void BeginProcessing()
{
_octopus = Session.RetrieveSession(this);

var libraryVariableSet =
_octopus.LibraryVariableSets.FindOne(
v => v.Name.Equals(VariableSet, StringComparison.InvariantCultureIgnoreCase));

if (libraryVariableSet == null)
throw new Exception(string.Format("Library variable set '{0}' was not found.", VariableSet));

_variableSet = _octopus.VariableSets.Get(libraryVariableSet.Link("Variables"));
WriteDebug("Found variable set" + _variableSet.Id);
}

/// <summary>
/// ProcessRecord
/// </summary>
protected override void ProcessRecord()
{
WriteDebug("ParameterSetName: " + ParameterSetName);

switch (ParameterSetName)
{
case "ByName":
ProcessByName();
break;
default:
throw new ArgumentException("Unknown ParameterSetName: " + ParameterSetName);
}
}

private void ProcessByName()
{
foreach (var name in Name)
{
var found = false;
var nameForClosure = name;

var variables =
_variableSet.Variables.Where(
variable => variable.Name.Equals(nameForClosure, StringComparison.InvariantCultureIgnoreCase)).ToArray();

foreach (var variable in variables)
{
WriteVerbose(string.Format("Removing variable '{0}' from variable set '{1}'.", variable.Name, VariableSet));
_variableSet.Variables.Remove(variable);
found = true;
}

if (!found)
WriteWarning(string.Format("Variable '{0}' in variable set '{1}' does not exist.", name, VariableSet));
}
}

/// <summary>
/// EndProcessing
/// </summary>
protected override void EndProcessing()
{
// Save the variables
_octopus.VariableSets.Modify(_variableSet);
WriteVerbose("Saved changes");
}
}
}
13 changes: 12 additions & 1 deletion Octopus-Cmdlets/UpdateLibraryVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ public class UpdateLibraryVariable : PSCmdlet
ParameterSetName = "Parts")]
public string Value { get; set; }

/// <summary>
/// <para type="description">The type of the variable to create.</para>
/// </summary>
[Parameter(
ParameterSetName = "Parts",
Mandatory = false,
ValueFromPipelineByPropertyName = true)]
public VariableType? Type { get; set; }

/// <summary>
/// <para type="description">The environments to restrict the scope to.</para>
/// </summary>
Expand Down Expand Up @@ -198,6 +207,8 @@ private void ProcessByParts()
variable.IsSensitive = Sensitive;
if (Value != null)
variable.Value = Value;
if (Type.HasValue)
variable.Type = Type.Value;

if (Environments != null)
{
Expand Down Expand Up @@ -228,7 +239,7 @@ protected override void EndProcessing()
{
// Save the variables
_octopus.VariableSets.Modify(_variableSet);

WriteDebug("Modified the variable set");
}
}
Expand Down
11 changes: 11 additions & 0 deletions Octopus-Cmdlets/UpdateVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ public class UpdateVariable : PSCmdlet
ParameterSetName = "Parts")]
public string Value { get; set; }

/// <summary>
/// <para type="description">The type of the variable to create.</para>
/// </summary>
[Parameter(
ParameterSetName = "Parts",
Mandatory = false,
ValueFromPipelineByPropertyName = true)]
public VariableType? Type { get; set; }

/// <summary>
/// <para type="description">The environments to restrict the scope to.</para>
/// </summary>
Expand Down Expand Up @@ -190,6 +199,8 @@ private void ProcessByParts()
variable.IsSensitive = Sensitive;
if (Value != null)
variable.Value = Value;
if (Type.HasValue)
variable.Type = Type.Value;

if (Environments != null)
{
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Powershell Gallery (preferred method)

Binary Archive
--------------
Download the latest binary: [v0.6.0](https://github.com/Swoogan/Octopus-Cmdlets/releases/download/v0.6.0/Octopus-Cmdlets-v0.6.0.zip)
Download the latest binary: [v0.7.0](https://github.com/Swoogan/Octopus-Cmdlets/releases/download/v0.7.0/Octopus-Cmdlets-v0.7.0.zip)

Extract the zip and copy the `Octopus-Cmdlets` folder into a folder in your
`$env:PSModulePath`.
Expand Down
4 changes: 2 additions & 2 deletions Version.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Reflection;

[assembly: AssemblyVersion("0.6.0.0")]
[assembly: AssemblyFileVersion("0.6.0.0")]
[assembly: AssemblyVersion("0.7.0.0")]
[assembly: AssemblyFileVersion("0.7.0.0")]

0 comments on commit 4370b3e

Please sign in to comment.