// Removes VM from specified network by deleting a NIC
507354
func (s *VirtualMachineService) RemoveNicFromVirtualMachine(p *RemoveNicFromVirtualMachineParams) (*RemoveNicFromVirtualMachineResponse, error) {
	resp, err := s.cs.newRequest("removeNicFromVirtualMachine", p.toURLValues())
	if err != nil {
		return nil, err
	}

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

	// If we have a async client, we need to wait for the async result
	if s.cs.async {
		b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
		if err != nil {
			if err == AsyncTimeoutErr {
				return &r, err
			}
			return nil, err
		}

		b, err = getRawValue(b)
		if err != nil {
			return nil, err
		}

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

	return &r, nil
}
1287415
func (vm *VM) Destroy() error {
	vmclient, err := vm.getVMClient()
	if err != nil {
		return fmt.Errorf(errGetClient, err)
	}

	reqID, err := vmclient.DeleteDeployment(vm.ServiceName, vm.Name)
	if err != nil {
		return err
	}

	// and wait for the deletion:
	if err := client.WaitForOperation(reqID, nil); err != nil {
		return fmt.Errorf("Error waiting for instance %s to be deleted off the hosted service %s: %s",
			vm.Name, vm.Name, err)
	}

	return vm.deleteHostedService()
}
1190522
func (c *CloudAPI) DeleteFabricNetwork(vlanID int16, networkID string) error {
	vlan, err := c.getFabricWrapper(vlanID)
	if err != nil {
		return err
	}

	_, present := vlan.Networks[networkID]
	if !present {
		return fmt.Errorf("Network %s not found", networkID)
	}

	delete(vlan.Networks, networkID)
	return nil
}
668655
func (uvm *UtilityVM) RemoveNetNS(id string) (err error) {
	op := "uvm::RemoveNetNS"
	log := logrus.WithFields(logrus.Fields{
		logfields.UVMID: uvm.id,
		"netns-id":      id,
	})
	log.Debug(op + " - Begin Operation")
	defer func() {
		if err != nil {
			log.Data[logrus.ErrorKey] = err
			log.Error(op + " - End Operation - Error")
		} else {
			log.Debug(op + " - End Operation - Success")
		}
	}()

	uvm.m.Lock()
	defer uvm.m.Unlock()
	if ns, ok := uvm.namespaces[id]; ok {
		for _, ninfo := range ns.nics {
			if err := uvm.removeNIC(ninfo.ID, ninfo.Endpoint); err != nil {
				return err
			}
			ns.nics[ninfo.Endpoint.Id] = nil
		}
		// Remove the Guest Network namespace
		if uvm.isNetworkNamespaceSupported() {
			if uvm.operatingSystem == "windows" {
				hcnNamespace, err := hcn.GetNamespaceByID(id)
				if err != nil {
					return err
				}
				guestNamespace := hcsschema.ModifySettingRequest{
					GuestRequest: guestrequest.GuestRequest{
						ResourceType: guestrequest.ResourceTypeNetworkNamespace,
						RequestType:  requesttype.Remove,
						Settings:     hcnNamespace,
					},
				}
				if err := uvm.Modify(&guestNamespace); err != nil {
					return err
				}
			}
		}
		delete(uvm.namespaces, id)
	}
	return nil
}
1152360
func (daemon *Daemon) DeleteNetwork(networkID string) error {
	n, err := daemon.GetNetworkByID(networkID)
	if err != nil {
		return errors.Wrap(err, "could not find network by ID")
	}
	return daemon.deleteNetwork(n, false)
}
1360973
func (env *azureEnviron) deleteVirtualMachine(
	ctx context.ProviderCallContext,
	sdkCtx stdcontext.Context,
	instId instance.Id,
	maybeStorageClient internalazurestorage.Client,
	networkInterfaces []network.Interface,
	publicIPAddresses []network.PublicIPAddress,
) error {
	vmClient := compute.VirtualMachinesClient{env.compute}
	diskClient := compute.DisksClient{env.disk}
	nicClient := network.InterfacesClient{env.network}
	nsgClient := network.SecurityGroupsClient{env.network}
	securityRuleClient := network.SecurityRulesClient{env.network}
	pipClient := network.PublicIPAddressesClient{env.network}
	deploymentsClient := resources.DeploymentsClient{env.resources}
	vmName := string(instId)

	// TODO(axw) delete resources concurrently.

	// The VM must be deleted first, to release the lock on its resources.
	logger.Debugf("- deleting virtual machine (%s)", vmName)
	vmErrMsg := "deleting virtual machine"
	vmFuture, err := vmClient.Delete(sdkCtx, env.resourceGroup, vmName)
	if err != nil {
		if errorutils.MaybeInvalidateCredential(err, ctx) || !isNotFoundResponse(vmFuture.Response()) {
			return errors.Annotate(err, vmErrMsg)
		}
	} else {
		err = vmFuture.WaitForCompletionRef(sdkCtx, vmClient.Client)
		if err != nil {
			return errorutils.HandleCredentialError(errors.Annotate(err, vmErrMsg), ctx)
		}
		result, err := vmFuture.Result(vmClient)
		if err != nil {
			if errorutils.MaybeInvalidateCredential(err, ctx) || !isNotFoundResult(result) {
				return errors.Annotate(err, vmErrMsg)
			}
		}
	}
	if maybeStorageClient != nil {
		logger.Debugf("- deleting OS VHD (%s)", vmName)
		blobClient := maybeStorageClient.GetBlobService()
		vhdContainer := blobClient.GetContainerReference(osDiskVHDContainer)
		vhdBlob := vhdContainer.Blob(vmName)
		_, err := vhdBlob.DeleteIfExists(nil)
		return errorutils.HandleCredentialError(errors.Annotate(err, "deleting OS VHD"), ctx)
	}

	// Delete the managed OS disk.
	logger.Debugf("- deleting OS disk (%s)", vmName)
	diskErrMsg := "deleting OS disk"
	diskFuture, err := diskClient.Delete(sdkCtx, env.resourceGroup, vmName)
	if err != nil {
		if errorutils.MaybeInvalidateCredential(err, ctx) || !isNotFoundResponse(diskFuture.Response()) {
			return errors.Annotate(err, diskErrMsg)
		}
	}
	if err == nil {
		err = diskFuture.WaitForCompletionRef(sdkCtx, diskClient.Client)
		if err != nil {
			return errorutils.HandleCredentialError(errors.Annotate(err, diskErrMsg), ctx)
		}
		result, err := diskFuture.Result(diskClient)
		if err != nil {
			if errorutils.MaybeInvalidateCredential(err, ctx) || !isNotFoundResult(result) {
				return errors.Annotate(err, diskErrMsg)
			}
		}
	}
	logger.Debugf("- deleting security rules (%s)", vmName)
	if err := deleteInstanceNetworkSecurityRules(
		ctx,
		env.resourceGroup, instId,
		nsgClient, securityRuleClient,
	); err != nil {
		return errors.Annotate(err, "deleting network security rules")
	}

	logger.Debugf("- deleting network interfaces (%s)", vmName)
	networkErrMsg := "deleting NIC"
	for _, nic := range networkInterfaces {
		nicName := to.String(nic.Name)
		logger.Tracef("deleting NIC %q", nicName)
		nicFuture, err := nicClient.Delete(sdkCtx, env.resourceGroup, nicName)
		if err != nil {
			if errorutils.MaybeInvalidateCredential(err, ctx) || !isNotFoundResponse(nicFuture.Response()) {
				return errors.Annotate(err, networkErrMsg)
			}
		} else {
			err = nicFuture.WaitForCompletionRef(sdkCtx, nicClient.Client)
			if err != nil {
				return errorutils.HandleCredentialError(errors.Annotate(err, networkErrMsg), ctx)
			}
			result, err := nicFuture.Result(nicClient)
			if err != nil {
				if errorutils.MaybeInvalidateCredential(err, ctx) || !isNotFoundResult(result) {
					return errors.Annotate(err, networkErrMsg)
				}
			}
		}
	}

	logger.Debugf("- deleting public IPs (%s)", vmName)
	ipErrMsg := "deleting public IP"
	for _, pip := range publicIPAddresses {
		pipName := to.String(pip.Name)
		logger.Tracef("deleting public IP %q", pipName)
		ipFuture, err := pipClient.Delete(sdkCtx, env.resourceGroup, pipName)
		if err != nil {
			if errorutils.MaybeInvalidateCredential(err, ctx) || !isNotFoundResponse(ipFuture.Response()) {
				return errors.Annotate(err, ipErrMsg)
			}
		} else {
			err = ipFuture.WaitForCompletionRef(sdkCtx, pipClient.Client)
			if err != nil {
				return errorutils.HandleCredentialError(errors.Annotate(err, ipErrMsg), ctx)
			}
			result, err := ipFuture.Result(pipClient)
			if err != nil {
				if errorutils.MaybeInvalidateCredential(err, ctx) || !isNotFoundResult(result) {
					return errors.Annotate(err, ipErrMsg)
				}
			}
		}
	}

	// The deployment must be deleted last, or we risk leaking resources.
	logger.Debugf("- deleting deployment (%s)", vmName)
	deploymentFuture, err := deploymentsClient.Delete(sdkCtx, env.resourceGroup, vmName)
	deploymentErrMsg := "deleting deployment"
	if err != nil {
		if errorutils.MaybeInvalidateCredential(err, ctx) || !isNotFoundResponse(deploymentFuture.Response()) {
			return errors.Annotate(err, deploymentErrMsg)
		}
	} else {
		err = deploymentFuture.WaitForCompletionRef(sdkCtx, deploymentsClient.Client)
		if err != nil {
			return errorutils.HandleCredentialError(errors.Annotate(err, deploymentErrMsg), ctx)
		}
		deploymentResult, err := deploymentFuture.Result(deploymentsClient)
		if err != nil {
			if errorutils.MaybeInvalidateCredential(err, ctx) || !isNotFoundResult(deploymentResult) {
				return errors.Annotate(err, deploymentErrMsg)
			}
		}
	}
	return nil
}
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
}
965430
func (e *Engine) VirtualizationRemove(ctx context.Context, ID string, removeVolumes, force bool) error {
	return e.client.ContainerRemove(ctx, ID, dockertypes.ContainerRemoveOptions{RemoveVolumes: removeVolumes, Force: force})
}
396421
func (c *Client) DeleteFabricNetwork(vlanID int16, networkID string) error {
	req := request{
		method:         client.DELETE,
		url:            makeURL(apiFabricVLANs, strconv.Itoa(int(vlanID)), apiFabricNetworks, networkID),
		expectedStatus: http.StatusNoContent,
	}
	if _, err := c.sendRequest(req); err != nil {
		return errors.Newf(err, "failed to delete fabric network %s on vlan %d", networkID, vlanID)
	}
	return nil
}
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
}
844596
func (c *Client) RemoveNetwork(id string) error {
	resp, err := c.do("DELETE", "/networks/"+id, doOptions{})
	if err != nil {
		if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
			return &NoSuchNetwork{ID: id}
		}
		return err
	}
	resp.Body.Close()
	return nil
}
375006
func deleteVM(c context.Context, id, hostname string) error {
	vm := &model.VM{
		ID: id,
	}
	return datastore.RunInTransaction(c, func(c context.Context) error {
		switch err := datastore.Get(c, vm); {
		case err == datastore.ErrNoSuchEntity:
			return nil
		case err != nil:
			return errors.Annotate(err, "failed to fetch VM").Err()
		case vm.Hostname != hostname:
			logging.Debugf(c, "VM %q does not exist", hostname)
			return nil
		}
		if err := datastore.Delete(c, vm); err != nil {
			return errors.Annotate(err, "failed to delete VM").Err()
		}
		return nil
	}, nil)
}
1269664
func (d *VppDriver) DeleteNetwork(id, subnet, nwType, encap string, pktTag, extPktTag int, gateway string, tenant string) error {
	log.Infof("Not implemented")
	return nil
}
603755
func (c *Cluster) RemoveNetwork(input string) error {
	return c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
		network, err := getNetwork(ctx, state.controlClient, input)
		if err != nil {
			return err
		}

		_, err = state.controlClient.RemoveNetwork(ctx, &swarmapi.RemoveNetworkRequest{NetworkID: network.ID})
		return err
	})
}
1363158
func (network *HostComputeNetwork) RemovePolicy(networkPolicy PolicyNetworkRequest) error {
	logrus.Debugf("hcn::HostComputeNetwork::RemovePolicy id=%s", network.Id)

	settingsJson, err := json.Marshal(networkPolicy)
	if err != nil {
		return err
	}
	requestMessage := &ModifyNetworkSettingRequest{
		ResourceType: NetworkResourceTypePolicy,
		RequestType:  RequestTypeRemove,
		Settings:     settingsJson,
	}

	return network.ModifyNetworkSettings(requestMessage)
}
1118890
func jsonnet_destroy(vmRef *C.struct_JsonnetVm) {
	VMs[vmRef.id-1] = nil
	freedIDs = append(freedIDs, uint32(vmRef.id))
	C.jsonnet_internal_free_vm(vmRef)
}
1229151
func (sw *OvsSwitch) DeleteVtep(vtepIP string) error {
	// Build vtep interface name
	intfName := vxlanIfName(vtepIP)

	log.Infof("Deleting VTEP intf %s for IP %s", intfName, vtepIP)

	// Get the openflow port number for the interface
	ofpPort, err := sw.ovsdbDriver.GetOfpPortNo(intfName)
	if err != nil {
		log.Errorf("Could not find the OVS port %s. Err: %v", intfName, err)
		return err
	}

	// Add info about VTEP port to ofnet
	if sw.ofnetAgent != nil {
		err = sw.ofnetAgent.RemoveVtepPort(ofpPort, net.ParseIP(vtepIP))
		if err != nil {
			log.Errorf("Error deleting VTEP port %s to ofnet. Err: %v", intfName, err)
			return err
		}
	}

	// ask ovsdb to delete the VTEP
	return sw.ovsdbDriver.DeleteVtep(intfName)
}