You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

140 lines
3.6 KiB

  1. /*
  2. * virsh-completer-network.c: virsh completer callbacks related to networks
  3. *
  4. * Copyright (C) 2019 Red Hat, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include "virsh-completer-network.h"
  22. #include "viralloc.h"
  23. #include "virsh-network.h"
  24. #include "virsh.h"
  25. #include "virstring.h"
  26. char **
  27. virshNetworkNameCompleter(vshControl *ctl,
  28. const vshCmd *cmd G_GNUC_UNUSED,
  29. unsigned int flags)
  30. {
  31. virshControlPtr priv = ctl->privData;
  32. virNetworkPtr *nets = NULL;
  33. int nnets = 0;
  34. size_t i = 0;
  35. char **ret = NULL;
  36. VIR_AUTOSTRINGLIST tmp = NULL;
  37. virCheckFlags(VIR_CONNECT_LIST_NETWORKS_INACTIVE |
  38. VIR_CONNECT_LIST_NETWORKS_ACTIVE |
  39. VIR_CONNECT_LIST_NETWORKS_PERSISTENT,
  40. NULL);
  41. if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
  42. return NULL;
  43. if ((nnets = virConnectListAllNetworks(priv->conn, &nets, flags)) < 0)
  44. return NULL;
  45. if (VIR_ALLOC_N(tmp, nnets + 1) < 0)
  46. goto cleanup;
  47. for (i = 0; i < nnets; i++) {
  48. const char *name = virNetworkGetName(nets[i]);
  49. tmp[i] = g_strdup(name);
  50. }
  51. ret = g_steal_pointer(&tmp);
  52. cleanup:
  53. for (i = 0; i < nnets; i++)
  54. virNetworkFree(nets[i]);
  55. VIR_FREE(nets);
  56. return ret;
  57. }
  58. char **
  59. virshNetworkEventNameCompleter(vshControl *ctl G_GNUC_UNUSED,
  60. const vshCmd *cmd G_GNUC_UNUSED,
  61. unsigned int flags)
  62. {
  63. size_t i = 0;
  64. VIR_AUTOSTRINGLIST tmp = NULL;
  65. virCheckFlags(0, NULL);
  66. if (VIR_ALLOC_N(tmp, VIR_NETWORK_EVENT_ID_LAST + 1) < 0)
  67. return NULL;
  68. for (i = 0; i < VIR_NETWORK_EVENT_ID_LAST; i++)
  69. tmp[i] = g_strdup(virshNetworkEventCallbacks[i].name);
  70. return g_steal_pointer(&tmp);
  71. }
  72. char **
  73. virshNetworkPortUUIDCompleter(vshControl *ctl,
  74. const vshCmd *cmd G_GNUC_UNUSED,
  75. unsigned int flags)
  76. {
  77. virshControlPtr priv = ctl->privData;
  78. virNetworkPtr net = NULL;
  79. virNetworkPortPtr *ports = NULL;
  80. int nports = 0;
  81. size_t i = 0;
  82. char **ret = NULL;
  83. virCheckFlags(0, NULL);
  84. if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
  85. return NULL;
  86. if (!(net = virshCommandOptNetwork(ctl, cmd, NULL)))
  87. return false;
  88. if ((nports = virNetworkListAllPorts(net, &ports, flags)) < 0)
  89. return NULL;
  90. if (VIR_ALLOC_N(ret, nports + 1) < 0)
  91. goto error;
  92. for (i = 0; i < nports; i++) {
  93. char uuid[VIR_UUID_STRING_BUFLEN];
  94. if (virNetworkPortGetUUIDString(ports[i], uuid) < 0)
  95. goto error;
  96. ret[i] = g_strdup(uuid);
  97. virNetworkPortFree(ports[i]);
  98. }
  99. VIR_FREE(ports);
  100. return ret;
  101. error:
  102. for (; i < nports; i++)
  103. virNetworkPortFree(ports[i]);
  104. VIR_FREE(ports);
  105. for (i = 0; i < nports; i++)
  106. VIR_FREE(ret[i]);
  107. VIR_FREE(ret);
  108. return NULL;
  109. }