@@ -20,6 +20,7 @@ namespace Microsoft.Azure.Commands.Resources
20
20
using System . Management . Automation ;
21
21
using Microsoft . Azure . Commands . Resources . Models ;
22
22
using Microsoft . Azure . Management . Resources . Models ;
23
+ using ProjectResources = Microsoft . Azure . Commands . Resources . Properties . Resources ;
23
24
24
25
/// <summary>
25
26
/// Get an existing resource.
@@ -28,90 +29,93 @@ namespace Microsoft.Azure.Commands.Resources
28
29
public class GetAzureProviderOperationCommand : ResourcesBaseCmdlet
29
30
{
30
31
private const string WildCardCharacter = "*" ;
32
+ private static readonly char Separator = '/' ;
31
33
32
34
/// <summary>
33
35
/// Gets or sets the provider namespace
34
36
/// </summary>
35
37
[ Parameter ( Position = 0 , Mandatory = true , ValueFromPipelineByPropertyName = false , ValueFromPipeline = true , HelpMessage = "The action string." ) ]
36
38
[ ValidateNotNullOrEmpty ]
37
- public string ActionString { get ; set ; }
39
+ public string OperationSearchString { get ; set ; }
38
40
39
41
/// <summary>
40
42
/// Executes the cmdlet
41
43
/// </summary>
42
44
protected override void ProcessRecord ( )
43
45
{
44
46
// remove leading and trailing whitespaces
45
- this . ActionString = this . ActionString . Trim ( ) ;
47
+ this . OperationSearchString = this . OperationSearchString . Trim ( ) ;
46
48
47
- List < PSResourceProviderOperation > operationsToDisplay ;
49
+ ValidateActionSearchString ( this . OperationSearchString ) ;
50
+
51
+ List < PSResourceProviderOperation > operationsToDisplay ;
48
52
49
- if ( this . ActionString . Contains ( WildCardCharacter ) )
53
+ if ( this . OperationSearchString . Contains ( WildCardCharacter ) )
50
54
{
51
- operationsToDisplay = this . ProcessProviderOperationsWithWildCard ( ActionString ) ;
55
+ operationsToDisplay = this . ProcessProviderOperationsWithWildCard ( OperationSearchString ) ;
52
56
}
53
57
else
54
58
{
55
- operationsToDisplay = this . ProcessProviderOperationsWithoutWildCard ( ActionString ) ;
59
+ operationsToDisplay = this . ProcessProviderOperationsWithoutWildCard ( OperationSearchString ) ;
56
60
}
57
61
58
62
this . WriteObject ( operationsToDisplay , enumerateCollection : true ) ;
59
63
}
60
64
65
+ private static void ValidateActionSearchString ( string actionSearchString )
66
+ {
67
+ if ( actionSearchString . Contains ( "?" ) )
68
+ {
69
+ throw new ArgumentException ( ProjectResources . ProviderOperationUnsupportedWildcard ) ;
70
+ }
71
+
72
+ string [ ] parts = actionSearchString . Split ( Separator ) ;
73
+ if ( parts . Any ( p => p . Contains ( WildCardCharacter ) && p . Length != 1 ) )
74
+ {
75
+ throw new ArgumentException ( ProjectResources . OperationSearchStringInvalidWildcard ) ;
76
+ }
77
+
78
+ if ( parts . Length == 1 && parts [ 0 ] != WildCardCharacter )
79
+ {
80
+ throw new ArgumentException ( string . Format ( ProjectResources . OperationSearchStringInvalidProviderName , parts [ 0 ] ) ) ;
81
+ }
82
+ }
83
+
61
84
/// <summary>
62
85
/// Get a list of Provider operations in the case that the Actionstring input contains a wildcard
63
86
/// </summary>
64
- private List < PSResourceProviderOperation > ProcessProviderOperationsWithWildCard ( string actionString )
87
+ private List < PSResourceProviderOperation > ProcessProviderOperationsWithWildCard ( string actionSearchString )
65
88
{
66
89
// Filter the list of all operation names to what matches the wildcard
67
- WildcardPattern wildcard = new WildcardPattern ( actionString , WildcardOptions . IgnoreCase | WildcardOptions . Compiled ) ;
90
+ WildcardPattern wildcard = new WildcardPattern ( actionSearchString , WildcardOptions . IgnoreCase | WildcardOptions . Compiled ) ;
68
91
69
92
List < ProviderOperationsMetadata > providers = new List < ProviderOperationsMetadata > ( ) ;
70
-
71
- string nonWildCardPrefix = GetAzureProviderOperationCommand . GetNonWildcardPrefix ( actionString ) ;
72
- if ( string . IsNullOrWhiteSpace ( nonWildCardPrefix ) )
93
+ string provider = this . OperationSearchString . Split ( Separator ) . First ( ) ;
94
+ if ( provider . Equals ( WildCardCharacter ) )
73
95
{
74
96
// 'Get-AzureRmProviderOperation *' or 'Get-AzureRmProviderOperation */virtualmachines/*'
75
97
// get operations for all providers
76
98
providers . AddRange ( this . ResourcesClient . ListProviderOperationsMetadata ( ) ) ;
77
99
}
78
100
else
79
101
{
80
- // Some string exists before the wild card character - potentially the full name of the provider.
81
- string providerFullName = GetAzureProviderOperationCommand . GetResourceProviderFullName ( nonWildCardPrefix ) ;
82
- if ( ! string . IsNullOrWhiteSpace ( providerFullName ) )
83
- {
84
- // we have the full name of the provider. 'Get-AzureRmProviderOperation Microsoft.Sql/servers/*'
85
- // only query for that provider
86
- providers . Add ( this . ResourcesClient . GetProviderOperationsMetadata ( providerFullName ) ) ;
87
- }
88
- else
89
- {
90
- // We have only a partial name of the provider, say Microsoft.*/* or Microsoft.*/*/read.
91
- // query for all providers and then do prefix match on the operations
92
- providers . AddRange ( this . ResourcesClient . ListProviderOperationsMetadata ( ) ) ;
93
- }
102
+ // 'Get-AzureRmProviderOperation Microsoft.Compute/virtualmachines/*' or 'Get-AzureRmProviderOperation Microsoft.Sql/*'
103
+ providers . Add ( this . ResourcesClient . GetProviderOperationsMetadata ( provider ) ) ;
94
104
}
95
105
96
- return providers . SelectMany ( p => GetPSOperationsFromProviderOperationsMetadata ( p ) ) . Where ( operation => wildcard . IsMatch ( operation . Operation ) ) . ToList ( ) ;
106
+ return providers . SelectMany ( p => GetPSOperationsFromProviderOperationsMetadata ( p ) ) . Where ( operation => wildcard . IsMatch ( operation . Operation ) ) . ToList ( ) ;
97
107
}
98
108
99
109
/// <summary>
100
110
/// Gets a list of Provider operations in the case that the Actionstring input does not contain a wildcard
101
111
/// </summary>
102
- private List < PSResourceProviderOperation > ProcessProviderOperationsWithoutWildCard ( string actionString )
112
+ private List < PSResourceProviderOperation > ProcessProviderOperationsWithoutWildCard ( string operationString )
103
113
{
104
- List < PSResourceProviderOperation > operationsToDisplay = new List < PSResourceProviderOperation > ( ) ;
105
- string providerFullName = GetAzureProviderOperationCommand . GetResourceProviderFullName ( actionString ) ;
106
- if ( ! string . IsNullOrWhiteSpace ( providerFullName ) )
107
- {
108
- // We have the full name of the provider. get operations metadata for this provider
109
- ProviderOperationsMetadata providerOperations = this . ResourcesClient . GetProviderOperationsMetadata ( providerFullName ) ;
110
- IEnumerable < PSResourceProviderOperation > flattenedProviderOperations = GetAzureProviderOperationCommand . GetPSOperationsFromProviderOperationsMetadata ( providerOperations ) ;
111
- operationsToDisplay . AddRange ( flattenedProviderOperations . Where ( op => string . Equals ( op . Operation , actionString , StringComparison . OrdinalIgnoreCase ) ) ) ;
112
- }
114
+ string providerFullName = operationString . Split ( Separator ) . First ( ) ;
113
115
114
- return operationsToDisplay ;
116
+ ProviderOperationsMetadata providerOperations = this . ResourcesClient . GetProviderOperationsMetadata ( providerFullName ) ;
117
+ IEnumerable < PSResourceProviderOperation > flattenedProviderOperations = GetAzureProviderOperationCommand . GetPSOperationsFromProviderOperationsMetadata ( providerOperations ) ;
118
+ return flattenedProviderOperations . Where ( op => string . Equals ( op . Operation , operationString , StringComparison . OrdinalIgnoreCase ) ) . ToList ( ) ;
115
119
}
116
120
117
121
private static IEnumerable < PSResourceProviderOperation > GetPSOperationsFromProviderOperationsMetadata ( ProviderOperationsMetadata providerOperationsMetadata )
@@ -150,7 +154,7 @@ private static PSResourceProviderOperation ToPSResourceProviderOperation(Operati
150
154
/// </summary>
151
155
private static string GetResourceProviderFullName ( string nonWildCardPrefix )
152
156
{
153
- int index = nonWildCardPrefix . IndexOf ( "/" , 0 ) ;
157
+ int index = nonWildCardPrefix . IndexOf ( Separator . ToString ( ) , 0 ) ;
154
158
return index > 0 ? nonWildCardPrefix . Substring ( 0 , index ) : string . Empty ;
155
159
}
156
160
0 commit comments