Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable import/export #186

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions source/Octo/Commands/ExportCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ public ExportCommand(IExporterLocator exporterLocator, IOctopusFileSystem fileSy
this.exporterLocator = exporterLocator;

var options = Options.For("Export");
options.Add("type=", "The type to export", v => Type = v);
options.Add("type=", "The type to export, either Project, Release or Variables", v => Type = v);
options.Add("filePath=", "The full path and name of the export file", v => FilePath = v);
options.Add("project=", "[Optional] Name of the project", v => Project = v);
options.Add("name=", "[Optional] Name of the item to export", v => Name = v);
options.Add("releaseVersion=", "[Optional] The version number, or range of version numbers to export", v => ReleaseVersion = v);
options.Add("name=", "Name of the item to export (only for --type=Project)", v => Name = v);
options.Add("project=", "Name of the project (only for --type=Release or --type=Variables)", v => Project = v);
options.Add("releaseVersion=", "The version number, or range of version numbers to export (only for --type=Release)", v => ReleaseVersion = v);
}


public string Type { get; set; }
public string FilePath { get; set; }
public string Project { get; set; }
Expand All @@ -44,7 +45,7 @@ protected override Task Execute()
throw new CommandException("Error: Unrecognized exporter '" + Type + "'");

Log.Debug("Beginning the export");
return exporter.Export(string.Format("FilePath={0}", FilePath), string.Format("Project={0}", Project), string.Format("Name={0}", Name), string.Format("ReleaseVersion={0}", ReleaseVersion));
return exporter.Export($"FilePath={FilePath}", $"Project={Project}", $"Name={Name}", $"ReleaseVersion={ReleaseVersion}");
}
}
}
8 changes: 4 additions & 4 deletions source/Octo/Commands/ImportCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public ImportCommand(IImporterLocator importerLocator, IOctopusFileSystem fileSy
this.importerLocator = importerLocator;

var options = Options.For("Import");
options.Add("type=", "The Octopus object type to import", v => Type = v);
options.Add("type=", "The Octopus object type to import, either Project, Release or Variables", v => Type = v);
options.Add("filePath=", "The full path and name of the exported file", v => FilePath = v);
options.Add("project=", "[Optional] The name of the project", v => Project = v);
options.Add("project=", "The name of the project (only for --type=Release or --type=Variables)", v => Project = v);
options.Add("dryRun", "[Optional] Perform a dry run of the import", v => DryRun = true);
}

Expand All @@ -41,11 +41,11 @@ protected override async Task Execute()
throw new CommandException("Error: Unrecognized importer '" + Type + "'");

Log.Debug("Validating the import");
var validationResult = await importer.Validate(string.Format("FilePath={0}", FilePath), string.Format("Project={0}", Project)).ConfigureAwait(false);
var validationResult = await importer.Validate($"FilePath={FilePath}", $"Project={Project}").ConfigureAwait(false);
if (validationResult && !DryRun)
{
Log.Debug("Beginning the import");
await importer.Import(string.Format("FilePath={0}", FilePath), string.Format("Project={0}", Project)).ConfigureAwait(false);
await importer.Import($"FilePath={FilePath}", $"Project={Project}").ConfigureAwait(false);
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions source/Octo/Exporters/ExporterAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ namespace Octopus.Cli.Exporters
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class ExporterAttribute : Attribute, IExporterMetadata
{
public ExporterAttribute(string name, string entityType)
public ExporterAttribute(string name)
{
Name = name;
EntityType = entityType;
}

public string EntityType { get; set; }


public string Name { get; set; }
public string Description { get; set; }
}
}
3 changes: 1 addition & 2 deletions source/Octo/Exporters/ExporterLocator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Linq;
using System.Reflection;
using Autofac;
using Serilog;
Expand Down
1 change: 0 additions & 1 deletion source/Octo/Exporters/IExporterMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ namespace Octopus.Cli.Exporters
public interface IExporterMetadata
{
string Name { get; set; }
string Description { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using Octopus.Client.Model;

namespace Octopus.Cli.Commands
namespace Octopus.Cli.Exporters
{
public class ProjectExport
{
Expand Down
4 changes: 2 additions & 2 deletions source/Octo/Exporters/ProjectExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Octopus.Cli.Exporters
{
[Exporter("project", "ProjectWithDependencies", Description = "Exports a project as JSON to a file")]
[Exporter("project")]
public class ProjectExporter : BaseExporter
{
readonly ActionTemplateRepository actionTemplateRepository;
Expand Down Expand Up @@ -164,7 +164,7 @@ protected override async Task Export(Dictionary<string, string> parameters)
ExportedAt = DateTime.Now,
OctopusVersion = Repository.Client.RootDocument.Version,
Type = typeof (ProjectExporter).GetAttributeValue((ExporterAttribute ea) => ea.Name),
ContainerType = typeof (ProjectExporter).GetAttributeValue((ExporterAttribute ea) => ea.EntityType)
ContainerType = "ProjectWithDependencies"
};
FileSystemExporter.Export(FilePath, metadata, export);
}
Expand Down
4 changes: 2 additions & 2 deletions source/Octo/Exporters/ReleaseExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Octopus.Cli.Exporters
{
[Exporter("release", "List", Description = "Exports either a single release, or multiple releases")]
[Exporter("release")]
public class ReleaseExporter : BaseExporter
{
public ReleaseExporter(IOctopusAsyncRepository repository, IOctopusFileSystem fileSystem, ILogger log) :
Expand Down Expand Up @@ -92,7 +92,7 @@ await releases.Paginate(Repository, page =>
ExportedAt = DateTime.Now,
OctopusVersion = Repository.Client.RootDocument.Version,
Type = typeof (ReleaseExporter).GetAttributeValue((ExporterAttribute ea) => ea.Name),
ContainerType = typeof (ReleaseExporter).GetAttributeValue((ExporterAttribute ea) => ea.EntityType)
ContainerType = "List"
};
FileSystemExporter.Export(FilePath, metadata, releasesToExport);
}
Expand Down
52 changes: 52 additions & 0 deletions source/Octo/Exporters/VariablesExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Serilog;
using Octopus.Cli.Commands;
using Octopus.Cli.Extensions;
using Octopus.Cli.Infrastructure;
using Octopus.Cli.Repositories;
using Octopus.Cli.Util;
using Octopus.Client;
using Octopus.Client.Model;

namespace Octopus.Cli.Exporters
{
[Exporter("variables")]
public class VariablesExporter : BaseExporter
{

public VariablesExporter(IOctopusAsyncRepository repository, IOctopusFileSystem fileSystem, ILogger log)
: base(repository, fileSystem, log)
{
}

protected override async Task Export(Dictionary<string, string> parameters)
{
var projectName = parameters["Project"];
if (string.IsNullOrWhiteSpace(projectName))
throw new CommandException("Please specify the name of the project to export using the parameter: --project=XYZ");


Log.Debug("Finding project: {Project:l}", projectName);
var project = await Repository.Projects.FindByName(projectName).ConfigureAwait(false);
if (project == null)
throw new CouldNotFindException("a project named", projectName);

Log.Debug("Finding variable set for project");
var variables = await Repository.VariableSets.Get(project.VariableSetId).ConfigureAwait(false);
if (variables == null)
throw new CouldNotFindException("variable set for project", project.Name);

var metadata = new ExportMetadata
{
ExportedAt = DateTime.Now,
OctopusVersion = Repository.Client.RootDocument.Version,
Type = "variables",
ContainerType = "VariableSet"
};
FileSystemExporter.Export(FilePath, metadata, variables);
}
}
}
1 change: 0 additions & 1 deletion source/Octo/Importers/IImporterMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ namespace Octopus.Cli.Importers
public interface IImporterMetadata
{
string Name { get; set; }
string Description { get; set; }
}
}
6 changes: 1 addition & 5 deletions source/Octo/Importers/ImporterAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ namespace Octopus.Cli.Importers
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class ImporterAttribute : Attribute, IImporterMetadata
{
public ImporterAttribute(string name, string entityType)
public ImporterAttribute(string name)
{
Name = name;
EntityType = entityType;
}

public string EntityType { get; set; }

public string Name { get; set; }
public string Description { get; set; }
}
}
5 changes: 3 additions & 2 deletions source/Octo/Importers/ProjectImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Serilog;
using Octopus.Cli.Commands;
using Octopus.Cli.Exporters;
using Octopus.Cli.Extensions;
using Octopus.Cli.Infrastructure;
using Octopus.Cli.Repositories;
Expand All @@ -15,7 +16,7 @@

namespace Octopus.Cli.Importers
{
[Importer("project", "ProjectWithDependencies", Description = "Imports a project from an export file")]
[Importer("project")]
public class ProjectImporter : BaseImporter
{
readonly protected ActionTemplateRepository actionTemplateRepository;
Expand Down Expand Up @@ -49,7 +50,7 @@ public ProjectImporter(IOctopusAsyncRepository repository, IOctopusFileSystem fi

protected override async Task<bool> Validate(Dictionary<string, string> paramDictionary)
{
var importedObject = FileSystemImporter.Import<ProjectExport>(FilePath, typeof(ProjectImporter).GetAttributeValue((ImporterAttribute ia) => ia.EntityType));
var importedObject = FileSystemImporter.Import<ProjectExport>(FilePath, "ProjectWithDependencies");

var project = importedObject.Project;
if (new SemanticVersion(Repository.Client.RootDocument.Version) >= new SemanticVersion(2, 6, 0, 0))
Expand Down
4 changes: 2 additions & 2 deletions source/Octo/Importers/ReleaseImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Octopus.Cli.Importers
{
[Importer("release", "List", Description = "Imports a projects releases from an export file")]
[Importer("release")]
public class ReleaseImporter : BaseImporter
{
ValidatedImportSettings validatedImportSettings;
Expand Down Expand Up @@ -44,7 +44,7 @@ protected override async Task<bool> Validate(Dictionary<string, string> paramDic
errorList.Add("Could not find project named '" + projectName + "'");
}

var releases = FileSystemImporter.Import<List<ReleaseResource>>(FilePath, typeof(ReleaseImporter).GetAttributeValue((ImporterAttribute ia) => ia.EntityType));
var releases = FileSystemImporter.Import<List<ReleaseResource>>(FilePath, "List");
if (releases == null)
errorList.Add("Unable to deserialize the specified export file");

Expand Down
82 changes: 82 additions & 0 deletions source/Octo/Importers/VariablesImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Serilog;
using Octopus.Cli.Extensions;
using Octopus.Cli.Util;
using Octopus.Client;
using Octopus.Client.Model;
using Octopus.Cli.Infrastructure;

namespace Octopus.Cli.Importers
{
[Importer("variables")]
public class VariablesImporter : BaseImporter
{
ValidatedImportSettings validatedImportSettings;
public bool ReadyToImport => validatedImportSettings != null && !validatedImportSettings.ErrorList.Any();

public VariablesImporter(IOctopusAsyncRepository repository, IOctopusFileSystem fileSystem, ILogger log)
: base(repository, fileSystem, log)
{
}

class ValidatedImportSettings : BaseValidatedImportSettings
{
public ProjectResource Project { get; set; }
public VariableSetResource Variables { get; internal set; }
}

protected override async Task<bool> Validate(Dictionary<string, string> parameters)
{
var projectName = parameters["Project"];
if (string.IsNullOrWhiteSpace(projectName))
{
Log.Error("Please specify the name of the project to export using the parameter: --project=XYZ");
return false;
}

Log.Debug("Finding project: {Project:l}", projectName);
var project = await Repository.Projects.FindByName(projectName).ConfigureAwait(false);
if (project == null)
{
Log.Error("a project named", projectName);
return false;
}

var variables = FileSystemImporter.Import<VariableSetResource>(FilePath, "VariableSet");
if (variables == null)
{
Log.Error("Unable to deserialize the specified export file");
return false;
}

validatedImportSettings = new ValidatedImportSettings
{
Project = project,
Variables = variables
};

return true;
}

protected override async Task Import(Dictionary<string, string> paramDictionary)
{
var project = validatedImportSettings.Project;
var variables = validatedImportSettings.Variables;

Log.Debug("Retrieving the current variables for {Project:l}", project.Name);
var current = await Repository.VariableSets.Get(project.VariableSetId);

variables.Id = current.Id;
variables.OwnerId = current.OwnerId;
variables.Version = current.Version;

Log.Debug("Updating the variables for {Project:l}", project.Name);
await Repository.VariableSets.Modify(variables);

Log.Debug("Successfully updated the variables for {Project:l}", project.Name);
}
}
}