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
+ namespace Microsoft . Azure . Commands . Management . IotHub
16
+ {
17
+ using System ;
18
+ using System . Collections . Generic ;
19
+ using System . Linq ;
20
+ using System . Management . Automation ;
21
+ using Microsoft . Azure . Commands . Management . IotHub . Common ;
22
+ using Microsoft . Azure . Commands . Management . IotHub . Models ;
23
+ using Microsoft . Azure . Management . IotHub ;
24
+ using Microsoft . Azure . Management . IotHub . Models ;
25
+ using ResourceManager . Common . ArgumentCompleters ;
26
+
27
+ [ Cmdlet ( "Add" , ResourceManager . Common . AzureRMConstants . AzureRMPrefix + "IotHubMessageEnrichment" , DefaultParameterSetName = ResourceParameterSet , SupportsShouldProcess = true ) , OutputType ( typeof ( PSEnrichmentMetadata ) ) ]
28
+ [ Alias ( "Add-" + ResourceManager . Common . AzureRMConstants . AzureRMPrefix + "IotHubMsgEnrich" ) ]
29
+ public class AddAzureRmIotHubMessageEnrichment : IotHubBaseCmdlet
30
+ {
31
+ private const string ResourceIdParameterSet = "ResourceIdSet" ;
32
+ private const string ResourceParameterSet = "ResourceSet" ;
33
+ private const string InputObjectParameterSet = "InputObjectSet" ;
34
+
35
+ [ Parameter ( Position = 0 , Mandatory = true , ParameterSetName = InputObjectParameterSet , ValueFromPipeline = true , HelpMessage = "IotHub object" ) ]
36
+ [ ValidateNotNullOrEmpty ]
37
+ public PSIotHub InputObject { get ; set ; }
38
+
39
+ [ Parameter ( Position = 0 , Mandatory = true , ParameterSetName = ResourceParameterSet , HelpMessage = "Name of the Resource Group" ) ]
40
+ [ ValidateNotNullOrEmpty ]
41
+ [ ResourceGroupCompleter ]
42
+ public string ResourceGroupName { get ; set ; }
43
+
44
+ [ Parameter ( Position = 0 , Mandatory = true , ParameterSetName = ResourceIdParameterSet , ValueFromPipelineByPropertyName = true , HelpMessage = "IotHub Resource Id" ) ]
45
+ [ ValidateNotNullOrEmpty ]
46
+ [ ResourceIdCompleter ( "Microsoft.Devices/IotHubs" ) ]
47
+ public string ResourceId { get ; set ; }
48
+
49
+ [ Parameter ( Position = 1 , Mandatory = true , ParameterSetName = ResourceParameterSet , HelpMessage = "Name of the Iot Hub" ) ]
50
+ [ ValidateNotNullOrEmpty ]
51
+ public string Name { get ; set ; }
52
+
53
+ [ Parameter ( Position = 1 , Mandatory = true , ParameterSetName = InputObjectParameterSet , HelpMessage = "The enrichment's key." ) ]
54
+ [ Parameter ( Position = 1 , Mandatory = true , ParameterSetName = ResourceIdParameterSet , HelpMessage = "The enrichment's key." ) ]
55
+ [ Parameter ( Position = 2 , Mandatory = true , ParameterSetName = ResourceParameterSet , HelpMessage = "The enrichment's key." ) ]
56
+ [ ValidateNotNullOrEmpty ]
57
+ public string Key { get ; set ; }
58
+
59
+ [ Parameter ( Mandatory = true , ParameterSetName = InputObjectParameterSet , HelpMessage = "The enrichment's value." ) ]
60
+ [ Parameter ( Mandatory = true , ParameterSetName = ResourceIdParameterSet , HelpMessage = "The enrichment's value." ) ]
61
+ [ Parameter ( Mandatory = true , ParameterSetName = ResourceParameterSet , HelpMessage = "The enrichment's value." ) ]
62
+ [ ValidateNotNullOrEmpty ]
63
+ public string Value { get ; set ; }
64
+
65
+ [ Parameter ( Mandatory = true , ParameterSetName = InputObjectParameterSet , HelpMessage = "Endpoint(s) to apply enrichments to." ) ]
66
+ [ Parameter ( Mandatory = true , ParameterSetName = ResourceIdParameterSet , HelpMessage = "Endpoint(s) to apply enrichments to." ) ]
67
+ [ Parameter ( Mandatory = true , ParameterSetName = ResourceParameterSet , HelpMessage = "Endpoint(s) to apply enrichments to." ) ]
68
+ [ ValidateNotNullOrEmpty ]
69
+ public string [ ] Endpoint { get ; set ; }
70
+
71
+ public override void ExecuteCmdlet ( )
72
+ {
73
+ if ( ShouldProcess ( this . Key , Properties . Resources . AddIotHubMessageEnrichment ) )
74
+ {
75
+ IotHubDescription iotHubDescription ;
76
+ if ( ParameterSetName . Equals ( InputObjectParameterSet ) )
77
+ {
78
+ this . ResourceGroupName = this . InputObject . Resourcegroup ;
79
+ this . Name = this . InputObject . Name ;
80
+ iotHubDescription = IotHubUtils . ConvertObject < PSIotHub , IotHubDescription > ( this . InputObject ) ;
81
+ }
82
+ else
83
+ {
84
+ if ( ParameterSetName . Equals ( ResourceIdParameterSet ) )
85
+ {
86
+ this . ResourceGroupName = IotHubUtils . GetResourceGroupName ( this . ResourceId ) ;
87
+ this . Name = IotHubUtils . GetIotHubName ( this . ResourceId ) ;
88
+ }
89
+
90
+ iotHubDescription = this . IotHubClient . IotHubResource . Get ( this . ResourceGroupName , this . Name ) ;
91
+ }
92
+
93
+ if ( iotHubDescription . Properties . Routing . Enrichments == null )
94
+ {
95
+ iotHubDescription . Properties . Routing . Enrichments = new List < EnrichmentProperties > ( ) ;
96
+ }
97
+
98
+ if ( ! iotHubDescription . Properties . Routing . Enrichments . Any ( x => x . Key . Equals ( this . Key . Trim ( ) , StringComparison . OrdinalIgnoreCase ) ) )
99
+ {
100
+ IList < string > endpointNames = new List < string > ( ) ;
101
+ foreach ( string endpoint in this . Endpoint )
102
+ {
103
+ endpointNames . Add ( endpoint . Trim ( ) ) ;
104
+ }
105
+
106
+ iotHubDescription . Properties . Routing . Enrichments . Add (
107
+ new EnrichmentProperties (
108
+ this . Key . Trim ( ) ,
109
+ this . Value . Trim ( ) ,
110
+ endpointNames
111
+ ) ) ;
112
+
113
+ this . IotHubClient . IotHubResource . CreateOrUpdate ( this . ResourceGroupName , this . Name , iotHubDescription ) ;
114
+ IotHubDescription updatedIotHubDescription = this . IotHubClient . IotHubResource . Get ( this . ResourceGroupName , this . Name ) ;
115
+ this . WriteObject ( IotHubUtils . ToPSEnrichmentMetadata ( updatedIotHubDescription . Properties . Routing . Enrichments . FirstOrDefault ( x => x . Key . Equals ( this . Key , StringComparison . OrdinalIgnoreCase ) ) ) , false ) ;
116
+ }
117
+ else
118
+ {
119
+ throw new ArgumentException ( string . Format ( Properties . Resources . MessageEnrichmentKeyExist , this . Key ) ) ;
120
+ }
121
+ }
122
+ }
123
+ }
124
+ }
0 commit comments