Skip to content

Commit dc6196b

Browse files
unknownunknown
authored andcommitted
Fix Get-AzureRole regarding ErrorAction
Azure#124
1 parent 17eab23 commit dc6196b

File tree

1 file changed

+81
-74
lines changed
  • src/ServiceManagement/Compute/Commands.ServiceManagement/HostedServices

1 file changed

+81
-74
lines changed

src/ServiceManagement/Compute/Commands.ServiceManagement/HostedServices/GetAzureRole.cs

Lines changed: 81 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System;
16-
using System.Collections.Generic;
17-
using System.Collections.ObjectModel;
18-
using System.Linq;
19-
using System.Management.Automation;
2015
using AutoMapper;
2116
using Microsoft.WindowsAzure.Commands.ServiceManagement.Helpers;
2217
using Microsoft.WindowsAzure.Commands.ServiceManagement.Properties;
2318
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2419
using Microsoft.WindowsAzure.Management.Compute;
2520
using Microsoft.WindowsAzure.Management.Compute.Models;
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Collections.ObjectModel;
24+
using System.Linq;
25+
using System.Management.Automation;
2626

2727
namespace Microsoft.WindowsAzure.Commands.ServiceManagement.HostedServices
2828
{
@@ -70,88 +70,95 @@ protected override void OnProcessRecord()
7070
public void GetRoleProcess()
7171
{
7272
OperationStatusResponse getDeploymentOperation;
73-
var currentDeployment = this.GetCurrentDeployment(out getDeploymentOperation);
74-
if (currentDeployment != null)
73+
try
7574
{
76-
if (this.InstanceDetails.IsPresent)
75+
var currentDeployment = this.GetCurrentDeployment(out getDeploymentOperation);
76+
if (currentDeployment != null)
7777
{
78-
Collection<PVM.RoleInstanceContext> instanceContexts = new Collection<PVM.RoleInstanceContext>();
79-
IList<RoleInstance> roleInstances = null;
80-
81-
if (string.IsNullOrEmpty(this.RoleName))
82-
{
83-
roleInstances = currentDeployment.RoleInstances;
84-
}
85-
else
86-
{
87-
roleInstances = new List<RoleInstance>(currentDeployment.RoleInstances.Where(r => r.RoleName.Equals(this.RoleName, StringComparison.OrdinalIgnoreCase)));
88-
}
89-
90-
foreach (var role in roleInstances)
78+
if (this.InstanceDetails.IsPresent)
9179
{
92-
var vmRole = currentDeployment.Roles == null || !currentDeployment.Roles.Any() ? null
93-
: currentDeployment.Roles.FirstOrDefault(r => string.Equals(r.RoleName, role.RoleName, StringComparison.OrdinalIgnoreCase));
80+
Collection<PVM.RoleInstanceContext> instanceContexts = new Collection<PVM.RoleInstanceContext>();
81+
IList<RoleInstance> roleInstances = null;
9482

95-
instanceContexts.Add(new PVM.RoleInstanceContext
83+
if (string.IsNullOrEmpty(this.RoleName))
9684
{
97-
ServiceName = this.ServiceName,
98-
OperationId = getDeploymentOperation.Id,
99-
OperationDescription = this.CommandRuntime.ToString(),
100-
OperationStatus = getDeploymentOperation.Status.ToString(),
101-
InstanceErrorCode = role.InstanceErrorCode,
102-
InstanceFaultDomain = role.InstanceFaultDomain,
103-
InstanceName = role.InstanceName,
104-
InstanceSize = role.InstanceSize,
105-
InstanceStateDetails = role.InstanceStateDetails,
106-
InstanceStatus = role.InstanceStatus,
107-
InstanceUpgradeDomain = role.InstanceUpgradeDomain,
108-
RoleName = role.RoleName,
109-
IPAddress = role.IPAddress,
110-
PublicIPAddress = role.PublicIPs == null || !role.PublicIPs.Any() ? null : role.PublicIPs.First().Address,
111-
PublicIPName = role.PublicIPs == null || !role.PublicIPs.Any() ? null
112-
: !string.IsNullOrEmpty(role.PublicIPs.First().Name) ? role.PublicIPs.First().Name
113-
: PersistentVMHelper.GetPublicIPName(vmRole),
114-
PublicIPIdleTimeoutInMinutes = role.PublicIPs == null || !role.PublicIPs.Any() ? null
115-
: role.PublicIPs.First().IdleTimeoutInMinutes,
116-
PublicIPDomainNameLabel = role.PublicIPs == null || !role.PublicIPs.Any() ? null : role.PublicIPs.First().DomainNameLabel,
117-
PublicIPFqdns = role.PublicIPs == null || !role.PublicIPs.Any() ? null : role.PublicIPs.First().Fqdns.ToList(),
118-
DeploymentID = currentDeployment.PrivateId,
119-
InstanceEndpoints = Mapper.Map<PVM.InstanceEndpointList>(role.InstanceEndpoints)
120-
});
121-
}
85+
roleInstances = currentDeployment.RoleInstances;
86+
}
87+
else
88+
{
89+
roleInstances = new List<RoleInstance>(currentDeployment.RoleInstances.Where(r => r.RoleName.Equals(this.RoleName, StringComparison.OrdinalIgnoreCase)));
90+
}
12291

123-
WriteObject(instanceContexts, true);
124-
}
125-
else
126-
{
127-
var roleContexts = new Collection<PVM.RoleContext>();
128-
IList<Role> roles = null;
129-
if (string.IsNullOrEmpty(this.RoleName))
130-
{
131-
roles = currentDeployment.Roles;
92+
foreach (var role in roleInstances)
93+
{
94+
var vmRole = currentDeployment.Roles == null || !currentDeployment.Roles.Any() ? null
95+
: currentDeployment.Roles.FirstOrDefault(r => string.Equals(r.RoleName, role.RoleName, StringComparison.OrdinalIgnoreCase));
96+
97+
instanceContexts.Add(new PVM.RoleInstanceContext
98+
{
99+
ServiceName = this.ServiceName,
100+
OperationId = getDeploymentOperation.Id,
101+
OperationDescription = this.CommandRuntime.ToString(),
102+
OperationStatus = getDeploymentOperation.Status.ToString(),
103+
InstanceErrorCode = role.InstanceErrorCode,
104+
InstanceFaultDomain = role.InstanceFaultDomain,
105+
InstanceName = role.InstanceName,
106+
InstanceSize = role.InstanceSize,
107+
InstanceStateDetails = role.InstanceStateDetails,
108+
InstanceStatus = role.InstanceStatus,
109+
InstanceUpgradeDomain = role.InstanceUpgradeDomain,
110+
RoleName = role.RoleName,
111+
IPAddress = role.IPAddress,
112+
PublicIPAddress = role.PublicIPs == null || !role.PublicIPs.Any() ? null : role.PublicIPs.First().Address,
113+
PublicIPName = role.PublicIPs == null || !role.PublicIPs.Any() ? null
114+
: !string.IsNullOrEmpty(role.PublicIPs.First().Name) ? role.PublicIPs.First().Name
115+
: PersistentVMHelper.GetPublicIPName(vmRole),
116+
PublicIPIdleTimeoutInMinutes = role.PublicIPs == null || !role.PublicIPs.Any() ? null
117+
: role.PublicIPs.First().IdleTimeoutInMinutes,
118+
PublicIPDomainNameLabel = role.PublicIPs == null || !role.PublicIPs.Any() ? null : role.PublicIPs.First().DomainNameLabel,
119+
PublicIPFqdns = role.PublicIPs == null || !role.PublicIPs.Any() ? null : role.PublicIPs.First().Fqdns.ToList(),
120+
DeploymentID = currentDeployment.PrivateId,
121+
InstanceEndpoints = Mapper.Map<PVM.InstanceEndpointList>(role.InstanceEndpoints)
122+
});
123+
}
124+
125+
WriteObject(instanceContexts, true);
132126
}
133127
else
134128
{
135-
roles = new List<Role>(currentDeployment.Roles.Where(r => r.RoleName.Equals(this.RoleName, StringComparison.OrdinalIgnoreCase)));
136-
}
129+
var roleContexts = new Collection<PVM.RoleContext>();
130+
IList<Role> roles = null;
131+
if (string.IsNullOrEmpty(this.RoleName))
132+
{
133+
roles = currentDeployment.Roles;
134+
}
135+
else
136+
{
137+
roles = new List<Role>(currentDeployment.Roles.Where(r => r.RoleName.Equals(this.RoleName, StringComparison.OrdinalIgnoreCase)));
138+
}
137139

138-
foreach (var r in roles.Select(role => new PVM.RoleContext
139-
{
140-
InstanceCount = currentDeployment.RoleInstances.Count(ri => ri.RoleName.Equals(role.RoleName, StringComparison.OrdinalIgnoreCase)),
141-
RoleName = role.RoleName,
142-
OperationDescription = this.CommandRuntime.ToString(),
143-
OperationStatus = getDeploymentOperation.Status.ToString(),
144-
OperationId = getDeploymentOperation.Id,
145-
ServiceName = this.ServiceName,
146-
DeploymentID = currentDeployment.PrivateId
147-
}))
148-
{
149-
roleContexts.Add(r);
150-
}
140+
foreach (var r in roles.Select(role => new PVM.RoleContext
141+
{
142+
InstanceCount = currentDeployment.RoleInstances.Count(ri => ri.RoleName.Equals(role.RoleName, StringComparison.OrdinalIgnoreCase)),
143+
RoleName = role.RoleName,
144+
OperationDescription = this.CommandRuntime.ToString(),
145+
OperationStatus = getDeploymentOperation.Status.ToString(),
146+
OperationId = getDeploymentOperation.Id,
147+
ServiceName = this.ServiceName,
148+
DeploymentID = currentDeployment.PrivateId
149+
}))
150+
{
151+
roleContexts.Add(r);
152+
}
151153

152-
WriteObject(roleContexts, true);
154+
WriteObject(roleContexts, true);
155+
}
153156
}
154157
}
158+
catch (Exception ex)
159+
{
160+
WriteExceptionError(ex);
161+
}
155162
}
156163

157164
private DeploymentGetResponse GetCurrentDeployment(out OperationStatusResponse operation)

0 commit comments

Comments
 (0)