16
16
using Microsoft . Azure . Management . RemoteApp ;
17
17
using Microsoft . Azure . Management . RemoteApp . Models ;
18
18
using System ;
19
- using System . Collections . Generic ;
19
+ using System . Collections . ObjectModel ;
20
20
using System . IO ;
21
- using System . Linq ;
22
21
using System . Management . Automation ;
23
22
using System . Net ;
24
23
24
+
25
25
namespace Microsoft . Azure . Management . RemoteApp . Cmdlets
26
26
{
27
27
28
28
[ Cmdlet ( VerbsCommon . Get , "AzureRemoteAppCollectionUsageDetails" ) , OutputType ( typeof ( string ) ) ]
29
29
public class GetAzureRemoteAppCollectionUsageDetails : RdsCmdlet
30
30
{
31
- [ Parameter (
32
- Position = 0 ,
33
- Mandatory = true ,
34
- ValueFromPipelineByPropertyName = true ,
35
- HelpMessage = "RemoteApp collection name" ) ]
31
+ [ Parameter ( Mandatory = true ,
32
+ Position = 0 ,
33
+ ValueFromPipelineByPropertyName = true ,
34
+ HelpMessage = "RemoteApp collection name" ) ]
35
+ [ ValidatePattern ( NameValidatorString ) ]
36
+ [ Alias ( "Name" ) ]
36
37
public string CollectionName { get ; set ; }
37
38
38
39
[ Parameter ( Mandatory = false ,
@@ -47,73 +48,39 @@ public class GetAzureRemoteAppCollectionUsageDetails : RdsCmdlet
47
48
[ ValidatePattern ( FullYearPattern ) ]
48
49
public string UsageYear { get ; set ; }
49
50
50
- public override void ExecuteCmdlet ( )
51
- {
52
- DateTime today = DateTime . Now ;
53
- CollectionUsageDetailsResult detailsUsage = null ;
54
- string locale = String . Empty ;
55
- RemoteAppOperationStatusResult operationResult = null ;
56
- int maxRetryCount = 60 ;
57
-
58
- if ( String . IsNullOrWhiteSpace ( UsageMonth ) )
59
- {
60
- UsageMonth = today . Month . ToString ( ) ;
61
- }
62
-
63
- if ( String . IsNullOrWhiteSpace ( UsageYear ) )
64
- {
65
- UsageYear = today . Year . ToString ( ) ;
66
- }
67
-
68
- locale = System . Globalization . CultureInfo . CurrentCulture . ToString ( ) ;
51
+ [ Parameter ( Mandatory = false ,
52
+ HelpMessage = "Allows running the cmdlet in the background as a PS job." ) ]
53
+ public SwitchParameter AsJob { get ; set ; }
69
54
70
- detailsUsage = CallClient ( ( ) => Client . Collections . GetUsageDetails ( CollectionName , UsageYear , UsageMonth , locale ) , Client . Collections ) ;
55
+ private LongRunningTask < GetAzureRemoteAppCollectionUsageDetails > task = null ;
71
56
72
- if ( detailsUsage == null )
73
- {
74
- return ;
75
- }
57
+ private void GetUsageDetails ( CollectionUsageDetailsResult detailsUsage )
58
+ {
59
+ RemoteAppOperationStatusResult operationResult = null ;
76
60
77
61
// The request is async and we have to wait for the usage details to be produced here
78
62
do
79
63
{
80
-
81
64
System . Threading . Thread . Sleep ( 5000 ) ;
82
65
83
66
operationResult = CallClient ( ( ) => Client . OperationResults . Get ( detailsUsage . UsageDetails . OperationTrackingId ) , Client . OperationResults ) ;
84
67
}
85
- while ( operationResult . RemoteAppOperationResult . Status != RemoteAppOperationStatus . Success &&
86
- operationResult . RemoteAppOperationResult . Status != RemoteAppOperationStatus . Failed &&
87
- -- maxRetryCount > 0 ) ;
68
+ while ( operationResult . RemoteAppOperationResult . Status != RemoteAppOperationStatus . Success &&
69
+ operationResult . RemoteAppOperationResult . Status != RemoteAppOperationStatus . Failed ) ;
88
70
89
71
if ( operationResult . RemoteAppOperationResult . Status == RemoteAppOperationStatus . Success )
90
72
{
91
73
WriteUsageDetails ( detailsUsage ) ;
92
74
}
93
75
else
94
76
{
95
- if ( operationResult . RemoteAppOperationResult . Status == RemoteAppOperationStatus . Failed )
96
- {
97
- ErrorRecord error = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
98
- Commands_RemoteApp . DetailedUsageFailureMessage ,
99
- String . Empty ,
100
- Client . Collections ,
101
- ErrorCategory . ResourceUnavailable ) ;
77
+ ErrorRecord error = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
78
+ Commands_RemoteApp . DetailedUsageFailureMessage ,
79
+ String . Empty ,
80
+ Client . Collections ,
81
+ ErrorCategory . ResourceUnavailable ) ;
102
82
103
- WriteError ( error ) ;
104
- }
105
- else if ( maxRetryCount <= 0 )
106
- {
107
- ErrorRecord error = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
108
- String . Format ( System . Globalization . CultureInfo . InvariantCulture ,
109
- Commands_RemoteApp . RequestTimedOutFormat ,
110
- detailsUsage . UsageDetails . OperationTrackingId ) ,
111
- String . Empty ,
112
- Client . Collections ,
113
- ErrorCategory . OperationTimeout ) ;
114
-
115
- WriteError ( error ) ;
116
- }
83
+ WriteError ( error ) ;
117
84
}
118
85
}
119
86
@@ -129,11 +96,19 @@ private void WriteUsageDetails(CollectionUsageDetailsResult detailsUsage)
129
96
try
130
97
{
131
98
response = ( HttpWebResponse ) request . GetResponse ( ) ;
99
+ if ( response == null )
100
+ {
101
+ ErrorRecord error = RemoteAppCollectionErrorState . CreateErrorRecordFromString (
102
+ "Unable to retrieve Usage data" , String . Empty , null , ErrorCategory . InvalidResult ) ;
103
+ WriteError ( error ) ;
104
+ return ;
105
+ }
132
106
}
133
107
catch ( Exception e )
134
108
{
135
109
ErrorRecord error = RemoteAppCollectionErrorState . CreateErrorRecordFromException ( e , String . Empty , Client . Collections , ErrorCategory . InvalidResult ) ;
136
110
WriteError ( error ) ;
111
+ return ;
137
112
}
138
113
139
114
using ( Stream dataStream = response . GetResponseStream ( ) )
@@ -145,5 +120,50 @@ private void WriteUsageDetails(CollectionUsageDetailsResult detailsUsage)
145
120
}
146
121
}
147
122
}
123
+
124
+ public override void ExecuteCmdlet ( )
125
+ {
126
+ DateTime today = DateTime . Now ;
127
+ CollectionUsageDetailsResult detailsUsage = null ;
128
+ string locale = String . Empty ;
129
+
130
+ if ( String . IsNullOrWhiteSpace ( UsageMonth ) )
131
+ {
132
+ UsageMonth = today . Month . ToString ( ) ;
133
+ }
134
+
135
+ if ( String . IsNullOrWhiteSpace ( UsageYear ) )
136
+ {
137
+ UsageYear = today . Year . ToString ( ) ;
138
+ }
139
+
140
+ locale = System . Globalization . CultureInfo . CurrentCulture . ToString ( ) ;
141
+
142
+ detailsUsage = CallClient ( ( ) => Client . Collections . GetUsageDetails ( CollectionName , UsageYear , UsageMonth , locale ) , Client . Collections ) ;
143
+
144
+ if ( detailsUsage == null )
145
+ {
146
+ return ;
147
+ }
148
+
149
+ if ( AsJob . IsPresent )
150
+ {
151
+ task = new LongRunningTask < GetAzureRemoteAppCollectionUsageDetails > ( this , "RemoteAppBackgroundTask" , Commands_RemoteApp . UsageDetails ) ;
152
+
153
+ task . ProcessJob ( ( ) =>
154
+ {
155
+ task . SetStatus ( Commands_RemoteApp . DownloadingUsageDetails ) ;
156
+ GetUsageDetails ( detailsUsage ) ;
157
+ task . SetStatus ( Commands_RemoteApp . JobComplete ) ;
158
+ } ) ;
159
+
160
+ WriteObject ( task ) ;
161
+
162
+ }
163
+ else
164
+ {
165
+ GetUsageDetails ( detailsUsage ) ;
166
+ }
167
+ }
148
168
}
149
169
}
0 commit comments