+class R_2_nodes_name_role(baserlib.R_Generic):
+ """ /2/nodes/[node_name]/role resource.
+
+ """
+ def GET(self):
+ """Returns the current node role.
+
+ @return: Node role
+
+ """
+ node_name = self.items[0]
+ client = baserlib.GetClient()
+ result = client.QueryNodes(names=[node_name], fields=["role"],
+ use_locking=self.useLocking())
+
+ return _NR_MAP[result[0][0]]
+
+ def PUT(self):
+ """Sets the node role.
+
+ @return: a job id
+
+ """
+ if not isinstance(self.request_body, basestring):
+ raise http.HttpBadRequest("Invalid body contents, not a string")
+
+ node_name = self.items[0]
+ role = self.request_body
+
+ auto_promote = bool(self._checkIntVariable("auto-promote"))
+
+ if role == _NR_REGULAR:
+ candidate = False
+ offline = False
+ drained = False
+
+ elif role == _NR_MASTER_CANDIATE:
+ candidate = True
+ offline = drained = None
+
+ elif role == _NR_DRAINED:
+ drained = True
+ candidate = offline = None
+
+ elif role == _NR_OFFLINE:
+ offline = True
+ candidate = drained = None
+
+ else:
+ raise http.HttpBadRequest("Can't set '%s' role" % role)
+
+ op = opcodes.OpNodeSetParams(node_name=node_name,
+ master_candidate=candidate,
+ offline=offline,
+ drained=drained,
+ auto_promote=auto_promote,
+ force=bool(self.useForce()))
+
+ return baserlib.SubmitJob([op])
+
+
+class R_2_nodes_name_evacuate(baserlib.R_Generic):
+ """/2/nodes/[node_name]/evacuate resource.
+
+ """
+ def POST(self):
+ """Evacuate all instances off a node.
+
+ """
+ op = baserlib.FillOpcode(opcodes.OpNodeEvacuate, self.request_body, {
+ "node_name": self.items[0],
+ "dry_run": self.dryRun(),
+ })
+
+ return baserlib.SubmitJob([op])
+
+
+class R_2_nodes_name_migrate(baserlib.R_Generic):
+ """/2/nodes/[node_name]/migrate resource.
+
+ """
+ def POST(self):
+ """Migrate all primary instances from a node.
+
+ """
+ node_name = self.items[0]
+
+ if self.queryargs:
+ # Support old-style requests
+ if "live" in self.queryargs and "mode" in self.queryargs:
+ raise http.HttpBadRequest("Only one of 'live' and 'mode' should"
+ " be passed")
+
+ if "live" in self.queryargs:
+ if self._checkIntVariable("live", default=1):
+ mode = constants.HT_MIGRATION_LIVE
+ else:
+ mode = constants.HT_MIGRATION_NONLIVE
+ else:
+ mode = self._checkStringVariable("mode", default=None)
+
+ data = {
+ "mode": mode,
+ }
+ else:
+ data = self.request_body
+
+ op = baserlib.FillOpcode(opcodes.OpNodeMigrate, data, {
+ "node_name": node_name,
+ })
+
+ return baserlib.SubmitJob([op])
+
+
+class R_2_nodes_name_modify(baserlib.R_Generic):
+ """/2/nodes/[node_name]/modify resource.
+
+ """
+ def POST(self):
+ """Changes parameters of a node.
+
+ @return: a job id
+
+ """
+ baserlib.CheckType(self.request_body, dict, "Body contents")
+
+ op = baserlib.FillOpcode(opcodes.OpNodeSetParams, self.request_body, {
+ "node_name": self.items[0],
+ })
+
+ return baserlib.SubmitJob([op])
+
+
+class R_2_nodes_name_storage(baserlib.R_Generic):
+ """/2/nodes/[node_name]/storage resource.
+
+ """
+ # LUNodeQueryStorage acquires locks, hence restricting access to GET
+ GET_ACCESS = [rapi.RAPI_ACCESS_WRITE]
+
+ def GET(self):
+ node_name = self.items[0]
+
+ storage_type = self._checkStringVariable("storage_type", None)
+ if not storage_type:
+ raise http.HttpBadRequest("Missing the required 'storage_type'"
+ " parameter")
+
+ output_fields = self._checkStringVariable("output_fields", None)
+ if not output_fields:
+ raise http.HttpBadRequest("Missing the required 'output_fields'"
+ " parameter")
+
+ op = opcodes.OpNodeQueryStorage(nodes=[node_name],
+ storage_type=storage_type,
+ output_fields=output_fields.split(","))
+ return baserlib.SubmitJob([op])
+
+
+class R_2_nodes_name_storage_modify(baserlib.R_Generic):
+ """/2/nodes/[node_name]/storage/modify resource.
+
+ """
+ def PUT(self):
+ node_name = self.items[0]
+
+ storage_type = self._checkStringVariable("storage_type", None)
+ if not storage_type:
+ raise http.HttpBadRequest("Missing the required 'storage_type'"
+ " parameter")
+
+ name = self._checkStringVariable("name", None)
+ if not name:
+ raise http.HttpBadRequest("Missing the required 'name'"
+ " parameter")
+
+ changes = {}
+
+ if "allocatable" in self.queryargs:
+ changes[constants.SF_ALLOCATABLE] = \
+ bool(self._checkIntVariable("allocatable", default=1))
+
+ op = opcodes.OpNodeModifyStorage(node_name=node_name,
+ storage_type=storage_type,
+ name=name,
+ changes=changes)
+ return baserlib.SubmitJob([op])
+
+
+class R_2_nodes_name_storage_repair(baserlib.R_Generic):
+ """/2/nodes/[node_name]/storage/repair resource.
+
+ """
+ def PUT(self):
+ node_name = self.items[0]
+
+ storage_type = self._checkStringVariable("storage_type", None)
+ if not storage_type:
+ raise http.HttpBadRequest("Missing the required 'storage_type'"
+ " parameter")
+
+ name = self._checkStringVariable("name", None)
+ if not name:
+ raise http.HttpBadRequest("Missing the required 'name'"
+ " parameter")
+
+ op = opcodes.OpRepairNodeStorage(node_name=node_name,
+ storage_type=storage_type,
+ name=name)
+ return baserlib.SubmitJob([op])
+
+
+def _ParseCreateGroupRequest(data, dry_run):
+ """Parses a request for creating a node group.
+
+ @rtype: L{opcodes.OpGroupAdd}
+ @return: Group creation opcode
+
+ """
+ override = {
+ "dry_run": dry_run,
+ }
+
+ rename = {
+ "name": "group_name",
+ }
+
+ return baserlib.FillOpcode(opcodes.OpGroupAdd, data, override,
+ rename=rename)
+
+
+class R_2_groups(baserlib.R_Generic):
+ """/2/groups resource.
+
+ """
+ def GET(self):
+ """Returns a list of all node groups.
+
+ """
+ client = baserlib.GetClient()
+
+ if self.useBulk():
+ bulkdata = client.QueryGroups([], G_FIELDS, False)
+ return baserlib.MapBulkFields(bulkdata, G_FIELDS)
+ else:
+ data = client.QueryGroups([], ["name"], False)
+ groupnames = [row[0] for row in data]
+ return baserlib.BuildUriList(groupnames, "/2/groups/%s",
+ uri_fields=("name", "uri"))
+
+ def POST(self):
+ """Create a node group.
+
+ @return: a job id
+
+ """
+ baserlib.CheckType(self.request_body, dict, "Body contents")
+ op = _ParseCreateGroupRequest(self.request_body, self.dryRun())
+ return baserlib.SubmitJob([op])
+
+
+class R_2_groups_name(baserlib.R_Generic):
+ """/2/groups/[group_name] resource.
+
+ """
+ def GET(self):
+ """Send information about a node group.
+
+ """
+ group_name = self.items[0]
+ client = baserlib.GetClient()
+
+ result = baserlib.HandleItemQueryErrors(client.QueryGroups,
+ names=[group_name], fields=G_FIELDS,
+ use_locking=self.useLocking())
+
+ return baserlib.MapFields(G_FIELDS, result[0])
+
+ def DELETE(self):
+ """Delete a node group.
+
+ """
+ op = opcodes.OpGroupRemove(group_name=self.items[0],
+ dry_run=bool(self.dryRun()))
+
+ return baserlib.SubmitJob([op])
+
+
+def _ParseModifyGroupRequest(name, data):
+ """Parses a request for modifying a node group.
+
+ @rtype: L{opcodes.OpGroupSetParams}
+ @return: Group modify opcode
+
+ """
+ return baserlib.FillOpcode(opcodes.OpGroupSetParams, data, {
+ "group_name": name,
+ })
+
+
+class R_2_groups_name_modify(baserlib.R_Generic):
+ """/2/groups/[group_name]/modify resource.
+
+ """
+ def PUT(self):
+ """Changes some parameters of node group.
+
+ @return: a job id
+
+ """
+ baserlib.CheckType(self.request_body, dict, "Body contents")
+
+ op = _ParseModifyGroupRequest(self.items[0], self.request_body)
+
+ return baserlib.SubmitJob([op])
+
+
+def _ParseRenameGroupRequest(name, data, dry_run):
+ """Parses a request for renaming a node group.
+
+ @type name: string
+ @param name: name of the node group to rename
+ @type data: dict
+ @param data: the body received by the rename request
+ @type dry_run: bool
+ @param dry_run: whether to perform a dry run
+
+ @rtype: L{opcodes.OpGroupRename}
+ @return: Node group rename opcode
+
+ """
+ return baserlib.FillOpcode(opcodes.OpGroupRename, data, {
+ "group_name": name,
+ "dry_run": dry_run,
+ })
+
+
+class R_2_groups_name_rename(baserlib.R_Generic):
+ """/2/groups/[group_name]/rename resource.
+
+ """
+ def PUT(self):
+ """Changes the name of a node group.
+
+ @return: a job id
+
+ """
+ baserlib.CheckType(self.request_body, dict, "Body contents")
+ op = _ParseRenameGroupRequest(self.items[0], self.request_body,
+ self.dryRun())
+ return baserlib.SubmitJob([op])
+
+
+class R_2_groups_name_assign_nodes(baserlib.R_Generic):
+ """/2/groups/[group_name]/assign-nodes resource.
+
+ """
+ def PUT(self):
+ """Assigns nodes to a group.
+
+ @return: a job id
+
+ """
+ op = baserlib.FillOpcode(opcodes.OpGroupAssignNodes, self.request_body, {
+ "group_name": self.items[0],
+ "dry_run": self.dryRun(),
+ "force": self.useForce(),
+ })
+
+ return baserlib.SubmitJob([op])
+
+
+def _ParseInstanceCreateRequestVersion1(data, dry_run):
+ """Parses an instance creation request version 1.
+
+ @rtype: L{opcodes.OpInstanceCreate}
+ @return: Instance creation opcode
+
+ """
+ override = {
+ "dry_run": dry_run,
+ }
+
+ rename = {
+ "os": "os_type",
+ "name": "instance_name",
+ }
+
+ return baserlib.FillOpcode(opcodes.OpInstanceCreate, data, override,
+ rename=rename)
+
+