Skip to content

Commit b420237

Browse files
committed
Merge pull request #3 from Azure/clu
Clu
2 parents 7198df7 + edb0832 commit b420237

File tree

79 files changed

+101390
-46821
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+101390
-46821
lines changed

clu-getstart.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ To test on osx/linux boxes, do #1, open `<repo-root>\drop\clurun`, copy the flav
6363
(All of those are subject to change, contact yugangw or adxsdkdev for any questions)
6464

6565
### Quick introductions on cmdlets
66-
* Run commands using the ‘az’ prefix, cmdlet nouns, and cmdlet verbs, for example, `az environment get` maps to the cmdlet `Get-AzureRmEnvironment`
66+
* Run commands using the ‘az’ prefix, cmdlet nouns, and cmdlet verbs, for example, `az env get` maps to the cmdlet `Get-AzureRmEnvironment`
6767
* Cmdlet parameters use the double dash (--) so for example, getting a subscription with a particular name would be: `az subscription get –-SubscriptionName “name of subscription"`
6868
* To log in, 3 options
6969
* login interactively using device flow, this is the only option for msa account or any org-id with 2fa enforced, example: `az account add`
@@ -73,14 +73,14 @@ To test on osx/linux boxes, do #1, open `<repo-root>\drop\clurun`, copy the flav
7373
```az subscription get --SubscriptionName | az context set```
7474
* You can capture piped output using redirection to a file - the result will be the json serialization of the output object.
7575
```az subscription get > subscriptions.json```
76-
* You can use file input tu aparameter using '@' notation:
76+
* You can use file input to a parameter using '@' notation:
7777
```az command --param1 @file1.json```
7878
Reads input from file1.json and attempts to deserialize the .net object that is the Parameter type for ```param1```
7979
```az command --param1 @@file1.json```
8080
Does the same thing, but treats the input from ```file1.json``` as if it come from the pipeline, so that multiple objects will result in multiple invocations of ```ProcessRecord()``` for the target cmdlet.
8181
* There are some known issues with the current approach to sessions, which can cause session variables to not be propagated when running cmdlets in a pipeline, to work around this, set the 'CmdletSessionId' environment variable to a numeric value - all cmdlets running from the shell will use that session id, and sessions will work with pipelining
8282

83-
```set CmdletSessionId=1010 ```
83+
```set AzureProfile=1010 ```
8484

8585
### Testing Cmdlets
8686

@@ -119,7 +119,8 @@ Please set the environment variables for either Username/Password (no 2FA) or Se
119119

120120
- To implement an xunit bash scenario test you must
121121
- Add a ```[Collection("SampleCollection")]``` attribute to your test class
122-
- Add a field to your class of type ```ScenarioTestFixture``` and add a constructor that initializes it
122+
- Add a field to your class of type ```ScenarioTestFixture``` and add a constructor that initializes it.
123+
123124
```C#
124125
[Collection("SampleCollection")]
125126
public class SampleTestClass

examples/compute-management/01-VirtualMachineSizes.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/env/bash
22
set -e
33
printf "\n=== Managing Virtual Machine Sizes in Azure Compute ===\n"
44

examples/compute-management/02-VirtualMachineCreation.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/env/bash
22
set -e
33
printf "\n=== Managing Virtual Machine Creation in Azure Compute ===\n"
44

examples/lib/helper.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/env/bash
22

33
randomName() {
44
echo "$1$RANDOM"

examples/lib/loginService.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#!/bin/bash
1+
#!/bin/env/bash
22
az account add --spn --appid "$spn" --secret "$secret" -t "$tenant" -s "$spnSubscription"

examples/lib/loginUser.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#!/bin/bash
1+
#!/bin/env/bash
22
az account add -u "$azureUser" -p "$password" -s "$userSubscription"

examples/lib/testrunner.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/env/bash
22
export TESTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
33
. $TESTDIR/helper.sh
44
export groupName=`randomName testrg`

examples/resource-management/01-ResourceGroups.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/env/bash
22
set -e
33
printf "\n=== Managing Resource Groups in Azure ===\n"
44

examples/resource-management/02-Resource.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/env/bash
22
set -e
33
printf "\n=== Managing Resources in Azure ===\n"
44

examples/resource-management/03-Deployments.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/env/bash
22
set -e
33
printf "\n=== Provisioning Deployments in Azure ===\n"
44

examples/resource-management/04-RoleAssignments.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/env/bash
22
set -e
33
printf "\n=== Managing Role Assignments in Azure ===\n"
44

examples/resource-management/05-RoleDefinitions.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/env/bash
22
set -e
33
printf "\n=== Managing Role Definitions in Azure ===\n"
44

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Param(
2+
[string]$rgname,
3+
[string]$resourceGroupLocation
4+
)
5+
Write-Host "=== Managing Storage Accounts Resources in Azure ==="
6+
7+
$stoname = 'sto' + $rgname;
8+
$stotype = 'Standard_GRS';
9+
$loc = 'West US';
10+
11+
New-AzureRmResourceGroup -Name $rgname -Location $resourceGroupLocation;
12+
13+
Write-Host "1. Create a new storage account"
14+
New-AzureRmStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype;
15+
16+
Write-Host "2. Get info of a storage account"
17+
$stos = Get-AzureRmStorageAccount -ResourceGroupName $rgname;
18+
19+
Write-Host "3. Update storage account type"
20+
$stotype = 'Standard_RAGRS';
21+
Set-AzureRmStorageAccount -ResourceGroupName $rgname -Name $stoname -Type $stotype;
22+
23+
Write-Host "4. Get account key of a storage account"
24+
$stokeys=Get-AzureRmStorageAccountKey -ResourceGroupName $rgname -Name $stoname;
25+
26+
Write-Host "5. Renew key1 of a storage account"
27+
New-AzureRmStorageAccountKey -ResourceGroupName $rgname -Name $stoname -KeyName key1;
28+
29+
Write-Host "6. Renew key2 of a storage account"
30+
New-AzureRmStorageAccountKey -ResourceGroupName $rgname -Name $stoname -KeyName key2;
31+
32+
Write-Host "7. Remove storage account"
33+
Remove-AzureRmStorageAccount -ResourceGroupName $rgname -Name $stoname;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
printf "\n=== Managing Storage Accounts Resources in Azure ===\n"
3+
export BASEDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
4+
5+
azure group get -n $groupName
6+
if [ $? -ne 0 ]; then
7+
printf "\n Creating group %s in location %s \n" $groupName $location
8+
azure group create -n "$groupName" --location "$location"
9+
fi
10+
11+
set -e
12+
accountName=`randomName $groupName`
13+
accountType="Standard_GRS"
14+
15+
printf "\n1. Creating storage account %s in resrouce group %s with type %s in location %s \n" $accountName $groupName $accountType $location
16+
azure storage account new -g "$groupName" -n "$accountName" -t "$accountType" -l "$location"
17+
18+
printf "\n2. Get account info of storage account %s in group %s\n" $accountName $groupName
19+
azure storage account get -g $groupName -n $accountName > $BASEDIR/$accountName.json
20+
[ $(cat $BASEDIR/$accountName.json | jq '.ResourceGroupName' --raw-output) == "$groupName" ]
21+
[ $(cat $BASEDIR/$accountName.json | jq '.StorageAccountName' --raw-output) == "$accountName" ]
22+
[ $(cat $BASEDIR/$accountName.json | jq '.AccountType' --raw-output) == "$accountType" ]
23+
[ $(cat $BASEDIR/$accountName.json | jq '.Location' --raw-output) == "$location" ]
24+
rm -f $BASEDIR/$accountName.json
25+
26+
accountType1="Standard_RAGRS"
27+
printf "\n3. Set account type from %s to %s of storage account %s in group %s\n" $accountType $accountType1 $accountName $groupName
28+
azure storage account set -g $groupName -n $accountName -t "$accountType1" > $BASEDIR/$accountName.json
29+
[ $(cat $BASEDIR/$accountName.json | jq '.AccountType' --raw-output) == "$accountType1" ]
30+
rm -f $BASEDIR/$accountName.json
31+
32+
printf "\n4. Get account key of storage account %s in group %s\n" $accountName $groupName
33+
azure storage account key get -g $groupName -n $accountName > $BASEDIR/$accountName.json
34+
key1=$(cat $BASEDIR/$accountName.json | jq '.key1' --raw-output)
35+
key2=$(cat $BASEDIR/$accountName.json | jq '.key2' --raw-output)
36+
[ $key1 != $key2 ]
37+
rm -f $BASEDIR/$accountName.json
38+
39+
printf "\n5. Renew account key1 of storage account %s in group %s\n" $accountName $groupName
40+
azure storage account key new -g $groupName -n $accountName -k "key1"
41+
azure storage account key get -g $groupName -n $accountName > $BASEDIR/$accountName.json
42+
[ $(cat $BASEDIR/$accountName.json | jq '.key1' --raw-output) != $key1 ]
43+
rm -f $BASEDIR/$accountName.json
44+
45+
printf "\n6. Renew account key2 of storage account %s in group %s\n" $accountName $groupName
46+
azure storage account key new -g $groupName -n $accountName -k "key2"
47+
azure storage account key get -g $groupName -n $accountName > $BASEDIR/$accountName.json
48+
[ $(cat $BASEDIR/$accountName.json | jq '.key2' --raw-output) != $key2 ]
49+
rm -f $BASEDIR/$accountName.json
50+
51+
printf "\n7. Removing account %s in group %s\n" $accountName $groupName
52+
azure storage account remove -g $groupName -n $accountName

examples/virtual-hard-disk/01-VirtualHardDisks.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/env/bash
22
set -e
33
printf "\n=== Managing Virtual Hard Disks in Azure Compute ===\n"
44

examples/webapp-management/01-AppServicePlan.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/env/bash
22
set -e
33
printf "\n=== Managing App Service Plans in Azure ===\n"
44

examples/webapp-management/02-WebAppSlot.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/env/bash
22
set -e
33
printf "\n=== Managing Web App Slot in Azure ===\n"
44

examples/webapp-management/03-WebApp.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/env/bash
22
printf "\n=== Managing Web Apps in Azure ===\n"
33

44
printf "\nSetup: Creating a new resource group: %s at location: %s.\n" "$groupName" "$location"

src/CLU/Commands.ScenarioTests.ResourceManager.Common/Mocks/MockCommandRuntime.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Globalization;
2121
using System.Management.Automation;
2222
using System.Management.Automation.Host;
23+
using Microsoft.CLU;
2324

2425
namespace Microsoft.Azure.Commands.Common.Test.Mocks
2526
{
@@ -285,6 +286,8 @@ public override Version Version
285286
}
286287
}
287288

289+
public override OutputFormat RequestedOutputFormat { get; set; }
290+
288291
class MockPSHostUI : PSHostUserInterface
289292
{
290293
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)

src/CLU/Microsoft.Azure.Commands.Management.Storage/Models/PSStorageAccount.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ public static PSStorageAccount Create(StorageModels.StorageAccount storageAccoun
8282
var credentials = StorageUtilities.GenerateStorageCredentials(client, result.ResourceGroupName, result.StorageAccountName);
8383
CloudStorageAccount account = new CloudStorageAccount(
8484
credentials,
85-
new Uri(storageAccount.PrimaryEndpoints.Blob),
86-
new Uri(storageAccount.PrimaryEndpoints.Queue),
87-
new Uri(storageAccount.PrimaryEndpoints.Table),
88-
new Uri(storageAccount.PrimaryEndpoints.File));
85+
null == storageAccount.PrimaryEndpoints.Blob ? null : new Uri(storageAccount.PrimaryEndpoints.Blob),
86+
null == storageAccount.PrimaryEndpoints.Queue ? null : new Uri(storageAccount.PrimaryEndpoints.Queue),
87+
null == storageAccount.PrimaryEndpoints.Table ? null : new Uri(storageAccount.PrimaryEndpoints.Table),
88+
null == storageAccount.PrimaryEndpoints.File ? null : new Uri(storageAccount.PrimaryEndpoints.File));
8989
result.Context = new AzureStorageContext(account);
9090
return result;
9191
}

src/CLU/Microsoft.Azure.Commands.Management.Storage/StorageAccount/NewAzureStorageAccount.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public class NewAzureStorageAccountCommand : StorageAccountBaseCmdlet
7272
ValueFromPipelineByPropertyName = true,
7373
HelpMessage = "Storage Account Tags.")]
7474
[ValidateNotNull]
75-
[Alias("t")]
7675
public Hashtable[] Tags { get; set; }
7776

7877
protected override void ProcessRecord()

src/CLU/Microsoft.Azure.Commands.Management.Storage/StorageAccount/NewAzureStorageAccountKey.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class NewAzureStorageAccountKeyCommand : StorageAccountBaseCmdlet
5050
ValueFromPipelineByPropertyName = true,
5151
HelpMessage = "Storage Account Key StorageAccountName.")]
5252
[ValidateSet(Key1, Key2, IgnoreCase = true)]
53+
[Alias("k")]
5354
public string KeyName { get; set; }
5455

5556
protected override void ProcessRecord()

src/CLU/Microsoft.Azure.Commands.Management.Storage/StorageAccount/SetAzureStorageAccount.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ public class SetAzureStorageAccountCommand : StorageAccountBaseCmdlet
9191
HelpMessage = "Storage Account Tags.")]
9292
[AllowEmptyCollection]
9393
[ValidateNotNull]
94-
[Alias("t")]
9594
public Hashtable[] Tags { get; set; }
9695

9796
protected override void ProcessRecord()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
using System.Management.Automation;
7+
8+
namespace Microsoft.CLU.Test
9+
{
10+
[Cmdlet(VerbsCommon.New, "String")]
11+
public class StringGenerator : PSCmdlet
12+
{
13+
public StringGenerator()
14+
{
15+
Count = 10;
16+
StringFormat = "String {0}";
17+
}
18+
19+
[Parameter()]
20+
public int Count { get; set; }
21+
22+
[Parameter()]
23+
public string StringFormat { get; set; }
24+
25+
protected override void ProcessRecord()
26+
{
27+
base.ProcessRecord();
28+
for (int i = 1; i <= Count; ++i)
29+
{
30+
WriteObject(String.Format(StringFormat, i));
31+
}
32+
}
33+
34+
}
35+
}

src/CLU/Microsoft.CLU/CommandModel/CmdletCommandModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public CommandModelErrorCode Run(ConfigurationDictionary commandConfiguration, s
4343
// The runtime host is a Cmdlet's path to accessing system features, such as Console I/O
4444
// and session state. The runtime instance is created here and passed into the binder,
4545
// which will be creating the Cmdlet instance.
46-
var runtimeHost = new System.Management.Automation.Host.CLUHost(arguments, hostStreamInfo);
46+
var runtimeHost = new System.Management.Automation.Host.CLUHost(ref arguments, hostStreamInfo);
4747

4848
// Create instance of ICommandBinder and ICommand implementation for cmdlet model
4949
var binderAndCommand = new CmdletBinderAndCommand(commandConfiguration, runtimeHost);

src/CLU/Microsoft.CLU/System.Management.Automation/CLUHost.cs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal class CLUHost : PSHost, ICommandRuntime
2222
/// Creates an instance of CLUHost.
2323
/// </summary>
2424
/// <param name="args">The commandline arguments</param>
25-
internal CLUHost(string[] args, HostStreamInfo hostStreamInfo)
25+
internal CLUHost(ref string[] args, HostStreamInfo hostStreamInfo)
2626
{
2727
Debug.Assert(args != null);
2828
Debug.Assert(hostStreamInfo != null);
@@ -37,12 +37,29 @@ internal CLUHost(string[] args, HostStreamInfo hostStreamInfo)
3737
if (!string.IsNullOrEmpty(vpref))
3838
_doVerbose = InterpretStreamPreference(Constants.VerbosePreference, vpref, _doVerbose);
3939

40+
var argList = args.ToList();
41+
4042
// Command-line switch overrides environment variable
41-
if (args.Select(arg => arg.ToLowerInvariant()).Where(arg => arg.Equals("--debug") || arg.Equals("/debug")).Any())
43+
if (RemoveArgumentIfFound(argList, "debug"))
44+
{
4245
_doDebug = Constants.CmdletPreferencesInquire;
43-
44-
if (args.Select(arg => arg.ToLowerInvariant()).Where(arg => arg.Equals("--verbose") || arg.Equals("/verbose")).Any())
46+
}
47+
48+
if (RemoveArgumentIfFound(argList, "verbose"))
49+
{
4550
_doVerbose = Constants.CmdletPreferencesContinue;
51+
}
52+
53+
if (RemoveArgumentIfFound(argList, "json"))
54+
{
55+
this.RequestedOutputFormat = OutputFormat.JSON;
56+
}
57+
else if (RemoveArgumentIfFound(argList, "display"))
58+
{
59+
this.RequestedOutputFormat = OutputFormat.Display;
60+
}
61+
62+
args = argList.ToArray();
4663
}
4764

4865
internal CLUHost(string[] args, HostStreamInfo hostStreamInfo, string debugPreference, string verbosePreference)
@@ -82,6 +99,11 @@ internal HostStreamInfo StreamInfo
8299
/// <remarks>If this is true, your code is not doing console I/O</remarks>
83100
public override bool IsInputRedirected { get { return _hostStreamInfo.IsInputRedirected; } }
84101

102+
/// <summary>
103+
/// Command line override of requested output format...
104+
/// </summary>
105+
public override OutputFormat RequestedOutputFormat { get; set; }
106+
85107
/// <summary>
86108
/// Tells whether the STDOUT byte stream has been redirected to a file or pipe
87109
/// </summary>
@@ -514,6 +536,19 @@ private string InterpretStreamPreference(string variable, string input, string c
514536
new ChoiceDescription("&Halt Command", "Stop this command.")
515537
};
516538
private HostStreamInfo _hostStreamInfo;
539+
private bool RemoveArgumentIfFound(List<string> args, string argName)
540+
{
541+
int index = args.FindIndex(arg => arg.ToLowerInvariant().Equals($"--{argName}") || arg.ToLowerInvariant().Equals($"/{argName}"));
542+
if (index > -1)
543+
{
544+
args.RemoveAt(index);
545+
return true;
546+
}
547+
else
548+
{
549+
return false;
550+
}
551+
}
517552

518553
#endregion
519554

0 commit comments

Comments
 (0)