Skip to content

Commit 24dead8

Browse files
authored
Merge pull request Azure#9462 from filizt/ModuleUpdates
[Blueprints] Update Export and Import cmdlets with improvements
2 parents 73dc701 + 4aced03 commit 24dead8

File tree

9 files changed

+77
-41
lines changed

9 files changed

+77
-41
lines changed

src/Blueprint/Blueprint/Az.Blueprint.psd1

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,7 @@ PrivateData = @{
114114
# IconUri = ''
115115

116116
# ReleaseNotes of this module
117-
ReleaseNotes = '* Added new cmdlets:
118-
- New-AzBlueprint
119-
- Set-AzBlueprint
120-
- Publish-AzBlueprint
121-
- New-AzBlueprintArtifact
122-
- Set-AzBlueprintArtifact
123-
- Get-AzBlueprintArtifact
124-
- Export-AzBlueprintWithArtifact
125-
- Import-AzBlueprintWithArtifact'''
117+
ReleaseNotes = 'Bug fixes and improvements'
126118

127119
# Prerelease string of this module
128120
# Prerelease = ''

src/Blueprint/Blueprint/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Bug fixes and improvements
2122

2223
## Version 0.2.0
2324
* Added new cmdlets:

src/Blueprint/Blueprint/Cmdlets/BlueprintCmdletBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ protected string GetValidatedFilePath(string path, string fileName)
169169
}
170170

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

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

188188
return artifactsPath;

src/Blueprint/Blueprint/Cmdlets/BlueprintDefinition/BlueprintDefinitionCmdletBase.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ private async Task<string> GetManagementGroupAncestorsAsync(string subscriptionI
100100

101101
protected void ImportBlueprint(string blueprintName, string scope, string inputPath, bool force)
102102
{
103-
var blueprintPath = GetValidatedFilePath(inputPath, blueprintName);
103+
const string blueprintFileName = "Blueprint";
104+
var blueprintPath = GetValidatedFilePath(inputPath, blueprintFileName);
104105

105106
BlueprintModel bpObject;
106107
try
@@ -149,8 +150,17 @@ protected string CreateFolderIfNotExist(string path, string folderName)
149150
{
150151
var folderPath = Path.Combine(path, folderName);
151152

152-
if (!AzureSession.Instance.DataStore.DirectoryExists(folderPath))
153+
if (AzureSession.Instance.DataStore.DirectoryExists(folderPath))
153154
{
155+
AzureSession.Instance.DataStore.EmptyDirectory(folderPath);
156+
157+
if (AzureSession.Instance.DataStore.DirectoryExists(Path.Combine(folderPath, "Artifacts")))
158+
{
159+
AzureSession.Instance.DataStore.DeleteDirectory(Path.Combine(folderPath, "Artifacts"));
160+
}
161+
}
162+
else
163+
{
154164
AzureSession.Instance.DataStore.CreateDirectory(folderPath);
155165
}
156166

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

164174
var artifactsPath = GetValidatedFolderPath(inputPath, artifacts);
165175

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

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

172183
foreach (var artifactFile in artifactFiles)
173184
{

src/Blueprint/Blueprint/Cmdlets/BlueprintDefinition/ExportAzureRMBlueprint.cs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using Microsoft.Azure.Commands.Blueprint.Models;
1616
using System.IO;
17+
using System.Linq;
1718
using System.Management.Automation;
1819
using Microsoft.Azure.Commands.Common.Authentication;
1920
using Microsoft.Azure.PowerShell.Cmdlets.Blueprint.Properties;
@@ -48,38 +49,45 @@ public class ExportAzureRmBlueprint : BlueprintDefinitionCmdletBase
4849
#region Cmdlet Overrides
4950
public override void ExecuteCmdlet()
5051
{
51-
// Get serialized blueprint and write it to disk
52+
const string blueprintFileName = "Blueprint";
53+
54+
// Get serialized blueprint definition
5255
string serializedDefinition = BlueprintClient.GetBlueprintDefinitionJsonFromObject(Blueprint, Version);
5356

54-
var blueprintFolderPath = CreateFolderIfNotExist(OutputPath, Blueprint.Name);
55-
var blueprintJsonFilePath = Path.Combine(blueprintFolderPath, $"{Blueprint.Name}.json");
57+
var resolvedPath = ResolveUserPath(OutputPath);
58+
59+
var blueprintFolderPath = Path.Combine(resolvedPath, Blueprint.Name);
5660

61+
// if directory exists, let's ask if we can clean contents of it. If directory doesn't exist, we'll create one.
5762
this.ConfirmAction(
58-
this.Force || !AzureSession.Instance.DataStore.FileExists(blueprintJsonFilePath),
59-
string.Format(Resources.OverwriteExistingOutputFileProcessMessage,Blueprint.Name),
60-
Resources.OverwriteExistingOutputFileContinueMessage,
61-
blueprintJsonFilePath,
62-
() => AzureSession.Instance.DataStore.WriteFile(blueprintJsonFilePath, serializedDefinition)
63+
this.Force || !AzureSession.Instance.DataStore.DirectoryExists(blueprintFolderPath),
64+
string.Format(Resources.DeleteBlueprintFolderContentsProcessString, Blueprint.Name),
65+
Resources.DeleteBlueprintFolderContentsContinueMessage,
66+
blueprintFolderPath,
67+
() => CreateFolderIfNotExist(resolvedPath, Blueprint.Name)
6368
);
69+
70+
var blueprintJsonFilePath = Path.Combine(blueprintFolderPath, $"{blueprintFileName}.json");
6471

65-
// Get serialized artifacts from this blueprint and write them to disk
66-
var artifactsPath = CreateFolderIfNotExist(blueprintFolderPath, "Artifacts");
67-
72+
AzureSession.Instance.DataStore.WriteFile(blueprintJsonFilePath, serializedDefinition);
73+
6874
var artifacts = BlueprintClient.ListArtifacts(Blueprint.Scope, Blueprint.Name, Version);
69-
70-
foreach (var artifact in artifacts)
75+
76+
if (artifacts != null && artifacts.Any())
7177
{
72-
string serializedArtifact = BlueprintClient.GetBlueprintArtifactJsonFromObject(Blueprint.Scope, Blueprint.Name, artifact, Version);
78+
// Get serialized artifacts from this blueprint and write them to disk
79+
var artifactsPath = CreateFolderIfNotExist(blueprintFolderPath, "Artifacts");
80+
81+
foreach (var artifact in artifacts)
82+
{
83+
string serializedArtifact =
84+
BlueprintClient.GetBlueprintArtifactJsonFromObject(Blueprint.Scope, Blueprint.Name, artifact,
85+
Version);
7386

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

76-
this.ConfirmAction(
77-
this.Force || !AzureSession.Instance.DataStore.FileExists(artifactFilePath),
78-
string.Format(Resources.OverwriteExistingOutputFileProcessMessage, artifact.Name),
79-
Resources.OverwriteExistingOutputFileContinueMessage,
80-
artifactFilePath,
81-
() => AzureSession.Instance.DataStore.WriteFile(artifactFilePath, serializedArtifact)
82-
);
89+
AzureSession.Instance.DataStore.WriteFile(artifactFilePath, serializedArtifact);
90+
}
8391
}
8492

8593
if (PassThru.IsPresent)

src/Blueprint/Blueprint/Properties/Resources.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Blueprint/Blueprint/Properties/Resources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@
153153
<data name="DeleteAssignmentShouldProcessString" xml:space="preserve">
154154
<value>Delete blueprint assignment '{0}'</value>
155155
</data>
156+
<data name="DeleteBlueprintFolderContentsContinueMessage" xml:space="preserve">
157+
<value>Deleting folder contents.</value>
158+
</data>
159+
<data name="DeleteBlueprintFolderContentsProcessString" xml:space="preserve">
160+
<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>
161+
</data>
156162
<data name="OverwriteExistingOutputFileContinueMessage" xml:space="preserve">
157163
<value>Overwriting the output file.</value>
158164
</data>

src/Blueprint/Blueprint/help/Export-AzBlueprintWithArtifact.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Export-AzBlueprintWithArtifact -Blueprint <PSBlueprintBase> -OutputPath <String>
1818
```
1919

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

2323
## EXAMPLES
2424

src/Blueprint/Blueprint/help/Import-AzBlueprintWithArtifact.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Import a blueprint definition with its artifacts.
2424

2525
### Example 1
2626
```powershell
27-
PS C:\> Import-AzBlueprintWithArtifact -Name SimpleBlueprint -SubscriptionId 00000000-1111-0000-1111-000000000000 -InputPath C:\Blueprints
27+
PS C:\> Import-AzBlueprintWithArtifact -Name MySimpleBlueprint -SubscriptionId 00000000-1111-0000-1111-000000000000 -InputPath C:\Blueprints\SimpleBlueprint
2828
```
2929

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

0 commit comments

Comments
 (0)