Exemple #1
0
 public static DeploymentAction ToModel(this DeploymentActionResource resource, IOctopusRepository repository)
 {
     return(new DeploymentAction(
                resource.Name,
                resource.ActionType,
                resource.Properties.ToModel(), resource.Environments.ToModel(repository.Environments)));
 }
        public void With_Arguments()
        {
            // Execute cmdlet
            _ps.AddCommand(CmdletName)
            .AddArgument("Octopus")
            .AddArgument("Website")
            .AddArgument("Webservice");
            _ps.Invoke();

            Assert.AreEqual(2, _process.Steps.Count);

            var step = _process.Steps[1];

            Assert.AreEqual("Webservice", step.Name);
            Assert.AreNotEqual("deploymentsteps-1", step.Id);

            var action = new DeploymentActionResource {
                Name = "Webservice"
            };

            action.Environments.Add("environments-1");

            var actionResource = step.Actions[0];

            Assert.AreEqual(action.Name, actionResource.Name);
            Assert.AreEqual("NuGet", actionResource.ActionType);

            Assert.AreEqual(action.Environments.ToString(), actionResource.Environments.ToString());
            Assert.IsTrue(actionResource.Properties.ContainsKey("Something"));
            Assert.AreEqual("Value", actionResource.Properties["Something"].Value);
            Assert.IsTrue(actionResource.Properties["SomethingElse"].IsSensitive);
            Assert.AreEqual("Secret", actionResource.Properties["SomethingElse"].SensitiveValue.NewValue);
        }
        private async Task <string> GetNuGetPackageIdFromAction(DeploymentActionResource action, string projectId)
        {
            PropertyValueResource nuGetPackageId;

            if (action.Properties.TryGetValue("Octopus.Action.Package.NuGetPackageId", out nuGetPackageId) ||
                action.Properties.TryGetValue("Octopus.Action.Package.PackageId", out nuGetPackageId))
            {
                // some packages are actually referenced by hashes (so a.Properties["Octopus.Action.Package.NuGetPackageId"] = "{#NuGetPackage}")
                const string regexPattern = @"\#\{[a-zA-Z]+\}";
                var          regex        = new Regex(regexPattern, RegexOptions.IgnoreCase);
                var          match        = regex.Match(nuGetPackageId.Value);
                if (match.Success)
                {
                    var refKey      = nuGetPackageId.Value.Replace("#{", "").Replace("}", "");
                    var variableSet = await _octopusProxy.GetVariableSetForProject(projectId);

                    var variable = variableSet.Variables.FirstOrDefault(v => string.Equals(refKey, v.Name));
                    if (variable != null)
                    {
                        return(variable.Value);
                    }
                }
            }
            return(nuGetPackageId?.Value);
        }
Exemple #4
0
 public static DeploymentActionResource UpdateWith(this DeploymentActionResource resource, DeploymentAction model, IOctopusRepository repository)
 {
     resource.Name       = model.Name;
     resource.ActionType = model.ActionType;
     resource.Properties.UpdateWith(model.Properties);
     resource.Environments.UpdateWith(model.EnvironmentRefs.Select(r => repository.Environments.ResolveResourceId(r)));
     return(resource);
 }
Exemple #5
0
 public static async Task <DeploymentAction> ToModel(this DeploymentActionResource resource, IOctopusAsyncRepository repository)
 {
     return(new DeploymentAction(
                resource.Name,
                resource.ActionType,
                resource.Properties.ToModel(),
                await Task.WhenAll(resource.Environments.ToModel(repository.Environments))));
 }
        private static string GetNuGetFeedIdFromAction(DeploymentActionResource action)
        {
            PropertyValueResource nuGetFeed;

            return((action.Properties.TryGetValue("Octopus.Action.Package.NuGetFeedId", out nuGetFeed) ||
                    action.Properties.TryGetValue("Octopus.Action.Package.FeedId", out nuGetFeed))
                ? nuGetFeed.Value
                : null);
        }
        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", It.IsAny <string>(), It.IsAny <object>())).Returns(project);
            octoRepo.Setup(o => o.Projects.FindByName("Gibberish", It.IsAny <string>(), It.IsAny <object>())).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.Properties.Add("SomethingElse", new PropertyValueResource("Secret", true));

            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());
        }
Exemple #8
0
        static void UpdateChannels(DeploymentActionResource resource, DeploymentAction model, ProjectResource projectResource, IOctopusRepository repository)
        {
            List <ChannelResource> channelResources = new List <ChannelResource>();

            foreach (var channelRef in model.ChannelRefs)
            {
                var channel = repository.Channels.FindByName(projectResource, channelRef.Name);
                channelResources.Add(channel);
            }

            if (channelResources.Any())
            {
                resource.ForChannels(channelResources.ToArray());
            }
        }
Exemple #9
0
        private void CopyActions(DeploymentStepResource step, DeploymentStepResource newStep)
        {
            foreach (var action in step.Actions)
            {
                var newAction = new DeploymentActionResource
                {
                    Name       = GetName(action.Name),
                    ActionType = action.ActionType,
                };

                newAction.Environments.AddRange(action.Environments);
                newAction.Properties.AddRange(action.Properties);
                newAction.SensitiveProperties.AddRange(action.SensitiveProperties);

                newStep.Actions.Add(newAction);
            }
        }
Exemple #10
0
        private static string GetNuGetPackageIdFromAction(DeploymentActionResource action)
        {
            PropertyValueResource nuGetPackageId;

            if (action.Properties.TryGetValue("Octopus.Action.Package.NuGetPackageId", out nuGetPackageId))
            {
                // some packages are actually referenced by hashes (so a.Properties["Octopus.Action.Package.NuGetPackageId"] = "{#NuGetPackage}")
                var regexPattern = @"\#\{[a-zA-Z]+\}";
                var regex        = new Regex(regexPattern, RegexOptions.IgnoreCase);
                var match        = regex.Match(nuGetPackageId.Value);
                if (match.Success)
                {
                    // TODO: clean up this refKey nonsense
                    var refKey = nuGetPackageId.Value.Replace("#{", "").Replace("}", "");
                    nuGetPackageId = action.Properties[refKey];
                }
            }
            return(nuGetPackageId.Value);
        }
Exemple #11
0
        private void CopyActions(DeploymentStepResource step, DeploymentStepResource newStep)
        {
            foreach (var action in step.Actions)
            {
                var newAction = new DeploymentActionResource
                {
                    Name       = GetName(action.Name),
                    ActionType = action.ActionType,
                };

                foreach (var env in action.Environments)
                {
                    newAction.Environments.Add(env);
                }

                foreach (var property in action.Properties)
                {
                    newAction.Properties.Add(property.Key, property.Value);
                }

                newStep.Actions.Add(newAction);
            }
        }
Exemple #12
0
 public static DeploymentActionResource WithChannel(this DeploymentActionResource action, string channelId)
 {
     action.Channels.Add(channelId);
     return(action);
 }
Exemple #13
0
 public static DeploymentActionResource Disabled(this DeploymentActionResource action)
 {
     action.IsDisabled = true;
     return(action);
 }
Exemple #14
0
 public static DeploymentActionResource WithPackage(this DeploymentActionResource action)
 {
     action.Properties["Octopus.Action.Package.PackageId"] = TestHelpers.GetId("package");
     return(action);
 }
Exemple #15
0
        static void Main(string[] args)
        {
            var octoUrl = "";
            var apiKey  = "";
            var client  = new OctopusClient(new OctopusServerEndpoint(octoUrl, apiKey));
            var repo    = new OctopusRepository(client);

            //Project you want to create or modify
            var project = new ProjectResource
            {
                Name           = "", //name of project
                ProjectGroupId = "", //group project should be added to.  to find by name: repo.ProjectGroups.FindByName("").Id)
                LifecycleId    = ""  //lifecycle project should use. to find by name: repo.Lifecycles.FindByName("").Id
            };

            var newproject = repo.Projects.FindByName(project.Name);

            //Check if project exists, if not create it
            if (newproject == null)
            {
                //Creates Project
                repo.Projects.Create(project);
                //get the new project
                newproject = repo.Projects.FindByName(project.Name);
            }
            if (newproject == null)
            {
                Console.WriteLine($"{project.Name} didn't exist, creating...");
                repo.Projects.Create(project);
                Console.WriteLine($"New Project {project.Name} Created!");
                newproject = repo.Projects.FindByName(project.Name);
            }
            Console.WriteLine($"New Project {project.Name} Found");

            //This will find the step template you want to add
            var chainStepTemplate = repo.ActionTemplates.FindByName("Step Template"); //Change this to the name of your step template to add

            Console.WriteLine("Template Found");

            //Get the deployment process for the project
            var deploymentProcess = repo.DeploymentProcesses.Get(newproject.DeploymentProcessId);

            //Initialize a new Deployment Step Resource
            var deploymentStep = new DeploymentStepResource
            {
                Name         = "New Step", //Change this to what you want the step to be named.
                Condition    = DeploymentStepCondition.Success,
                StartTrigger = DeploymentStepStartTrigger.StartAfterPrevious
            };

            //Initialize new Action
            var action = new DeploymentActionResource
            {
                Name       = deploymentStep.Name,
                ActionType = chainStepTemplate.ActionType,
            };

            Console.WriteLine("Adding actions");
            //Add the template Id
            action.Properties.Add(new KeyValuePair <string, PropertyValueResource>("Octopus.Action.Template.Id", chainStepTemplate.Id));
            //Add the template Version
            action.Properties.Add(new KeyValuePair <string, PropertyValueResource>("Octopus.Action.Template.Version", chainStepTemplate.Version.ToString()));

            //Add the parameters for the step template and any default values.  Modify this to set other values.
            foreach (var parameter in chainStepTemplate.Parameters)
            {
                var property = new KeyValuePair <string, PropertyValueResource>(parameter.Name, parameter.DefaultValue);

                action.Properties.Add(property);
                Console.WriteLine($"{parameter.Name}");
            }

            //Add the standard properties
            foreach (var property in chainStepTemplate.Properties)
            {
                action.Properties.Add(property);
                Console.WriteLine($"{property.Key}");
            }

            //Add the action to your deployment step
            deploymentStep.Actions.Add(action);
            Console.WriteLine("Action added");
            //Add your step to the deployment process
            deploymentProcess.Steps.Add(deploymentStep);
            Console.WriteLine("Step added to Deployment Process");

            //Finally save the deployment process.
            try
            {
                repo.DeploymentProcesses.Modify(deploymentProcess);
                Console.WriteLine("Deployment Process was modified");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
        public CopyChannelTests()
        {
            _ps       = Utilities.CreatePowerShell(CmdletName, typeof(CopyChannel));
            _octoRepo = Utilities.AddOctopusRepo(_ps.Runspace.SessionStateProxy.PSVariable);

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

            // Create projects
            _octoRepo.Setup(o => o.Projects.FindByName("Octopus", It.IsAny <string>(), It.IsAny <object>())).Returns(project);
            _octoRepo.Setup(o => o.Projects.FindByName("Gibberish", It.IsAny <string>(), It.IsAny <object>())).Returns((ProjectResource)null);

            // Create channel
            var action = new DeploymentActionResource {
                Name = "NuGet", ActionType = "NuGet"
            };

            action.Environments.Add("environments-1");
            action.Properties.Add("Something", "Value");
            action.Properties.Add("SomethingElse", new PropertyValueResource("Secret", true));

            _channel = new ChannelResource
            {
                ProjectId   = "project-1",
                Id          = "channel-1",
                LifecycleId = "lifecycle-1",
                Name        = "Priority",
                Description = "Description",
                IsDefault   = true,
                Links       = new LinkCollection {
                    ["LinkA1"] = "HrefA1", ["LinkA2"] = "HrefA2"
                },
                Rules = new List <ChannelVersionRuleResource>
                {
                    new ChannelVersionRuleResource
                    {
                        Id           = "version-rule-1",
                        VersionRange = "version-range",
                        Actions      = new ReferenceCollection {
                            "action-1", "action-2"
                        },
                        Tag   = "tag",
                        Links = new LinkCollection {
                            ["LinkB1"] = "HrefB1", ["LinkB2"] = "HrefB2"
                        },
                    }
                },
                TenantTags = new ReferenceCollection {
                    "tag1", "tag2"
                }
            };

            _octoRepo.Setup(o => o.Channels.FindByName(project, "Priority")).Returns(_channel);
            _octoRepo.Setup(o => o.Channels.FindByName(It.IsAny <ProjectResource>(), "Default")).Returns((ChannelResource)null);
        }
        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);
        }