public static bool IsEquals(this VariableInstance variableInstance, VariableResource variableResource)
        {
            if (variableInstance.Variable.Value == variableResource.Value)
            {
                if (variableInstance.Variable.IsEditable == variableResource.IsEditable)
                {
                    if (variableInstance.Variable.IsSensitive == variableResource.IsSensitive)
                    {
                        if (variableInstance.Variable.Id == variableResource.Id)
                        {
                            if (variableInstance.Variable.Scope.GetHashCode() == variableResource.Scope.GetHashCode())
                            {
                                    if (variableInstance.Variable.Prompt?.Required == variableResource.Prompt?.Required)
                                    {
                                        if (variableInstance.Variable.Prompt?.Label == variableResource.Prompt?.Label)
                                        {
                                            if (variableInstance.Variable.Prompt?.Description ==
                                                variableResource.Prompt?.Description)
                                            {
                                                return true;
                                            }
                                        }
                                    }
                            }
                        }
                    }
                }
            }

            return false;
        }
        public bool ShouldReturnVariable(VariableScopeValues scopeValues, VariableResource variable)
        {
            var scopes = BuildScopeMap(scopeValues, _filter);

            return variable
                .Scope
                .IsExcludedBy(scopes) == false;
        }
        public void Init()
        {
            _ps = Utilities.CreatePowerShell(CmdletName, typeof(UpdateVariable));
            var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);

            var variable = new VariableResource
            {
                Id = "variables-1",
                Name = "Test",
                Value = "Test Value",
                IsSensitive = false
            };
            variable.Scope.Add(ScopeField.Action, "actions-1");
            variable.Scope.Add(ScopeField.Environment, "environments-1");
            variable.Scope.Add(ScopeField.Role, "DB");

            _variableSet.Variables.Add(variable);

            var project = new ProjectResource { DeploymentProcessId = "deploymentprocesses-1" };
            project.Links.Add("Variables", "variablesets-1");
            octoRepo.Setup(o => o.Projects.FindByName("Octopus")).Returns(project);
            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"},
                new EnvironmentResource {Id = "environments-2", Name = "TEST"}
            };

            octoRepo.Setup(o => o.Environments.FindByNames(It.IsAny<string[]>()))
                .Returns((string[] names) => (from n in names
                    from e in envs
                    where e.Name.Equals(n, StringComparison.InvariantCultureIgnoreCase)
                    select e).ToList());

            var machines = new List<MachineResource>
            {
                new MachineResource {Id = "machines-1", Name = "db-01"},
                new MachineResource {Id = "machines-2", Name = "web-01"}
            };
            octoRepo.Setup(o => o.Machines.FindByNames(It.IsAny<string[]>())).Returns(
                (string[] names) => (from n in names
                                     from m in machines
                                     where m.Name.Equals(n, StringComparison.InvariantCultureIgnoreCase)
                                     select m).ToList());
        }
        private VariableResource CreateVariable(string key, VariableSetResource variableSet)
        {
            var scope = new ScopeSpecification();
            scope.AddRange(VariableFilter.BuildScopeMap(variableSet.ScopeValues, _config.Filter));

            var v = new VariableResource
            {
                Name = key,
                Scope = scope
            };

            variableSet.Variables.Add(v);

            return v;
        }
        public void Init()
        {
            _ps = Utilities.CreatePowerShell(CmdletName, typeof (RemoveVariable));
            var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);

            _variableSet.Variables.Clear();

            var project = new ProjectResource();
            project.Links.Add("Variables", "variablesets-1");
            octoRepo.Setup(o => o.Projects.FindByName("Octopus")).Returns(project);
            octoRepo.Setup(o => o.Projects.FindByName("Gibberish")).Returns((ProjectResource) null);

            _variable = new VariableResource {Name = "Azure"};
            _variableSet.Variables.Add(_variable);
            _variableSet.Variables.Add(new VariableResource {Name = "ConnectionString"});
            _variableSet.Variables.Add(new VariableResource {Name = "ServerName"});
            octoRepo.Setup(o => o.VariableSets.Get("variablesets-1")).Returns(_variableSet);
        }
Exemple #6
0
        public void Init()
        {
            _ps = Utilities.CreatePowerShell(CmdletName, typeof(CopyStep));
            var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);

            // Create project
            var project = new ProjectResource
            {
                Name = "Octopus",
                Description = "Test Source",
                DeploymentProcessId = "deploymentprocesses-1",
                VariableSetId = "variablesets-1",
            };

            // Create projects
            octoRepo.Setup(o => o.Projects.FindByName("Octopus")).Returns(project);
            octoRepo.Setup(o => o.Projects.FindByName("Gibberish")).Returns((ProjectResource)null);

            // Create deployment process
            var action = new DeploymentActionResource { Name = "NuGet", ActionType = "NuGet" };
            action.Environments.Add("environments-1");
            action.Properties.Add("Something", "Value");
            action.SensitiveProperties.Add("SomethingElse", "Secret");

            var step = new DeploymentStepResource { Id = "deploymentsteps-1", Name = "Website" };
            step.Actions.Add(action);

            _process = new DeploymentProcessResource();
            _process.Steps.Add(step);

            octoRepo.Setup(o => o.DeploymentProcesses.Get(It.IsIn(new[] { "deploymentprocesses-1" }))).Returns(_process);
            octoRepo.Setup(o => o.DeploymentProcesses.Get(It.IsIn(new[] { "deploymentprocesses-2" }))).Returns(new DeploymentProcessResource());

            // Create variable set
            var variable = new VariableResource { Name = "Name", Value = "Value" };
            variable.Scope.Add(ScopeField.Action, "DeploymentsActions-1");
            variable.Scope.Add(ScopeField.Environment, "Environments-1");

            var variableSet = new VariableSetResource();
            variableSet.Variables.Add(variable);

            octoRepo.Setup(o => o.VariableSets.Get(It.IsIn(new[] { "variablesets-1" }))).Returns(variableSet);
            octoRepo.Setup(o => o.VariableSets.Get(It.IsIn(new[] { "variablesets-2" }))).Returns(new VariableSetResource());
        }
        public VariableSetResource AddOrUpdateVariableValue(string name, string value, ScopeSpecification scope)
        {
            var existing = Variables.FirstOrDefault(x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase) && x.Scope.Equals(scope));
            if (existing == null)
            {
                var template = new VariableResource
                {
                    Name = name,
                    Value = value,
                    Scope = scope,
                };

                Variables.Add(template);
            }
            else
            {
                existing.Name = name;
                existing.Value = value;
                existing.Scope = scope;
            }

            return this;
        }
        public VariableSetResource AddOrUpdateVariableValue(string name, string value)
        {
            var existing = Variables.FirstOrDefault(x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase) &&
                                                    (x.Scope == null || x.Scope.Equals(new ScopeSpecification())));

            if (existing == null)
            {
                var template = new VariableResource
                {
                    Name  = name,
                    Value = value,
                };

                Variables.Add(template);
            }
            else
            {
                existing.Name  = name;
                existing.Value = value;
            }

            return(this);
        }
Exemple #9
0
        public void CopyVariables(IList<VariableResource> source, CopyScopeValue copyAction = null)
        {
            foreach (var variable in source)
            {
                if (variable.IsSensitive)
                {
                    const string warning =
                        "Variable '{0}' was sensitive. Sensitive flag has been removed and the value has been set to an empty string.";
                    _writeWarning(string.Format(warning, variable.Name));
                }

                var newVariable = new VariableResource
                {
                    Name = variable.Name,
                    IsEditable = variable.IsEditable,
                    IsSensitive = false,
                    Prompt = variable.Prompt,
                    Value = variable.IsSensitive ? "" : variable.Value,
                    Scope = CreateScope(variable.Scope, copyAction)
                };

                _variableSet.Add(newVariable);
            }
        }
        private void ProcessByParts()
        {
            var variable = new VariableResource { Name = Name, Value = Value, IsSensitive = Sensitive };

            if (Environments != null)
                AddEnvironments(variable);

            if (Machines != null)
                AddMachines(variable);

            if (Roles != null && Roles.Length > 0)
                variable.Scope.Add(ScopeField.Role, new ScopeValue(Roles));

            _variableSet.Variables.Add(variable);
        }
        private void AddMachines(VariableResource variable)
        {
            var machines = _octopus.Machines.FindByNames(Machines);
            var ids = machines.Select(m => m.Id).ToList();

            if (ids.Count > 0)
                variable.Scope.Add(ScopeField.Machine, new ScopeValue(ids));
        }
        private void AddEnvironments(VariableResource variable)
        {
            var environments = _octopus.Environments.FindByNames(Environments);
            var ids = environments.Select(environment => environment.Id).ToList();

            if (ids.Count > 0)
                variable.Scope.Add(ScopeField.Environment, new ScopeValue(ids));
        }
 public static bool VariablesAreEqual(this VariableResource vr1, VariableResource vr2)
 {
     return
         vr1.Id.Equals(vr2.Id) &&
         vr1.Scope.Equals(vr2.Scope) &&
         vr1.Name.Equals(vr2.Name) &&
         vr1.Value == vr2.Value &&
         vr1.IsSensitive.Equals(vr2.IsSensitive) &&
         vr1.IsEditable.Equals(vr2.IsEditable);
 }
        public void Init()
        {
            _ps = Utilities.CreatePowerShell(CmdletName, typeof (CopyProject));
            var octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);

            // Create a project group
            var groupResource = new ProjectGroupResource {Name = "Octopus", Id = "projectgroups-1"};
            octoRepo.Setup(o => o.ProjectGroups.FindByName("Octopus")).Returns(groupResource);
            octoRepo.Setup(o => o.ProjectGroups.FindByName("Gibberish")).Returns((ProjectGroupResource) null);

            // Create project
            var project = new ProjectResource
            {
                Name = "Source",
                Description = "Test Source",
                DeploymentProcessId = "deploymentprocesses-1",
                VariableSetId = "variablesets-1",
                DefaultToSkipIfAlreadyInstalled = true,
                IncludedLibraryVariableSetIds = new List<string> { "libraryvariablesets-1" },
                VersioningStrategy = new VersioningStrategyResource(),
                AutoCreateRelease = false,
                ReleaseCreationStrategy = new ReleaseCreationStrategyResource(),
                IsDisabled = false,
                LifecycleId = "lifecycle-1"
            };

            // Create projects
            _projects.Clear();
            _projects.Add(project);

            octoRepo.Setup(o => o.Projects.FindByName("Source")).Returns(project);
            octoRepo.Setup(o => o.Projects.FindByName("Gibberish")).Returns((ProjectResource) null);
            octoRepo.Setup(o => o.Projects.Create(It.IsAny<ProjectResource>())).Returns(
                delegate(ProjectResource p)
                {
                    p.VariableSetId = "variablesets-2";
                    p.DeploymentProcessId = "deploymentprocesses-2";
                    _projects.Add(p);
                    return p;
                }
            );

            // Create deployment process
            var action = new DeploymentActionResource {Name = "Action"};
            action.Environments.Add("environments-1");

            var step = new DeploymentStepResource { Id = "deploymentsteps-1", Name = "Database"};
            step.Actions.Add(action);

            var process = new DeploymentProcessResource();
            process.Steps.Add(step);

            octoRepo.Setup(o => o.DeploymentProcesses.Get(It.IsIn(new[] { "deploymentprocesses-1" }))).Returns(process);
            _copyProcess = new DeploymentProcessResource();
            octoRepo.Setup(o => o.DeploymentProcesses.Get(It.IsIn(new[] { "deploymentprocesses-2" }))).Returns(_copyProcess);

            // Create variable set
            var variable = new VariableResource { Name = "Name", Value = "Value" };
            variable.Scope.Add(ScopeField.Action, "deploymentsactions-1");
            variable.Scope.Add(ScopeField.Environment, "environments-1");

            var sourceVariables = new VariableSetResource();
            sourceVariables.Variables.Add(variable);

            octoRepo.Setup(o => o.VariableSets.Get(It.IsIn(new[] { "variablesets-1" }))).Returns(sourceVariables);
            _copyVariables = new VariableSetResource();
            octoRepo.Setup(o => o.VariableSets.Get(It.IsIn(new[] { "variablesets-2" }))).Returns(_copyVariables);
        }
        public void RevertChanges(VariableResource variable)
        {
            EnvironmentSelectedItems.Clear();
            MachineSelectedItems.Clear();
            RoleSelectedItems.Clear();
            ActionSelectedItems.Clear();

            KeyValuePair<string, object> environment = EnvironmentItems.First();

            SetSelectedItems(variable, environment.Value.ToString(), environment.Key);
        }
        private void SetSelectedItems(VariableResource variableResource, string environmentId, string environmentName)
        {
            foreach (KeyValuePair<ScopeField, ScopeValue> keyValuePair in variableResource.Scope)
            {
                if (keyValuePair.Key == ScopeField.Environment)
                {
                    foreach (string strValue in keyValuePair.Value)
                    {
                        if (strValue != environmentId)
                        {
                            throw new InvalidDataException("Expected environment id " + environmentId + ". Received " + strValue);
                        }

                        EnvironmentSelectedItems.Add(environmentName, environmentId);
                    }
                }
                else if (keyValuePair.Key == ScopeField.Role)
                {
                    foreach (string strValue in keyValuePair.Value)
                    {
                        if (!RoleItems.ContainsKey(strValue))
                        {
                            throw new InvalidDataException("A variable references a role called '" + strValue +
                                                           "' which does not exist.");
                        }

                        RoleSelectedItems.Add(strValue, strValue);
                    }
                }
                else if (keyValuePair.Key == ScopeField.Machine)
                {
                    foreach (string strValue in keyValuePair.Value)
                    {
                        if (!MachineItems.ContainsValue(strValue))
                        {
                            throw new InvalidDataException("A variable references a machine called '" + strValue +
                                                           "' which does not exist.");
                        }

                        MachineSelectedItems.Add(MachineItems.Single(mi => mi.Value.ToString() == strValue).Key,
                            strValue);
                    }
                }
                else if (keyValuePair.Key == ScopeField.Action)
                {
                    foreach (string strValue in keyValuePair.Value)
                    {
                        if (!ActionItems.ContainsValue(strValue))
                        {
                            throw new InvalidDataException("A variable references a action called '" + strValue +
                                                           "' which does not exist.");
                        }

                        ActionSelectedItems.Add(ActionItems.Single(mi => mi.Value.ToString() == strValue).Key, strValue);
                    }
                }
            }
        }