@@ -84,58 +84,61 @@ public async Task<Tuple<Template,Dictionary<string,BackendApiParameters> > > Gen
84
84
backendTemplateResource . name = $ "[concat(parameters('{ ParameterNames . ApimServiceName } '), '/{ backendName } ')]";
85
85
backendTemplateResource . apiVersion = GlobalConstants . APIVersion ;
86
86
87
- if ( exc . paramBackend )
87
+ bool includeBackend = false ;
88
+ ////only extract the backend if this is a full extraction, or in the case of a single api, if it is referenced by one of the policies
89
+ if ( singleApiName == null )
90
+ {
91
+ // if the user is extracting all apis, extract all the backends
92
+ includeBackend = true ;
93
+ }
94
+ else
88
95
{
89
- var apiToken = new BackendApiParameters ( ) ;
90
- string validApiParamName = ExtractorUtils . GenValidParamName ( backendName , ParameterPrefix . Diagnostic ) . ToLower ( ) ;
91
-
92
-
93
- if ( ! string . IsNullOrEmpty ( backendTemplateResource . properties . resourceId ) )
96
+ // check extracted policies to see if the backend is referenced.
97
+ foreach ( PolicyTemplateResource policyTemplateResource in policyResources )
94
98
{
95
- apiToken . resourceId = backendTemplateResource . properties . resourceId ;
96
- backendTemplateResource . properties . resourceId = $ "[parameters('{ ParameterNames . BackendSettings } ').{ validApiParamName } .resourceId]";
97
- }
99
+ string policyContent = ExtractorUtils . GetPolicyContent ( exc , policyTemplateResource ) ;
98
100
99
- if ( ! string . IsNullOrEmpty ( backendTemplateResource . properties . url ) )
100
- {
101
- apiToken . url = backendTemplateResource . properties . url ;
102
- backendTemplateResource . properties . url = $ "[parameters('{ ParameterNames . BackendSettings } ').{ validApiParamName } .url]";
101
+ if ( DoesPolicyReferenceBackend ( policyContent , namedValueResources , backendName , backendTemplateResource ) )
102
+ {
103
+ // backend was used in policy, extract it
104
+ includeBackend = true ;
105
+
106
+ // dont need to go through all policies if the back end has already been found
107
+ break ;
108
+ }
103
109
}
110
+ }
104
111
105
- if ( ! string . IsNullOrEmpty ( backendTemplateResource . properties . protocol ) )
112
+ if ( includeBackend )
113
+ {
114
+ if ( exc . paramBackend )
106
115
{
107
- apiToken . protocol = backendTemplateResource . properties . protocol ;
108
- backendTemplateResource . properties . protocol = $ "[parameters('{ ParameterNames . BackendSettings } ').{ validApiParamName } .protocol]";
116
+ var apiToken = new BackendApiParameters ( ) ;
117
+ string validApiParamName = ExtractorUtils . GenValidParamName ( backendName , ParameterPrefix . Diagnostic ) . ToLower ( ) ;
118
+
119
+ if ( ! string . IsNullOrEmpty ( backendTemplateResource . properties . resourceId ) )
120
+ {
121
+ apiToken . resourceId = backendTemplateResource . properties . resourceId ;
122
+ backendTemplateResource . properties . resourceId = $ "[parameters('{ ParameterNames . BackendSettings } ').{ validApiParamName } .resourceId]";
123
+ }
124
+
125
+ if ( ! string . IsNullOrEmpty ( backendTemplateResource . properties . url ) )
126
+ {
127
+ apiToken . url = backendTemplateResource . properties . url ;
128
+ backendTemplateResource . properties . url = $ "[parameters('{ ParameterNames . BackendSettings } ').{ validApiParamName } .url]";
129
+ }
130
+
131
+ if ( ! string . IsNullOrEmpty ( backendTemplateResource . properties . protocol ) )
132
+ {
133
+ apiToken . protocol = backendTemplateResource . properties . protocol ;
134
+ backendTemplateResource . properties . protocol = $ "[parameters('{ ParameterNames . BackendSettings } ').{ validApiParamName } .protocol]";
135
+ }
136
+ oBackendParameters . Add ( validApiParamName , apiToken ) ;
109
137
}
110
- oBackendParameters . Add ( validApiParamName , apiToken ) ;
111
- }
112
138
113
- ////only extract the backend if this is a full extraction, or in the case of a single api, if it is referenced by one of the policies
114
- //if (singleApiName == null)
115
- //{
116
- // // if the user is extracting all apis, extract all the backends
117
- // Console.WriteLine("'{0}' Backend found", backendName);
118
- // templateResources.Add(backendTemplateResource);
119
- //}
120
- //else
121
- //{
122
- // bool isReferencedInPolicy = false;
123
- // foreach (PolicyTemplateResource policyTemplateResource in policyResources)
124
- // {
125
- // // the backend is used in a policy if the xml contains a set-backend-service policy, which will reference the backend's url or id
126
- // string policyContent = policyTemplateResource.properties.policyContent;
127
- // isReferencedInPolicy = DoesPolicyReferenceBackend(policyContent, namedValueResources, backendName, backendTemplateResource);
128
- // }
129
- // if (isReferencedInPolicy == true)
130
- // {
131
- // // backend was used in policy, extract it
132
- // Console.WriteLine("'{0}' Backend found", backendName);
133
- // templateResources.Add(backendTemplateResource);
134
- // }
135
- //}
136
-
137
- Console . WriteLine ( "'{0}' Backend found" , backendName ) ;
138
- templateResources . Add ( backendTemplateResource ) ;
139
+ Console . WriteLine ( "'{0}' Backend found" , backendName ) ;
140
+ templateResources . Add ( backendTemplateResource ) ;
141
+ }
139
142
}
140
143
141
144
skipNumberOfBackends += GlobalConstants . NumOfRecords ;
@@ -159,7 +162,8 @@ public bool DoesPolicyReferenceBackend(string policyContent, IEnumerable<Templat
159
162
}
160
163
foreach ( PropertyTemplateResource namedValueResource in namedValueResourcesUsedByBackend )
161
164
{
162
- if ( policyContent . Contains ( namedValueResource . properties . displayName ) || policyContent . Contains ( namedValueResource . properties . value ) )
165
+ if ( ( namedValueResource . properties . displayName != null && policyContent . Contains ( namedValueResource . properties . displayName ) ) ||
166
+ ( namedValueResource . properties . value != null && policyContent . Contains ( namedValueResource . properties . value ) ) )
163
167
{
164
168
return true ;
165
169
}
0 commit comments