Skip to content
This repository has been archived by the owner on Jan 7, 2022. It is now read-only.

Commit

Permalink
Add nwfilter binding APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
  • Loading branch information
berrange committed Jun 26, 2018
1 parent 6d4a4d7 commit 1e221cc
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 0 deletions.
52 changes: 52 additions & 0 deletions connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,20 @@ func (c *Connect) LookupNWFilterByUUID(uuid []byte) (*NWFilter, error) {
return &NWFilter{ptr: ptr}, nil
}

// See also https://libvirt.org/html/libvirt-libvirt-nwfilter.html#virNWFilterBindingLookupByPortDev
func (c *Connect) LookupNWFilterBindingByPortDev(name string) (*NWFilterBinding, error) {
if C.LIBVIR_VERSION_NUMBER < 4005000 {
return nil, GetNotImplementedError("virNWFilterBindingLookupByPortDev")
}
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
ptr := C.virNWFilterBindingLookupByPortDevCompat(c.ptr, cName)
if ptr == nil {
return nil, GetLastError()
}
return &NWFilterBinding{ptr: ptr}, nil
}

// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virStorageVolLookupByKey
func (c *Connect) LookupStorageVolByKey(key string) (*StorageVol, error) {
cKey := C.CString(key)
Expand Down Expand Up @@ -1457,6 +1471,30 @@ func (c *Connect) ListAllNWFilters(flags uint32) ([]NWFilter, error) {
return filters, nil
}

// See also https://libvirt.org/html/libvirt-libvirt-nwfilter.html#virConnectListAllNWFilterBindings
func (c *Connect) ListAllNWFilterBindings(flags uint32) ([]NWFilterBinding, error) {
var cList *C.virNWFilterBindingPtr
if C.LIBVIR_VERSION_NUMBER < 4005000 {
return []NWFilterBinding{}, GetNotImplementedError("virConnectListAllNWFilterBindings")
}
numNWFilters := C.virConnectListAllNWFilterBindingsCompat(c.ptr, (**C.virNWFilterBindingPtr)(&cList), C.uint(flags))
if numNWFilters == -1 {
return nil, GetLastError()
}
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(cList)),
Len: int(numNWFilters),
Cap: int(numNWFilters),
}
var filters []NWFilterBinding
slice := *(*[]C.virNWFilterBindingPtr)(unsafe.Pointer(&hdr))
for _, ptr := range slice {
filters = append(filters, NWFilterBinding{ptr})
}
C.free(unsafe.Pointer(cList))
return filters, nil
}

// See also https://libvirt.org/html/libvirt-libvirt-storage.html#virConnectListAllStoragePools
func (c *Connect) ListAllStoragePools(flags ConnectListAllStoragePoolsFlags) ([]StoragePool, error) {
var cList *C.virStoragePoolPtr
Expand Down Expand Up @@ -2824,3 +2862,17 @@ func (c *Connect) GetSEVInfo(flags uint32) (*NodeSEVParameters, error) {

return params, nil
}

// See also https://libvirt.org/html/libvirt-libvirt-domain.html#virNWFilterBindingCreateXML
func (c *Connect) NWFilterBindingCreateXML(xmlConfig string, flags uint32) (*NWFilterBinding, error) {
if C.LIBVIR_VERSION_NUMBER < 4005000 {
return nil, GetNotImplementedError("virNWFilterBindingCreateXML")
}
cXml := C.CString(string(xmlConfig))
defer C.free(unsafe.Pointer(cXml))
ptr := C.virNWFilterBindingCreateXMLCompat(c.ptr, cXml, C.uint(flags))
if ptr == nil {
return nil, GetLastError()
}
return &NWFilterBinding{ptr: ptr}, nil
}
33 changes: 33 additions & 0 deletions connect_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,38 @@ int virNodeGetSEVInfoCompat(virConnectPtr conn,
#endif
}
int virConnectListAllNWFilterBindingsCompat(virConnectPtr conn,
virNWFilterBindingPtr **bindings,
unsigned int flags)
{
#if LIBVIR_VERSION_NUMBER < 4005000
assert(0); // Caller should have checked version
#else
return virConnectListAllNWFilterBindings(conn, bindings, flags);
#endif
}
virNWFilterBindingPtr virNWFilterBindingCreateXMLCompat(virConnectPtr conn,
const char *xml,
unsigned int flags)
{
#if LIBVIR_VERSION_NUMBER < 4005000
assert(0); // Caller should have checked version
#else
return virNWFilterBindingCreateXML(conn, xml, flags);
#endif
}
virNWFilterBindingPtr virNWFilterBindingLookupByPortDevCompat(virConnectPtr conn,
const char *portdev)
{
#if LIBVIR_VERSION_NUMBER < 4005000
assert(0); // Caller should have checked version
#else
return virNWFilterBindingLookupByPortDev(conn, portdev);
#endif
}
*/
import "C"
15 changes: 15 additions & 0 deletions connect_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,19 @@ int virNodeGetSEVInfoCompat(virConnectPtr conn,
#define VIR_NODE_SEV_CERT_CHAIN "cert-chain"
#endif

#if LIBVIR_VERSION_NUMBER < 4005000
typedef struct _virNWFilterBinding *virNWFilterBindingPtr;
#endif

int virConnectListAllNWFilterBindingsCompat(virConnectPtr conn,
virNWFilterBindingPtr **bindings,
unsigned int flags);

virNWFilterBindingPtr virNWFilterBindingCreateXMLCompat(virConnectPtr conn,
const char *xml,
unsigned int flags);

virNWFilterBindingPtr virNWFilterBindingLookupByPortDevCompat(virConnectPtr conn,
const char *portdev);

#endif /* LIBVIRT_GO_CONNECT_COMPAT_H__ */
6 changes: 6 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,12 @@ const (

// libvirt fail to find the desired device
ERR_DEVICE_MISSING = ErrorNumber(C.VIR_ERR_DEVICE_MISSING)

// Invalid nwfilter binding object
ERR_INVALID_NWFILTER_BINDING = ErrorNumber(C.VIR_ERR_INVALID_NWFILTER_BINDING)

// Requested nwfilter binding does not exist
ERR_NO_NWFILTER_BINDING = ErrorNumber(C.VIR_ERR_NO_NWFILTER_BINDING)
)

type ErrorDomain int
Expand Down
10 changes: 10 additions & 0 deletions error_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,14 @@
#define VIR_ERR_DEVICE_MISSING 99
#endif

/* 4.5.0 */

#ifndef VIR_ERR_INVALID_NWFILTER_BINDING
#define VIR_ERR_INVALID_NWFILTER_BINDING 100
#endif

#ifndef VIR_ERR_NO_NWFILTER_BINDING
#define VIR_ERR_NO_NWFILTER_BINDING 101
#endif

#endif /* LIBVIRT_GO_ERROR_COMPAT_H__ */
121 changes: 121 additions & 0 deletions nwfilter_binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* This file is part of the libvirt-go project
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Copyright (C) 2018 Red Hat, Inc.
*
*/

package libvirt

/*
#cgo pkg-config: libvirt
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
#include <stdlib.h>
#include "nwfilter_binding_compat.h"
*/
import "C"

import (
"unsafe"
)

type NWFilterBinding struct {
ptr C.virNWFilterBindingPtr
}

// See also https://libvirt.org/html/libvirt-libvirt-nwfilter.html#virNWFilterBindingFree
func (f *NWFilterBinding) Free() error {
if C.LIBVIR_VERSION_NUMBER < 4005000 {
return GetNotImplementedError("virNWFilterBindingFree")
}
ret := C.virNWFilterBindingFreeCompat(f.ptr)
if ret == -1 {
return GetLastError()
}
return nil
}

// See also https://libvirt.org/html/libvirt-libvirt-nwfilter.html#virNWFilterBindingRef
func (c *NWFilterBinding) Ref() error {
if C.LIBVIR_VERSION_NUMBER < 4005000 {
return GetNotImplementedError("virNWFilterBindingRef")
}
ret := C.virNWFilterBindingRefCompat(c.ptr)
if ret == -1 {
return GetLastError()
}
return nil
}

// See also https://libvirt.org/html/libvirt-libvirt-nwfilter.html#virNWFilterBindingDelete
func (f *NWFilterBinding) Delete() error {
if C.LIBVIR_VERSION_NUMBER < 4005000 {
return GetNotImplementedError("virNWFilterBindingDelete")
}
result := C.virNWFilterBindingDeleteCompat(f.ptr)
if result == -1 {
return GetLastError()
}
return nil
}

// See also https://libvirt.org/html/libvirt-libvirt-nwfilter.html#virNWFilterBindingGetPortDev
func (f *NWFilterBinding) GetPortDev() (string, error) {
if C.LIBVIR_VERSION_NUMBER < 4005000 {
return "", GetNotImplementedError("virNWFilterBindingGetPortDev")
}
result := C.virNWFilterBindingGetPortDevCompat(f.ptr)
if result == nil {
return "", GetLastError()
}
name := C.GoString(result)
C.free(unsafe.Pointer(result))
return name, nil
}

// See also https://libvirt.org/html/libvirt-libvirt-nwfilter.html#virNWFilterBindingGetFilterName
func (f *NWFilterBinding) GetFilterName() (string, error) {
if C.LIBVIR_VERSION_NUMBER < 4005000 {
return "", GetNotImplementedError("virNWFilterBindingGetFilterName")
}
result := C.virNWFilterBindingGetFilterNameCompat(f.ptr)
if result == nil {
return "", GetLastError()
}
name := C.GoString(result)
C.free(unsafe.Pointer(result))
return name, nil
}

// See also https://libvirt.org/html/libvirt-libvirt-nwfilter.html#virNWFilterBindingGetXMLDesc
func (f *NWFilterBinding) GetXMLDesc(flags uint32) (string, error) {
if C.LIBVIR_VERSION_NUMBER < 4005000 {
return "", GetNotImplementedError("virNWFilterBindingGetXMLDesc")
}
result := C.virNWFilterBindingGetXMLDescCompat(f.ptr, C.uint(flags))
if result == nil {
return "", GetLastError()
}
xml := C.GoString(result)
C.free(unsafe.Pointer(result))
return xml, nil
}
96 changes: 96 additions & 0 deletions nwfilter_binding_compat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* This file is part of the libvirt-go project
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Copyright (C) 2018 Red Hat, Inc.
*
*/

package libvirt

/*
#cgo pkg-config: libvirt
#include <libvirt/libvirt.h>
#include <assert.h>
#include "nwfilter_binding_compat.h"
const char *virNWFilterBindingGetPortDevCompat(virNWFilterBindingPtr binding)
{
#if LIBVIR_VERSION_NUMBER < 4005000
assert(0); // Caller should have checked version
#else
return virNWFilterBindingGetPortDev(binding);
#endif
}
const char *virNWFilterBindingGetFilterNameCompat(virNWFilterBindingPtr binding)
{
#if LIBVIR_VERSION_NUMBER < 4005000
assert(0); // Caller should have checked version
#else
return virNWFilterBindingGetFilterName(binding);
#endif
}
char *virNWFilterBindingGetXMLDescCompat(virNWFilterBindingPtr binding,
unsigned int flags)
{
#if LIBVIR_VERSION_NUMBER < 4005000
assert(0); // Caller should have checked version
#else
return virNWFilterBindingGetXMLDesc(binding, flags);
#endif
}
int virNWFilterBindingDeleteCompat(virNWFilterBindingPtr binding)
{
#if LIBVIR_VERSION_NUMBER < 4005000
assert(0); // Caller should have checked version
#else
return virNWFilterBindingDelete(binding);
#endif
}
int virNWFilterBindingRefCompat(virNWFilterBindingPtr binding)
{
#if LIBVIR_VERSION_NUMBER < 4005000
assert(0); // Caller should have checked version
#else
return virNWFilterBindingRef(binding);
#endif
}
int virNWFilterBindingFreeCompat(virNWFilterBindingPtr binding)
{
#if LIBVIR_VERSION_NUMBER < 4005000
assert(0); // Caller should have checked version
#else
return virNWFilterBindingFree(binding);
#endif
}
*/
import "C"
Loading

0 comments on commit 1e221cc

Please sign in to comment.