@@ -235,7 +235,7 @@ func (ac *APIController) EndpointGroupUpdate(endpointGroup, params *contivModel.
235
235
return core .Errorf ("Policy not found" )
236
236
}
237
237
238
- // attach policy to epg
238
+ // detach policy to epg
239
239
err := master .PolicyDetach (endpointGroup , policy )
240
240
if err != nil && err != master .EpgPolicyExists {
241
241
log .Errorf ("Error detaching policy %s from epg %s" , policyName , endpointGroup .Key )
@@ -261,6 +261,34 @@ func (ac *APIController) EndpointGroupUpdate(endpointGroup, params *contivModel.
261
261
// EndpointGroupDelete deletes end point group
262
262
func (ac * APIController ) EndpointGroupDelete (endpointGroup * contivModel.EndpointGroup ) error {
263
263
log .Infof ("Received EndpointGroupDelete: %+v" , endpointGroup )
264
+
265
+ // Detach the endpoint group from the Policies
266
+ for _ , policyName := range endpointGroup .Policies {
267
+ policyKey := endpointGroup .TenantName + ":" + policyName
268
+
269
+ // find the policy
270
+ policy := contivModel .FindPolicy (policyKey )
271
+ if policy == nil {
272
+ log .Errorf ("Could not find policy %s" , policyName )
273
+ return core .Errorf ("Policy not found" )
274
+ }
275
+
276
+ // detach policy to epg
277
+ err := master .PolicyDetach (endpointGroup , policy )
278
+ if err != nil && err != master .EpgPolicyExists {
279
+ log .Errorf ("Error detaching policy %s from epg %s" , policyName , endpointGroup .Key )
280
+ return err
281
+ }
282
+
283
+ // Remove links
284
+ modeldb .RemoveLinkSet (& policy .LinkSets .EndpointGroups , endpointGroup )
285
+ modeldb .RemoveLinkSet (& endpointGroup .LinkSets .Policies , policy )
286
+ err = policy .Write ()
287
+ if err != nil {
288
+ return err
289
+ }
290
+ }
291
+
264
292
return nil
265
293
}
266
294
@@ -369,6 +397,21 @@ func (ac *APIController) PolicyUpdate(policy, params *contivModel.Policy) error
369
397
// PolicyDelete deletes policy
370
398
func (ac * APIController ) PolicyDelete (policy * contivModel.Policy ) error {
371
399
log .Infof ("Received PolicyDelete: %+v" , policy )
400
+
401
+ // Check if any endpoint group is using the Policy
402
+ if len (policy .LinkSets .EndpointGroups ) != 0 {
403
+ return core .Errorf ("Policy is being used" )
404
+ }
405
+
406
+ // Delete all associated Rules
407
+ for key , _ := range policy .LinkSets .Rules {
408
+ // delete the rule
409
+ err := contivModel .DeleteRule (key )
410
+ if err != nil {
411
+ log .Errorf ("Error deleting the rule: %s. Err: %v" , key , err )
412
+ }
413
+ }
414
+
372
415
return nil
373
416
}
374
417
0 commit comments