+
+ def GetNetworks(self, bulk=False):
+ """Gets all networks in the cluster.
+
+ @type bulk: bool
+ @param bulk: whether to return all information about the networks
+
+ @rtype: list of dict or str
+ @return: if bulk is true, a list of dictionaries with info about all
+ networks in the cluster, else a list of names of those networks
+
+ """
+ query = []
+ _AppendIf(query, bulk, ("bulk", 1))
+
+ networks = self._SendRequest(HTTP_GET, "/%s/networks" % GANETI_RAPI_VERSION,
+ query, None)
+ if bulk:
+ return networks
+ else:
+ return [n["name"] for n in networks]
+
+ def GetNetwork(self, network):
+ """Gets information about a network.
+
+ @type network: str
+ @param network: name of the network whose info to return
+
+ @rtype: dict
+ @return: info about the network
+
+ """
+ return self._SendRequest(HTTP_GET,
+ "/%s/networks/%s" % (GANETI_RAPI_VERSION, network),
+ None, None)
+
+ def CreateNetwork(self, network_name, network, gateway=None, network6=None,
+ gateway6=None, mac_prefix=None,
+ add_reserved_ips=None, tags=None, dry_run=False):
+ """Creates a new network.
+
+ @type network_name: str
+ @param network_name: the name of network to create
+ @type dry_run: bool
+ @param dry_run: whether to peform a dry run
+
+ @rtype: string
+ @return: job id
+
+ """
+ query = []
+ _AppendDryRunIf(query, dry_run)
+
+ if add_reserved_ips:
+ add_reserved_ips = add_reserved_ips.split(",")
+
+ if tags:
+ tags = tags.split(",")
+
+ body = {
+ "network_name": network_name,
+ "gateway": gateway,
+ "network": network,
+ "gateway6": gateway6,
+ "network6": network6,
+ "mac_prefix": mac_prefix,
+ "add_reserved_ips": add_reserved_ips,
+ "tags": tags,
+ }
+
+ return self._SendRequest(HTTP_POST, "/%s/networks" % GANETI_RAPI_VERSION,
+ query, body)
+
+ def ConnectNetwork(self, network_name, group_name, mode, link, dry_run=False):
+ """Connects a Network to a NodeGroup with the given netparams
+
+ """
+ body = {
+ "group_name": group_name,
+ "network_mode": mode,
+ "network_link": link,
+ }
+
+ query = []
+ _AppendDryRunIf(query, dry_run)
+
+ return self._SendRequest(HTTP_PUT,
+ ("/%s/networks/%s/connect" %
+ (GANETI_RAPI_VERSION, network_name)), query, body)
+
+ def DisconnectNetwork(self, network_name, group_name, dry_run=False):
+ """Connects a Network to a NodeGroup with the given netparams
+
+ """
+ body = {
+ "group_name": group_name,
+ }
+
+ query = []
+ _AppendDryRunIf(query, dry_run)
+
+ return self._SendRequest(HTTP_PUT,
+ ("/%s/networks/%s/disconnect" %
+ (GANETI_RAPI_VERSION, network_name)), query, body)
+
+ def ModifyNetwork(self, network, **kwargs):
+ """Modifies a network.
+
+ More details for parameters can be found in the RAPI documentation.
+
+ @type network: string
+ @param network: Network name
+ @rtype: string
+ @return: job id
+
+ """
+ return self._SendRequest(HTTP_PUT,
+ ("/%s/networks/%s/modify" %
+ (GANETI_RAPI_VERSION, network)), None, kwargs)
+
+ def DeleteNetwork(self, network, dry_run=False):
+ """Deletes a network.
+
+ @type network: str
+ @param network: the network to delete
+ @type dry_run: bool
+ @param dry_run: whether to peform a dry run
+
+ @rtype: string
+ @return: job id
+
+ """
+ query = []
+ _AppendDryRunIf(query, dry_run)
+
+ return self._SendRequest(HTTP_DELETE,
+ ("/%s/networks/%s" %
+ (GANETI_RAPI_VERSION, network)), query, None)
+
+ def GetNetworkTags(self, network):
+ """Gets tags for a network.
+
+ @type network: string
+ @param network: Node group whose tags to return
+
+ @rtype: list of strings
+ @return: tags for the network
+
+ """
+ return self._SendRequest(HTTP_GET,
+ ("/%s/networks/%s/tags" %
+ (GANETI_RAPI_VERSION, network)), None, None)
+
+ def AddNetworkTags(self, network, tags, dry_run=False):
+ """Adds tags to a network.
+
+ @type network: str
+ @param network: network to add tags to
+ @type tags: list of string
+ @param tags: tags to add to the network
+ @type dry_run: bool
+ @param dry_run: whether to perform a dry run
+
+ @rtype: string
+ @return: job id
+
+ """
+ query = [("tag", t) for t in tags]
+ _AppendDryRunIf(query, dry_run)
+
+ return self._SendRequest(HTTP_PUT,
+ ("/%s/networks/%s/tags" %
+ (GANETI_RAPI_VERSION, network)), query, None)
+
+ def DeleteNetworkTags(self, network, tags, dry_run=False):
+ """Deletes tags from a network.
+
+ @type network: str
+ @param network: network to delete tags from
+ @type tags: list of string
+ @param tags: tags to delete
+ @type dry_run: bool
+ @param dry_run: whether to perform a dry run
+ @rtype: string
+ @return: job id
+
+ """
+ query = [("tag", t) for t in tags]
+ _AppendDryRunIf(query, dry_run)
+
+ return self._SendRequest(HTTP_DELETE,
+ ("/%s/networks/%s/tags" %
+ (GANETI_RAPI_VERSION, network)), query, None)
+
+ def GetGroups(self, bulk=False):
+ """Gets all node groups in the cluster.
+
+ @type bulk: bool
+ @param bulk: whether to return all information about the groups
+
+ @rtype: list of dict or str
+ @return: if bulk is true, a list of dictionaries with info about all node
+ groups in the cluster, else a list of names of those node groups
+
+ """
+ query = []
+ _AppendIf(query, bulk, ("bulk", 1))
+
+ groups = self._SendRequest(HTTP_GET, "/%s/groups" % GANETI_RAPI_VERSION,
+ query, None)
+ if bulk:
+ return groups
+ else:
+ return [g["name"] for g in groups]
+
+ def GetGroup(self, group):
+ """Gets information about a node group.
+
+ @type group: str
+ @param group: name of the node group whose info to return
+
+ @rtype: dict
+ @return: info about the node group
+
+ """
+ return self._SendRequest(HTTP_GET,
+ "/%s/groups/%s" % (GANETI_RAPI_VERSION, group),
+ None, None)
+
+ def CreateGroup(self, name, alloc_policy=None, dry_run=False):
+ """Creates a new node group.
+
+ @type name: str
+ @param name: the name of node group to create
+ @type alloc_policy: str
+ @param alloc_policy: the desired allocation policy for the group, if any
+ @type dry_run: bool
+ @param dry_run: whether to peform a dry run
+
+ @rtype: string
+ @return: job id
+
+ """
+ query = []
+ _AppendDryRunIf(query, dry_run)
+
+ body = {
+ "name": name,
+ "alloc_policy": alloc_policy,
+ }
+
+ return self._SendRequest(HTTP_POST, "/%s/groups" % GANETI_RAPI_VERSION,
+ query, body)
+
+ def ModifyGroup(self, group, **kwargs):
+ """Modifies a node group.
+
+ More details for parameters can be found in the RAPI documentation.
+
+ @type group: string
+ @param group: Node group name
+ @rtype: string
+ @return: job id
+
+ """
+ return self._SendRequest(HTTP_PUT,
+ ("/%s/groups/%s/modify" %
+ (GANETI_RAPI_VERSION, group)), None, kwargs)
+
+ def DeleteGroup(self, group, dry_run=False):
+ """Deletes a node group.
+
+ @type group: str
+ @param group: the node group to delete
+ @type dry_run: bool
+ @param dry_run: whether to peform a dry run
+
+ @rtype: string
+ @return: job id
+
+ """
+ query = []
+ _AppendDryRunIf(query, dry_run)
+
+ return self._SendRequest(HTTP_DELETE,
+ ("/%s/groups/%s" %
+ (GANETI_RAPI_VERSION, group)), query, None)
+
+ def RenameGroup(self, group, new_name):
+ """Changes the name of a node group.
+
+ @type group: string
+ @param group: Node group name
+ @type new_name: string
+ @param new_name: New node group name
+
+ @rtype: string
+ @return: job id
+
+ """
+ body = {
+ "new_name": new_name,
+ }
+
+ return self._SendRequest(HTTP_PUT,
+ ("/%s/groups/%s/rename" %
+ (GANETI_RAPI_VERSION, group)), None, body)
+
+ def AssignGroupNodes(self, group, nodes, force=False, dry_run=False):
+ """Assigns nodes to a group.
+
+ @type group: string
+ @param group: Node group name
+ @type nodes: list of strings
+ @param nodes: List of nodes to assign to the group
+
+ @rtype: string
+ @return: job id
+
+ """
+ query = []
+ _AppendForceIf(query, force)
+ _AppendDryRunIf(query, dry_run)
+
+ body = {
+ "nodes": nodes,
+ }
+
+ return self._SendRequest(HTTP_PUT,
+ ("/%s/groups/%s/assign-nodes" %
+ (GANETI_RAPI_VERSION, group)), query, body)
+
+ def GetGroupTags(self, group):
+ """Gets tags for a node group.
+
+ @type group: string
+ @param group: Node group whose tags to return
+
+ @rtype: list of strings
+ @return: tags for the group
+
+ """
+ return self._SendRequest(HTTP_GET,
+ ("/%s/groups/%s/tags" %
+ (GANETI_RAPI_VERSION, group)), None, None)
+
+ def AddGroupTags(self, group, tags, dry_run=False):
+ """Adds tags to a node group.
+
+ @type group: str
+ @param group: group to add tags to
+ @type tags: list of string
+ @param tags: tags to add to the group
+ @type dry_run: bool
+ @param dry_run: whether to perform a dry run
+
+ @rtype: string
+ @return: job id
+
+ """
+ query = [("tag", t) for t in tags]
+ _AppendDryRunIf(query, dry_run)
+
+ return self._SendRequest(HTTP_PUT,
+ ("/%s/groups/%s/tags" %
+ (GANETI_RAPI_VERSION, group)), query, None)
+
+ def DeleteGroupTags(self, group, tags, dry_run=False):
+ """Deletes tags from a node group.
+
+ @type group: str
+ @param group: group to delete tags from
+ @type tags: list of string
+ @param tags: tags to delete
+ @type dry_run: bool
+ @param dry_run: whether to perform a dry run
+ @rtype: string
+ @return: job id
+
+ """
+ query = [("tag", t) for t in tags]
+ _AppendDryRunIf(query, dry_run)
+
+ return self._SendRequest(HTTP_DELETE,
+ ("/%s/groups/%s/tags" %
+ (GANETI_RAPI_VERSION, group)), query, None)
+
+ def Query(self, what, fields, qfilter=None):
+ """Retrieves information about resources.
+
+ @type what: string
+ @param what: Resource name, one of L{constants.QR_VIA_RAPI}
+ @type fields: list of string
+ @param fields: Requested fields
+ @type qfilter: None or list
+ @param qfilter: Query filter
+
+ @rtype: string
+ @return: job id
+
+ """
+ body = {
+ "fields": fields,
+ }
+
+ _SetItemIf(body, qfilter is not None, "qfilter", qfilter)
+ # TODO: remove "filter" after 2.7
+ _SetItemIf(body, qfilter is not None, "filter", qfilter)
+
+ return self._SendRequest(HTTP_PUT,
+ ("/%s/query/%s" %
+ (GANETI_RAPI_VERSION, what)), None, body)
+
+ def QueryFields(self, what, fields=None):
+ """Retrieves available fields for a resource.
+
+ @type what: string
+ @param what: Resource name, one of L{constants.QR_VIA_RAPI}
+ @type fields: list of string
+ @param fields: Requested fields
+
+ @rtype: string
+ @return: job id
+
+ """
+ query = []
+
+ if fields is not None:
+ _AppendIf(query, True, ("fields", ",".join(fields)))
+
+ return self._SendRequest(HTTP_GET,
+ ("/%s/query/%s/fields" %
+ (GANETI_RAPI_VERSION, what)), query, None)