[libvirt] [PATCH perl 2/2] Add support for secret event APIs

Daniel P. Berrange berrange at redhat.com
Mon Jan 9 17:58:54 UTC 2017


Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
---
 Changes                |   1 +
 Virt.xs                | 140 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/Sys/Virt.pm        |  36 +++++++++++++
 lib/Sys/Virt/Secret.pm |  33 ++++++++++++
 t/030-api-coverage.t   |   3 ++
 5 files changed, 213 insertions(+)

diff --git a/Changes b/Changes
index e425ad4..efa1058 100644
--- a/Changes
+++ b/Changes
@@ -11,6 +11,7 @@ Revision history for perl module Sys::Virt
  - Add PERF_PARAM_REF_CPU_CYCLES constant
  - Add virStorageVolGetInfoFlags & associated constants
  - Add domain metdata change event
+ - Add secret event APIs
 
 2.5.0 2016-12-05
 
diff --git a/Virt.xs b/Virt.xs
index 9a7ce17..a2947b6 100644
--- a/Virt.xs
+++ b/Virt.xs
@@ -1363,6 +1363,82 @@ _node_device_event_lifecycle_callback(virConnectPtr con,
 }
 
 
+static int
+_secret_event_lifecycle_callback(virConnectPtr con,
+				 virSecretPtr secret,
+				 int event,
+				 int detail,
+				 void *opaque)
+{
+    AV *data = opaque;
+    SV **self;
+    SV **cb;
+    SV *secretref;
+    dSP;
+
+    self = av_fetch(data, 0, 0);
+    cb = av_fetch(data, 1, 0);
+
+    SvREFCNT_inc(*self);
+
+    ENTER;
+    SAVETMPS;
+
+    PUSHMARK(SP);
+    XPUSHs(*self);
+    secretref = sv_newmortal();
+    sv_setref_pv(secretref, "Sys::Virt::Secret", (void*)secret);
+    virSecretRef(secret);
+    XPUSHs(secretref);
+    XPUSHs(sv_2mortal(newSViv(event)));
+    XPUSHs(sv_2mortal(newSViv(detail)));
+    PUTBACK;
+
+    call_sv(*cb, G_DISCARD);
+
+    FREETMPS;
+    LEAVE;
+
+    return 0;
+}
+
+
+static int
+_secret_event_generic_callback(virConnectPtr con,
+				virSecretPtr secret,
+				void *opaque)
+{
+    AV *data = opaque;
+    SV **self;
+    SV **cb;
+    SV *secretref;
+    dSP;
+
+    self = av_fetch(data, 0, 0);
+    cb = av_fetch(data, 1, 0);
+
+    SvREFCNT_inc(*self);
+
+    ENTER;
+    SAVETMPS;
+
+    PUSHMARK(SP);
+    XPUSHs(*self);
+    secretref = sv_newmortal();
+    sv_setref_pv(secretref, "Sys::Virt::Secret", (void*)secret);
+    virSecretRef(secret);
+    XPUSHs(secretref);
+    PUTBACK;
+
+    call_sv(*cb, G_DISCARD);
+
+    FREETMPS;
+    LEAVE;
+
+    return 0;
+}
+
+
 static void
 _domain_event_free(void *opaque)
 {
@@ -1396,6 +1472,14 @@ _node_device_event_free(void *opaque)
 
 
 static void
+_secret_event_free(void *opaque)
+{
+  SV *sv = opaque;
+  SvREFCNT_dec(sv);
+}
+
+
+static void
 _close_callback(virConnectPtr con,
                 int reason,
                 void *opaque)
@@ -3462,6 +3546,56 @@ node_device_event_deregister_any(con, callbackID)
       virConnectNodeDeviceEventDeregisterAny(con, callbackID);
 
 
+int
+secret_event_register_any(conref, secretref, eventID, cb)
+      SV* conref;
+      SV* secretref;
+      int eventID;
+      SV* cb;
+PREINIT:
+      AV *opaque;
+      virConnectPtr con;
+      virSecretPtr secret;
+      virConnectSecretEventGenericCallback callback;
+    CODE:
+      con = (virConnectPtr)SvIV((SV*)SvRV(conref));
+      if (SvROK(secretref)) {
+          secret = (virSecretPtr)SvIV((SV*)SvRV(secretref));
+      } else {
+          secret = NULL;
+      }
+
+      switch (eventID) {
+      case VIR_SECRET_EVENT_ID_LIFECYCLE:
+          callback = VIR_SECRET_EVENT_CALLBACK(_secret_event_lifecycle_callback);
+          break;
+      case VIR_SECRET_EVENT_ID_VALUE_CHANGED:
+          callback = VIR_SECRET_EVENT_CALLBACK(_secret_event_generic_callback);
+          break;
+      default:
+          callback = VIR_SECRET_EVENT_CALLBACK(_secret_event_generic_callback);
+          break;
+      }
+
+      opaque = newAV();
+      SvREFCNT_inc(cb);
+      SvREFCNT_inc(conref);
+      av_push(opaque, conref);
+      av_push(opaque, cb);
+      if ((RETVAL = virConnectSecretEventRegisterAny(con, secret, eventID, callback, opaque, _secret_event_free)) < 0)
+          _croak_error();
+OUTPUT:
+      RETVAL
+
+
+void
+secret_event_deregister_any(con, callbackID)
+      virConnectPtr con;
+      int callbackID;
+ PPCODE:
+      virConnectSecretEventDeregisterAny(con, callbackID);
+
+
 void
 register_close_callback(conref, cb)
       SV* conref;
@@ -8767,6 +8901,12 @@ BOOT:
       REGISTER_CONSTANT(VIR_CONNECT_LIST_SECRETS_PRIVATE, LIST_PRIVATE);
       REGISTER_CONSTANT(VIR_CONNECT_LIST_SECRETS_NO_PRIVATE, LIST_NO_PRIVATE);
 
+      REGISTER_CONSTANT(VIR_SECRET_EVENT_ID_LIFECYCLE, EVENT_ID_LIFECYCLE);
+      REGISTER_CONSTANT(VIR_SECRET_EVENT_ID_VALUE_CHANGED, EVENT_ID_VALUE_CHANGED);
+
+      REGISTER_CONSTANT(VIR_SECRET_EVENT_DEFINED, EVENT_DEFINED);
+      REGISTER_CONSTANT(VIR_SECRET_EVENT_UNDEFINED, EVENT_UNDEFINED);
+
 
       stash = gv_stashpv( "Sys::Virt::Stream", TRUE );
       REGISTER_CONSTANT(VIR_STREAM_NONBLOCK, NONBLOCK);
diff --git a/lib/Sys/Virt.pm b/lib/Sys/Virt.pm
index f2975a7..f1a6e16 100644
--- a/lib/Sys/Virt.pm
+++ b/lib/Sys/Virt.pm
@@ -1648,6 +1648,42 @@ unregistering the event.
 Unregister a callback, associated with the C<$callbackID> previously
 obtained from C<node_device_event_register_any>.
 
+=item $callback = $conn->secret_event_register_any($secret, $eventID, $callback)
+
+Register a callback to received notifications of secret events.
+The C<$secret> parameter can be C<undef> to request events on all
+known secrets, or a specific C<Sys::Virt::Secret> object to
+filter events. The C<$eventID> parameter is one of the EVENT ID
+constants described later in this document. The C<$callback> is
+a subroutine reference that will receive the events.
+
+All callbacks receive a C<Sys::Virt> connection as the first parameter
+and a C<Sys::Virt::Secret> object indicating the secret on which the
+event occurred as the second parameter. Subsequent parameters vary
+according to the event type
+
+=over
+
+=item EVENT_ID_LIFECYCLE
+
+Extra C<event> and C<detail> parameters defining the lifecycle
+transition that occurred.
+ 
+=item EVENT_ID_VALUE_CHANGED
+
+No extra parameters.
+ 
+=back
+
+The return value is a unique callback ID that must be used when
+unregistering the event.
+
+
+=item $conn->secret_event_deregister_any($callbackID)
+
+Unregister a callback, associated with the C<$callbackID> previously
+obtained from C<secret_event_register_any>.
+
 =item $conn->register_close_callback($coderef);
 
 Register a callback to be invoked when the connection is closed.
diff --git a/lib/Sys/Virt/Secret.pm b/lib/Sys/Virt/Secret.pm
index 1f2defc..0c6a021 100644
--- a/lib/Sys/Virt/Secret.pm
+++ b/lib/Sys/Virt/Secret.pm
@@ -182,6 +182,39 @@ Include any secrets not marked as private
 
 =back
 
+=head2 EVENT ID CONSTANTS
+
+=over 4
+
+=item Sys::Virt::Secret::EVENT_ID_LIFECYCLE
+
+Secret lifecycle events
+
+=item Sys::Virt::Secret::EVENT_ID_VALUE_CHANGED
+
+Secret value change events
+
+=back
+
+=head2 LIFECYCLE CHANGE EVENTS
+
+The following constants allow secret lifecycle change events to be
+interpreted. The events contain both a state change, and a
+reason though the reason is currently unsed.
+
+=over 4
+
+=item Sys::Virt::Secret::EVENT_DEFINED
+
+Indicates that a persistent configuration has been defined for
+the secret.
+
+=item Sys::Virt::Secret::EVENT_UNDEFINED
+
+The persistent configuration has gone away
+
+=back
+
 =cut
 
 
diff --git a/t/030-api-coverage.t b/t/030-api-coverage.t
index 7bc33fb..3a0d0c5 100644
--- a/t/030-api-coverage.t
+++ b/t/030-api-coverage.t
@@ -102,6 +102,8 @@ virConnectStoragePoolEventLifecycleCallback
 
 virConnectNodeDeviceEventLifecycleCallback
 
+virConnectSecretEventLifecycleCallback
+
 virEventAddHandleFunc
 virEventAddTimeoutFunc
 virEventRemoveHandleFunc
@@ -180,6 +182,7 @@ VIR_DOMAIN_EVENT_CALLBACK
 VIR_NETWORK_EVENT_CALLBACK
 VIR_STORAGE_POOL_EVENT_CALLBACK
 VIR_NODE_DEVICE_EVENT_CALLBACK
+VIR_SECRET_EVENT_CALLBACK
 VIR_DOMAIN_MEMORY_FIELD_LENGTH
 VIR_DOMAIN_MEMORY_PARAM_UNLIMITED
 VIR_DOMAIN_SCHED_FIELD_LENGTH
-- 
2.9.3




More information about the libvir-list mailing list