From 9fab53746d1f5493228063b11a14cec5491af802 Mon Sep 17 00:00:00 2001 From: Vojtech Vit Date: Mon, 15 Apr 2019 23:14:17 +0200 Subject: [PATCH 1/4] feat: support filtering certificates by thumbprint, environment as a secondary filter --- Octopus-Cmdlets/GetCertificate.cs | 64 +++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/Octopus-Cmdlets/GetCertificate.cs b/Octopus-Cmdlets/GetCertificate.cs index 2627d54..fe826b3 100644 --- a/Octopus-Cmdlets/GetCertificate.cs +++ b/Octopus-Cmdlets/GetCertificate.cs @@ -56,12 +56,20 @@ public class GetCertificate : PSCmdlet public string[] CertificateId { get; set; } /// - /// The environments of the certificates to retrieve. + /// The thumbprint of the certificate to retrieve. /// [Parameter( - ParameterSetName = "ByEnvironment", + ParameterSetName = "ByThumbprint", Mandatory = true, ValueFromPipelineByPropertyName = true)] + public string[] Thumbprint { get; set; } + + /// + /// The environments of the certificates to retrieve. + /// + [Parameter( + Mandatory = false, + ValueFromPipelineByPropertyName = true)] public string[] Environment { get; set; } /// @@ -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 FilterEnvironment(IEnumerable 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; + } } } From 819159748fd9a7f8616b45b16ceb9303e3d3f174 Mon Sep 17 00:00:00 2001 From: Vojtech Vit Date: Mon, 15 Apr 2019 23:14:34 +0200 Subject: [PATCH 2/4] feat: support variable types on update --- Octopus-Cmdlets/UpdateLibraryVariable.cs | 13 ++++++++++++- Octopus-Cmdlets/UpdateVariable.cs | 11 +++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Octopus-Cmdlets/UpdateLibraryVariable.cs b/Octopus-Cmdlets/UpdateLibraryVariable.cs index c6588dd..3ce3a31 100644 --- a/Octopus-Cmdlets/UpdateLibraryVariable.cs +++ b/Octopus-Cmdlets/UpdateLibraryVariable.cs @@ -84,6 +84,15 @@ public class UpdateLibraryVariable : PSCmdlet ParameterSetName = "Parts")] public string Value { get; set; } + /// + /// The type of the variable to create. + /// + [Parameter( + ParameterSetName = "Parts", + Mandatory = false, + ValueFromPipelineByPropertyName = true)] + public VariableType? Type { get; set; } + /// /// The environments to restrict the scope to. /// @@ -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) { @@ -228,7 +239,7 @@ protected override void EndProcessing() { // Save the variables _octopus.VariableSets.Modify(_variableSet); - + WriteDebug("Modified the variable set"); } } diff --git a/Octopus-Cmdlets/UpdateVariable.cs b/Octopus-Cmdlets/UpdateVariable.cs index e95cec3..9d0e313 100644 --- a/Octopus-Cmdlets/UpdateVariable.cs +++ b/Octopus-Cmdlets/UpdateVariable.cs @@ -79,6 +79,15 @@ public class UpdateVariable : PSCmdlet ParameterSetName = "Parts")] public string Value { get; set; } + /// + /// The type of the variable to create. + /// + [Parameter( + ParameterSetName = "Parts", + Mandatory = false, + ValueFromPipelineByPropertyName = true)] + public VariableType? Type { get; set; } + /// /// The environments to restrict the scope to. /// @@ -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) { From f1ed9f68fae83b44af2c99471d268d0aa6008f67 Mon Sep 17 00:00:00 2001 From: Vojtech Vit Date: Mon, 15 Apr 2019 23:14:46 +0200 Subject: [PATCH 3/4] feat: support removing library variables --- Octopus-Cmdlets/RemoveLibraryVariable.cs | 128 +++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 Octopus-Cmdlets/RemoveLibraryVariable.cs diff --git a/Octopus-Cmdlets/RemoveLibraryVariable.cs b/Octopus-Cmdlets/RemoveLibraryVariable.cs new file mode 100644 index 0000000..6baeef4 --- /dev/null +++ b/Octopus-Cmdlets/RemoveLibraryVariable.cs @@ -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 +{ + /// + /// Remove a variable from the Octopus Deploy library variable set. + /// The Remove-OctoLibraryVariable cmdlet removes a variable from the Octopus Deploy library variable set. + /// + /// + /// PS C:\>remove-octolibraryvariable -VariableSet Database -Name ConnectionString + /// + /// Remove the variable 'ConnectionString' from the variable set 'Database'. + /// + /// + [Cmdlet(VerbsCommon.Remove, "LibraryVariable", DefaultParameterSetName = "ByName")] + public class RemoveLibraryVariable : PSCmdlet + { + /// + /// The name of the library variable set to remove the variable from. + /// + [Parameter( + Position = 0, + Mandatory = true, + ValueFromPipelineByPropertyName = true)] + public string VariableSet { get; set; } + + /// + /// The name of the variable to remove. + /// + [Parameter( + ParameterSetName = "ByName", + Position = 1, + Mandatory = true, + ValueFromPipelineByPropertyName = true)] + public string[] Name { get; set; } + + private IOctopusRepository _octopus; + private VariableSetResource _variableSet; + + /// + /// BeginProcessing + /// + 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); + } + + /// + /// ProcessRecord + /// + 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)); + } + } + + /// + /// EndProcessing + /// + protected override void EndProcessing() + { + // Save the variables + _octopus.VariableSets.Modify(_variableSet); + WriteVerbose("Saved changes"); + } + } +} From 94767b3ee895f44a6b40f3709c70b1d04771809f Mon Sep 17 00:00:00 2001 From: Vojtech Vit Date: Mon, 15 Apr 2019 23:17:43 +0200 Subject: [PATCH 4/4] chore: version number increase --- Octopus-Cmdlets/Octopus-Cmdlets.psd1 | Bin 6290 -> 6290 bytes README.md | 2 +- Version.cs | 4 ++-- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Octopus-Cmdlets/Octopus-Cmdlets.psd1 b/Octopus-Cmdlets/Octopus-Cmdlets.psd1 index be5a0fa7ef83f9b1c4597e4d9d49f5848648e37e..61cc0aa4d9bcef6814a7c739fc9078ec458763ea 100644 GIT binary patch delta 14 VcmbPaILUB>9TTJZW_zY!F#sbb1PlNG delta 14 VcmbPaILUB>9TTJ3W_zY!F#sbV1PcHF diff --git a/README.md b/README.md index 6bcf12a..e0f9f1d 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/Version.cs b/Version.cs index 3d1c903..307555b 100644 --- a/Version.cs +++ b/Version.cs @@ -1,4 +1,4 @@ using System.Reflection; -[assembly: AssemblyVersion("0.6.0.0")] -[assembly: AssemblyFileVersion("0.6.0.0")] \ No newline at end of file +[assembly: AssemblyVersion("0.7.0.0")] +[assembly: AssemblyFileVersion("0.7.0.0")] \ No newline at end of file