#
#
-# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Google Inc.
+# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# C0103: Invalid name, since the R_* names are not conforming
from ganeti import opcodes
+from ganeti import objects
from ganeti import http
from ganeti import constants
from ganeti import cli
I_FIELDS = ["name", "admin_state", "os",
"pnode", "snodes",
"disk_template",
- "nic.ips", "nic.macs", "nic.modes", "nic.links", "nic.bridges",
+ "nic.ips", "nic.macs", "nic.modes", "nic.uuids", "nic.names",
+ "nic.links", "nic.networks", "nic.networks.names", "nic.bridges",
"network_port",
- "disk.sizes", "disk_usage",
+ "disk.sizes", "disk.spindles", "disk_usage", "disk.uuids",
+ "disk.names",
"beparams", "hvparams",
"oper_state", "oper_ram", "oper_vcpus", "status",
"custom_hvparams", "custom_beparams", "custom_nicparams",
] + _COMMON_FIELDS
N_FIELDS = ["name", "offline", "master_candidate", "drained",
- "dtotal", "dfree",
+ "dtotal", "dfree", "sptotal", "spfree",
"mtotal", "mnode", "mfree",
"pinst_cnt", "sinst_cnt",
- "ctotal", "cnodes", "csockets",
+ "ctotal", "cnos", "cnodes", "csockets",
"pip", "sip", "role",
"pinst_list", "sinst_list",
"master_capable", "vm_capable",
+ "ndparams",
"group.uuid",
] + _COMMON_FIELDS
+NET_FIELDS = ["name", "network", "gateway",
+ "network6", "gateway6",
+ "mac_prefix",
+ "free_count", "reserved_count",
+ "map", "group_list", "inst_list",
+ "external_reservations",
+ ] + _COMMON_FIELDS
+
G_FIELDS = [
"alloc_policy",
"name",
"node_cnt",
"node_list",
+ "ipolicy",
+ "custom_ipolicy",
+ "diskparams",
+ "custom_diskparams",
+ "ndparams",
+ "custom_ndparams",
] + _COMMON_FIELDS
J_FIELDS_BULK = [
# Feature string for node evacuation with LU-generated jobs
_NODE_EVAC_RES1 = "node-evac-res1"
-ALL_FEATURES = frozenset([
+ALL_FEATURES = compat.UniqueFrozenset([
_INST_CREATE_REQV1,
_INST_REINSTALL_REQV1,
_NODE_MIGRATE_REQV1,
_WFJC_TIMEOUT = 10
+# FIXME: For compatibility we update the beparams/memory field. Needs to be
+# removed in Ganeti 2.8
+def _UpdateBeparams(inst):
+ """Updates the beparams dict of inst to support the memory field.
+
+ @param inst: Inst dict
+ @return: Updated inst dict
+
+ """
+ beparams = inst["beparams"]
+ beparams[constants.BE_MEMORY] = beparams[constants.BE_MAXMEM]
+
+ return inst
+
+
class R_root(baserlib.ResourceBase):
"""/ resource.
"""Returns cluster information.
"""
- client = self.GetClient()
+ client = self.GetClient(query=True)
return client.QueryClusterInfo()
@return: a dictionary with jobs id and uri.
"""
- client = self.GetClient()
+ client = self.GetClient(query=True)
if self.useBulk():
bulkdata = client.QueryJobs(None, J_FIELDS_BULK)
"""
job_id = self.items[0]
- result = self.GetClient().QueryJobs([job_id, ], J_FIELDS)[0]
+ result = self.GetClient(query=True).QueryJobs([job_id, ], J_FIELDS)[0]
if result is None:
raise http.HttpNotFound()
return baserlib.MapFields(J_FIELDS, result)
"""Returns a list of all nodes.
"""
- client = self.GetClient()
+ client = self.GetClient(query=True)
if self.useBulk():
bulkdata = client.QueryNodes([], N_FIELDS, False)
"""
node_name = self.items[0]
- client = self.GetClient()
+ client = self.GetClient(query=True)
result = baserlib.HandleItemQueryErrors(client.QueryNodes,
names=[node_name], fields=N_FIELDS,
"""
node_name = self.items[0]
- client = self.GetClient()
+ client = self.GetClient(query=True)
result = client.QueryNodes(names=[node_name], fields=["role"],
use_locking=self.useLocking())
})
+class R_2_networks(baserlib.OpcodeResource):
+ """/2/networks resource.
+
+ """
+ GET_OPCODE = opcodes.OpNetworkQuery
+ POST_OPCODE = opcodes.OpNetworkAdd
+ POST_RENAME = {
+ "name": "network_name",
+ }
+
+ def GetPostOpInput(self):
+ """Create a network.
+
+ """
+ assert not self.items
+ return (self.request_body, {
+ "dry_run": self.dryRun(),
+ })
+
+ def GET(self):
+ """Returns a list of all networks.
+
+ """
+ client = self.GetClient(query=True)
+
+ if self.useBulk():
+ bulkdata = client.QueryNetworks([], NET_FIELDS, False)
+ return baserlib.MapBulkFields(bulkdata, NET_FIELDS)
+ else:
+ data = client.QueryNetworks([], ["name"], False)
+ networknames = [row[0] for row in data]
+ return baserlib.BuildUriList(networknames, "/2/networks/%s",
+ uri_fields=("name", "uri"))
+
+
+class R_2_networks_name(baserlib.OpcodeResource):
+ """/2/networks/[network_name] resource.
+
+ """
+ DELETE_OPCODE = opcodes.OpNetworkRemove
+
+ def GET(self):
+ """Send information about a network.
+
+ """
+ network_name = self.items[0]
+ client = self.GetClient(query=True)
+
+ result = baserlib.HandleItemQueryErrors(client.QueryNetworks,
+ names=[network_name],
+ fields=NET_FIELDS,
+ use_locking=self.useLocking())
+
+ return baserlib.MapFields(NET_FIELDS, result[0])
+
+ def GetDeleteOpInput(self):
+ """Delete a network.
+
+ """
+ assert len(self.items) == 1
+ return (self.request_body, {
+ "network_name": self.items[0],
+ "dry_run": self.dryRun(),
+ })
+
+
+class R_2_networks_name_connect(baserlib.OpcodeResource):
+ """/2/networks/[network_name]/connect resource.
+
+ """
+ PUT_OPCODE = opcodes.OpNetworkConnect
+
+ def GetPutOpInput(self):
+ """Changes some parameters of node group.
+
+ """
+ assert self.items
+ return (self.request_body, {
+ "network_name": self.items[0],
+ "dry_run": self.dryRun(),
+ })
+
+
+class R_2_networks_name_disconnect(baserlib.OpcodeResource):
+ """/2/networks/[network_name]/disconnect resource.
+
+ """
+ PUT_OPCODE = opcodes.OpNetworkDisconnect
+
+ def GetPutOpInput(self):
+ """Changes some parameters of node group.
+
+ """
+ assert self.items
+ return (self.request_body, {
+ "network_name": self.items[0],
+ "dry_run": self.dryRun(),
+ })
+
+
+class R_2_networks_name_modify(baserlib.OpcodeResource):
+ """/2/networks/[network_name]/modify resource.
+
+ """
+ PUT_OPCODE = opcodes.OpNetworkSetParams
+
+ def GetPutOpInput(self):
+ """Changes some parameters of network.
+
+ """
+ assert self.items
+ return (self.request_body, {
+ "network_name": self.items[0],
+ })
+
+
class R_2_groups(baserlib.OpcodeResource):
"""/2/groups resource.
def GetPostOpInput(self):
"""Create a node group.
+
"""
assert not self.items
return (self.request_body, {
"""Returns a list of all node groups.
"""
- client = self.GetClient()
+ client = self.GetClient(query=True)
if self.useBulk():
bulkdata = client.QueryGroups([], G_FIELDS, False)
"""
group_name = self.items[0]
- client = self.GetClient()
+ client = self.GetClient(query=True)
result = baserlib.HandleItemQueryErrors(client.QueryGroups,
names=[group_name], fields=G_FIELDS,
use_locking = self.useLocking()
if self.useBulk():
bulkdata = client.QueryInstances([], I_FIELDS, use_locking)
- return baserlib.MapBulkFields(bulkdata, I_FIELDS)
+ return map(_UpdateBeparams, baserlib.MapBulkFields(bulkdata, I_FIELDS))
else:
instancesdata = client.QueryInstances([], ["name"], use_locking)
instanceslist = [row[0] for row in instancesdata]
})
+class R_2_instances_multi_alloc(baserlib.OpcodeResource):
+ """/2/instances-multi-alloc resource.
+
+ """
+ POST_OPCODE = opcodes.OpInstanceMultiAlloc
+
+ def GetPostOpInput(self):
+ """Try to allocate multiple instances.
+
+ @return: A dict with submitted jobs, allocatable instances and failed
+ allocations
+
+ """
+ if "instances" not in self.request_body:
+ raise http.HttpBadRequest("Request is missing required 'instances' field"
+ " in body")
+
+ op_id = {
+ "OP_ID": self.POST_OPCODE.OP_ID, # pylint: disable=E1101
+ }
+ body = objects.FillDict(self.request_body, {
+ "instances": [objects.FillDict(inst, op_id)
+ for inst in self.request_body["instances"]],
+ })
+
+ return (body, {
+ "dry_run": self.dryRun(),
+ })
+
+
class R_2_instances_name(baserlib.OpcodeResource):
"""/2/instances/[instance_name] resource.
fields=I_FIELDS,
use_locking=self.useLocking())
- return baserlib.MapFields(I_FIELDS, result[0])
+ return _UpdateBeparams(baserlib.MapFields(I_FIELDS, result[0]))
def GetDeleteOpInput(self):
"""Delete an instance.
"""/2/instances/[instance_name]/console resource.
"""
- GET_ACCESS = [rapi.RAPI_ACCESS_WRITE]
+ GET_ACCESS = [rapi.RAPI_ACCESS_WRITE, rapi.RAPI_ACCESS_READ]
GET_OPCODE = opcodes.OpInstanceConsole
def GET(self):
def _GetQueryFields(args):
- """
+ """Tries to extract C{fields} query parameter.
+
+ @type args: dictionary
+ @rtype: list of string
+ @raise http.HttpBadRequest: When parameter can't be found
"""
try:
def _SplitQueryFields(fields):
- """
+ """Splits fields as given for a query request.
+
+ @type fields: string
+ @rtype: list of string
"""
return [i.strip() for i in fields.split(",")]
"""
# Results might contain sensitive information
- GET_ACCESS = [rapi.RAPI_ACCESS_WRITE]
+ GET_ACCESS = [rapi.RAPI_ACCESS_WRITE, rapi.RAPI_ACCESS_READ]
+ PUT_ACCESS = GET_ACCESS
GET_OPCODE = opcodes.OpQuery
PUT_OPCODE = opcodes.OpQuery
class _R_Tags(baserlib.OpcodeResource):
- """ Quasiclass for tagging resources
+ """Quasiclass for tagging resources.
Manages tags. When inheriting this class you must define the
TAG_LEVEL for it.
if not self.name:
raise http.HttpBadRequest("Missing name on tag request")
- cl = self.GetClient()
- if kind == constants.TAG_INSTANCE:
- fn = cl.QueryInstances
- elif kind == constants.TAG_NODEGROUP:
- fn = cl.QueryGroups
- else:
- fn = cl.QueryNodes
- result = fn(names=[self.name], fields=["tags"], use_locking=False)
- if not result or not result[0]:
- raise http.HttpBadGateway("Invalid response from tag query")
- tags = result[0][0]
+ cl = self.GetClient(query=True)
+ tags = list(cl.QueryTags(kind, self.name))
elif kind == constants.TAG_CLUSTER:
assert not self.name
TAG_LEVEL = constants.TAG_NODEGROUP
+class R_2_networks_name_tags(_R_Tags):
+ """ /2/networks/[network_name]/tags resource.
+
+ Manages per-network tags.
+
+ """
+ TAG_LEVEL = constants.TAG_NETWORK
+
+
class R_2_tags(_R_Tags):
""" /2/tags resource.