src: Add DMA localagent
[barometer.git] / src / dma / vendor / github.com / libvirt / libvirt-go / secret_events.go
1 /*
2  * This file is part of the libvirt-go project
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  *
22  * Copyright (c) 2013 Alex Zorin
23  * Copyright (C) 2016 Red Hat, Inc.
24  *
25  */
26
27 package libvirt
28
29 import (
30         "fmt"
31         "unsafe"
32 )
33
34 /*
35 #cgo pkg-config: libvirt
36 #include "secret_events_wrapper.h"
37 */
38 import "C"
39
40 type SecretEventLifecycle struct {
41         Event SecretEventLifecycleType
42         // TODO: we can make Detail typesafe somehow ?
43         Detail int
44 }
45
46 type SecretEventLifecycleCallback func(c *Connect, n *Secret, event *SecretEventLifecycle)
47
48 type SecretEventGenericCallback func(c *Connect, n *Secret)
49
50 //export secretEventLifecycleCallback
51 func secretEventLifecycleCallback(c C.virConnectPtr, n C.virSecretPtr,
52         event int, detail int,
53         goCallbackId int) {
54
55         secret := &Secret{ptr: n}
56         connection := &Connect{ptr: c}
57
58         eventDetails := &SecretEventLifecycle{
59                 Event:  SecretEventLifecycleType(event),
60                 Detail: detail,
61         }
62
63         callbackFunc := getCallbackId(goCallbackId)
64         callback, ok := callbackFunc.(SecretEventLifecycleCallback)
65         if !ok {
66                 panic("Inappropriate callback type called")
67         }
68         callback(connection, secret, eventDetails)
69 }
70
71 //export secretEventGenericCallback
72 func secretEventGenericCallback(c C.virConnectPtr, n C.virSecretPtr,
73         goCallbackId int) {
74
75         secret := &Secret{ptr: n}
76         connection := &Connect{ptr: c}
77
78         callbackFunc := getCallbackId(goCallbackId)
79         callback, ok := callbackFunc.(SecretEventGenericCallback)
80         if !ok {
81                 panic("Inappropriate callback type called")
82         }
83         callback(connection, secret)
84 }
85
86 func (c *Connect) SecretEventLifecycleRegister(secret *Secret, callback SecretEventLifecycleCallback) (int, error) {
87         goCallBackId := registerCallbackId(callback)
88         if C.LIBVIR_VERSION_NUMBER < 3000000 {
89                 return 0, makeNotImplementedError("virConnectSecretEventRegisterAny")
90         }
91
92         callbackPtr := unsafe.Pointer(C.secretEventLifecycleCallbackHelper)
93         var csecret C.virSecretPtr
94         if secret != nil {
95                 csecret = secret.ptr
96         }
97         var err C.virError
98         ret := C.virConnectSecretEventRegisterAnyWrapper(c.ptr, csecret,
99                 C.VIR_SECRET_EVENT_ID_LIFECYCLE,
100                 C.virConnectSecretEventGenericCallback(callbackPtr),
101                 C.long(goCallBackId), &err)
102         if ret == -1 {
103                 freeCallbackId(goCallBackId)
104                 return 0, makeError(&err)
105         }
106         return int(ret), nil
107 }
108
109 func (c *Connect) SecretEventValueChangedRegister(secret *Secret, callback SecretEventGenericCallback) (int, error) {
110         goCallBackId := registerCallbackId(callback)
111         if C.LIBVIR_VERSION_NUMBER < 3000000 {
112                 return 0, makeNotImplementedError("virConnectSecretEventRegisterAny")
113         }
114
115         callbackPtr := unsafe.Pointer(C.secretEventGenericCallbackHelper)
116         var csecret C.virSecretPtr
117         if secret != nil {
118                 csecret = secret.ptr
119         }
120         var err C.virError
121         ret := C.virConnectSecretEventRegisterAnyWrapper(c.ptr, csecret,
122                 C.VIR_SECRET_EVENT_ID_VALUE_CHANGED,
123                 C.virConnectSecretEventGenericCallback(callbackPtr),
124                 C.long(goCallBackId), &err)
125         if ret == -1 {
126                 freeCallbackId(goCallBackId)
127                 return 0, makeError(&err)
128         }
129         return int(ret), nil
130 }
131
132 func (c *Connect) SecretEventDeregister(callbackId int) error {
133         if C.LIBVIR_VERSION_NUMBER < 3000000 {
134                 return makeNotImplementedError("virConnectSecretEventDeregisterAny")
135         }
136         // Deregister the callback
137         var err C.virError
138         ret := int(C.virConnectSecretEventDeregisterAnyWrapper(c.ptr, C.int(callbackId), &err))
139         if ret < 0 {
140                 return makeError(&err)
141         }
142         return nil
143 }
144
145 func (e SecretEventLifecycle) String() string {
146         var event string
147         switch e.Event {
148         case SECRET_EVENT_DEFINED:
149                 event = "defined"
150
151         case SECRET_EVENT_UNDEFINED:
152                 event = "undefined"
153
154         default:
155                 event = "unknown"
156         }
157
158         return fmt.Sprintf("Secret event=%q", event)
159 }