Skip to content

Commit 7052244

Browse files
committed
managed app cmdlets
1 parent 5964ec4 commit 7052244

File tree

6 files changed

+1035
-0
lines changed

6 files changed

+1035
-0
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 + "ManagedClusterApplication", DefaultParameterSetName = ByResourceGroupAndCluster), OutputType(typeof(PSManagedApplication))]
26+
public class GetAzServiceFabricManagedClusterApplication : 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, ValueFromPipelineByPropertyName = true, ParameterSetName = ByName,
50+
HelpMessage = "Specify the name of the managed application.")]
51+
[ValidateNotNullOrEmpty]
52+
[Alias("ApplicationName")]
53+
public string Name { get; set; }
54+
55+
[Parameter(Mandatory = true, ParameterSetName = ByResourceId, ValueFromPipelineByPropertyName = true,
56+
HelpMessage = "Arm ResourceId of the managed application.")]
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 managedAppList = this.ReturnListByPageResponse(
70+
this.SfrpMcClient.Applications.List(this.ResourceGroupName, this.ClusterName),
71+
this.SfrpMcClient.Applications.ListNext);
72+
WriteObject(managedAppList.Select(app => new PSManagedApplication(app)), 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 managedApp = this.SfrpMcClient.Applications.Get(this.ResourceGroupName, this.ClusterName, this.Name);
95+
WriteObject(new PSManagedApplication(managedApp), false);
96+
}
97+
98+
private void SetParametersByResourceId(string resourceId)
99+
{
100+
this.GetParametersByResourceId(resourceId, Constants.applicationProvider, 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,179 @@
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.Linq;
18+
using System.Management.Automation;
19+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
20+
using Microsoft.Azure.Commands.ServiceFabric.Common;
21+
using Microsoft.Azure.Commands.ServiceFabric.Models.ManagedClusters;
22+
using Microsoft.Azure.Management.ServiceFabricManagedClusters;
23+
using Microsoft.Azure.Management.ServiceFabricManagedClusters.Models;
24+
25+
namespace Microsoft.Azure.Commands.ServiceFabric.Commands
26+
{
27+
[Cmdlet(VerbsCommon.New, ResourceManager.Common.AzureRMConstants.AzurePrefix + Constants.ServiceFabricPrefix + "ManagedClusterApplication", SupportsShouldProcess = true, DefaultParameterSetName = SkipAppTypeVersion), OutputType(typeof(PSManagedApplication))]
28+
public class NewAzServiceFabricManagedClusterApplication : ManagedApplicationCmdletBase
29+
{
30+
protected const string SkipAppTypeVersion = "SkipAppTypeVersion";
31+
protected const string CreateAppTypeVersion = "CreateAppTypeVersion";
32+
33+
protected const string AppTypeArmResourceIdFormat = "/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.ServiceFabric/managedclusters/{2}/applicationTypes/{3}/versions/{4}";
34+
35+
#region Parameters
36+
[Parameter(Mandatory = true, Position = 0, ParameterSetName = SkipAppTypeVersion, ValueFromPipelineByPropertyName = true,
37+
HelpMessage = "Specify the name of the resource group.")]
38+
[Parameter(Mandatory = true, Position = 0, ParameterSetName = CreateAppTypeVersion, ValueFromPipelineByPropertyName = true,
39+
HelpMessage = "Specify the name of the resource group.")]
40+
[ResourceGroupCompleter]
41+
[ValidateNotNullOrEmpty]
42+
public override string ResourceGroupName { get; set; }
43+
44+
[Parameter(Mandatory = true, Position = 1, ParameterSetName = SkipAppTypeVersion, ValueFromPipelineByPropertyName = true,
45+
HelpMessage = "Specify the name of the cluster.")]
46+
[Parameter(Mandatory = true, Position = 1, ParameterSetName = CreateAppTypeVersion, ValueFromPipelineByPropertyName = true,
47+
HelpMessage = "Specify the name of the cluster.")]
48+
[ResourceNameCompleter(Constants.ManagedClustersFullType, nameof(ResourceGroupName))]
49+
[ValidateNotNullOrEmpty]
50+
public override string ClusterName { get; set; }
51+
52+
[Parameter(Mandatory = true, Position = 2, ParameterSetName = SkipAppTypeVersion,
53+
HelpMessage = "Specify the name of the managed application type")]
54+
[Parameter(Mandatory = true, Position = 2, ParameterSetName = CreateAppTypeVersion,
55+
HelpMessage = "Specify the name of the managed application type")]
56+
[ValidateNotNullOrEmpty]
57+
public string ApplicationTypeName { get; set; }
58+
59+
[Parameter(Mandatory = true, Position = 3, ParameterSetName = SkipAppTypeVersion,
60+
HelpMessage = "Specify the managed application type version")]
61+
[Parameter(Mandatory = true, Position = 3, ParameterSetName = CreateAppTypeVersion,
62+
HelpMessage = "Specify the managed application type version")]
63+
[ValidateNotNullOrEmpty]
64+
public string ApplicationTypeVersion { get; set; }
65+
66+
[Parameter(Mandatory = true, ParameterSetName = SkipAppTypeVersion,
67+
HelpMessage = "Specify the name of the managed application")]
68+
[Parameter(Mandatory = true, ParameterSetName = CreateAppTypeVersion,
69+
HelpMessage = "Specify the name of the managed application")]
70+
[ValidateNotNullOrEmpty]
71+
[Alias("ApplicationName")]
72+
public string Name { get; set; }
73+
74+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = SkipAppTypeVersion,
75+
HelpMessage = "Specify the application parameters as key/value pairs. These parameters must exist in the application manifest.")]
76+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, ParameterSetName = CreateAppTypeVersion,
77+
HelpMessage = "Specify the application parameters as key/value pairs. These parameters must exist in the application manifest.")]
78+
[ValidateNotNullOrEmpty]
79+
public Hashtable ApplicationParameter { get; set; }
80+
81+
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = CreateAppTypeVersion,
82+
HelpMessage = "Specify the url of the application package sfpkg file")]
83+
[ValidateNotNullOrEmpty]
84+
public string PackageUrl { get; set; }
85+
86+
[Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = SkipAppTypeVersion, HelpMessage = "Specify the tags as key/value pairs.")]
87+
[Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = CreateAppTypeVersion, HelpMessage = "Specify the tags as key/value pairs.")]
88+
public Hashtable Tags { get; set; }
89+
90+
[Parameter(Mandatory = false, HelpMessage = "Continue without prompts")]
91+
public SwitchParameter Force { get; set; }
92+
93+
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background and return a Job to track progress.")]
94+
public SwitchParameter AsJob { get; set; }
95+
#endregion
96+
97+
public override void ExecuteCmdlet()
98+
{
99+
if (ShouldProcess(target: this.Name, action:
100+
$"Create new application. name {this.Name}, typename: {this.ApplicationTypeName}, version {this.ApplicationTypeVersion}, in resource group {this.ResourceGroupName}"))
101+
{
102+
try
103+
{
104+
ManagedCluster cluster = SafeGetResource(() => this.SfrpMcClient.ManagedClusters.Get(this.ResourceGroupName, this.ClusterName));
105+
if (cluster == null)
106+
{
107+
WriteError(new ErrorRecord(new InvalidOperationException($"Parent cluster '{this.ClusterName}' does not exist."),
108+
"ResourceDoesNotExist", ErrorCategory.InvalidOperation, null));
109+
}
110+
else
111+
{
112+
if (ParameterSetName == CreateAppTypeVersion)
113+
{
114+
CreateManagedApplicationType(this.ApplicationTypeName, cluster.Location);
115+
CreateManagedApplicationTypeVersion(
116+
applicationTypeName: this.ApplicationTypeName,
117+
typeVersion: this.ApplicationTypeVersion,
118+
location: cluster.Location,
119+
packageUrl: this.PackageUrl,
120+
force: this.Force.IsPresent);
121+
}
122+
123+
var managedApp = CreateManagedApplication(cluster.Location);
124+
WriteObject(new PSManagedApplication(managedApp), false);
125+
}
126+
}
127+
catch (Exception ex)
128+
{
129+
PrintSdkExceptionDetail(ex);
130+
throw;
131+
}
132+
}
133+
}
134+
135+
private ApplicationResource CreateManagedApplication(string location)
136+
{
137+
var managedApp = SafeGetResource(() =>
138+
this.SfrpMcClient.Applications.Get(
139+
this.ResourceGroupName,
140+
this.ClusterName,
141+
this.Name),
142+
false);
143+
144+
if (managedApp != null)
145+
{
146+
WriteError(new ErrorRecord(new InvalidOperationException($"Managed application '{this.Name}' already exists."),
147+
"ResourceAlreadyExists", ErrorCategory.InvalidOperation, null));
148+
return managedApp;
149+
}
150+
151+
WriteVerbose($"Creating managed application '{this.Name}'");
152+
153+
ApplicationResource appParams = GetNewAppParameters(location);
154+
155+
var beginRequestResponse = this.SfrpMcClient.Applications.BeginCreateOrUpdateWithHttpMessagesAsync(
156+
this.ResourceGroupName,
157+
this.ClusterName,
158+
this.Name,
159+
appParams).GetAwaiter().GetResult();
160+
161+
return this.PollLongRunningOperation(beginRequestResponse);
162+
}
163+
164+
private ApplicationResource GetNewAppParameters(string location)
165+
{
166+
return new ApplicationResource(
167+
name: this.Name,
168+
version: this.GetAppTypeArmResourceId(this.DefaultContext.Subscription.Id, this.ResourceGroupName, this.ClusterName, this.ApplicationTypeName, this.ApplicationTypeVersion),
169+
parameters: this.ApplicationParameter?.Cast<DictionaryEntry>().ToDictionary(d => d.Key as string, d => d.Value as string),
170+
location: location,
171+
tags: this.Tags?.Cast<DictionaryEntry>().ToDictionary(d => d.Key as string, d => d.Value as string));
172+
}
173+
174+
private string GetAppTypeArmResourceId(string subscriptionId, string resourceGroup, string clusterName, string appTypeName, string appTypeVersion)
175+
{
176+
return string.Format(AppTypeArmResourceIdFormat, subscriptionId, resourceGroup, clusterName, appTypeName, appTypeVersion);
177+
}
178+
}
179+
}

0 commit comments

Comments
 (0)