12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
+ using System ;
15
16
using Microsoft . Azure . Commands . ResourceManager . Common . Tags ;
16
17
using Microsoft . Azure . Commands . Sql . Database . Model ;
17
18
using System . Collections ;
@@ -28,6 +29,9 @@ namespace Microsoft.Azure.Commands.Sql.Database.Cmdlet
28
29
ConfirmImpact = ConfirmImpact . Medium ) ]
29
30
public class SetAzureSqlDatabase : AzureSqlDatabaseCmdletBase < IEnumerable < AzureSqlDatabaseModel > >
30
31
{
32
+ private const string UpdateParameterSetName = "Update" ;
33
+ private const string RenameParameterSetName = "Rename" ;
34
+
31
35
/// <summary>
32
36
/// Gets or sets the name of the Azure SQL Database
33
37
/// </summary>
@@ -43,57 +47,72 @@ public class SetAzureSqlDatabase : AzureSqlDatabaseCmdletBase<IEnumerable<AzureS
43
47
/// Gets or sets the maximum size of the Azure SQL Database in bytes
44
48
/// </summary>
45
49
[ Parameter ( Mandatory = false ,
46
- HelpMessage = "The maximum size of the Azure SQL Database in bytes." ) ]
50
+ HelpMessage = "The maximum size of the Azure SQL Database in bytes." ,
51
+ ParameterSetName = UpdateParameterSetName ) ]
47
52
[ ValidateNotNullOrEmpty ]
48
53
public long MaxSizeBytes { get ; set ; }
49
54
50
55
/// <summary>
51
56
/// Gets or sets the edition to assign to the Azure SQL Database
52
57
/// </summary>
53
58
[ Parameter ( Mandatory = false ,
54
- HelpMessage = "The edition to assign to the Azure SQL Database." ) ]
59
+ HelpMessage = "The edition to assign to the Azure SQL Database." ,
60
+ ParameterSetName = UpdateParameterSetName ) ]
55
61
[ ValidateNotNullOrEmpty ]
56
62
public DatabaseEdition Edition { get ; set ; }
57
63
58
64
/// <summary>
59
65
/// Gets or sets the name of the service objective to assign to the Azure SQL Database
60
66
/// </summary>
61
67
[ Parameter ( Mandatory = false ,
62
- HelpMessage = "The name of the service objective to assign to the Azure SQL Database." ) ]
68
+ HelpMessage = "The name of the service objective to assign to the Azure SQL Database." ,
69
+ ParameterSetName = UpdateParameterSetName ) ]
63
70
[ ValidateNotNullOrEmpty ]
64
71
public string RequestedServiceObjectiveName { get ; set ; }
65
72
66
73
/// <summary>
67
74
/// Gets or sets the name of the Elastic Pool to put the database in
68
75
/// </summary>
69
76
[ Parameter ( Mandatory = false ,
70
- HelpMessage = "The name of the Elastic Pool to put the database in." ) ]
77
+ HelpMessage = "The name of the Elastic Pool to put the database in." ,
78
+ ParameterSetName = UpdateParameterSetName ) ]
71
79
[ ValidateNotNullOrEmpty ]
72
80
public string ElasticPoolName { get ; set ; }
73
81
74
82
/// <summary>
75
83
/// Gets or sets the read scale option to assign to the Azure SQL Database
76
84
/// </summary>
77
85
[ Parameter ( Mandatory = false ,
78
- HelpMessage = "The read scale option to assign to the Azure SQL Database.(Enabled/Disabled)" ) ]
86
+ HelpMessage = "The read scale option to assign to the Azure SQL Database.(Enabled/Disabled)" ,
87
+ ParameterSetName = UpdateParameterSetName ) ]
79
88
[ ValidateNotNullOrEmpty ]
80
89
public DatabaseReadScale ReadScale { get ; set ; }
81
90
82
91
/// <summary>
83
92
/// Gets or sets the tags associated with the Azure Sql Database
84
93
/// </summary>
85
94
[ Parameter ( Mandatory = false ,
86
- HelpMessage = "The tags to associate with the Azure Sql Database" ) ]
95
+ HelpMessage = "The tags to associate with the Azure Sql Database" ,
96
+ ParameterSetName = UpdateParameterSetName ) ]
87
97
[ Alias ( "Tag" ) ]
88
98
public Hashtable Tags { get ; set ; }
89
99
90
100
/// <summary>
91
101
/// Gets or sets the zone redundant option to assign to the Azure SQL Database
92
102
/// </summary>
93
103
[ Parameter ( Mandatory = false ,
94
- HelpMessage = "The zone redundancy to associate with the Azure Sql Database" ) ]
104
+ HelpMessage = "The zone redundancy to associate with the Azure Sql Database" ,
105
+ ParameterSetName = UpdateParameterSetName ) ]
95
106
public SwitchParameter ZoneRedundant { get ; set ; }
96
107
108
+ /// <summary>
109
+ /// Gets or sets the new name.
110
+ /// </summary>
111
+ [ Parameter ( Mandatory = true ,
112
+ HelpMessage = "The new name to rename the database to." ,
113
+ ParameterSetName = RenameParameterSetName ) ]
114
+ public string NewName { get ; set ; }
115
+
97
116
/// <summary>
98
117
/// Overriding to add warning message
99
118
/// </summary>
@@ -121,20 +140,26 @@ protected override IEnumerable<AzureSqlDatabaseModel> GetEntity()
121
140
protected override IEnumerable < AzureSqlDatabaseModel > ApplyUserInputToModel ( IEnumerable < AzureSqlDatabaseModel > model )
122
141
{
123
142
List < Model . AzureSqlDatabaseModel > newEntity = new List < AzureSqlDatabaseModel > ( ) ;
124
- newEntity . Add ( new AzureSqlDatabaseModel ( )
143
+ if ( this . ParameterSetName == UpdateParameterSetName )
125
144
{
126
- ResourceGroupName = ResourceGroupName ,
127
- ServerName = ServerName ,
128
- DatabaseName = DatabaseName ,
129
- Edition = Edition ,
130
- MaxSizeBytes = MaxSizeBytes ,
131
- RequestedServiceObjectiveName = RequestedServiceObjectiveName ,
132
- Tags = TagsConversionHelper . ReadOrFetchTags ( this , model . FirstOrDefault ( ) . Tags ) ,
133
- ElasticPoolName = ElasticPoolName ,
134
- Location = model . FirstOrDefault ( ) . Location ,
135
- ReadScale = ReadScale ,
136
- ZoneRedundant = MyInvocation . BoundParameters . ContainsKey ( "ZoneRedundant" ) ? ( bool ? ) ZoneRedundant . ToBool ( ) : null
137
- } ) ;
145
+ newEntity . Add ( new AzureSqlDatabaseModel ( )
146
+ {
147
+ ResourceGroupName = ResourceGroupName ,
148
+ ServerName = ServerName ,
149
+ DatabaseName = DatabaseName ,
150
+ Edition = Edition ,
151
+ MaxSizeBytes = MaxSizeBytes ,
152
+ RequestedServiceObjectiveName = RequestedServiceObjectiveName ,
153
+ Tags = TagsConversionHelper . ReadOrFetchTags ( this , model . FirstOrDefault ( ) . Tags ) ,
154
+ ElasticPoolName = ElasticPoolName ,
155
+ Location = model . FirstOrDefault ( ) . Location ,
156
+ ReadScale = ReadScale ,
157
+ ZoneRedundant =
158
+ MyInvocation . BoundParameters . ContainsKey ( "ZoneRedundant" )
159
+ ? ( bool ? ) ZoneRedundant . ToBool ( )
160
+ : null
161
+ } ) ;
162
+ }
138
163
return newEntity ;
139
164
}
140
165
@@ -145,16 +170,38 @@ protected override IEnumerable<AzureSqlDatabaseModel> ApplyUserInputToModel(IEnu
145
170
/// <returns>The input entity</returns>
146
171
protected override IEnumerable < AzureSqlDatabaseModel > PersistChanges ( IEnumerable < AzureSqlDatabaseModel > entity )
147
172
{
148
- return new List < AzureSqlDatabaseModel >
173
+ switch ( this . ParameterSetName )
149
174
{
150
- ModelAdapter . UpsertDatabaseWithNewSdk (
151
- this . ResourceGroupName ,
152
- this . ServerName ,
153
- new AzureSqlDatabaseCreateOrUpdateModel
175
+ case UpdateParameterSetName :
176
+ return new List < AzureSqlDatabaseModel >
154
177
{
155
- Database = entity . First ( )
156
- } )
157
- } ;
178
+ ModelAdapter . UpsertDatabaseWithNewSdk (
179
+ this . ResourceGroupName ,
180
+ this . ServerName ,
181
+ new AzureSqlDatabaseCreateOrUpdateModel
182
+ {
183
+ Database = entity . First ( )
184
+ } )
185
+ } ;
186
+
187
+ case RenameParameterSetName :
188
+ ModelAdapter . RenameDatabase (
189
+ this . ResourceGroupName ,
190
+ this . ServerName ,
191
+ this . DatabaseName ,
192
+ this . NewName ) ;
193
+
194
+ return new List < AzureSqlDatabaseModel >
195
+ {
196
+ ModelAdapter . GetDatabase (
197
+ this . ResourceGroupName ,
198
+ this . ServerName ,
199
+ this . NewName )
200
+ } ;
201
+
202
+ default :
203
+ throw new ArgumentException ( this . ParameterSetName ) ;
204
+ }
158
205
}
159
206
}
160
207
}
0 commit comments