Skip to content

Commit 2623e0c

Browse files
Account set cmdlets
1 parent 9b10fea commit 2623e0c

File tree

7 files changed

+144
-10
lines changed

7 files changed

+144
-10
lines changed

src/ResourceManager/Automation/Commands.Automation/Cmdlet/NewAzureAutomationAccount.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System.Collections;
1516
using System.Collections.Generic;
1617
using System.Management.Automation;
1718
using System.Security.Permissions;
@@ -42,13 +43,26 @@ public class NewAzureAutomationAccount : AzureAutomationBaseCmdlet
4243
[Parameter(Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The geo region of the automation account")]
4344
public string Location { get; set; }
4445

46+
/// <summary>
47+
/// Gets or sets the plan.
48+
/// </summary>
49+
[Parameter( Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The plan of the automation account")]
50+
public string Plan { get; set; }
51+
52+
/// <summary>
53+
/// Gets or sets the module tags.
54+
/// </summary>
55+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The automation account tags.")]
56+
[Alias("Tag")]
57+
public IDictionary Tags { get; set; }
58+
4559
/// <summary>
4660
/// Execute this cmdlet.
4761
/// </summary>
4862
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
4963
public override void ExecuteCmdlet()
5064
{
51-
var account = this.AutomationClient.CreateAutomationAccount(this.ResourceGroupName, this.Name, this.Location);
65+
var account = this.AutomationClient.CreateAutomationAccount(this.ResourceGroupName, this.Name, this.Location, this.Plan, this.Tags);
5266
this.WriteObject(account);
5367
}
5468
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System.Collections;
16+
using System.Collections.Generic;
17+
using System.Management.Automation;
18+
using System.Security.Permissions;
19+
using Microsoft.Azure.Commands.Automation.Common;
20+
using Microsoft.Azure.Commands.Automation.Model;
21+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
22+
23+
namespace Microsoft.Azure.Commands.Automation.Cmdlet
24+
{
25+
/// <summary>
26+
/// Creates azure automation accounts based on automation account name and location.
27+
/// </summary>
28+
[Cmdlet(VerbsCommon.Set, "AzureAutomationAccount", DefaultParameterSetName = AutomationCmdletParameterSets.ByName)]
29+
[OutputType(typeof(AutomationAccount))]
30+
public class SetAzureAutomationAccount : AzureAutomationBaseCmdlet
31+
{
32+
/// <summary>
33+
/// Gets or sets the automation account name.
34+
/// </summary>
35+
[Parameter(Position = 1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The automation account name.")]
36+
[Alias("AutomationAccountName")]
37+
[ValidateNotNullOrEmpty]
38+
public string Name { get; set; }
39+
40+
/// <summary>
41+
/// Gets or sets the plan.
42+
/// </summary>
43+
[Parameter( Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The plan of the automation account")]
44+
public string Plan { get; set; }
45+
46+
/// <summary>
47+
/// Gets or sets the module tags.
48+
/// </summary>
49+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The automation account tags.")]
50+
[Alias("Tag")]
51+
public IDictionary Tags { get; set; }
52+
53+
/// <summary>
54+
/// Execute this cmdlet.
55+
/// </summary>
56+
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
57+
public override void ExecuteCmdlet()
58+
{
59+
var account = this.AutomationClient.UpdateAutomationAccount(this.ResourceGroupName, this.Name, this.Plan, this.Tags);
60+
this.WriteObject(account);
61+
}
62+
}
63+
}

src/ResourceManager/Automation/Commands.Automation/Commands.ResourceManagement.Automation.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
</ItemGroup>
117117
<ItemGroup>
118118
<Compile Include="Cmdlet\AzureAutomationBaseCmdlet.cs" />
119+
<Compile Include="Cmdlet\SetAzureAutomationAccount.cs" />
119120
<Compile Include="Cmdlet\RemoveAzureAutomationAccount.cs" />
120121
<Compile Include="Cmdlet\NewAzureAutomationAccount.cs" />
121122
<Compile Include="Cmdlet\GetAzureAutomationAccount.cs" />

src/ResourceManager/Automation/Commands.Automation/Common/AutomationClient.cs

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ void SetClientIdHeader(string clientRequestId)
7878
{
7979
Requires.Argument("ResourceGroupName", resourceGroupName).NotNull();
8080

81-
// todo fix paging
8281
return AutomationManagementClient
8382
.ContinuationTokenHandler(
8483
skipToken =>
@@ -90,7 +89,7 @@ void SetClientIdHeader(string clientRequestId)
9089
}).Select(c => new Model.AutomationAccount(resourceGroupName, c));
9190
}
9291

93-
public Model.AutomationAccount GetAutomationAccount(string resourceGroupName, string automationAccountName)
92+
public AutomationAccount GetAutomationAccount(string resourceGroupName, string automationAccountName)
9493
{
9594
Requires.Argument("ResourceGroupName", resourceGroupName).NotNull();
9695
Requires.Argument("AutomationAccountName", automationAccountName).NotNull();
@@ -100,12 +99,15 @@ public Model.AutomationAccount GetAutomationAccount(string resourceGroupName, st
10099
return new Model.AutomationAccount(resourceGroupName, account);
101100
}
102101

103-
public Model.AutomationAccount CreateAutomationAccount(string resourceGroupName, string automationAccountName, string location)
102+
public AutomationAccount CreateAutomationAccount(string resourceGroupName, string automationAccountName, string location, string plan, IDictionary tags)
104103
{
105104
Requires.Argument("ResourceGroupName", resourceGroupName).NotNull();
106105
Requires.Argument("Location", location).NotNull();
107106
Requires.Argument("AutomationAccountName", automationAccountName).ValidAutomationAccountName();
108107

108+
IDictionary<string, string> accountTags = null;
109+
if (tags != null) accountTags = tags.Cast<DictionaryEntry>().ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.ToString());
110+
109111
var accountCreateParameters = new AutomationAccountCreateOrUpdateParameters()
110112
{
111113
Location = location,
@@ -114,19 +116,57 @@ public Model.AutomationAccount CreateAutomationAccount(string resourceGroupName,
114116
{
115117
Sku = new Sku()
116118
{
117-
// todo take input
118-
Name = "Free",
119-
Capacity = 1,
119+
Name = String.IsNullOrWhiteSpace(plan) ? Constants.DefaultPlan : plan,
120120
}
121-
}
121+
},
122+
Tags = accountTags
122123
};
123124

124125
var account =
125126
this.automationManagementClient.AutomationAccounts.CreateOrUpdate(resourceGroupName,
126127
accountCreateParameters).AutomationAccount;
127128

128129

129-
return new Model.AutomationAccount(resourceGroupName, account);
130+
return new AutomationAccount(resourceGroupName, account);
131+
}
132+
133+
public AutomationAccount UpdateAutomationAccount(string resourceGroupName, string automationAccountName, string plan, IDictionary tags)
134+
{
135+
Requires.Argument("ResourceGroupName", resourceGroupName).NotNull();
136+
Requires.Argument("AutomationAccountName", automationAccountName).NotNull();
137+
138+
var automationAccount = GetAutomationAccount(resourceGroupName, automationAccountName);
139+
140+
IDictionary<string, string> accountTags = null;
141+
if (tags != null)
142+
{
143+
accountTags = tags.Cast<DictionaryEntry>()
144+
.ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.ToString());
145+
}
146+
else
147+
{
148+
accountTags = automationAccount.Tags.Cast<DictionaryEntry>().ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.ToString()); ;
149+
}
150+
151+
var accountUpdateParameters = new AutomationAccountPatchParameters()
152+
{
153+
Name = automationAccountName,
154+
Properties = new AutomationAccountPatchProperties()
155+
{
156+
Sku = new Sku()
157+
{
158+
Name = String.IsNullOrWhiteSpace(plan) ? automationAccount.Plan : plan,
159+
}
160+
},
161+
Tags = accountTags,
162+
};
163+
164+
var account =
165+
this.automationManagementClient.AutomationAccounts.Patch(resourceGroupName,
166+
accountUpdateParameters).AutomationAccount;
167+
168+
169+
return new AutomationAccount(resourceGroupName, account);
130170
}
131171

132172
public void DeleteAutomationAccount(string resourceGroupName, string automationAccountName)

src/ResourceManager/Automation/Commands.Automation/Common/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class Constants
2828

2929
public const string Draft = "Draft";
3030

31+
public const string DefaultPlan = "Free";
32+
3133
public const string AutomationServicePrefix = "OaasCS";
3234

3335
public const string JobStartedByParameterName = "MicrosoftApplicationManagementStartedBy";

src/ResourceManager/Automation/Commands.Automation/Common/IAutomationClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public interface IAutomationClient
3131

3232
AutomationAccount GetAutomationAccount(string resourceGroupName, string automationAccountName);
3333

34-
AutomationAccount CreateAutomationAccount(string resourceGroupName, string automationAccountName, string location);
34+
AutomationAccount CreateAutomationAccount(string resourceGroupName, string automationAccountName, string location, string plan, IDictionary tags);
35+
36+
AutomationAccount UpdateAutomationAccount(string resourceGroupName, string automationAccountName, string plan, IDictionary tags);
3537

3638
void DeleteAutomationAccount(string resourceGroupName, string automationAccountName);
3739

src/ResourceManager/Automation/Commands.Automation/Model/AutomationAccount.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16+
using System.Collections;
1617
using Microsoft.Azure.Commands.Automation.Common;
1718

1819
namespace Microsoft.Azure.Commands.Automation.Model
@@ -41,6 +42,12 @@ public AutomationAccount(string resourceGroupName, AutomationManagement.Models.A
4142
this.ResourceGroupName = resourceGroupName;
4243
this.AutomationAccountName = automationAccount.Name;
4344
this.Location = automationAccount.Location;
45+
46+
this.Tags = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
47+
foreach (var kvp in automationAccount.Tags)
48+
{
49+
this.Tags.Add(kvp.Key, kvp.Value);
50+
}
4451

4552
if (automationAccount.Properties == null) return;
4653

@@ -90,5 +97,10 @@ public AutomationAccount()
9097
/// Gets or sets the LastPublishTime.
9198
/// </summary>
9299
public DateTimeOffset LastModifiedTime { get; set; }
100+
101+
/// <summary>
102+
/// Gets or sets the tags.
103+
/// </summary>
104+
public Hashtable Tags { get; set; }
93105
}
94106
}

0 commit comments

Comments
 (0)