@@ -47,7 +47,7 @@ public async Task<string> GetPropertyDetailsAsync(string ApiManagementName, stri
47
47
return await CallApiManagementAsync ( azToken , requestUrl ) ;
48
48
}
49
49
50
- public async Task < Template > GenerateNamedValuesTemplateAsync ( string singleApiName , List < TemplateResource > apiTemplateResources , Extractor exc )
50
+ public async Task < Template > GenerateNamedValuesTemplateAsync ( string singleApiName , List < TemplateResource > apiTemplateResources , Extractor exc , BackendExtractor backendExtractor , List < TemplateResource > loggerTemplateResources )
51
51
{
52
52
Template armTemplate = GenerateEmptyPropertyTemplateWithParameters ( ) ;
53
53
@@ -82,6 +82,9 @@ public async Task<Template> GenerateNamedValuesTemplateAsync(string singleApiNam
82
82
// pull all named values (properties) for service
83
83
string [ ] properties = await GetPropertiesAsync ( exc . sourceApimName , exc . resourceGroup ) ;
84
84
85
+ // isolate api and operation policy resources in the case of a single api extraction, as they may reference named value
86
+ var policyResources = apiTemplateResources . Where ( resource => ( resource . type == ResourceTypeConstants . APIPolicy || resource . type == ResourceTypeConstants . APIOperationPolicy || resource . type == ResourceTypeConstants . ProductPolicy ) ) ;
87
+
85
88
foreach ( var extractedProperty in properties )
86
89
{
87
90
JToken oProperty = JObject . Parse ( extractedProperty ) ;
@@ -119,14 +122,58 @@ public async Task<Template> GenerateNamedValuesTemplateAsync(string singleApiNam
119
122
}
120
123
else
121
124
{
122
- // TODO - if the user is executing a single api, extract all the named values used in the template resources
123
- Console . WriteLine ( "'{0}' Named value found" , propertyName ) ;
124
- templateResources . Add ( propertyTemplateResource ) ;
125
- } ;
125
+ // if the user is executing a single api, extract all the named values used in the template resources
126
+ bool foundInPolicy = DoesPolicyReferenceNamedValue ( exc , policyResources , propertyName , propertyTemplateResource ) ;
127
+ bool foundInBackEnd = await backendExtractor . IsNamedValueUsedInBackends ( exc . sourceApimName , exc . resourceGroup , singleApiName , apiTemplateResources , exc , propertyName , propertyTemplateResource . properties . displayName ) ;
128
+ bool foundInLogger = DoesLoggerReferenceNamedValue ( loggerTemplateResources , propertyName , propertyTemplateResource ) ;
129
+
130
+ // check if named value is referenced in a backend
131
+ if ( foundInPolicy || foundInBackEnd || foundInLogger )
132
+ {
133
+ // named value was used in policy, extract it
134
+ Console . WriteLine ( "'{0}' Named value found" , propertyName ) ;
135
+ templateResources . Add ( propertyTemplateResource ) ;
136
+ }
137
+ }
126
138
}
127
139
128
140
armTemplate . resources = templateResources . ToArray ( ) ;
129
141
return armTemplate ;
130
142
}
143
+
144
+ private bool DoesPolicyReferenceNamedValue ( Extractor exc , IEnumerable < TemplateResource > policyResources , string propertyName , PropertyTemplateResource propertyTemplateResource )
145
+ {
146
+ // check if named value is referenced in a policy file
147
+ foreach ( PolicyTemplateResource policyTemplateResource in policyResources )
148
+ {
149
+ string policyContent = ExtractorUtils . GetPolicyContent ( exc , policyTemplateResource ) ;
150
+
151
+ if ( policyContent . Contains ( string . Concat ( "{{" , propertyTemplateResource . properties . displayName , "}}" ) ) || policyContent . Contains ( string . Concat ( "{{" , propertyName , "}}" ) ) )
152
+ {
153
+ // dont need to go through all policies if the named value has already been found
154
+ return true ;
155
+ }
156
+ }
157
+ return false ;
158
+ }
159
+
160
+ private bool DoesLoggerReferenceNamedValue ( IEnumerable < TemplateResource > loggerTemplateResources , string propertyName , PropertyTemplateResource propertyTemplateResource )
161
+ {
162
+ foreach ( LoggerTemplateResource logger in loggerTemplateResources )
163
+ {
164
+ if ( logger . properties . credentials != null )
165
+ {
166
+ if ( ( ! string . IsNullOrEmpty ( logger . properties . credentials . connectionString ) && logger . properties . credentials . connectionString . Contains ( propertyName ) ) ||
167
+ ( ! string . IsNullOrEmpty ( logger . properties . credentials . instrumentationKey ) && logger . properties . credentials . instrumentationKey . Contains ( propertyName ) ) ||
168
+ ( ! string . IsNullOrEmpty ( logger . properties . credentials . connectionString ) && logger . properties . credentials . connectionString . Contains ( propertyTemplateResource . properties . displayName ) ) ||
169
+ ( ! string . IsNullOrEmpty ( logger . properties . credentials . instrumentationKey ) && logger . properties . credentials . instrumentationKey . Contains ( propertyTemplateResource . properties . displayName ) ) )
170
+ {
171
+ // dont need to go through all loggers if the named value has already been found
172
+ return true ;
173
+ }
174
+ }
175
+ }
176
+ return false ;
177
+ }
131
178
}
132
179
}
0 commit comments