[PATCH go 3/3] Add binding for secret event APIs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
---
 api_test.go             |   5 ++
 secret.go               |  14 +++++
 secret_compat.go        |  47 +++++++++++++++
 secret_compat.h         |  30 +++++++++
 secret_events.go        | 157 ++++++++++++++++++++++++++++++++++++++++++++++++
 secret_events_cfuncs.go |  65 ++++++++++++++++++++
 secret_events_cfuncs.h  |  40 ++++++++++++
 7 files changed, 358 insertions(+)
 create mode 100644 secret_compat.go
 create mode 100644 secret_events.go
 create mode 100644 secret_events_cfuncs.go
 create mode 100644 secret_events_cfuncs.h

diff --git a/api_test.go b/api_test.go
index fafd35e..8c5cffb 100644
--- a/api_test.go
+++ b/api_test.go
@@ -117,6 +117,10 @@ var (
 		"virConnectNodeDeviceEventGenericCallback",
 		"virConnectNodeDeviceEventLifecycleCallback",
 
+		/* Secret event callback typedefs */
+		"virConnectSecretEventGenericCallback",
+		"virConnectSecretEventLifecycleCallback",
+
 		/* Storage pool event callback typedefs */
 		"virConnectStoragePoolEventGenericCallback",
 		"virConnectStoragePoolEventLifecycleCallback",
@@ -214,6 +218,7 @@ var (
 		"VIR_GET_CPUMAP",
 		"VIR_NETWORK_EVENT_CALLBACK",
 		"VIR_NODE_DEVICE_EVENT_CALLBACK",
+		"VIR_SECRET_EVENT_CALLBACK",
 		"VIR_SECURITY_DOI_BUFLEN",
 		"VIR_SECURITY_LABEL_BUFLEN",
 		"VIR_SECURITY_MODEL_BUFLEN",
diff --git a/secret.go b/secret.go
index 3245f28..5cf6e13 100644
--- a/secret.go
+++ b/secret.go
@@ -49,6 +49,20 @@ const (
 	SECRET_USAGE_TYPE_TLS    = SecretUsageType(C.VIR_SECRET_USAGE_TYPE_TLS)
 )
 
+type SecretEventLifecycleType int
+
+const (
+	SECRET_EVENT_DEFINED   = SecretEventLifecycleType(C.VIR_SECRET_EVENT_DEFINED)
+	SECRET_EVENT_UNDEFINED = SecretEventLifecycleType(C.VIR_SECRET_EVENT_UNDEFINED)
+)
+
+type SecretEventID int
+
+const (
+	SECRET_EVENT_ID_LIFECYCLE     = SecretEventID(C.VIR_SECRET_EVENT_ID_LIFECYCLE)
+	SECRET_EVENT_ID_VALUE_CHANGED = SecretEventID(C.VIR_SECRET_EVENT_ID_VALUE_CHANGED)
+)
+
 type Secret struct {
 	ptr C.virSecretPtr
 }
diff --git a/secret_compat.go b/secret_compat.go
new file mode 100644
index 0000000..88cd3c1
--- /dev/null
+++ b/secret_compat.go
@@ -0,0 +1,47 @@
+/*
+ * 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) 2013 Alex Zorin
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ */
+
+package libvirt
+
+/*
+#cgo pkg-config: libvirt
+#include <libvirt/libvirt.h>
+#include <assert.h>
+#include "secret_compat.h"
+
+int virConnectSecretEventDeregisterAnyCompat(virConnectPtr conn,
+					      int callbackID)
+{
+#if LIBVIR_VERSION_NUMBER < 1002001
+    assert(0); // Caller should have checked version
+#else
+    return virConnectSecretEventDeregisterAny(conn, callbackID);
+#endif
+}
+
+
+*/
+import "C"
diff --git a/secret_compat.h b/secret_compat.h
index 41cc2c8..745e672 100644
--- a/secret_compat.h
+++ b/secret_compat.h
@@ -27,6 +27,36 @@
 #ifndef LIBVIRT_GO_SECRET_COMPAT_H__
 #define LIBVIRT_GO_SECRET_COMPAT_H__
 
+/* 3.0.0 */
+
+#ifndef VIR_SECRET_EVENT_DEFINED
+#define VIR_SECRET_EVENT_DEFINED 0
+#endif
+
+#ifndef VIR_SECRET_EVENT_UNDEFINED
+#define VIR_SECRET_EVENT_UNDEFINED 1
+#endif
+
+#ifndef VIR_SECRET_EVENT_ID_LIFECYCLE
+#define VIR_SECRET_EVENT_ID_LIFECYCLE 0
+#endif
+
+#ifndef VIR_SECRET_EVENT_ID_VALUE_CHANGED
+#define VIR_SECRET_EVENT_ID_VALUE_CHANGED 1
+#endif
+
+
+#if LIBVIR_VERSION_NUMBER < 3000000
+typedef void (*virConnectSecretEventGenericCallback)(virConnectPtr conn,
+						     virSecretPtr secret,
+						     void *opaque);
+#endif
+
+int virConnectSecretEventDeregisterAnyCompat(virConnectPtr conn,
+					     int callbackID);
+
+/* 2.2.1 */
+
 #ifndef VIR_SECRET_USAGE_TYPE_TLS
 #define VIR_SECRET_USAGE_TYPE_TLS 4
 #endif
diff --git a/secret_events.go b/secret_events.go
new file mode 100644
index 0000000..5d4d299
--- /dev/null
+++ b/secret_events.go
@@ -0,0 +1,157 @@
+/*
+ * 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) 2013 Alex Zorin
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ */
+
+package libvirt
+
+import (
+	"fmt"
+	"unsafe"
+)
+
+/*
+#cgo pkg-config: libvirt
+#include <libvirt/libvirt.h>
+#include "secret_compat.h"
+#include "secret_events_cfuncs.h"
+*/
+import "C"
+
+type SecretEventLifecycle struct {
+	Event SecretEventLifecycleType
+	// TODO: we can make Detail typesafe somehow ?
+	Detail int
+}
+
+type SecretEventLifecycleCallback func(c *Connect, n *Secret, event *SecretEventLifecycle)
+
+type SecretEventGenericCallback func(c *Connect, n *Secret)
+
+//export secretEventLifecycleCallback
+func secretEventLifecycleCallback(c C.virConnectPtr, n C.virSecretPtr,
+	event int, detail int,
+	goCallbackId int) {
+
+	secret := &Secret{ptr: n}
+	connection := &Connect{ptr: c}
+
+	eventDetails := &SecretEventLifecycle{
+		Event:  SecretEventLifecycleType(event),
+		Detail: detail,
+	}
+
+	callbackFunc := getCallbackId(goCallbackId)
+	callback, ok := callbackFunc.(SecretEventLifecycleCallback)
+	if !ok {
+		panic("Inappropriate callback type called")
+	}
+	callback(connection, secret, eventDetails)
+}
+
+//export secretEventGenericCallback
+func secretEventGenericCallback(c C.virConnectPtr, n C.virSecretPtr,
+	goCallbackId int) {
+
+	secret := &Secret{ptr: n}
+	connection := &Connect{ptr: c}
+
+	callbackFunc := getCallbackId(goCallbackId)
+	callback, ok := callbackFunc.(SecretEventGenericCallback)
+	if !ok {
+		panic("Inappropriate callback type called")
+	}
+	callback(connection, secret)
+}
+
+func (c *Connect) SecretEventLifecycleRegister(secret *Secret, callback SecretEventLifecycleCallback) (int, error) {
+	goCallBackId := registerCallbackId(callback)
+	if C.LIBVIR_VERSION_NUMBER < 3000000 {
+		return 0, GetNotImplementedError()
+	}
+
+	callbackPtr := unsafe.Pointer(C.secretEventLifecycleCallback_cgo)
+	var csecret C.virSecretPtr
+	if secret != nil {
+		csecret = secret.ptr
+	}
+	ret := C.virConnectSecretEventRegisterAny_cgo(c.ptr, csecret,
+		C.VIR_SECRET_EVENT_ID_LIFECYCLE,
+		C.virConnectSecretEventGenericCallback(callbackPtr),
+		C.long(goCallBackId))
+	if ret == -1 {
+		freeCallbackId(goCallBackId)
+		return 0, GetLastError()
+	}
+	return int(ret), nil
+}
+
+func (c *Connect) SecretEventValueChangedRegister(secret *Secret, callback SecretEventGenericCallback) (int, error) {
+	goCallBackId := registerCallbackId(callback)
+	if C.LIBVIR_VERSION_NUMBER < 3000000 {
+		return 0, GetNotImplementedError()
+	}
+
+	callbackPtr := unsafe.Pointer(C.secretEventGenericCallback_cgo)
+	var csecret C.virSecretPtr
+	if secret != nil {
+		csecret = secret.ptr
+	}
+	ret := C.virConnectSecretEventRegisterAny_cgo(c.ptr, csecret,
+		C.VIR_SECRET_EVENT_ID_VALUE_CHANGED,
+		C.virConnectSecretEventGenericCallback(callbackPtr),
+		C.long(goCallBackId))
+	if ret == -1 {
+		freeCallbackId(goCallBackId)
+		return 0, GetLastError()
+	}
+	return int(ret), nil
+}
+
+func (c *Connect) SecretEventDeregister(callbackId int) error {
+	if C.LIBVIR_VERSION_NUMBER < 3000000 {
+		return GetNotImplementedError()
+	}
+	// Deregister the callback
+	if i := int(C.virConnectSecretEventDeregisterAnyCompat(c.ptr, C.int(callbackId))); i != 0 {
+		return GetLastError()
+	}
+	return nil
+}
+
+func (e SecretEventLifecycle) String() string {
+	var event string
+	switch e.Event {
+	case SECRET_EVENT_DEFINED:
+		event = "defined"
+
+	case SECRET_EVENT_UNDEFINED:
+		event = "undefined"
+
+	default:
+		event = "unknown"
+	}
+
+	return fmt.Sprintf("Secret event=%q", event)
+}
diff --git a/secret_events_cfuncs.go b/secret_events_cfuncs.go
new file mode 100644
index 0000000..158008b
--- /dev/null
+++ b/secret_events_cfuncs.go
@@ -0,0 +1,65 @@
+/*
+ * 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) 2013 Alex Zorin
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ */
+
+package libvirt
+
+/*
+#cgo pkg-config: libvirt
+#include <libvirt/libvirt.h>
+#include <libvirt/virterror.h>
+#include <assert.h>
+#include "secret_compat.h"
+#include "secret_events_cfuncs.h"
+#include "callbacks_cfuncs.h"
+#include <stdint.h>
+
+extern void secretEventLifecycleCallback(virConnectPtr, virSecretPtr, int, int, int);
+void secretEventLifecycleCallback_cgo(virConnectPtr c, virSecretPtr d,
+                                     int event, int detail, void *data)
+{
+    secretEventLifecycleCallback(c, d, event, detail, (int)(intptr_t)data);
+}
+
+extern void secretEventGenericCallback(virConnectPtr, virSecretPtr, int);
+void secretEventGenericCallback_cgo(virConnectPtr c, virSecretPtr d,
+                                    void *data)
+{
+    secretEventGenericCallback(c, d, (int)(intptr_t)data);
+}
+
+int virConnectSecretEventRegisterAny_cgo(virConnectPtr c,  virSecretPtr d,
+                                         int eventID, virConnectSecretEventGenericCallback cb,
+                                         long goCallbackId) {
+    void* id = (void*)goCallbackId;
+#if LIBVIR_VERSION_NUMBER < 3000000
+    assert(0); // Caller should have checked version
+#else
+    return virConnectSecretEventRegisterAny(c, d, eventID, cb, id, freeGoCallback_cgo);
+#endif
+}
+
+*/
+import "C"
diff --git a/secret_events_cfuncs.h b/secret_events_cfuncs.h
new file mode 100644
index 0000000..96401d5
--- /dev/null
+++ b/secret_events_cfuncs.h
@@ -0,0 +1,40 @@
+/*
+ * 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) 2013 Alex Zorin
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ */
+
+#ifndef LIBVIRT_GO_SECRET_EVENTS_CFUNCS_H__
+#define LIBVIRT_GO_SECRET_EVENTS_CFUNCS_H__
+
+void secretEventLifecycleCallback_cgo(virConnectPtr c, virSecretPtr d,
+				      int event, int detail, void* data);
+void secretEventGenericCallback_cgo(virConnectPtr c, virSecretPtr d,
+				    void* data);
+
+int virConnectSecretEventRegisterAny_cgo(virConnectPtr c,  virSecretPtr d,
+                                         int eventID, virConnectSecretEventGenericCallback cb,
+                                         long goCallbackId);
+
+
+#endif /* LIBVIRT_GO_SECRET_EVENTS_CFUNCS_H__ */
-- 
2.9.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]
  Powered by Linux