Skip to content

Commit 89e8c31

Browse files
committed
managed app type cmdlets
1 parent 12b2f1d commit 89e8c31

7 files changed

+698
-1
lines changed
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;
16+
using System.Linq;
17+
using System.Management.Automation;
18+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
19+
using Microsoft.Azure.Commands.ServiceFabric.Common;
20+
using Microsoft.Azure.Commands.ServiceFabric.Models.ManagedClusters;
21+
using Microsoft.Azure.Management.ServiceFabricManagedClusters;
22+
23+
namespace Microsoft.Azure.Commands.ServiceFabric.Commands
24+
{
25+
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzurePrefix + Constants.ServiceFabricPrefix + "ManagedClusterApplicationType", DefaultParameterSetName = ByResourceGroupAndCluster), OutputType(typeof(PSManagedApplicationType))]
26+
public class GetAzServiceFabricManagedClusterApplicationType : ManagedApplicationCmdletBase
27+
{
28+
private const string ByResourceGroupAndCluster = "ByResourceGroupAndCluster";
29+
private const string ByName = "ByName";
30+
private const string ByResourceId = "ByResourceId";
31+
32+
#region Parameters
33+
[Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = ByResourceGroupAndCluster,
34+
HelpMessage = "Specify the name of the resource group.")]
35+
[Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = ByName,
36+
HelpMessage = "Specify the name of the resource group.")]
37+
[ResourceGroupCompleter]
38+
[ValidateNotNullOrEmpty]
39+
public override string ResourceGroupName { get; set; }
40+
41+
[Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = ByResourceGroupAndCluster,
42+
HelpMessage = "Specify the name of the cluster.")]
43+
[Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = ByName,
44+
HelpMessage = "Specify the name of the cluster.")]
45+
[ResourceNameCompleter(Constants.ManagedClustersFullType, nameof(ResourceGroupName))]
46+
[ValidateNotNullOrEmpty]
47+
public override string ClusterName { get; set; }
48+
49+
[Parameter(Mandatory = true, Position = 2, ValueFromPipeline = true, ParameterSetName = ByName,
50+
HelpMessage = "Specify the name of the managed application type")]
51+
[Alias("ApplicationTypeName")]
52+
[ValidateNotNullOrEmpty]
53+
public string Name { get; set; }
54+
55+
[Parameter(Mandatory = true, ParameterSetName = ByResourceId, ValueFromPipelineByPropertyName = true,
56+
HelpMessage = "Arm ResourceId of the managed application type.")]
57+
[ResourceIdCompleter(Constants.ManagedClustersFullType)]
58+
[ValidateNotNullOrEmpty]
59+
public string ResourceId { get; set; }
60+
#endregion
61+
62+
public override void ExecuteCmdlet()
63+
{
64+
try
65+
{
66+
switch (ParameterSetName)
67+
{
68+
case ByResourceGroupAndCluster:
69+
var managedAppTypeList = this.ReturnListByPageResponse(
70+
this.SfrpMcClient.ApplicationTypes.List(this.ResourceGroupName, this.ClusterName),
71+
this.SfrpMcClient.ApplicationTypes.ListNext);
72+
WriteObject(managedAppTypeList.Select(appType => new PSManagedApplicationType(appType)), true);
73+
break;
74+
case ByName:
75+
GetByName();
76+
break;
77+
case ByResourceId:
78+
SetParametersByResourceId(this.ResourceId);
79+
GetByName();
80+
break;
81+
default:
82+
throw new PSArgumentException("Invalid ParameterSetName");
83+
}
84+
}
85+
catch (Exception ex)
86+
{
87+
this.PrintSdkExceptionDetail(ex);
88+
throw;
89+
}
90+
}
91+
92+
private void GetByName()
93+
{
94+
var managedAppType = this.SfrpMcClient.ApplicationTypes.Get(this.ResourceGroupName, this.ClusterName, this.Name);
95+
WriteObject(new PSManagedApplicationType(managedAppType), false);
96+
}
97+
98+
private void SetParametersByResourceId(string resourceId)
99+
{
100+
this.GetParametersByResourceId(resourceId, Constants.applicationTypeProvider, out string resourceGroup, out string resourceName, out string parentResourceName);
101+
this.ResourceGroupName = resourceGroup;
102+
this.Name = resourceName;
103+
this.ClusterName = parentResourceName;
104+
}
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.ResourceManager.Common.ArgumentCompleters;
16+
using Microsoft.Azure.Commands.ServiceFabric.Common;
17+
using Microsoft.Azure.Commands.ServiceFabric.Models.ManagedClusters;
18+
using Microsoft.Azure.Management.ServiceFabricManagedClusters;
19+
using Microsoft.Azure.Management.ServiceFabricManagedClusters.Models;
20+
using System;
21+
using System.Collections;
22+
using System.Management.Automation;
23+
24+
namespace Microsoft.Azure.Commands.ServiceFabric.Commands
25+
{
26+
[Cmdlet(VerbsCommon.New, ResourceManager.Common.AzureRMConstants.AzurePrefix + Constants.ServiceFabricPrefix + "ManagedClusterApplicationType", SupportsShouldProcess = true), OutputType(typeof(PSManagedApplicationType))]
27+
public class NewAzServiceFabricManagedClusterApplicationType : ManagedApplicationCmdletBase
28+
{
29+
#region Parameters
30+
[Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true,
31+
HelpMessage = "Specify the name of the resource group.")]
32+
[ResourceGroupCompleter]
33+
[ValidateNotNullOrEmpty]
34+
public override string ResourceGroupName { get; set; }
35+
36+
[Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true,
37+
HelpMessage = "Specify the name of the cluster.")]
38+
[ResourceNameCompleter(Constants.ManagedClustersFullType, nameof(ResourceGroupName))]
39+
[ValidateNotNullOrEmpty]
40+
public override string ClusterName { get; set; }
41+
42+
[Parameter(Mandatory = true, Position = 2, ValueFromPipeline = true,
43+
HelpMessage = "Specify the name of the application type")]
44+
[ValidateNotNullOrEmpty]
45+
[Alias("ApplicationTypeName")]
46+
public string Name { get; set; }
47+
48+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Specify the tags as key/value pairs.")]
49+
public Hashtable Tags { get; set; }
50+
#endregion
51+
52+
public override void ExecuteCmdlet()
53+
{
54+
if (ShouldProcess(target: this.Name, action: $"Create new managed application type {this.Name} in resource group {this.ResourceGroupName}"))
55+
{
56+
try
57+
{
58+
ManagedCluster cluster = SafeGetResource(() => this.SfrpMcClient.ManagedClusters.Get(this.ResourceGroupName, this.ClusterName));
59+
if (cluster == null)
60+
{
61+
WriteError(new ErrorRecord(new InvalidOperationException($"Parent cluster '{this.ClusterName}' does not exist."),
62+
"ResourceDoesNotExist", ErrorCategory.InvalidOperation, null));
63+
}
64+
else
65+
{
66+
var appType = CreateManagedApplicationType(this.Name, cluster.Location, this.Tags);
67+
WriteObject(new PSManagedApplicationType(appType), false);
68+
}
69+
}
70+
catch (Exception ex)
71+
{
72+
PrintSdkExceptionDetail(ex);
73+
throw;
74+
}
75+
}
76+
}
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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;
16+
using System.Management.Automation;
17+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
18+
using Microsoft.Azure.Commands.ServiceFabric.Common;
19+
using Microsoft.Azure.Commands.ServiceFabric.Models.ManagedClusters;
20+
using Microsoft.Azure.Management.ServiceFabricManagedClusters;
21+
22+
namespace Microsoft.Azure.Commands.ServiceFabric.Commands
23+
{
24+
[Cmdlet(VerbsCommon.Remove, ResourceManager.Common.AzureRMConstants.AzurePrefix + Constants.ServiceFabricPrefix + "ManagedClusterApplicationType", DefaultParameterSetName = ByResourceGroup, SupportsShouldProcess = true), OutputType(typeof(bool))]
25+
public class RemoveAzServiceFabricManagedClusterApplicationType : ManagedApplicationCmdletBase
26+
{
27+
private const string ByResourceGroup = "ByResourceGroup";
28+
private const string ByInputObject = "ByInputObject";
29+
private const string ByResourceId = "ByResourceId";
30+
31+
#region Parameters
32+
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ByResourceGroup, ValueFromPipelineByPropertyName = true,
33+
HelpMessage = "Specify the name of the resource group.")]
34+
[ResourceGroupCompleter]
35+
[ValidateNotNullOrEmpty]
36+
public override string ResourceGroupName { get; set; }
37+
38+
[Parameter(Mandatory = true, Position = 1, ParameterSetName = ByResourceGroup, ValueFromPipelineByPropertyName = true,
39+
HelpMessage = "Specify the name of the cluster.")]
40+
[ResourceNameCompleter(Constants.ManagedClustersFullType, nameof(ResourceGroupName))]
41+
[ValidateNotNullOrEmpty]
42+
public override string ClusterName { get; set; }
43+
44+
[Parameter(Mandatory = false, ParameterSetName = ByResourceGroup,
45+
HelpMessage = "Specify the name of the managed application type.")]
46+
[ValidateNotNullOrEmpty]
47+
[Alias("ApplicationTypeName")]
48+
public string Name { get; set; }
49+
50+
[Parameter(Mandatory = true, ParameterSetName = ByResourceId, ValueFromPipelineByPropertyName = true,
51+
HelpMessage = "Arm ResourceId of the managed application type.")]
52+
[ResourceIdCompleter(Constants.ManagedClustersFullType)]
53+
[ValidateNotNullOrEmpty]
54+
public string ResourceId { get; set; }
55+
56+
[Parameter(Mandatory = true, ParameterSetName = ByInputObject, ValueFromPipeline = true,
57+
HelpMessage = "The managed application type resource.")]
58+
public PSManagedApplicationType InputObject { get; set; }
59+
60+
[Parameter(Mandatory = false, ParameterSetName = ByResourceGroup)]
61+
[Parameter(Mandatory = false, ParameterSetName = ByInputObject)]
62+
[Parameter(Mandatory = false, ParameterSetName = ByResourceId)]
63+
public SwitchParameter PassThru { get; set; }
64+
65+
[Parameter(Mandatory = false, ParameterSetName = ByResourceGroup, HelpMessage = "Remove without prompt.")]
66+
[Parameter(Mandatory = false, ParameterSetName = ByInputObject, HelpMessage = "Remove without prompt.")]
67+
[Parameter(Mandatory = false, ParameterSetName = ByResourceId, HelpMessage = "Remove without prompt.")]
68+
public SwitchParameter Force { get; set; }
69+
#endregion
70+
71+
public override void ExecuteCmdlet()
72+
{
73+
switch (ParameterSetName)
74+
{
75+
case ByInputObject:
76+
if (string.IsNullOrEmpty(this.InputObject?.Id))
77+
{
78+
throw new ArgumentException("ResourceId is null.");
79+
}
80+
this.ResourceId = this.InputObject.Id;
81+
SetParametersByResourceId(this.ResourceId);
82+
break;
83+
case ByResourceId:
84+
SetParametersByResourceId(this.ResourceId);
85+
break;
86+
case ByResourceGroup:
87+
// intentionally left empty
88+
break;
89+
default:
90+
throw new PSArgumentException("Invalid ParameterSetName");
91+
}
92+
93+
var resourceMessage = $"Managed ApplicationType '{this.Name}' on cluster {this.ClusterName}, in resource group {this.ResourceGroupName}";
94+
if (ShouldProcess(target: this.Name, action: $"Remove {resourceMessage}"))
95+
{
96+
ConfirmAction(Force.IsPresent,
97+
"Do you want to remove the managed application type? This will remove all type versions under this resource. Please remove all managed applications under this resource before running this command.",
98+
"Removing managed application type.",
99+
resourceMessage,
100+
() =>
101+
{
102+
try
103+
{
104+
this.SfrpMcClient.ApplicationTypes.Delete(this.ResourceGroupName, this.ClusterName, this.Name);
105+
if (PassThru)
106+
{
107+
WriteObject(true);
108+
}
109+
}
110+
catch (Exception ex)
111+
{
112+
this.PrintSdkExceptionDetail(ex);
113+
throw;
114+
}
115+
});
116+
}
117+
}
118+
119+
private void SetParametersByResourceId(string resourceId)
120+
{
121+
this.GetParametersByResourceId(resourceId, Constants.applicationTypeProvider, out string resourceGroup, out string resourceName, out string parentResourceName);
122+
this.ResourceGroupName = resourceGroup;
123+
this.Name = resourceName;
124+
this.ClusterName = parentResourceName;
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)