secret_events.go 4.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * 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
36
#include "secret_events_wrapper.h"
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
*/
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 {
89
		return 0, makeNotImplementedError("virConnectSecretEventRegisterAny")
90 91
	}

92
	callbackPtr := unsafe.Pointer(C.secretEventLifecycleCallbackHelper)
93 94 95 96
	var csecret C.virSecretPtr
	if secret != nil {
		csecret = secret.ptr
	}
97
	var err C.virError
98
	ret := C.virConnectSecretEventRegisterAnyWrapper(c.ptr, csecret,
99 100
		C.VIR_SECRET_EVENT_ID_LIFECYCLE,
		C.virConnectSecretEventGenericCallback(callbackPtr),
101
		C.long(goCallBackId), &err)
102 103
	if ret == -1 {
		freeCallbackId(goCallBackId)
104
		return 0, makeError(&err)
105 106 107 108 109 110 111
	}
	return int(ret), nil
}

func (c *Connect) SecretEventValueChangedRegister(secret *Secret, callback SecretEventGenericCallback) (int, error) {
	goCallBackId := registerCallbackId(callback)
	if C.LIBVIR_VERSION_NUMBER < 3000000 {
112
		return 0, makeNotImplementedError("virConnectSecretEventRegisterAny")
113 114
	}

115
	callbackPtr := unsafe.Pointer(C.secretEventGenericCallbackHelper)
116 117 118 119
	var csecret C.virSecretPtr
	if secret != nil {
		csecret = secret.ptr
	}
120
	var err C.virError
121
	ret := C.virConnectSecretEventRegisterAnyWrapper(c.ptr, csecret,
122 123
		C.VIR_SECRET_EVENT_ID_VALUE_CHANGED,
		C.virConnectSecretEventGenericCallback(callbackPtr),
124
		C.long(goCallBackId), &err)
125 126
	if ret == -1 {
		freeCallbackId(goCallBackId)
127
		return 0, makeError(&err)
128 129 130 131 132 133
	}
	return int(ret), nil
}

func (c *Connect) SecretEventDeregister(callbackId int) error {
	if C.LIBVIR_VERSION_NUMBER < 3000000 {
134
		return makeNotImplementedError("virConnectSecretEventDeregisterAny")
135 136
	}
	// Deregister the callback
137 138 139 140
	var err C.virError
	ret := int(C.virConnectSecretEventDeregisterAnyWrapper(c.ptr, C.int(callbackId), &err))
	if ret < 0 {
		return makeError(&err)
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	}
	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)
}