]> xenbits.xensource.com Git - libvirt.git/commitdiff
list: Implement listAllSecrets
authorOsier Yang <jyang@redhat.com>
Fri, 14 Sep 2012 08:38:50 +0000 (16:38 +0800)
committerOsier Yang <jyang@redhat.com>
Mon, 17 Sep 2012 05:18:12 +0000 (13:18 +0800)
Simply returns the object list. Supports to filter the secrets
by its storage location, and whether it's private or not.

src/secret/secret_driver.c: Implement listAllSecrets

src/conf/secret_conf.h
src/secret/secret_driver.c

index 511c52df6eb2178fc5b6eae81dcce7c882549ecb..02e1bf90663cbca017ab6e13825cba5ad6170b14 100644 (file)
@@ -47,4 +47,16 @@ virSecretDefPtr virSecretDefParseString(const char *xml);
 virSecretDefPtr virSecretDefParseFile(const char *filename);
 char *virSecretDefFormat(const virSecretDefPtr def);
 
+# define VIR_CONNECT_LIST_SECRETS_FILTERS_EPHEMERAL       \
+                (VIR_CONNECT_LIST_SECRETS_EPHEMERAL     | \
+                 VIR_CONNECT_LIST_SECRETS_NO_EPHEMERAL)
+
+# define VIR_CONNECT_LIST_SECRETS_FILTERS_PRIVATE       \
+                (VIR_CONNECT_LIST_SECRETS_PRIVATE     | \
+                 VIR_CONNECT_LIST_SECRETS_NO_PRIVATE)
+
+# define VIR_CONNECT_LIST_SECRETS_FILTERS_ALL                  \
+                (VIR_CONNECT_LIST_SECRETS_FILTERS_EPHEMERAL  | \
+                 VIR_CONNECT_LIST_SECRETS_FILTERS_PRIVATE)
+
 #endif
index 7f92776af8359c40a08778954220f51b3bfe8a58..2ad66dd3e8cb801c7a81b22d3ea184695425510a 100644 (file)
@@ -601,7 +601,6 @@ cleanup:
     return -1;
 }
 
-
 static const char *
 secretUsageIDForDef(virSecretDefPtr def)
 {
@@ -620,6 +619,86 @@ secretUsageIDForDef(virSecretDefPtr def)
     }
 }
 
+#define MATCH(FLAG) (flags & (FLAG))
+static int
+secretListAllSecrets(virConnectPtr conn,
+                     virSecretPtr **secrets,
+                     unsigned int flags) {
+    virSecretDriverStatePtr driver = conn->secretPrivateData;
+    virSecretPtr *tmp_secrets = NULL;
+    int nsecrets = 0;
+    int ret_nsecrets = 0;
+    virSecretPtr secret = NULL;
+    virSecretEntryPtr entry = NULL;
+    int i = 0;
+    int ret = -1;
+
+    virCheckFlags(VIR_CONNECT_LIST_SECRETS_FILTERS_ALL, -1);
+
+    secretDriverLock(driver);
+
+    for (entry = driver->secrets; entry != NULL; entry = entry->next)
+        nsecrets++;
+
+    if (secrets) {
+        if (VIR_ALLOC_N(tmp_secrets, nsecrets + 1) < 0) {
+            virReportOOMError();
+            goto cleanup;
+        }
+    }
+
+    for (entry = driver->secrets; entry != NULL; entry = entry->next) {
+        /* filter by whether it's ephemeral */
+        if (MATCH(VIR_CONNECT_LIST_SECRETS_FILTERS_EPHEMERAL) &&
+            !((MATCH(VIR_CONNECT_LIST_SECRETS_EPHEMERAL) &&
+               entry->def->ephemeral) ||
+              (MATCH(VIR_CONNECT_LIST_SECRETS_NO_EPHEMERAL) &&
+               !entry->def->ephemeral)))
+            continue;
+
+        /* filter by whether it's private */
+        if (MATCH(VIR_CONNECT_LIST_SECRETS_FILTERS_PRIVATE) &&
+            !((MATCH(VIR_CONNECT_LIST_SECRETS_PRIVATE) &&
+               entry->def->private) ||
+              (MATCH(VIR_CONNECT_LIST_SECRETS_NO_PRIVATE) &&
+               !entry->def->private)))
+            continue;
+
+        if (secrets) {
+            if (!(secret = virGetSecret(conn,
+                                        entry->def->uuid,
+                                        entry->def->usage_type,
+                                        secretUsageIDForDef(entry->def))))
+                goto cleanup;
+            tmp_secrets[ret_nsecrets] = secret;
+        }
+        ret_nsecrets++;
+    }
+
+    if (tmp_secrets) {
+        /* trim the array to the final size */
+        ignore_value(VIR_REALLOC_N(tmp_secrets, ret_nsecrets + 1));
+        *secrets = tmp_secrets;
+        tmp_secrets = NULL;
+    }
+
+    ret = ret_nsecrets;
+
+ cleanup:
+    secretDriverUnlock(driver);
+    if (tmp_secrets) {
+        for (i = 0; i < ret_nsecrets; i ++) {
+            if (tmp_secrets[i])
+                virSecretFree(tmp_secrets[i]);
+        }
+    }
+    VIR_FREE(tmp_secrets);
+
+    return ret;
+}
+#undef MATCH
+
+
 static virSecretPtr
 secretLookupByUUID(virConnectPtr conn, const unsigned char *uuid)
 {
@@ -1072,6 +1151,7 @@ static virSecretDriver secretDriver = {
     .close = secretClose, /* 0.7.1 */
     .numOfSecrets = secretNumOfSecrets, /* 0.7.1 */
     .listSecrets = secretListSecrets, /* 0.7.1 */
+    .listAllSecrets = secretListAllSecrets, /* 0.10.2 */
     .lookupByUUID = secretLookupByUUID, /* 0.7.1 */
     .lookupByUsage = secretLookupByUsage, /* 0.7.1 */
     .defineXML = secretDefineXML, /* 0.7.1 */