/// <summary> /// Creates a new VariableSet LibraryVariableSet from a passed string. /// </summary> /// <param name="octRepository">The repository to call against.</param> /// <param name="variableSetName"></param> /// <param name="description"></param> /// <param name="variableSetText"></param> public static void CreateVariableSetFromText(OctopusRepository octRepository, string variableSetName, string description, string variableSetText) { var newVariables = new List <VariableResource>(); var variables = JsonConvert.DeserializeObject <List <VariableResource> >(variableSetText); var libraryVariableSet = new LibraryVariableSetResource { ContentType = VariableSetContentType.Variables, Description = description, Name = variableSetName }; var outputLibraryVariableSet = octRepository.LibraryVariableSets.Create(libraryVariableSet); var variableSet = octRepository.VariableSets.Get(outputLibraryVariableSet.VariableSetId); foreach (var variableToAdd in variables) { var newVariable = new VariableResource() { Name = variableToAdd.Name, Value = variableToAdd.Value, Scope = variableToAdd.Scope, IsSensitive = variableToAdd.IsSensitive, IsEditable = variableToAdd.IsEditable, Prompt = variableToAdd.Prompt }; if (newVariable.IsSensitive && newVariable.Value == null) { newVariable.Value = string.Empty; } newVariables.Add(newVariable); } variableSet.Variables = newVariables; octRepository.VariableSets.Modify(variableSet); }
public static LibraryVariableSetResource UpdateWith(this LibraryVariableSetResource resource, LibraryVariableSet model) { resource.Name = model.Identifier.Name; resource.ContentType = (VariableSetContentType)model.ContentType; resource.Description = model.Description; return(resource); }
public void Init() { _ps = Utilities.CreatePowerShell(CmdletName, typeof(AddLibraryVariable)); var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable); _variableSet.Variables.Clear(); var lib = new LibraryVariableSetResource { Name = "Octopus" }; var libs = new List<LibraryVariableSetResource> {lib}; lib.Links.Add("Variables", "variablesets-1"); octoRepo.Setup(o => o.LibraryVariableSets.FindOne(It.IsAny<Func<LibraryVariableSetResource, bool>>())) .Returns( (Func<LibraryVariableSetResource, bool> f) => (from l in libs where f(l) select l).FirstOrDefault()); octoRepo.Setup(o => o.Projects.FindByName("Gibberish")).Returns((ProjectResource)null); octoRepo.Setup(o => o.VariableSets.Get("variablesets-1")).Returns(_variableSet); var process = new DeploymentProcessResource(); process.Steps.Add(new DeploymentStepResource { Name = "Website", Id = "Step-1" }); octoRepo.Setup(o => o.DeploymentProcesses.Get("deploymentprocesses-1")).Returns(process); var envs = new List<EnvironmentResource> { new EnvironmentResource {Id = "Environments-1", Name = "DEV"} }; octoRepo.Setup(o => o.Environments.FindByNames(new[] { "DEV" })).Returns(envs); var machines = new List<MachineResource> { new MachineResource {Id = "Machines-1", Name = "web-01"} }; octoRepo.Setup(o => o.Machines.FindByNames(new[] { "web-01" })).Returns(machines); }
protected override void ProcessRecord() { object baseresource = null; switch (Resource) { case "Environment": baseresource = new EnvironmentResource(); break; case "Project": baseresource = new ProjectResource(); break; case "ProjectGroup": baseresource = new ProjectGroupResource(); break; case "NugetFeed": case "ExternalFeed": baseresource = new NuGetFeedResource(); break; case "LibraryVariableSet": baseresource = new LibraryVariableSetResource(); break; case "Machine": case "Target": baseresource = new MachineResource(); break; case "Lifecycle": baseresource = new LifecycleResource(); break; case "Team": baseresource = new TeamResource(); break; case "User": baseresource = new UserResource(); break; case "Channel": baseresource = new ChannelResource(); break; case "Tenant": baseresource = new TenantResource(); break; case "TagSet": baseresource = new TagSetResource(); break; } WriteObject(baseresource); }
public static LibraryVariableSet ToModel(this LibraryVariableSetResource resource, IOctopusRepository repository) { var variableSetResource = repository.VariableSets.Get(resource.VariableSetId); return(new LibraryVariableSet( new ElementIdentifier(resource.Name), resource.Description, (LibraryVariableSet.VariableSetContentType)resource.ContentType, variableSetResource.Variables.Select(v => v.ToModel(null, repository)))); }
public static async Task <LibraryVariableSet> ToModel(this LibraryVariableSetResource resource, IOctopusAsyncRepository repository) { var variableSetResource = await repository.VariableSets.Get(resource.VariableSetId); return(new LibraryVariableSet( new ElementIdentifier(resource.Name), resource.Description, (LibraryVariableSet.VariableSetContentType)resource.ContentType, await Task.WhenAll(variableSetResource.Variables.Select(v => v.ToModel(null, repository))))); }
public void CreateAndRemoveLibraryVariableSet() { #region LibraryVariableSetCreate var resource = new LibraryVariableSetResource() { Name = TestResourceName, }; var createParameters = new List <CmdletParameter> { new CmdletParameter() { Name = "Resource", Resource = resource } }; var createPowershell = new CmdletRunspace().CreatePowershellcmdlet(CreateCmdletName, CreateCmdletType, createParameters); //The fact that the line below doesn't throw is enough to prove that the cmdlet returns the expected object type really. Couldn't figure out a way to make the assert around the Powershell.invoke call var createResult = createPowershell.Invoke <LibraryVariableSetResource>().FirstOrDefault(); if (createResult != null) { Assert.AreEqual(createResult.Name, TestResourceName); Console.WriteLine("Created resource [{0}] of type [{1}]", createResult.Name, createResult.GetType()); } #endregion #region LibraryVariableSetDelete var removeParameters = new List <CmdletParameter> { new CmdletParameter() { Name = "Resource", Resource = createResult } }; var removePowershell = new CmdletRunspace().CreatePowershellcmdlet(RemoveCmdletName, RemoveCmdletType); var removeResult = removePowershell.Invoke <bool>(removeParameters).FirstOrDefault(); Assert.IsTrue(removeResult); Console.WriteLine("Deleted resource [{0}] of type [{1}]", createResult.Name, createResult.GetType()); #endregion }
public void Init() { _ps = Utilities.CreatePowerShell(CmdletName, typeof(AddLibraryVariable)); var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable); _variableSet.Variables.Clear(); var lib = new LibraryVariableSetResource { Name = "Octopus" }; var libs = new List <LibraryVariableSetResource> { lib }; lib.Links.Add("Variables", "variablesets-1"); octoRepo.Setup(o => o.LibraryVariableSets.FindOne(It.IsAny <Func <LibraryVariableSetResource, bool> >())) .Returns( (Func <LibraryVariableSetResource, bool> f) => (from l in libs where f(l) select l).FirstOrDefault()); octoRepo.Setup(o => o.Projects.FindByName("Gibberish")).Returns((ProjectResource)null); octoRepo.Setup(o => o.VariableSets.Get("variablesets-1")).Returns(_variableSet); var process = new DeploymentProcessResource(); process.Steps.Add(new DeploymentStepResource { Name = "Website", Id = "Step-1" }); octoRepo.Setup(o => o.DeploymentProcesses.Get("deploymentprocesses-1")).Returns(process); var envs = new List <EnvironmentResource> { new EnvironmentResource { Id = "Environments-1", Name = "DEV" } }; octoRepo.Setup(o => o.Environments.FindByNames(new[] { "DEV" })).Returns(envs); var machines = new List <MachineResource> { new MachineResource { Id = "Machines-1", Name = "web-01" } }; octoRepo.Setup(o => o.Machines.FindByNames(new[] { "web-01" })).Returns(machines); }
/// <summary> /// Creates a new ScriptModule LibraryVariableSet from a passed file. /// </summary> /// <param name="octRepository">The repository to call against.</param> /// <param name="scriptModuleName"></param> /// <param name="description"></param> /// <param name="scriptModuleText"></param> public static void CreateScriptModuleFromText(OctopusRepository octRepository, string scriptModuleName, string description, string scriptModuleText) { var libraryVariableSet = new LibraryVariableSetResource { ContentType = VariableSetContentType.ScriptModule, Description = description, Name = scriptModuleName }; var outputLibraryVariableSet = octRepository.LibraryVariableSets.Create(libraryVariableSet); var variableSet = octRepository.VariableSets.Get(outputLibraryVariableSet.VariableSetId); variableSet.Variables = new List <VariableResource> { new VariableResource { Name = string.Format(ResourceStrings.ScriptModuleNameFormat, scriptModuleName), Value = scriptModuleText } }; octRepository.VariableSets.Modify(variableSet); }
/// <summary> /// Gonna go out for a pack of smokes. /// </summary> /// <param name="octRepository">Ill be</param> /// <param name="variable">back</param> /// <param name="library">soon</param> public VariableInfoResource(OctopusRepository octRepository, VariableResource variable, LibraryVariableSetResource library) { Variable = variable; LibraryVariableSet = library; LibrarySetName = library.Name; VariableSetId = library.VariableSetId; LibrarySetId = library.Id; OctopusRepo = octRepository; Id = variable.Id; Name = variable.Name; Sensative = variable.IsSensitive; Value = variable.Value; DeploymentTimeValue = variable.Value; if (variable.Scope.Count > 0) { if (variable.Scope.ContainsKey(ScopeField.Environment)) { foreach (var environment in variable.Scope[ScopeField.Environment]) { EnvironmentScope.Add(EnvironmentHelper.GetEnvironmentById(OctopusRepo, environment)); } } if (variable.Scope.ContainsKey(ScopeField.Role)) { RoleScope = variable.Scope[ScopeField.Role].ToList(); } if (variable.Scope.ContainsKey(ScopeField.Machine)) { foreach (var machine in variable.Scope[ScopeField.Machine]) { MachineScope.Add(MachineHelper.GetMachineById(OctopusRepo, machine)); } } } }
/// <summary> /// Replaces a ScriptModule LibraryVariableSet from a passed string. /// </summary> /// <param name="octRepository">The repository to call against.</param> /// <param name="libraryVariableSet"></param> /// <param name="scriptModuleText"></param> public static void ReplaceScriptModuleFromText(OctopusRepository octRepository, LibraryVariableSetResource libraryVariableSet, string scriptModuleText) { var variableSet = octRepository.VariableSets.Get(libraryVariableSet.VariableSetId); variableSet.Variables.FirstOrDefault().Value = scriptModuleText; octRepository.VariableSets.Modify(variableSet); }
private LibraryVariableSet ReadLibraryVariableSet(LibraryVariableSetResource resource) { Logger.Info($"Downloading {nameof(LibraryVariableSetResource)}: {resource.Name}"); return(resource.ToModel(_repository)); }
/// <summary> /// Outputs a VariableSet LibraryVariableSet to a string. /// </summary> /// <param name="octRepository">The repository to call against.</param> /// <param name="libraryVariableSet"></param> public static string OutputVariableSetToText(OctopusRepository octRepository, LibraryVariableSetResource libraryVariableSet) { var variableSet = octRepository.VariableSets.Get(libraryVariableSet.VariableSetId); return(JsonConvert.SerializeObject(variableSet.Variables, Formatting.Indented)); }
private async Task <LibraryVariableSet> ReadLibraryVariableSet(LibraryVariableSetResource resource) { _logger.LogInformation($"Downloading {nameof(LibraryVariableSetResource)}: {resource.Name}"); return(await resource.ToModel(_repository)); }
public async Task AllEnvironmentsAreValidated([Frozen] Mock <IMachineRoleRepository> mockRole, [Frozen] Mock <IEnvironmentRepository> mockEnv, LibraryVariableSetResource libResource, List <EnvironmentResource> environments, List <string> roles, List <SecretVariable> vars, LibraryManager sut) { mockRole.Setup(m => m.GetAllRoleNames()).Returns(Task.FromResult(roles)); mockEnv.Setup(m => m.FindByName(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <object>())) .Returns <string, string, object>((n, _, __) => Task.FromResult(environments.Single(e => e.Name == n))) .Verifiable(); await sut.UpdateVars(vars, libResource.Name, environments.Select(e => e.Name), roles, false).ConfigureAwait(false); mockEnv.Verify(m => m.FindByName(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <object>()), Times.Exactly(environments.Count)); }
/// <summary> /// Replaces a VariableSet LibraryVariableSet from a passed string. /// </summary> /// <param name="octRepository">The repository to call against.</param> /// <param name="libraryVariableSet"></param> /// <param name="variableSetText"></param> public static void ReplaceVariableSetFromText(OctopusRepository octRepository, LibraryVariableSetResource libraryVariableSet, string variableSetText) { var newVariables = new List <VariableResource>(); var variables = JsonConvert.DeserializeObject <List <VariableResource> >(variableSetText); var variableSet = octRepository.VariableSets.Get(libraryVariableSet.VariableSetId); foreach (var variableToAdd in variables) { var newVariable = new VariableResource() { Name = variableToAdd.Name, Value = variableToAdd.Value, Scope = variableToAdd.Scope, IsSensitive = variableToAdd.IsSensitive, IsEditable = variableToAdd.IsEditable, Prompt = variableToAdd.Prompt }; if (newVariable.IsSensitive && newVariable.Value == null) { newVariable.Value = string.Empty; } newVariables.Add(newVariable); } variableSet.Variables = newVariables; octRepository.VariableSets.Modify(variableSet); }
public LibraryVariableSet DownloadLibraryVariableSet(LibraryVariableSetResource resource) { Logger.Trace($"Downloading {nameof(LibraryVariableSetResource)}: {resource.Name}"); return(resource.ToModel(_repository)); }
public async Task EmptyVariablesShoudBePassedToModify([Frozen] Mock <IOctopusAsyncRepository> mockRepo, [Frozen] Mock <IVariableSetRepository> mockVar, [Frozen] Mock <ILibraryVariableSetRepository> mockLib, LibraryVariableSetResource libResource, VariableSetResource varResource, LibraryManager sut) { mockLib.Setup(m => m.FindByName(It.Is <string>(s => s.Equals(libResource.Name)), It.IsAny <string>(), It.IsAny <string>())) .Returns(Task.FromResult(libResource)); mockVar.Setup(m => m.Get(It.Is <string>(s => s.Equals(varResource.Id)))) .Returns(Task.FromResult(varResource)); mockRepo.Setup(m => m.VariableSets).Returns(mockVar.Object); var expected = varResource.Variables.Count; var actual = await sut.ClearLibrarySet(libResource.Name).ConfigureAwait(false); Assert.Equal(expected, actual); mockVar.Verify(m => m.Modify(It.Is <VariableSetResource>(v => v.Variables.Count == 0)), Times.Once); }
public async Task VariablesAreAddedToSet([Frozen] Mock <IMachineRoleRepository> mockRole, [Frozen] Mock <IEnvironmentRepository> mockEnv, [Frozen] Mock <IVariableSetRepository> mockVar, LibraryVariableSetResource libResource, List <EnvironmentResource> environments, List <string> roles, List <SecretVariable> vars, LibraryManager sut) { mockEnv.Setup(m => m.FindByName(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <object>())) .Returns <string, string, object>((n, _, __) => Task.FromResult(environments.Single(e => e.Name == n))); mockRole.Setup(m => m.GetAllRoleNames()).Returns(Task.FromResult(roles)); await sut.UpdateVars(vars, libResource.Name, environments.Select(e => e.Name), roles, true).ConfigureAwait(false); mockVar.Verify(m => m.Modify(It.Is <VariableSetResource>(vsr => vars.All(sv => vsr.Variables.Any(vr => vr.Name == sv.Name)))), Times.Once); }
/// <summary> /// Removes the LibraryVariableSet to the passed Project. /// </summary> /// <param name="octRepository">The repository to call against.</param> /// <param name="project"></param> /// <param name="libraryVariableSet"></param> public static void RemoveLibararyVariableSetFromProject(OctopusRepository octRepository, ProjectResource project, LibraryVariableSetResource libraryVariableSet) { project.IncludedLibraryVariableSetIds.Remove(libraryVariableSet.Id); octRepository.Projects.Modify(project); }
/// <summary> /// Gathers the VariableSet From a LibraryVariableSet. /// </summary> /// <param name="octRepository">The repository to call against.</param> /// <param name="libraryVariableSet">LibraryVariableSet to gather from.</param> /// <returns>VariableSetResource</returns> public static VariableSetResource GetVariableSetFromLibraryVariableSet(OctopusRepository octRepository, LibraryVariableSetResource libraryVariableSet) { return(octRepository.VariableSets.Get(libraryVariableSet.VariableSetId)); }
/// <summary> /// Outputs a ScriptModule LibraryVariableSet to a string. /// </summary> /// <param name="octRepository">The repository to call against.</param> /// <param name="libraryVariableSet"></param> public static string OutputScriptModuleToText(OctopusRepository octRepository, LibraryVariableSetResource libraryVariableSet) { var variableSet = octRepository.VariableSets.Get(libraryVariableSet.VariableSetId); return(variableSet.Variables.FirstOrDefault().Value); }
public async Task ModifyCalledWhenApplyIsTrue([Frozen] Mock <IMachineRoleRepository> mockRole, [Frozen] Mock <IVariableSetRepository> mockVar, LibraryVariableSetResource libResource, List <EnvironmentResource> environments, List <string> roles, List <SecretVariable> vars, LibraryManager sut) { mockRole.Setup(m => m.GetAllRoleNames()).Returns(Task.FromResult(roles)); await sut.UpdateVars(vars, libResource.Name, environments.Select(e => e.Name), roles, true).ConfigureAwait(false); mockVar.Verify(m => m.Modify(It.IsAny <VariableSetResource>()), Times.Once); }