Skip to content

Commit 5964ec4

Browse files
committed
managed app type version cmdlets
1 parent 89e8c31 commit 5964ec4

File tree

5 files changed

+583
-0
lines changed

5 files changed

+583
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 + "ManagedClusterApplicationTypeVersion", DefaultParameterSetName = ByResourceGroupAndCluster), OutputType(typeof(PSManagedApplicationTypeVersion))]
26+
public class GetAzServiceFabricManagedClusterApplicationTypeVersion : ManagedApplicationCmdletBase
27+
{
28+
private const string ByResourceGroupAndCluster = "ByResourceGroupAndCluster";
29+
private const string ByVersion = "ByVersion";
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 = ByVersion,
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 = ByVersion,
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, ValueFromPipelineByPropertyName = true, ParameterSetName = ByResourceGroupAndCluster,
50+
HelpMessage = "Specify the name of the managed application type.")]
51+
[Parameter(Mandatory = true, Position = 2, ValueFromPipelineByPropertyName = true, ParameterSetName = ByVersion,
52+
HelpMessage = "Specify the name of the managed application type.")]
53+
[ValidateNotNullOrEmpty]
54+
[Alias("ApplicationTypeName")]
55+
public string Name { get; set; }
56+
57+
[Parameter(Mandatory = true, Position = 3, ValueFromPipelineByPropertyName = true, ParameterSetName = ByVersion,
58+
HelpMessage = "Specify the version of the managed application type.")]
59+
[ValidateNotNullOrEmpty]
60+
[Alias("ApplicationTypeVersion")]
61+
public string Version { get; set; }
62+
63+
[Parameter(Mandatory = true, ParameterSetName = ByResourceId, ValueFromPipelineByPropertyName = true,
64+
HelpMessage = "Arm ResourceId of the managed application type version.")]
65+
[ResourceIdCompleter(Constants.ManagedClustersFullType)]
66+
[ValidateNotNullOrEmpty]
67+
public string ResourceId { get; set; }
68+
#endregion
69+
70+
public override void ExecuteCmdlet()
71+
{
72+
try
73+
{
74+
switch (ParameterSetName)
75+
{
76+
case ByResourceGroupAndCluster:
77+
var managedAppTypeVersionList = this.ReturnListByPageResponse(
78+
this.SfrpMcClient.ApplicationTypeVersions.ListByApplicationTypes(this.ResourceGroupName, this.ClusterName, this.Name),
79+
this.SfrpMcClient.ApplicationTypeVersions.ListByApplicationTypesNext);
80+
WriteObject(managedAppTypeVersionList.Select(appType => new PSManagedApplicationTypeVersion(appType)), true);
81+
break;
82+
case ByVersion:
83+
GetByVersion();
84+
break;
85+
case ByResourceId:
86+
SetParametersByResourceId(this.ResourceId);
87+
GetByVersion();
88+
break;
89+
default:
90+
throw new PSArgumentException("Invalid ParameterSetName");
91+
}
92+
}
93+
catch (Exception ex)
94+
{
95+
this.PrintSdkExceptionDetail(ex);
96+
throw;
97+
}
98+
}
99+
100+
private void GetByVersion()
101+
{
102+
var managedAppTypeVersion = this.SfrpMcClient.ApplicationTypeVersions.Get(this.ResourceGroupName, this.ClusterName, this.Name, this.Version);
103+
WriteObject(new PSManagedApplicationTypeVersion(managedAppTypeVersion), false);
104+
}
105+
106+
private void SetParametersByResourceId(string resourceId)
107+
{
108+
this.GetParametersByResourceId(resourceId, Constants.applicationTypeVersionProvider, out string resourceGroup, out string resourceName, out string parentResourceName, out string grandParentResourceName);
109+
this.ResourceGroupName = resourceGroup;
110+
this.Name = parentResourceName;
111+
this.Version = resourceName;
112+
this.ClusterName = grandParentResourceName;
113+
}
114+
}
115+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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.Collections;
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+
using Microsoft.Azure.Management.ServiceFabricManagedClusters.Models;
23+
24+
namespace Microsoft.Azure.Commands.ServiceFabric.Commands
25+
{
26+
[Cmdlet(VerbsCommon.New, ResourceManager.Common.AzureRMConstants.AzurePrefix + Constants.ServiceFabricPrefix + "ManagedClusterApplicationTypeVersion", SupportsShouldProcess = true), OutputType(typeof(PSManagedApplicationTypeVersion))]
27+
public class NewAzServiceFabricManagedClustersApplicationTypeVersion : ManagedApplicationCmdletBase
28+
{
29+
#region Paramters
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,
43+
HelpMessage = "Specify the name of the managed application type")]
44+
[ValidateNotNullOrEmpty]
45+
[Alias("ApplicationTypeName")]
46+
public string Name { get; set; }
47+
48+
[Parameter(Mandatory = true, Position = 3,
49+
HelpMessage = "Specify the managed application type version")]
50+
[ValidateNotNullOrEmpty]
51+
[Alias("ApplicationTypeVersion")]
52+
public string Version { get; set; }
53+
54+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true,
55+
HelpMessage = "Specify the url of the application package sfpkg file")]
56+
[ValidateNotNullOrEmpty]
57+
public string PackageUrl { get; set; }
58+
59+
[Parameter(Mandatory = false, ValueFromPipeline = true, HelpMessage = "Specify the tags as key/value pairs.")]
60+
public Hashtable Tags { get; set; }
61+
62+
[Parameter(Mandatory = false, HelpMessage = "Continue without prompts")]
63+
public SwitchParameter Force { get; set; }
64+
65+
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background and return a Job to track progress.")]
66+
public SwitchParameter AsJob { get; set; }
67+
#endregion
68+
69+
public override void ExecuteCmdlet()
70+
{
71+
if (ShouldProcess(target: this.Version, action: $"Create new managed application type version. typename: {this.Name}, version {this.Version} in resource group {this.ResourceGroupName}"))
72+
{
73+
try
74+
{
75+
ManagedCluster cluster = SafeGetResource(() => this.SfrpMcClient.ManagedClusters.Get(this.ResourceGroupName, this.ClusterName));
76+
if (cluster == null)
77+
{
78+
WriteError(new ErrorRecord(new InvalidOperationException($"Parent cluster '{this.ClusterName}' does not exist."),
79+
"ResourceDoesNotExist", ErrorCategory.InvalidOperation, null));
80+
}
81+
else
82+
{
83+
CreateManagedApplicationType(this.Name, cluster.Location, errorIfPresent: false);
84+
var managedAppTypeVersion = CreateManagedApplicationTypeVersion(
85+
applicationTypeName: this.Name,
86+
typeVersion: this.Version,
87+
location: cluster.Location,
88+
packageUrl: this.PackageUrl,
89+
force: this.Force.IsPresent,
90+
tags: this.Tags);
91+
WriteObject(new PSManagedApplicationTypeVersion(managedAppTypeVersion), false);
92+
}
93+
}
94+
catch (Exception ex)
95+
{
96+
PrintSdkExceptionDetail(ex);
97+
throw;
98+
}
99+
}
100+
}
101+
}
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
21+
namespace Microsoft.Azure.Commands.ServiceFabric.Commands
22+
{
23+
[Cmdlet(VerbsCommon.Remove, ResourceManager.Common.AzureRMConstants.AzurePrefix + Constants.ServiceFabricPrefix + "ManagedClusterApplicationTypeVersion", DefaultParameterSetName = ByResourceGroup, SupportsShouldProcess = true), OutputType(typeof(bool))]
24+
public class RemoveAzServiceFabricManagedClusterApplicationTypeVersion : ManagedApplicationCmdletBase
25+
{
26+
private const string ByResourceGroup = "ByResourceGroup";
27+
private const string ByInputObject = "ByInputObject";
28+
private const string ByResourceId = "ByResourceId";
29+
30+
#region Parameters
31+
[Parameter(Mandatory = true, Position = 0, ParameterSetName = ByResourceGroup, ValueFromPipelineByPropertyName = true,
32+
HelpMessage = "Specify the name of the resource group.")]
33+
[ResourceGroupCompleter]
34+
[ValidateNotNullOrEmpty]
35+
public override string ResourceGroupName { get; set; }
36+
37+
[Parameter(Mandatory = true, Position = 1, ParameterSetName = ByResourceGroup, ValueFromPipelineByPropertyName = true,
38+
HelpMessage = "Specify the name of the cluster.")]
39+
[ResourceNameCompleter(Constants.ManagedClustersFullType, nameof(ResourceGroupName))]
40+
[ValidateNotNullOrEmpty]
41+
public override string ClusterName { get; set; }
42+
43+
[Parameter(Mandatory = true, ParameterSetName = ByResourceGroup, HelpMessage = "Specify the name of the managed application type.")]
44+
[ValidateNotNullOrEmpty]
45+
[Alias("ApplicationTypeName")]
46+
public string Name { get; set; }
47+
48+
[Parameter(Mandatory = true, ParameterSetName = ByResourceGroup, HelpMessage = "Specify the managed application type version.")]
49+
[ValidateNotNullOrEmpty]
50+
[Alias("ApplicationTypeVersion")]
51+
public string Version { get; set; }
52+
53+
[Parameter(Mandatory = true, ParameterSetName = ByResourceId, ValueFromPipelineByPropertyName = true,
54+
HelpMessage = "Arm ResourceId of the managed application type version.")]
55+
[ResourceIdCompleter(Constants.ManagedClustersFullType)]
56+
[ValidateNotNullOrEmpty]
57+
public string ResourceId { get; set; }
58+
59+
[Parameter(Mandatory = true, ParameterSetName = ByInputObject, ValueFromPipeline = true,
60+
HelpMessage = "The managed application type version resource.")]
61+
public PSManagedApplicationTypeVersion InputObject { get; set; }
62+
63+
[Parameter(Mandatory = false, ParameterSetName = ByResourceGroup)]
64+
[Parameter(Mandatory = false, ParameterSetName = ByInputObject)]
65+
[Parameter(Mandatory = false, ParameterSetName = ByResourceId)]
66+
public SwitchParameter PassThru { get; set; }
67+
68+
[Parameter(Mandatory = false, ParameterSetName = ByResourceGroup, HelpMessage = "Remove without prompt.")]
69+
[Parameter(Mandatory = false, ParameterSetName = ByInputObject, HelpMessage = "Remove without prompt.")]
70+
[Parameter(Mandatory = false, ParameterSetName = ByResourceId, HelpMessage = "Remove without prompt.")]
71+
public SwitchParameter Force { get; set; }
72+
73+
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background and return a Job to track progress.")]
74+
public SwitchParameter AsJob { get; set; }
75+
#endregion
76+
77+
public override void ExecuteCmdlet()
78+
{
79+
switch (ParameterSetName)
80+
{
81+
case ByInputObject:
82+
if (string.IsNullOrEmpty(this.InputObject?.Id))
83+
{
84+
throw new ArgumentException("ResourceId is null.");
85+
}
86+
this.ResourceId = InputObject.Id;
87+
SetParametersByResourceId(this.ResourceId);
88+
break;
89+
case ByResourceId:
90+
SetParametersByResourceId(this.ResourceId);
91+
break;
92+
case ByResourceGroup:
93+
// intentionally left empty
94+
break;
95+
default:
96+
throw new PSArgumentException("Invalid ParameterSetName");
97+
}
98+
99+
var resourceMessage = $"Managed ApplicationType '{this.Name}' in resource group '{this.ResourceGroupName}', cluster name {this.ClusterName}";
100+
if (ShouldProcess(target: this.Version, action: $"Remove {resourceMessage}"))
101+
{
102+
ConfirmAction(Force.IsPresent,
103+
"Do you want to remove the managed application type version? Please remove all managed applications under this resource before running this command.",
104+
"Removing managed application type version.",
105+
resourceMessage,
106+
() =>
107+
{
108+
try
109+
{
110+
var beginRequestResponse = this.SfrpMcClient.ApplicationTypeVersions.BeginDeleteWithHttpMessagesAsync(
111+
this.ResourceGroupName,
112+
this.ClusterName,
113+
this.Name,
114+
this.Version).GetAwaiter().GetResult();
115+
116+
this.PollLongRunningOperation(beginRequestResponse);
117+
if (PassThru)
118+
{
119+
WriteObject(true);
120+
}
121+
}
122+
catch (Exception ex)
123+
{
124+
this.PrintSdkExceptionDetail(ex);
125+
throw;
126+
}
127+
});
128+
}
129+
}
130+
131+
private void SetParametersByResourceId(string resourceId)
132+
{
133+
this.GetParametersByResourceId(resourceId, Constants.applicationTypeVersionProvider, out string resourceGroup, out string resourceName, out string parentResourceName, out string grandParentResourceName);
134+
this.ResourceGroupName = resourceGroup;
135+
this.Name = resourceName;
136+
this.ClusterName = parentResourceName;
137+
this.Version = grandParentResourceName;
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)