Skip to content

Commit c4789bc

Browse files
committed
Added JobAccount cmdlets
1 parent 9ebcf1f commit c4789bc

File tree

10 files changed

+771
-2
lines changed

10 files changed

+771
-2
lines changed

src/ResourceManager/Sql/Commands.Sql/Commands.Sql.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@
140140
<Compile Include="Database Backup\Model\AzureSqlServerBackupLongTermRetentionVaultModel.cs" />
141141
<Compile Include="Database Backup\Model\AzureSqlDeletedDatabaseBackupModel.cs" />
142142
<Compile Include="Database Backup\Model\AzureSqlDatabaseGeoBackupModel.cs" />
143+
<Compile Include="JobAccount\Cmdlet\AzureSqlJobAccountCmdletBase.cs" />
144+
<Compile Include="JobAccount\Cmdlet\GetAzureSqlJobAccount.cs" />
145+
<Compile Include="JobAccount\Cmdlet\NewAzureSqlJobAccount.cs" />
146+
<Compile Include="JobAccount\Cmdlet\RemoveAzureSqlJobAccount.cs" />
147+
<Compile Include="JobAccount\Model\AzureSqlJobAccountModel.cs" />
148+
<Compile Include="JobAccount\Services\AzureSqlJobAccountAdapter.cs" />
149+
<Compile Include="JobAccount\Services\AzureSqlJobAccountCommunicator.cs" />
143150
<Compile Include="ThreatDetection\Cmdlet\GetAzureSqlServerThreatDetection.cs" />
144151
<Compile Include="ThreatDetection\Cmdlet\RemoveSqlServerThreatDetection.cs" />
145152
<Compile Include="ThreatDetection\Cmdlet\SetAzureSqlServerThreatDetection.cs" />
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 Microsoft.Azure.Commands.Common.Authentication.Models;
16+
using Microsoft.Azure.Commands.Sql.Common;
17+
using Microsoft.Azure.Commands.Sql.JobAccount.Adapter;
18+
using Microsoft.Azure.Commands.Sql.JobAccount.Model;
19+
using System.Collections.Generic;
20+
21+
namespace Microsoft.Azure.Commands.Sql.JobAccount.Cmdlet
22+
{
23+
public abstract class AzureSqlJobAccountCmdletBase : AzureSqlCmdletBase<IEnumerable<AzureSqlJobAccountModel>, AzureSqlJobAccountAdapter>
24+
{
25+
/// <summary>
26+
/// Intializes the model adapter
27+
/// </summary>
28+
/// <param name="subscription">The subscription the cmdlets are operation under</param>
29+
/// <returns>The server adapter</returns>
30+
protected override AzureSqlJobAccountAdapter InitModelAdapter(AzureSubscription subscription)
31+
{
32+
return new AzureSqlJobAccountAdapter(DefaultContext);
33+
}
34+
}
35+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 Microsoft.Azure.Commands.Sql.JobAccount.Model;
16+
using System.Collections.Generic;
17+
using System.Management.Automation;
18+
19+
namespace Microsoft.Azure.Commands.Sql.JobAccount.Cmdlet
20+
{
21+
/// <summary>
22+
/// Defines the Get-AzureRmSqlJobAccount cmdlet
23+
/// </summary>
24+
[Cmdlet(VerbsCommon.Get, "AzureRmSqlJobAccount", ConfirmImpact = ConfirmImpact.None)]
25+
public class GetAzureSqlJobAccount : AzureSqlJobAccountCmdletBase
26+
{
27+
/// <summary>
28+
/// Gets or sets the name of the database server to use.
29+
/// </summary>
30+
[Parameter(Mandatory = true,
31+
ValueFromPipelineByPropertyName = true,
32+
Position = 1,
33+
HelpMessage = "SQL Database server name.")]
34+
[ValidateNotNullOrEmpty]
35+
public string ServerName { get; set; }
36+
37+
/// <summary>
38+
/// Gets or sets the name of the job account to use.
39+
/// </summary>
40+
[Parameter(Mandatory = false,
41+
ValueFromPipelineByPropertyName = true,
42+
Position = 2,
43+
HelpMessage = "SQL Database job account name.")]
44+
[ValidateNotNullOrEmpty]
45+
public string JobAccountName { get; set; }
46+
47+
/// <summary>
48+
/// Gets one or more job accounts from the service.
49+
/// </summary>
50+
/// <returns>A single server</returns>
51+
protected override IEnumerable<AzureSqlJobAccountModel> GetEntity()
52+
{
53+
ModelAdapter.ThrowIfJobAccountNotSupportedByServer(this.ResourceGroupName, this.ServerName, this.clientRequestId);
54+
55+
if (this.MyInvocation.BoundParameters.ContainsKey("JobAccountName"))
56+
{
57+
return new List<AzureSqlJobAccountModel>
58+
{
59+
ModelAdapter.GetJobAccount(this.ResourceGroupName, this.ServerName, this.JobAccountName, this.clientRequestId)
60+
};
61+
}
62+
else
63+
{
64+
return ModelAdapter.GetJobAccount(this.ResourceGroupName, this.ServerName, this.clientRequestId);
65+
}
66+
}
67+
68+
/// <summary>
69+
/// No changes, thus nothing to persist.
70+
/// </summary>
71+
/// <param name="entity">The entity retrieved</param>
72+
/// <returns>The unchanged entity</returns>
73+
protected override IEnumerable<AzureSqlJobAccountModel> PersistChanges(IEnumerable<AzureSqlJobAccountModel> entity)
74+
{
75+
return entity;
76+
}
77+
78+
/// <summary>
79+
/// No user input to apply to model.
80+
/// </summary>
81+
/// <param name="model">The model to modify</param>
82+
/// <returns>The input model</returns>
83+
protected override IEnumerable<AzureSqlJobAccountModel> ApplyUserInputToModel(IEnumerable<AzureSqlJobAccountModel> model)
84+
{
85+
return model;
86+
}
87+
}
88+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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 Hyak.Common;
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
using System.Management.Automation;
19+
20+
namespace Microsoft.Azure.Commands.Sql.JobAccount.Cmdlet
21+
{
22+
/// <summary>
23+
/// Defines the Get-AzureRmSqlJobAccount cmdlet
24+
/// </summary>
25+
[Cmdlet(VerbsCommon.New, "AzureRmSqlJobAccount",
26+
ConfirmImpact = ConfirmImpact.Low, SupportsShouldProcess = true)]
27+
public class NewAzureSqlJobAccount : AzureSqlJobAccountCmdletBase
28+
{
29+
/// <summary>
30+
/// Gets or sets the name of the database server to use.
31+
/// </summary>
32+
[Parameter(Mandatory = true,
33+
ValueFromPipelineByPropertyName = true,
34+
HelpMessage = "SQL Database server name.")]
35+
[ValidateNotNullOrEmpty]
36+
public string ServerName { get; set; }
37+
38+
/// <summary>
39+
/// Gets or sets the name of the job account to use.
40+
/// </summary>
41+
[Parameter(Mandatory = true,
42+
ValueFromPipelineByPropertyName = true,
43+
HelpMessage = "SQL Database job account name.")]
44+
[ValidateNotNullOrEmpty]
45+
public string JobAccountName { get; set; }
46+
47+
/// <summary>
48+
/// Gets or sets the name of the database to use.
49+
/// </summary>
50+
[Parameter(Mandatory = true,
51+
ValueFromPipelineByPropertyName = true,
52+
HelpMessage = "SQL Database database name.")]
53+
[ValidateNotNullOrEmpty]
54+
public string DatabaseName { get; set; }
55+
56+
/// <summary>
57+
/// The tags to associate with the Azure Sql Database Server
58+
/// </summary>
59+
[Parameter(Mandatory = false,
60+
HelpMessage = "The tags to associate with the Azure Sql Job Account")]
61+
public Dictionary<string, string> Tags { get; set; }
62+
63+
/// <summary>
64+
/// Check to see if the job account already exists in this resource group.
65+
/// </summary>
66+
/// <returns>Null if the job account doesn't exist. Otherwise throws exception</returns>
67+
protected override IEnumerable<Model.AzureSqlJobAccountModel> GetEntity()
68+
{
69+
try
70+
{
71+
WriteDebugWithTimestamp("JobAccountName: {0}", JobAccountName);
72+
ModelAdapter.GetJobAccount(this.ResourceGroupName, this.ServerName, this.JobAccountName, this.clientRequestId);
73+
}
74+
catch (CloudException ex)
75+
{
76+
if (ex.Response.StatusCode == System.Net.HttpStatusCode.NotFound)
77+
{
78+
// This is what we want. We looked and there is no job account with this name.
79+
return null;
80+
}
81+
82+
// Unexpected exception encountered
83+
throw;
84+
}
85+
86+
// The job account already exists
87+
throw new PSArgumentException(
88+
string.Format(Properties.Resources.JobAccountNameExists, this.JobAccountName, this.ServerName),
89+
"JobAccountName");
90+
}
91+
92+
/// <summary>
93+
/// Generates the model from user input.
94+
/// </summary>
95+
/// <param name="model">This is null since the server doesn't exist yet</param>
96+
/// <returns>The generated model from user input</returns>
97+
protected override IEnumerable<Model.AzureSqlJobAccountModel> ApplyUserInputToModel(IEnumerable<Model.AzureSqlJobAccountModel> model)
98+
{
99+
string location = ModelAdapter.GetServerLocationAndThrowIfJobAccountNotSupportedByServer(this.ResourceGroupName, this.ServerName, this.clientRequestId);
100+
101+
List<Model.AzureSqlJobAccountModel> newEntity = new List<Model.AzureSqlJobAccountModel>
102+
{
103+
new Model.AzureSqlJobAccountModel
104+
{
105+
Location = location,
106+
ResourceGroupName = this.ResourceGroupName,
107+
ServerName = this.ServerName,
108+
JobAccountName = this.JobAccountName,
109+
DatabaseName = this.DatabaseName,
110+
Tags = this.Tags
111+
}
112+
};
113+
return newEntity;
114+
}
115+
116+
/// <summary>
117+
/// Sends the changes to the service -> Creates the job account
118+
/// </summary>
119+
/// <param name="entity">The job account to create</param>
120+
/// <returns>The created job account</returns>
121+
protected override IEnumerable<Model.AzureSqlJobAccountModel> PersistChanges(IEnumerable<Model.AzureSqlJobAccountModel> entity)
122+
{
123+
return new List<Model.AzureSqlJobAccountModel>
124+
{
125+
ModelAdapter.UpsertJobAccount(entity.First(), this.clientRequestId)
126+
};
127+
}
128+
}
129+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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.Generic;
16+
using System.Globalization;
17+
using System.Management.Automation;
18+
19+
namespace Microsoft.Azure.Commands.Sql.JobAccount.Cmdlet
20+
{
21+
/// <summary>
22+
/// Defines the Remove-AzureRmSqlJobAccount cmdlet
23+
/// </summary>
24+
[Cmdlet(VerbsCommon.Remove, "AzureRmSqlJobAccount",
25+
SupportsShouldProcess = true,
26+
ConfirmImpact = ConfirmImpact.High)]
27+
public class RemoveAzureSqlJobAccount : AzureSqlJobAccountCmdletBase
28+
{
29+
/// <summary>
30+
/// Gets or sets the name of the database server to use.
31+
/// </summary>
32+
[Parameter(Mandatory = true,
33+
ValueFromPipelineByPropertyName = true,
34+
Position = 1,
35+
HelpMessage = "SQL Database server name.")]
36+
[ValidateNotNullOrEmpty]
37+
public string ServerName { get; set; }
38+
39+
/// <summary>
40+
/// Gets or sets the name of the job account to use.
41+
/// </summary>
42+
[Parameter(Mandatory = true,
43+
ValueFromPipelineByPropertyName = true,
44+
Position = 2,
45+
HelpMessage = "SQL Database job account name.")]
46+
[ValidateNotNullOrEmpty]
47+
public string JobAccountName { get; set; }
48+
49+
/// <summary>
50+
/// Defines whether it is ok to skip the requesting of rule removal confirmation
51+
/// </summary>
52+
[Parameter(HelpMessage = "Skip confirmation message for performing the action")]
53+
public SwitchParameter Force { get; set; }
54+
55+
/// <summary>
56+
/// Gets the entity to delete
57+
/// </summary>
58+
/// <returns>The entity going to be deleted</returns>
59+
protected override IEnumerable<Model.AzureSqlJobAccountModel> GetEntity()
60+
{
61+
ModelAdapter.ThrowIfJobAccountNotSupportedByServer(this.ResourceGroupName, this.ServerName, this.clientRequestId);
62+
63+
return new List<Model.AzureSqlJobAccountModel>
64+
{
65+
ModelAdapter.GetJobAccount(this.ResourceGroupName, this.ServerName, this.JobAccountName, this.clientRequestId)
66+
};
67+
}
68+
69+
/// <summary>
70+
/// Apply user input. Here nothing to apply
71+
/// </summary>
72+
/// <param name="model">The result of GetEntity</param>
73+
/// <returns>The input model</returns>
74+
protected override IEnumerable<Model.AzureSqlJobAccountModel> ApplyUserInputToModel(IEnumerable<Model.AzureSqlJobAccountModel> model)
75+
{
76+
return model;
77+
}
78+
79+
/// <summary>
80+
/// Deletes the job account.
81+
/// </summary>
82+
/// <param name="entity">The job account being deleted</param>
83+
/// <returns>The job account that was deleted</returns>
84+
protected override IEnumerable<Model.AzureSqlJobAccountModel> PersistChanges(IEnumerable<Model.AzureSqlJobAccountModel> entity)
85+
{
86+
ModelAdapter.RemoveJobAccount(this.ResourceGroupName, this.ServerName, this.JobAccountName, this.clientRequestId);
87+
return entity;
88+
}
89+
90+
/// <summary>
91+
/// Entry point for the cmdlet
92+
/// </summary>
93+
public override void ExecuteCmdlet()
94+
{
95+
if (!Force.IsPresent && !ShouldProcess(
96+
string.Format(CultureInfo.InvariantCulture, Properties.Resources.RemoveAzureSqlJobAccountDescription, this.JobAccountName, this.ServerName),
97+
string.Format(CultureInfo.InvariantCulture, Properties.Resources.RemoveAzureSqlJobAccountWarning, this.JobAccountName, this.ServerName),
98+
Properties.Resources.ShouldProcessCaption))
99+
{
100+
return;
101+
}
102+
103+
base.ExecuteCmdlet();
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)