// DeleteNetworkEndpointGroup deletes the name endpoint group from the zone
172238
func (g *Cloud) DeleteNetworkEndpointGroup(name string, zone string) error {
	ctx, cancel := cloud.ContextWithCallTimeout()
	defer cancel()

	mc := newNetworkEndpointGroupMetricContext("delete", zone)
	return mc.Observe(g.c.BetaNetworkEndpointGroups().Delete(ctx, meta.ZonalKey(name, zone)))
}
307726
func (c *openstackCloud) DeleteGroup(g *cloudinstances.CloudInstanceGroup) error {
	grp := g.Raw.(*servergroups.ServerGroup)

	for _, id := range grp.Members {
		err := c.DeleteInstanceWithID(id)
		if err != nil {
			return fmt.Errorf("Could not delete instance %q: %v", id, err)
		}
	}

	ports, err := c.ListPorts(ports.ListOpts{})
	if err != nil {
		return fmt.Errorf("Could not list ports %v", err)
	}

	for _, port := range ports {
		if strings.Contains(port.Name, grp.Name) {
			err := c.DeletePort(port.ID)
			if err != nil {
				return fmt.Errorf("Could not delete port %q: %v", port.ID, err)
			}
		}
	}

	err = c.DeleteServerGroup(grp.ID)
	if err != nil {
		return fmt.Errorf("Could not server group %q: %v", grp.ID, err)
	}

	return nil
}
1094718
func (p *NetPlugin) DeleteNetwork(id, subnet, nwType, encap string, pktTag, extPktTag int, Gw string, tenant string) error {
	p.Lock()
	defer p.Unlock()
	return p.NetworkDriver.DeleteNetwork(id, subnet, nwType, encap, pktTag, extPktTag, Gw, tenant)
}
1144097
func httpListEndpointGroups(w http.ResponseWriter, r *http.Request, vars map[string]string) (interface{}, error) {
	log.Debugf("Received httpListEndpointGroups: %+v", vars)

	list := make([]*EndpointGroup, 0)
	collections.endpointGroupMutex.Lock()
	defer collections.endpointGroupMutex.Unlock()
	for _, obj := range collections.endpointGroups {
		list = append(list, obj)
	}

	// Return the list
	return list, nil
}
397860
func (ac *APIController) EndpointGroupCreate(endpointGroup *contivModel.EndpointGroup) error {
	log.Infof("Received EndpointGroupCreate: %+v", endpointGroup)

	// Find the tenant
	tenant := contivModel.FindTenant(endpointGroup.TenantName)
	if tenant == nil {
		return core.Errorf("Tenant not found")
	}
	// Find the network
	nwObjKey := endpointGroup.TenantName + ":" + endpointGroup.NetworkName
	network := contivModel.FindNetwork(nwObjKey)
	if network == nil {
		return core.Errorf("Network %s not found", endpointGroup.NetworkName)
	}
	// If there is a Network with the same name as this endpointGroup, reject.
	nameClash := contivModel.FindNetwork(endpointGroup.Key)
	if nameClash != nil {
		return core.Errorf("Network %s conflicts with the endpointGroup name",
			nameClash.NetworkName)
	}
	// create the endpoint group state
	err := master.CreateEndpointGroup(endpointGroup.TenantName, endpointGroup.NetworkName,
		endpointGroup.GroupName, endpointGroup.IpPool, endpointGroup.CfgdTag)
	if err != nil {
		log.Errorf("Error creating endpoint group %+v. Err: %v", endpointGroup, err)
		return err
	}
	// for each policy create an epg policy Instance
	for _, policyName := range endpointGroup.Policies {
		policyKey := GetpolicyKey(endpointGroup.TenantName, policyName)
		// find the policy
		policy := contivModel.FindPolicy(policyKey)
		if policy == nil {
			log.Errorf("Could not find policy %s", policyName)
			endpointGroupCleanup(endpointGroup)
			return core.Errorf("Policy not found")
		}

		// attach policy to epg
		err = master.PolicyAttach(endpointGroup, policy)
		if err != nil {
			log.Errorf("Error attaching policy %s to epg %s", policyName, endpointGroup.Key)
			endpointGroupCleanup(endpointGroup)
			return err
		}

		// establish Links
		modeldb.AddLinkSet(&policy.LinkSets.EndpointGroups, endpointGroup)
		modeldb.AddLinkSet(&endpointGroup.LinkSets.Policies, policy)

		// Write the policy
		err = policy.Write()
		if err != nil {
			endpointGroupCleanup(endpointGroup)
			return err
		}
	}

	// If endpoint group is to be attached to any netprofile, then attach the netprofile and create links and linksets.
	if endpointGroup.NetProfile != "" {
		profileKey := GetNetprofileKey(endpointGroup.TenantName, endpointGroup.NetProfile)
		netprofile := contivModel.FindNetprofile(profileKey)
		if netprofile == nil {
			log.Errorf("Error finding netprofile: %s", profileKey)
			return errors.New("netprofile not found")
		}

		// attach NetProfile to epg
		err = master.UpdateEndpointGroup(netprofile.Bandwidth, endpointGroup.GroupName, endpointGroup.TenantName, netprofile.DSCP, netprofile.Burst)
		if err != nil {
			log.Errorf("Error attaching NetProfile %s to epg %s", endpointGroup.NetProfile, endpointGroup.Key)
			endpointGroupCleanup(endpointGroup)
			return err
		}

		//establish links (epg - netprofile)
		modeldb.AddLink(&endpointGroup.Links.NetProfile, netprofile)
		//establish linksets (Netprofile - epg)
		modeldb.AddLinkSet(&netprofile.LinkSets.EndpointGroups, endpointGroup)

		//Write the attached Netprofile to modeldb
		err = netprofile.Write()
		if err != nil {
			endpointGroupCleanup(endpointGroup)
			return err
		}
	}

	// Setup external contracts this EPG might have.
	err = setupExternalContracts(endpointGroup, endpointGroup.ExtContractsGrps)
	if err != nil {
		log.Errorf("Error setting up external contracts for epg %s", endpointGroup.Key)
		endpointGroupCleanup(endpointGroup)
		return err
	}

	// Setup links
	modeldb.AddLink(&endpointGroup.Links.Network, network)
	modeldb.AddLink(&endpointGroup.Links.Tenant, tenant)
	modeldb.AddLinkSet(&network.LinkSets.EndpointGroups, endpointGroup)
	modeldb.AddLinkSet(&tenant.LinkSets.EndpointGroups, endpointGroup)

	// Save the tenant and network since we added the links
	err = network.Write()
	if err != nil {
		endpointGroupCleanup(endpointGroup)
		return err
	}

	err = tenant.Write()
	if err != nil {
		endpointGroupCleanup(endpointGroup)
		return err
	}

	return nil
}
229189
func (ac *APIController) EndpointGroupUpdate(endpointGroup, params *contivModel.EndpointGroup) error {
	log.Infof("Received EndpointGroupUpdate: %+v, params: %+v", endpointGroup, params)

	// if the network association was changed, reject the update.
	if endpointGroup.NetworkName != params.NetworkName {
		return core.Errorf("Cannot change network association after epg is created.")
	}

	if endpointGroup.IpPool != params.IpPool {
		return core.Errorf("Cannot change IP pool after epg is created.")
	}

	// Only update policy attachments

	// Look for policy adds
	for _, policyName := range params.Policies {
		if !stringInSlice(policyName, endpointGroup.Policies) {
			policyKey := GetpolicyKey(endpointGroup.TenantName, policyName)

			// find the policy
			policy := contivModel.FindPolicy(policyKey)
			if policy == nil {
				log.Errorf("Could not find policy %s", policyName)
				return core.Errorf("Policy not found")
			}

			// attach policy to epg
			err := master.PolicyAttach(endpointGroup, policy)
			if err != nil && err != master.EpgPolicyExists {
				log.Errorf("Error attaching policy %s to epg %s", policyName, endpointGroup.Key)
				return err
			}

			// Setup links
			modeldb.AddLinkSet(&policy.LinkSets.EndpointGroups, endpointGroup)
			modeldb.AddLinkSet(&endpointGroup.LinkSets.Policies, policy)
			err = policy.Write()
			if err != nil {
				return err
			}
		}
	}

	//for netProfile removal,
	if endpointGroup.NetProfile != "" && params.NetProfile == "" {

		profileKey := GetNetprofileKey(endpointGroup.TenantName, endpointGroup.NetProfile)
		netprofile := contivModel.FindNetprofile(profileKey)
		if netprofile == nil {
			log.Errorf("Error finding netprofile: %s", profileKey)
			return errors.New("netprofile not found")
		}

		// attach NetProfile to epg
		err := master.UpdateEndpointGroup("", endpointGroup.GroupName, endpointGroup.TenantName, 0, 0)
		if err != nil {
			log.Errorf("Error attaching NetProfile %s to epg %s", endpointGroup.NetProfile, endpointGroup.Key)
			endpointGroupCleanup(endpointGroup)
			return err
		}

		modeldb.RemoveLink(&endpointGroup.Links.NetProfile, netprofile)
		modeldb.RemoveLinkSet(&netprofile.LinkSets.EndpointGroups, endpointGroup)

		err = netprofile.Write()
		if err != nil {
			endpointGroupCleanup(endpointGroup)
			return err
		}

		endpointGroup.NetProfile = params.NetProfile

	} else if params.NetProfile != "" {

		paramsKey := GetNetprofileKey(params.TenantName, params.NetProfile)
		netprofile := contivModel.FindNetprofile(paramsKey)
		if netprofile == nil {
			log.Errorf("Error finding netprofile: %s", paramsKey)
			return errors.New("netprofile not found")
		}

		// attach NetProfile to epg
		err := master.UpdateEndpointGroup(netprofile.Bandwidth, endpointGroup.GroupName, endpointGroup.TenantName, netprofile.DSCP, netprofile.Burst)
		if err != nil {
			log.Errorf("Error attaching NetProfile %s to epg %s", params.NetProfile, endpointGroup.Key)
			endpointGroupCleanup(endpointGroup)
			return err
		}

		if endpointGroup.NetProfile != "" && endpointGroup.NetProfile != params.NetProfile {
			// get the epg netprofile object.
			profileKey := GetNetprofileKey(endpointGroup.TenantName, endpointGroup.NetProfile)
			epgnetprofile := contivModel.FindNetprofile(profileKey)
			if epgnetprofile == nil {
				log.Errorf("Error finding netprofile: %s", profileKey)
				return errors.New("netprofile not found")
			}

			//remove links and linksets from the old netprofile.
			modeldb.RemoveLink(&endpointGroup.Links.NetProfile, epgnetprofile)
			modeldb.RemoveLinkSet(&epgnetprofile.LinkSets.EndpointGroups, endpointGroup)

			//add links and linksets to new netprofile.
			modeldb.AddLink(&endpointGroup.Links.NetProfile, netprofile)
			modeldb.AddLinkSet(&netprofile.LinkSets.EndpointGroups, endpointGroup)

		} else if endpointGroup.NetProfile == "" {
			//add links from epg to netprofile and linksets from netprofile to epg
			modeldb.AddLink(&endpointGroup.Links.NetProfile, netprofile)
			modeldb.AddLinkSet(&netprofile.LinkSets.EndpointGroups, endpointGroup)
		}

		err = netprofile.Write()
		if err != nil {
			endpointGroupCleanup(params)
			return err
		}

		endpointGroup.NetProfile = params.NetProfile

	}

	// now look for policy removals
	for _, policyName := range endpointGroup.Policies {
		if !stringInSlice(policyName, params.Policies) {
			policyKey := GetpolicyKey(endpointGroup.TenantName, policyName)

			// find the policy
			policy := contivModel.FindPolicy(policyKey)
			if policy == nil {
				log.Errorf("Could not find policy %s", policyName)
				return core.Errorf("Policy not found")
			}

			// detach policy to epg
			err := master.PolicyDetach(endpointGroup, policy)
			if err != nil && err != master.EpgPolicyExists {
				log.Errorf("Error detaching policy %s from epg %s", policyName, endpointGroup.Key)
				return err
			}

			// Remove linksets
			modeldb.RemoveLinkSet(&policy.LinkSets.EndpointGroups, endpointGroup)
			modeldb.RemoveLinkSet(&endpointGroup.LinkSets.Policies, policy)
			err = policy.Write()
			if err != nil {
				return err
			}
		}
	}
	// Update the policy list
	endpointGroup.Policies = params.Policies

	// For the external contracts, we can keep the update simple. Remove
	// all that we have now, and update the epg with the new list.
	// Step 1: Cleanup existing external contracts.
	err := cleanupExternalContracts(endpointGroup)
	if err != nil {
		return err
	}
	// Step 2: Add contracts from the update.
	// Consumed contracts
	err = setupExternalContracts(endpointGroup, params.ExtContractsGrps)
	if err != nil {
		return err
	}

	// Update the epg itself with the new contracts groups.
	endpointGroup.ExtContractsGrps = params.ExtContractsGrps

	if endpointGroup.Links.AppProfile.ObjKey != "" {
		// if there is an associated app profiles, update that as well
		profKey := endpointGroup.Links.AppProfile.ObjKey
		profObj := contivModel.FindAppProfile(profKey)
		if profObj == nil {
			log.Warnf("EndpointGroupUpdate prof %s not found", profKey)
		} else {
			log.Infof("EndpointGroupUpdate sync prof %s", profKey)
			DeleteAppNw(profObj)
			CreateAppNw(profObj)
		}
	}

	return nil
}
870082
func (c *awsCloudImplementation) DeleteGroup(g *cloudinstances.CloudInstanceGroup) error {
	if c.spotinst != nil {
		return spotinst.DeleteGroup(c.spotinst, g)
	}

	return deleteGroup(c, g)
}
178921
func (s *NetworkOfferingService) DeleteNetworkOffering(p *DeleteNetworkOfferingParams) (*DeleteNetworkOfferingResponse, error) {
	resp, err := s.cs.newRequest("deleteNetworkOffering", p.toURLValues())
	if err != nil {
		return nil, err
	}

	var r DeleteNetworkOfferingResponse
	if err := json.Unmarshal(resp, &r); err != nil {
		return nil, err
	}

	return &r, nil
}
466090
func (d *MasterDaemon) ClearEndpoints(stateDriver core.StateDriver, epCfgs *[]core.State, id, matchField string) error {
	for _, epCfg := range *epCfgs {
		ep := epCfg.(*mastercfg.CfgEndpointState)
		if (matchField == "net" && ep.NetID == id) ||
			(matchField == "group" && ep.ServiceName == id) ||
			(matchField == "ep" && strings.Contains(ep.EndpointID, id)) {
			// Delete the endpoint state from netmaster
			_, err := master.DeleteEndpointID(stateDriver, ep.ID)
			if err != nil {
				return fmt.Errorf("Cannot cleanup EP: %s. Err: %+v", ep.EndpointID, err)
			}

			pluginAddress, err := d.getPluginAddress(ep.HomingHost)
			if err != nil {
				return err
			}

			epDelURL := "http://" + pluginAddress + ":9090/debug/reclaimEndpoint/" + ep.ID
			err = utils.HTTPDel(epDelURL)
			if err != nil {
				return fmt.Errorf("Error sending HTTP delete request to %s. Err: %+v", pluginAddress, err)
			}
		}
	}

	return nil
}
1180412
func (g *Cloud) GetNetworkEndpointGroup(name string, zone string) (*computebeta.NetworkEndpointGroup, error) {
	ctx, cancel := cloud.ContextWithCallTimeout()
	defer cancel()

	mc := newNetworkEndpointGroupMetricContext("get", zone)
	v, err := g.c.BetaNetworkEndpointGroups().Get(ctx, meta.ZonalKey(name, zone))
	return v, mc.Observe(err)
}
288838
func CleanupEndpoint() {
	// In the case of ipvlan, the ipvlan slave device is removed by removal
	// of the endpoint netns in "cleanup" of spawn_netns.sh
	if option.Config.DatapathMode == option.DatapathModeVeth {
		scopedLog := log.WithField(logfields.Veth, vethName)
		if link, err := netlink.LinkByName(vethName); err == nil {
			err = netlink.LinkDel(link)
			if err != nil {
				scopedLog.WithError(err).Warning("Couldn't delete cilium-health device")
			}
		} else {
			scopedLog.WithError(err).Debug("Didn't find existing device")
		}
	}
}
1140661
func DeleteEndpoints(hostAddr string) error {
	// Get the state driver
	stateDriver, err := utils.GetStateDriver()
	if err != nil {
		return err
	}

	readEp := &mastercfg.CfgEndpointState{}
	readEp.StateDriver = stateDriver
	epCfgs, err := readEp.ReadAll()
	if err != nil {
		return err
	}

	for _, epCfg := range epCfgs {
		ep := epCfg.(*mastercfg.CfgEndpointState)
		nwCfg := &mastercfg.CfgNetworkState{}
		nwCfg.StateDriver = stateDriver
		err = nwCfg.Read(ep.NetID)
		if err != nil {
			log.Errorf("Network not found for NetID: %+v", ep.NetID)
			continue
		}

		netID := nwCfg.NetworkName + "." + nwCfg.Tenant
		epID := getEpName(netID, &intent.ConfigEP{Container: ep.EndpointID})
		if ep.HomingHost == hostAddr {
			log.Infof("Sending DeleteEndpoint for %+v", ep)
			_, err = DeleteEndpointID(stateDriver, epID)
			if err != nil {
				log.Errorf("Error delete endpoint: %+v. Err: %+v", ep, err)
			}
			epOper := &drivers.OperEndpointState{}
			epOper.StateDriver = stateDriver
			err := epOper.Read(epID)
			if err != nil {
				log.Errorf("Failed to read epOper: %+v", epOper)
				return err
			}
			err = epOper.Clear()
			if err != nil {
				log.Errorf("Error deleting oper state for %+v", epOper)
			} else {
				log.Infof("Deleted EP oper: %+v", epOper)
			}
		} else {
			log.Infof("EP not on host: %+v", hostAddr)
		}
	}

	return err
}
937893
func (s *XDSServer) RemoveNetworkPolicy(ep logger.EndpointInfoSource) {
	s.mutex.Lock()
	defer s.mutex.Unlock()

	if ep.GetIPv6Address() != "" {
		name := ep.GetIPv6Address()
		s.networkPolicyCache.Delete(NetworkPolicyTypeURL, name, false)
		delete(s.networkPolicyEndpoints, name)
	}
	if ep.GetIPv4Address() != "" {
		name := ep.GetIPv4Address()
		s.networkPolicyCache.Delete(NetworkPolicyTypeURL, name, false)
		delete(s.networkPolicyEndpoints, name)
	}
}
556161
func (s *ServiceCache) DeleteEndpoints(k8sEndpoints *types.Endpoints) ServiceID {
	svcID := ParseEndpointsID(k8sEndpoints)

	s.mutex.Lock()
	service, serviceOK := s.services[svcID]
	delete(s.endpoints, svcID)
	endpoints, serviceReady := s.correlateEndpoints(svcID)
	s.mutex.Unlock()

	if serviceOK {
		event := ServiceEvent{
			Action:    DeleteService,
			ID:        svcID,
			Service:   service,
			Endpoints: endpoints,
		}

		if serviceReady {
			event.Action = UpdateService
		}

		s.Events <- event
	}

	return svcID
}
1030249
func (c *Client) EndpointDelete(id string) error {
	params := endpoint.NewDeleteEndpointIDParams().WithID(id).WithTimeout(api.ClientTimeout)
	_, _, err := c.Endpoint.DeleteEndpointID(params)
	return Hint(err)
}
31824
func (g *Cloud) ListNetworkEndpointGroup(zone string) ([]*computebeta.NetworkEndpointGroup, error) {
	ctx, cancel := cloud.ContextWithCallTimeout()
	defer cancel()

	mc := newNetworkEndpointGroupMetricContext("list", zone)
	negs, err := g.c.BetaNetworkEndpointGroups().List(ctx, zone, filter.None)
	return negs, mc.Observe(err)
}
669726
func DeleteNetworkID(stateDriver core.StateDriver, netID string) error {
	nwCfg := &mastercfg.CfgNetworkState{}
	nwCfg.StateDriver = stateDriver
	err := nwCfg.Read(netID)
	if err != nil {
		log.Errorf("network %s is not operational", netID)
		return err
	}

	// Will Skip docker network deletion for ACI fabric mode.
	aci, _ := IsAciConfigured()

	if nwCfg.NwType != "infra" {
		// For Infra nw, endpoint delete initiated by netplugin
		// Check if there are any active endpoints
		if hasActiveEndpoints(nwCfg) {
			return core.Errorf("Error: Network has active endpoints")
		}

		if GetClusterMode() == core.Docker && aci == false {
			// Delete the docker network
			err = docknet.DeleteDockNet(nwCfg.Tenant, nwCfg.NetworkName, "")
			if err != nil {
				log.Errorf("Error deleting network %s. Err: %v", netID, err)
				// DeleteDockNet will fail when network has active endpoints.
				// No damage is done yet. It is safe to fail.
				return err
			}
		}
	}

	gstate.GlobalMutex.Lock()
	defer gstate.GlobalMutex.Unlock()
	gCfg := &gstate.Cfg{}
	gCfg.StateDriver = stateDriver
	err = gCfg.Read("")
	if err != nil {
		log.Errorf("error reading tenant info for %q. Error: %s", nwCfg.Tenant, err)
		return err
	}

	// Free resource associated with the network
	err = freeNetworkResources(stateDriver, nwCfg, gCfg)
	if err != nil {
		// Error while freeing up vlan/vxlan/subnet/gateway resources
		// This can only happen because of defects in code
		// No need of any corrective handling here
		return err
	}

	err = nwCfg.Clear()
	if err != nil {
		log.Errorf("error writing nw config. Error: %s", err)
		return err
	}

	return err
}