Skip to content

[Blueprints] Update Export and Import cmdlets with improvements #9462

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

Merged
merged 4 commits into from
Jun 21, 2019
Merged
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
10 changes: 1 addition & 9 deletions src/Blueprint/Blueprint/Az.Blueprint.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,7 @@ PrivateData = @{
# IconUri = ''

# ReleaseNotes of this module
ReleaseNotes = '* Added new cmdlets:
- New-AzBlueprint
- Set-AzBlueprint
- Publish-AzBlueprint
- New-AzBlueprintArtifact
- Set-AzBlueprintArtifact
- Get-AzBlueprintArtifact
- Export-AzBlueprintWithArtifact
- Import-AzBlueprintWithArtifact'''
ReleaseNotes = 'Bug fixes and improvements'

# Prerelease string of this module
# Prerelease = ''
Expand Down
1 change: 1 addition & 0 deletions src/Blueprint/Blueprint/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
* Bug fixes and improvements

## Version 0.2.0
* Added new cmdlets:
Expand Down
4 changes: 2 additions & 2 deletions src/Blueprint/Blueprint/Cmdlets/BlueprintCmdletBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected string GetValidatedFilePath(string path, string fileName)
}

/// <summary>
/// Combines input folder path and folder name and check if the resulting path exists.
/// Combines input folder path and folder name and check if the resulting path exists.
/// </summary>
/// <param name="inputPath"></param>
/// <param name="folderName"></param>
Expand All @@ -182,7 +182,7 @@ protected string GetValidatedFolderPath(string path, string folderName)

if (!AzureSession.Instance.DataStore.DirectoryExists(artifactsPath))
{
throw new DirectoryNotFoundException($"Can't find folder {folderName} in path {resolvedPath}.");
artifactsPath = null;
}

return artifactsPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ private async Task<string> GetManagementGroupAncestorsAsync(string subscriptionI

protected void ImportBlueprint(string blueprintName, string scope, string inputPath, bool force)
{
var blueprintPath = GetValidatedFilePath(inputPath, blueprintName);
const string blueprintFileName = "Blueprint";
var blueprintPath = GetValidatedFilePath(inputPath, blueprintFileName);

BlueprintModel bpObject;
try
Expand Down Expand Up @@ -149,8 +150,17 @@ protected string CreateFolderIfNotExist(string path, string folderName)
{
var folderPath = Path.Combine(path, folderName);

if (!AzureSession.Instance.DataStore.DirectoryExists(folderPath))
if (AzureSession.Instance.DataStore.DirectoryExists(folderPath))
{
AzureSession.Instance.DataStore.EmptyDirectory(folderPath);

if (AzureSession.Instance.DataStore.DirectoryExists(Path.Combine(folderPath, "Artifacts")))
{
AzureSession.Instance.DataStore.DeleteDirectory(Path.Combine(folderPath, "Artifacts"));
}
}
else
{
AzureSession.Instance.DataStore.CreateDirectory(folderPath);
}

Expand All @@ -163,11 +173,12 @@ protected void ImportArtifacts(string blueprintName, string scope, string inputP

var artifactsPath = GetValidatedFolderPath(inputPath, artifacts);

var artifactFiles = AzureSession.Instance.DataStore.GetFiles(artifactsPath, "*.json", SearchOption.TopDirectoryOnly);
if (artifactsPath == null)
{
return; // if blueprint doesn't contain artifacts don't proceed.
}

// To-Do - Remove this before release.
/*DirectoryInfo artifactsDirectory = new DirectoryInfo(artifactsPath);
FileInfo[] artifactsFiles = artifactsDirectory.GetFiles("*.json");*/
var artifactFiles = AzureSession.Instance.DataStore.GetFiles(artifactsPath, "*.json", SearchOption.TopDirectoryOnly);

foreach (var artifactFile in artifactFiles)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using Microsoft.Azure.Commands.Blueprint.Models;
using System.IO;
using System.Linq;
using System.Management.Automation;
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.PowerShell.Cmdlets.Blueprint.Properties;
Expand Down Expand Up @@ -48,38 +49,45 @@ public class ExportAzureRmBlueprint : BlueprintDefinitionCmdletBase
#region Cmdlet Overrides
public override void ExecuteCmdlet()
{
// Get serialized blueprint and write it to disk
const string blueprintFileName = "Blueprint";

// Get serialized blueprint definition
string serializedDefinition = BlueprintClient.GetBlueprintDefinitionJsonFromObject(Blueprint, Version);

var blueprintFolderPath = CreateFolderIfNotExist(OutputPath, Blueprint.Name);
var blueprintJsonFilePath = Path.Combine(blueprintFolderPath, $"{Blueprint.Name}.json");
var resolvedPath = ResolveUserPath(OutputPath);

var blueprintFolderPath = Path.Combine(resolvedPath, Blueprint.Name);

// if directory exists, let's ask if we can clean contents of it. If directory doesn't exist, we'll create one.
this.ConfirmAction(
this.Force || !AzureSession.Instance.DataStore.FileExists(blueprintJsonFilePath),
string.Format(Resources.OverwriteExistingOutputFileProcessMessage,Blueprint.Name),
Resources.OverwriteExistingOutputFileContinueMessage,
blueprintJsonFilePath,
() => AzureSession.Instance.DataStore.WriteFile(blueprintJsonFilePath, serializedDefinition)
this.Force || !AzureSession.Instance.DataStore.DirectoryExists(blueprintFolderPath),
string.Format(Resources.DeleteBlueprintFolderContentsProcessString, Blueprint.Name),
Resources.DeleteBlueprintFolderContentsContinueMessage,
blueprintFolderPath,
() => CreateFolderIfNotExist(resolvedPath, Blueprint.Name)
);

var blueprintJsonFilePath = Path.Combine(blueprintFolderPath, $"{blueprintFileName}.json");

// Get serialized artifacts from this blueprint and write them to disk
var artifactsPath = CreateFolderIfNotExist(blueprintFolderPath, "Artifacts");

AzureSession.Instance.DataStore.WriteFile(blueprintJsonFilePath, serializedDefinition);

var artifacts = BlueprintClient.ListArtifacts(Blueprint.Scope, Blueprint.Name, Version);
foreach (var artifact in artifacts)

if (artifacts != null && artifacts.Any())
{
string serializedArtifact = BlueprintClient.GetBlueprintArtifactJsonFromObject(Blueprint.Scope, Blueprint.Name, artifact, Version);
// Get serialized artifacts from this blueprint and write them to disk
var artifactsPath = CreateFolderIfNotExist(blueprintFolderPath, "Artifacts");

foreach (var artifact in artifacts)
{
string serializedArtifact =
BlueprintClient.GetBlueprintArtifactJsonFromObject(Blueprint.Scope, Blueprint.Name, artifact,
Version);

var artifactFilePath = Path.Combine(artifactsPath, artifact.Name + ".json");
var artifactFilePath = Path.Combine(artifactsPath, artifact.Name + ".json");

this.ConfirmAction(
this.Force || !AzureSession.Instance.DataStore.FileExists(artifactFilePath),
string.Format(Resources.OverwriteExistingOutputFileProcessMessage, artifact.Name),
Resources.OverwriteExistingOutputFileContinueMessage,
artifactFilePath,
() => AzureSession.Instance.DataStore.WriteFile(artifactFilePath, serializedArtifact)
);
AzureSession.Instance.DataStore.WriteFile(artifactFilePath, serializedArtifact);
}
}

if (PassThru.IsPresent)
Expand Down
18 changes: 18 additions & 0 deletions src/Blueprint/Blueprint/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Blueprint/Blueprint/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@
<data name="DeleteAssignmentShouldProcessString" xml:space="preserve">
<value>Delete blueprint assignment '{0}'</value>
</data>
<data name="DeleteBlueprintFolderContentsContinueMessage" xml:space="preserve">
<value>Deleting folder contents.</value>
</data>
<data name="DeleteBlueprintFolderContentsProcessString" xml:space="preserve">
<value>Folder '{0}' already exists. This operation will replace contents of the folder with specified blueprint and its artifacts. Would you like to continue?</value>
</data>
<data name="OverwriteExistingOutputFileContinueMessage" xml:space="preserve">
<value>Overwriting the output file.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Export-AzBlueprintWithArtifact -Blueprint <PSBlueprintBase> -OutputPath <String>
```

## DESCRIPTION
Export a blueprint definition with its artifacts and save to disk.
Export a blueprint definition with its artifacts and save to disk. This cmdlet exports the latest version(draft or published) of the blueprint.

## EXAMPLES

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Import a blueprint definition with its artifacts.

### Example 1
```powershell
PS C:\> Import-AzBlueprintWithArtifact -Name SimpleBlueprint -SubscriptionId 00000000-1111-0000-1111-000000000000 -InputPath C:\Blueprints
PS C:\> Import-AzBlueprintWithArtifact -Name MySimpleBlueprint -SubscriptionId 00000000-1111-0000-1111-000000000000 -InputPath C:\Blueprints\SimpleBlueprint
```

Import a blueprint definition with its artifacts and save within a subscription.
Expand Down