Statistics
| Branch: | Tag: | Revision:

root / lib / rapi / rlib2.py @ ab9e48d1

History | View | Annotate | Download (39 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Remote API resource implementations.
23

24
PUT or POST?
25
============
26

27
According to RFC2616 the main difference between PUT and POST is that
28
POST can create new resources but PUT can only create the resource the
29
URI was pointing to on the PUT request.
30

31
In the context of this module POST on ``/2/instances`` to change an existing
32
entity is legitimate, while PUT would not be. PUT creates a new entity (e.g. a
33
new instance) with a name specified in the request.
34

35
Quoting from RFC2616, section 9.6::
36

37
  The fundamental difference between the POST and PUT requests is reflected in
38
  the different meaning of the Request-URI. The URI in a POST request
39
  identifies the resource that will handle the enclosed entity. That resource
40
  might be a data-accepting process, a gateway to some other protocol, or a
41
  separate entity that accepts annotations. In contrast, the URI in a PUT
42
  request identifies the entity enclosed with the request -- the user agent
43
  knows what URI is intended and the server MUST NOT attempt to apply the
44
  request to some other resource. If the server desires that the request be
45
  applied to a different URI, it MUST send a 301 (Moved Permanently) response;
46
  the user agent MAY then make its own decision regarding whether or not to
47
  redirect the request.
48

49
So when adding new methods, if they are operating on the URI entity itself,
50
PUT should be prefered over POST.
51

52
"""
53

    
54
# pylint: disable=C0103
55

    
56
# C0103: Invalid name, since the R_* names are not conforming
57

    
58
from ganeti import opcodes
59
from ganeti import objects
60
from ganeti import http
61
from ganeti import constants
62
from ganeti import cli
63
from ganeti import rapi
64
from ganeti import ht
65
from ganeti import compat
66
from ganeti import ssconf
67
from ganeti.rapi import baserlib
68

    
69

    
70
_COMMON_FIELDS = ["ctime", "mtime", "uuid", "serial_no", "tags"]
71
I_FIELDS = ["name", "admin_state", "os",
72
            "pnode", "snodes",
73
            "disk_template",
74
            "nic.ips", "nic.macs", "nic.modes",
75
            "nic.links", "nic.networks", "nic.bridges",
76
            "network_port",
77
            "disk.sizes", "disk_usage",
78
            "beparams", "hvparams",
79
            "oper_state", "oper_ram", "oper_vcpus", "status",
80
            "custom_hvparams", "custom_beparams", "custom_nicparams",
81
            ] + _COMMON_FIELDS
82

    
83
N_FIELDS = ["name", "offline", "master_candidate", "drained",
84
            "dtotal", "dfree",
85
            "mtotal", "mnode", "mfree",
86
            "pinst_cnt", "sinst_cnt",
87
            "ctotal", "cnodes", "csockets",
88
            "pip", "sip", "role",
89
            "pinst_list", "sinst_list",
90
            "master_capable", "vm_capable",
91
            "ndparams",
92
            "group.uuid",
93
            ] + _COMMON_FIELDS
94

    
95
NET_FIELDS = ["name", "network", "gateway",
96
              "network6", "gateway6",
97
              "mac_prefix",
98
              "free_count", "reserved_count",
99
              "map", "group_list", "inst_list",
100
              "external_reservations", "tags",
101
              ]
102

    
103
G_FIELDS = [
104
  "alloc_policy",
105
  "name",
106
  "node_cnt",
107
  "node_list",
108
  "ipolicy",
109
  "custom_ipolicy",
110
  "diskparams",
111
  "custom_diskparams",
112
  "ndparams",
113
  "custom_ndparams",
114
  ] + _COMMON_FIELDS
115

    
116
J_FIELDS_BULK = [
117
  "id", "ops", "status", "summary",
118
  "opstatus",
119
  "received_ts", "start_ts", "end_ts",
120
  ]
121

    
122
J_FIELDS = J_FIELDS_BULK + [
123
  "oplog",
124
  "opresult",
125
  ]
126

    
127
_NR_DRAINED = "drained"
128
_NR_MASTER_CANDIDATE = "master-candidate"
129
_NR_MASTER = "master"
130
_NR_OFFLINE = "offline"
131
_NR_REGULAR = "regular"
132

    
133
_NR_MAP = {
134
  constants.NR_MASTER: _NR_MASTER,
135
  constants.NR_MCANDIDATE: _NR_MASTER_CANDIDATE,
136
  constants.NR_DRAINED: _NR_DRAINED,
137
  constants.NR_OFFLINE: _NR_OFFLINE,
138
  constants.NR_REGULAR: _NR_REGULAR,
139
  }
140

    
141
assert frozenset(_NR_MAP.keys()) == constants.NR_ALL
142

    
143
# Request data version field
144
_REQ_DATA_VERSION = "__version__"
145

    
146
# Feature string for instance creation request data version 1
147
_INST_CREATE_REQV1 = "instance-create-reqv1"
148

    
149
# Feature string for instance reinstall request version 1
150
_INST_REINSTALL_REQV1 = "instance-reinstall-reqv1"
151

    
152
# Feature string for node migration version 1
153
_NODE_MIGRATE_REQV1 = "node-migrate-reqv1"
154

    
155
# Feature string for node evacuation with LU-generated jobs
156
_NODE_EVAC_RES1 = "node-evac-res1"
157

    
158
ALL_FEATURES = compat.UniqueFrozenset([
159
  _INST_CREATE_REQV1,
160
  _INST_REINSTALL_REQV1,
161
  _NODE_MIGRATE_REQV1,
162
  _NODE_EVAC_RES1,
163
  ])
164

    
165
# Timeout for /2/jobs/[job_id]/wait. Gives job up to 10 seconds to change.
166
_WFJC_TIMEOUT = 10
167

    
168

    
169
# FIXME: For compatibility we update the beparams/memory field. Needs to be
170
#        removed in Ganeti 2.7
171
def _UpdateBeparams(inst):
172
  """Updates the beparams dict of inst to support the memory field.
173

174
  @param inst: Inst dict
175
  @return: Updated inst dict
176

177
  """
178
  beparams = inst["beparams"]
179
  beparams[constants.BE_MEMORY] = beparams[constants.BE_MAXMEM]
180

    
181
  return inst
182

    
183

    
184
class R_root(baserlib.ResourceBase):
185
  """/ resource.
186

187
  """
188
  @staticmethod
189
  def GET():
190
    """Supported for legacy reasons.
191

192
    """
193
    return None
194

    
195

    
196
class R_2(R_root):
197
  """/2 resource.
198

199
  """
200

    
201

    
202
class R_version(baserlib.ResourceBase):
203
  """/version resource.
204

205
  This resource should be used to determine the remote API version and
206
  to adapt clients accordingly.
207

208
  """
209
  @staticmethod
210
  def GET():
211
    """Returns the remote API version.
212

213
    """
214
    return constants.RAPI_VERSION
215

    
216

    
217
class R_2_info(baserlib.OpcodeResource):
218
  """/2/info resource.
219

220
  """
221
  GET_OPCODE = opcodes.OpClusterQuery
222

    
223
  def GET(self):
224
    """Returns cluster information.
225

226
    """
227
    client = self.GetClient(query=True)
228
    return client.QueryClusterInfo()
229

    
230

    
231
class R_2_features(baserlib.ResourceBase):
232
  """/2/features resource.
233

234
  """
235
  @staticmethod
236
  def GET():
237
    """Returns list of optional RAPI features implemented.
238

239
    """
240
    return list(ALL_FEATURES)
241

    
242

    
243
class R_2_os(baserlib.OpcodeResource):
244
  """/2/os resource.
245

246
  """
247
  GET_OPCODE = opcodes.OpOsDiagnose
248

    
249
  def GET(self):
250
    """Return a list of all OSes.
251

252
    Can return error 500 in case of a problem.
253

254
    Example: ["debian-etch"]
255

256
    """
257
    cl = self.GetClient()
258
    op = opcodes.OpOsDiagnose(output_fields=["name", "variants"], names=[])
259
    job_id = self.SubmitJob([op], cl=cl)
260
    # we use custom feedback function, instead of print we log the status
261
    result = cli.PollJob(job_id, cl, feedback_fn=baserlib.FeedbackFn)
262
    diagnose_data = result[0]
263

    
264
    if not isinstance(diagnose_data, list):
265
      raise http.HttpBadGateway(message="Can't get OS list")
266

    
267
    os_names = []
268
    for (name, variants) in diagnose_data:
269
      os_names.extend(cli.CalculateOSNames(name, variants))
270

    
271
    return os_names
272

    
273

    
274
class R_2_redist_config(baserlib.OpcodeResource):
275
  """/2/redistribute-config resource.
276

277
  """
278
  PUT_OPCODE = opcodes.OpClusterRedistConf
279

    
280

    
281
class R_2_cluster_modify(baserlib.OpcodeResource):
282
  """/2/modify resource.
283

284
  """
285
  PUT_OPCODE = opcodes.OpClusterSetParams
286

    
287

    
288
class R_2_jobs(baserlib.ResourceBase):
289
  """/2/jobs resource.
290

291
  """
292
  def GET(self):
293
    """Returns a dictionary of jobs.
294

295
    @return: a dictionary with jobs id and uri.
296

297
    """
298
    client = self.GetClient(query=True)
299

    
300
    if self.useBulk():
301
      bulkdata = client.QueryJobs(None, J_FIELDS_BULK)
302
      return baserlib.MapBulkFields(bulkdata, J_FIELDS_BULK)
303
    else:
304
      jobdata = map(compat.fst, client.QueryJobs(None, ["id"]))
305
      return baserlib.BuildUriList(jobdata, "/2/jobs/%s",
306
                                   uri_fields=("id", "uri"))
307

    
308

    
309
class R_2_jobs_id(baserlib.ResourceBase):
310
  """/2/jobs/[job_id] resource.
311

312
  """
313
  def GET(self):
314
    """Returns a job status.
315

316
    @return: a dictionary with job parameters.
317
        The result includes:
318
            - id: job ID as a number
319
            - status: current job status as a string
320
            - ops: involved OpCodes as a list of dictionaries for each
321
              opcodes in the job
322
            - opstatus: OpCodes status as a list
323
            - opresult: OpCodes results as a list of lists
324

325
    """
326
    job_id = self.items[0]
327
    result = self.GetClient(query=True).QueryJobs([job_id, ], J_FIELDS)[0]
328
    if result is None:
329
      raise http.HttpNotFound()
330
    return baserlib.MapFields(J_FIELDS, result)
331

    
332
  def DELETE(self):
333
    """Cancel not-yet-started job.
334

335
    """
336
    job_id = self.items[0]
337
    result = self.GetClient().CancelJob(job_id)
338
    return result
339

    
340

    
341
class R_2_jobs_id_wait(baserlib.ResourceBase):
342
  """/2/jobs/[job_id]/wait resource.
343

344
  """
345
  # WaitForJobChange provides access to sensitive information and blocks
346
  # machine resources (it's a blocking RAPI call), hence restricting access.
347
  GET_ACCESS = [rapi.RAPI_ACCESS_WRITE]
348

    
349
  def GET(self):
350
    """Waits for job changes.
351

352
    """
353
    job_id = self.items[0]
354

    
355
    fields = self.getBodyParameter("fields")
356
    prev_job_info = self.getBodyParameter("previous_job_info", None)
357
    prev_log_serial = self.getBodyParameter("previous_log_serial", None)
358

    
359
    if not isinstance(fields, list):
360
      raise http.HttpBadRequest("The 'fields' parameter should be a list")
361

    
362
    if not (prev_job_info is None or isinstance(prev_job_info, list)):
363
      raise http.HttpBadRequest("The 'previous_job_info' parameter should"
364
                                " be a list")
365

    
366
    if not (prev_log_serial is None or
367
            isinstance(prev_log_serial, (int, long))):
368
      raise http.HttpBadRequest("The 'previous_log_serial' parameter should"
369
                                " be a number")
370

    
371
    client = self.GetClient()
372
    result = client.WaitForJobChangeOnce(job_id, fields,
373
                                         prev_job_info, prev_log_serial,
374
                                         timeout=_WFJC_TIMEOUT)
375
    if not result:
376
      raise http.HttpNotFound()
377

    
378
    if result == constants.JOB_NOTCHANGED:
379
      # No changes
380
      return None
381

    
382
    (job_info, log_entries) = result
383

    
384
    return {
385
      "job_info": job_info,
386
      "log_entries": log_entries,
387
      }
388

    
389

    
390
class R_2_nodes(baserlib.OpcodeResource):
391
  """/2/nodes resource.
392

393
  """
394
  GET_OPCODE = opcodes.OpNodeQuery
395

    
396
  def GET(self):
397
    """Returns a list of all nodes.
398

399
    """
400
    client = self.GetClient(query=True)
401

    
402
    if self.useBulk():
403
      bulkdata = client.QueryNodes([], N_FIELDS, False)
404
      return baserlib.MapBulkFields(bulkdata, N_FIELDS)
405
    else:
406
      nodesdata = client.QueryNodes([], ["name"], False)
407
      nodeslist = [row[0] for row in nodesdata]
408
      return baserlib.BuildUriList(nodeslist, "/2/nodes/%s",
409
                                   uri_fields=("id", "uri"))
410

    
411

    
412
class R_2_nodes_name(baserlib.OpcodeResource):
413
  """/2/nodes/[node_name] resource.
414

415
  """
416
  GET_OPCODE = opcodes.OpNodeQuery
417

    
418
  def GET(self):
419
    """Send information about a node.
420

421
    """
422
    node_name = self.items[0]
423
    client = self.GetClient(query=True)
424

    
425
    result = baserlib.HandleItemQueryErrors(client.QueryNodes,
426
                                            names=[node_name], fields=N_FIELDS,
427
                                            use_locking=self.useLocking())
428

    
429
    return baserlib.MapFields(N_FIELDS, result[0])
430

    
431

    
432
class R_2_nodes_name_powercycle(baserlib.OpcodeResource):
433
  """/2/nodes/[node_name]/powercycle resource.
434

435
  """
436
  POST_OPCODE = opcodes.OpNodePowercycle
437

    
438
  def GetPostOpInput(self):
439
    """Tries to powercycle a node.
440

441
    """
442
    return (self.request_body, {
443
      "node_name": self.items[0],
444
      "force": self.useForce(),
445
      })
446

    
447

    
448
class R_2_nodes_name_role(baserlib.OpcodeResource):
449
  """/2/nodes/[node_name]/role resource.
450

451
  """
452
  PUT_OPCODE = opcodes.OpNodeSetParams
453

    
454
  def GET(self):
455
    """Returns the current node role.
456

457
    @return: Node role
458

459
    """
460
    node_name = self.items[0]
461
    client = self.GetClient(query=True)
462
    result = client.QueryNodes(names=[node_name], fields=["role"],
463
                               use_locking=self.useLocking())
464

    
465
    return _NR_MAP[result[0][0]]
466

    
467
  def GetPutOpInput(self):
468
    """Sets the node role.
469

470
    """
471
    baserlib.CheckType(self.request_body, basestring, "Body contents")
472

    
473
    role = self.request_body
474

    
475
    if role == _NR_REGULAR:
476
      candidate = False
477
      offline = False
478
      drained = False
479

    
480
    elif role == _NR_MASTER_CANDIDATE:
481
      candidate = True
482
      offline = drained = None
483

    
484
    elif role == _NR_DRAINED:
485
      drained = True
486
      candidate = offline = None
487

    
488
    elif role == _NR_OFFLINE:
489
      offline = True
490
      candidate = drained = None
491

    
492
    else:
493
      raise http.HttpBadRequest("Can't set '%s' role" % role)
494

    
495
    assert len(self.items) == 1
496

    
497
    return ({}, {
498
      "node_name": self.items[0],
499
      "master_candidate": candidate,
500
      "offline": offline,
501
      "drained": drained,
502
      "force": self.useForce(),
503
      "auto_promote": bool(self._checkIntVariable("auto-promote", default=0)),
504
      })
505

    
506

    
507
class R_2_nodes_name_evacuate(baserlib.OpcodeResource):
508
  """/2/nodes/[node_name]/evacuate resource.
509

510
  """
511
  POST_OPCODE = opcodes.OpNodeEvacuate
512

    
513
  def GetPostOpInput(self):
514
    """Evacuate all instances off a node.
515

516
    """
517
    return (self.request_body, {
518
      "node_name": self.items[0],
519
      "dry_run": self.dryRun(),
520
      })
521

    
522

    
523
class R_2_nodes_name_migrate(baserlib.OpcodeResource):
524
  """/2/nodes/[node_name]/migrate resource.
525

526
  """
527
  POST_OPCODE = opcodes.OpNodeMigrate
528

    
529
  def GetPostOpInput(self):
530
    """Migrate all primary instances from a node.
531

532
    """
533
    if self.queryargs:
534
      # Support old-style requests
535
      if "live" in self.queryargs and "mode" in self.queryargs:
536
        raise http.HttpBadRequest("Only one of 'live' and 'mode' should"
537
                                  " be passed")
538

    
539
      if "live" in self.queryargs:
540
        if self._checkIntVariable("live", default=1):
541
          mode = constants.HT_MIGRATION_LIVE
542
        else:
543
          mode = constants.HT_MIGRATION_NONLIVE
544
      else:
545
        mode = self._checkStringVariable("mode", default=None)
546

    
547
      data = {
548
        "mode": mode,
549
        }
550
    else:
551
      data = self.request_body
552

    
553
    return (data, {
554
      "node_name": self.items[0],
555
      })
556

    
557

    
558
class R_2_nodes_name_modify(baserlib.OpcodeResource):
559
  """/2/nodes/[node_name]/modify resource.
560

561
  """
562
  POST_OPCODE = opcodes.OpNodeSetParams
563

    
564
  def GetPostOpInput(self):
565
    """Changes parameters of a node.
566

567
    """
568
    assert len(self.items) == 1
569

    
570
    return (self.request_body, {
571
      "node_name": self.items[0],
572
      })
573

    
574

    
575
class R_2_nodes_name_storage(baserlib.OpcodeResource):
576
  """/2/nodes/[node_name]/storage resource.
577

578
  """
579
  # LUNodeQueryStorage acquires locks, hence restricting access to GET
580
  GET_ACCESS = [rapi.RAPI_ACCESS_WRITE]
581
  GET_OPCODE = opcodes.OpNodeQueryStorage
582

    
583
  def GetGetOpInput(self):
584
    """List storage available on a node.
585

586
    """
587
    storage_type = self._checkStringVariable("storage_type", None)
588
    output_fields = self._checkStringVariable("output_fields", None)
589

    
590
    if not output_fields:
591
      raise http.HttpBadRequest("Missing the required 'output_fields'"
592
                                " parameter")
593

    
594
    return ({}, {
595
      "nodes": [self.items[0]],
596
      "storage_type": storage_type,
597
      "output_fields": output_fields.split(","),
598
      })
599

    
600

    
601
class R_2_nodes_name_storage_modify(baserlib.OpcodeResource):
602
  """/2/nodes/[node_name]/storage/modify resource.
603

604
  """
605
  PUT_OPCODE = opcodes.OpNodeModifyStorage
606

    
607
  def GetPutOpInput(self):
608
    """Modifies a storage volume on a node.
609

610
    """
611
    storage_type = self._checkStringVariable("storage_type", None)
612
    name = self._checkStringVariable("name", None)
613

    
614
    if not name:
615
      raise http.HttpBadRequest("Missing the required 'name'"
616
                                " parameter")
617

    
618
    changes = {}
619

    
620
    if "allocatable" in self.queryargs:
621
      changes[constants.SF_ALLOCATABLE] = \
622
        bool(self._checkIntVariable("allocatable", default=1))
623

    
624
    return ({}, {
625
      "node_name": self.items[0],
626
      "storage_type": storage_type,
627
      "name": name,
628
      "changes": changes,
629
      })
630

    
631

    
632
class R_2_nodes_name_storage_repair(baserlib.OpcodeResource):
633
  """/2/nodes/[node_name]/storage/repair resource.
634

635
  """
636
  PUT_OPCODE = opcodes.OpRepairNodeStorage
637

    
638
  def GetPutOpInput(self):
639
    """Repairs a storage volume on a node.
640

641
    """
642
    storage_type = self._checkStringVariable("storage_type", None)
643
    name = self._checkStringVariable("name", None)
644
    if not name:
645
      raise http.HttpBadRequest("Missing the required 'name'"
646
                                " parameter")
647

    
648
    return ({}, {
649
      "node_name": self.items[0],
650
      "storage_type": storage_type,
651
      "name": name,
652
      })
653

    
654

    
655
class R_2_networks(baserlib.OpcodeResource):
656
  """/2/networks resource.
657

658
  """
659
  GET_OPCODE = opcodes.OpNetworkQuery
660
  POST_OPCODE = opcodes.OpNetworkAdd
661
  POST_RENAME = {
662
    "name": "network_name",
663
    }
664

    
665
  def GetPostOpInput(self):
666
    """Create a network.
667

668
    """
669
    assert not self.items
670
    return (self.request_body, {
671
      "dry_run": self.dryRun(),
672
      })
673

    
674
  def GET(self):
675
    """Returns a list of all networks.
676

677
    """
678
    client = self.GetClient(query=True)
679

    
680
    if self.useBulk():
681
      bulkdata = client.QueryNetworks([], NET_FIELDS, False)
682
      return baserlib.MapBulkFields(bulkdata, NET_FIELDS)
683
    else:
684
      data = client.QueryNetworks([], ["name"], False)
685
      networknames = [row[0] for row in data]
686
      return baserlib.BuildUriList(networknames, "/2/networks/%s",
687
                                   uri_fields=("name", "uri"))
688

    
689

    
690
class R_2_networks_name(baserlib.OpcodeResource):
691
  """/2/networks/[network_name] resource.
692

693
  """
694
  DELETE_OPCODE = opcodes.OpNetworkRemove
695

    
696
  def GET(self):
697
    """Send information about a network.
698

699
    """
700
    network_name = self.items[0]
701
    client = self.GetClient(query=True)
702

    
703
    result = baserlib.HandleItemQueryErrors(client.QueryNetworks,
704
                                            names=[network_name],
705
                                            fields=NET_FIELDS,
706
                                            use_locking=self.useLocking())
707

    
708
    return baserlib.MapFields(NET_FIELDS, result[0])
709

    
710
  def GetDeleteOpInput(self):
711
    """Delete a network.
712

713
    """
714
    assert len(self.items) == 1
715
    return (self.request_body, {
716
      "network_name": self.items[0],
717
      "dry_run": self.dryRun(),
718
      })
719

    
720

    
721
class R_2_networks_name_connect(baserlib.OpcodeResource):
722
  """/2/networks/[network_name]/connect resource.
723

724
  """
725
  PUT_OPCODE = opcodes.OpNetworkConnect
726

    
727
  def GetPutOpInput(self):
728
    """Changes some parameters of node group.
729

730
    """
731
    assert self.items
732
    return (self.request_body, {
733
      "network_name": self.items[0],
734
      "dry_run": self.dryRun(),
735
      })
736

    
737

    
738
class R_2_networks_name_disconnect(baserlib.OpcodeResource):
739
  """/2/networks/[network_name]/disconnect resource.
740

741
  """
742
  PUT_OPCODE = opcodes.OpNetworkDisconnect
743

    
744
  def GetPutOpInput(self):
745
    """Changes some parameters of node group.
746

747
    """
748
    assert self.items
749
    return (self.request_body, {
750
      "network_name": self.items[0],
751
      "dry_run": self.dryRun(),
752
      })
753

    
754

    
755
class R_2_networks_name_modify(baserlib.OpcodeResource):
756
  """/2/networks/[network_name]/modify resource.
757

758
  """
759
  PUT_OPCODE = opcodes.OpNetworkSetParams
760

    
761
  def GetPutOpInput(self):
762
    """Changes some parameters of network.
763

764
    """
765
    assert self.items
766
    return (self.request_body, {
767
      "network_name": self.items[0],
768
      })
769

    
770

    
771
class R_2_groups(baserlib.OpcodeResource):
772
  """/2/groups resource.
773

774
  """
775
  GET_OPCODE = opcodes.OpGroupQuery
776
  POST_OPCODE = opcodes.OpGroupAdd
777
  POST_RENAME = {
778
    "name": "group_name",
779
    }
780

    
781
  def GetPostOpInput(self):
782
    """Create a node group.
783

784

785
    """
786
    assert not self.items
787
    return (self.request_body, {
788
      "dry_run": self.dryRun(),
789
      })
790

    
791
  def GET(self):
792
    """Returns a list of all node groups.
793

794
    """
795
    client = self.GetClient(query=True)
796

    
797
    if self.useBulk():
798
      bulkdata = client.QueryGroups([], G_FIELDS, False)
799
      return baserlib.MapBulkFields(bulkdata, G_FIELDS)
800
    else:
801
      data = client.QueryGroups([], ["name"], False)
802
      groupnames = [row[0] for row in data]
803
      return baserlib.BuildUriList(groupnames, "/2/groups/%s",
804
                                   uri_fields=("name", "uri"))
805

    
806

    
807
class R_2_groups_name(baserlib.OpcodeResource):
808
  """/2/groups/[group_name] resource.
809

810
  """
811
  DELETE_OPCODE = opcodes.OpGroupRemove
812

    
813
  def GET(self):
814
    """Send information about a node group.
815

816
    """
817
    group_name = self.items[0]
818
    client = self.GetClient(query=True)
819

    
820
    result = baserlib.HandleItemQueryErrors(client.QueryGroups,
821
                                            names=[group_name], fields=G_FIELDS,
822
                                            use_locking=self.useLocking())
823

    
824
    return baserlib.MapFields(G_FIELDS, result[0])
825

    
826
  def GetDeleteOpInput(self):
827
    """Delete a node group.
828

829
    """
830
    assert len(self.items) == 1
831
    return ({}, {
832
      "group_name": self.items[0],
833
      "dry_run": self.dryRun(),
834
      })
835

    
836

    
837
class R_2_groups_name_modify(baserlib.OpcodeResource):
838
  """/2/groups/[group_name]/modify resource.
839

840
  """
841
  PUT_OPCODE = opcodes.OpGroupSetParams
842

    
843
  def GetPutOpInput(self):
844
    """Changes some parameters of node group.
845

846
    """
847
    assert self.items
848
    return (self.request_body, {
849
      "group_name": self.items[0],
850
      })
851

    
852

    
853
class R_2_groups_name_rename(baserlib.OpcodeResource):
854
  """/2/groups/[group_name]/rename resource.
855

856
  """
857
  PUT_OPCODE = opcodes.OpGroupRename
858

    
859
  def GetPutOpInput(self):
860
    """Changes the name of a node group.
861

862
    """
863
    assert len(self.items) == 1
864
    return (self.request_body, {
865
      "group_name": self.items[0],
866
      "dry_run": self.dryRun(),
867
      })
868

    
869

    
870
class R_2_groups_name_assign_nodes(baserlib.OpcodeResource):
871
  """/2/groups/[group_name]/assign-nodes resource.
872

873
  """
874
  PUT_OPCODE = opcodes.OpGroupAssignNodes
875

    
876
  def GetPutOpInput(self):
877
    """Assigns nodes to a group.
878

879
    """
880
    assert len(self.items) == 1
881
    return (self.request_body, {
882
      "group_name": self.items[0],
883
      "dry_run": self.dryRun(),
884
      "force": self.useForce(),
885
      })
886

    
887

    
888
class R_2_instances(baserlib.OpcodeResource):
889
  """/2/instances resource.
890

891
  """
892
  GET_OPCODE = opcodes.OpInstanceQuery
893
  POST_OPCODE = opcodes.OpInstanceCreate
894
  POST_RENAME = {
895
    "os": "os_type",
896
    "name": "instance_name",
897
    }
898

    
899
  def GET(self):
900
    """Returns a list of all available instances.
901

902
    """
903
    client = self.GetClient()
904

    
905
    use_locking = self.useLocking()
906
    if self.useBulk():
907
      bulkdata = client.QueryInstances([], I_FIELDS, use_locking)
908
      return map(_UpdateBeparams, baserlib.MapBulkFields(bulkdata, I_FIELDS))
909
    else:
910
      instancesdata = client.QueryInstances([], ["name"], use_locking)
911
      instanceslist = [row[0] for row in instancesdata]
912
      return baserlib.BuildUriList(instanceslist, "/2/instances/%s",
913
                                   uri_fields=("id", "uri"))
914

    
915
  def GetPostOpInput(self):
916
    """Create an instance.
917

918
    @return: a job id
919

920
    """
921
    baserlib.CheckType(self.request_body, dict, "Body contents")
922

    
923
    # Default to request data version 0
924
    data_version = self.getBodyParameter(_REQ_DATA_VERSION, 0)
925

    
926
    if data_version == 0:
927
      raise http.HttpBadRequest("Instance creation request version 0 is no"
928
                                " longer supported")
929
    elif data_version != 1:
930
      raise http.HttpBadRequest("Unsupported request data version %s" %
931
                                data_version)
932

    
933
    data = self.request_body.copy()
934
    # Remove "__version__"
935
    data.pop(_REQ_DATA_VERSION, None)
936

    
937
    return (data, {
938
      "dry_run": self.dryRun(),
939
      })
940

    
941

    
942
class R_2_instances_multi_alloc(baserlib.OpcodeResource):
943
  """/2/instances-multi-alloc resource.
944

945
  """
946
  POST_OPCODE = opcodes.OpInstanceMultiAlloc
947

    
948
  def GetPostOpInput(self):
949
    """Try to allocate multiple instances.
950

951
    @return: A dict with submitted jobs, allocatable instances and failed
952
             allocations
953

954
    """
955
    if "instances" not in self.request_body:
956
      raise http.HttpBadRequest("Request is missing required 'instances' field"
957
                                " in body")
958

    
959
    op_id = {
960
      "OP_ID": self.POST_OPCODE.OP_ID, # pylint: disable=E1101
961
      }
962
    body = objects.FillDict(self.request_body, {
963
      "instances": [objects.FillDict(inst, op_id)
964
                    for inst in self.request_body["instances"]],
965
      })
966

    
967
    return (body, {
968
      "dry_run": self.dryRun(),
969
      })
970

    
971

    
972
class R_2_instances_name(baserlib.OpcodeResource):
973
  """/2/instances/[instance_name] resource.
974

975
  """
976
  GET_OPCODE = opcodes.OpInstanceQuery
977
  DELETE_OPCODE = opcodes.OpInstanceRemove
978

    
979
  def GET(self):
980
    """Send information about an instance.
981

982
    """
983
    client = self.GetClient()
984
    instance_name = self.items[0]
985

    
986
    result = baserlib.HandleItemQueryErrors(client.QueryInstances,
987
                                            names=[instance_name],
988
                                            fields=I_FIELDS,
989
                                            use_locking=self.useLocking())
990

    
991
    return _UpdateBeparams(baserlib.MapFields(I_FIELDS, result[0]))
992

    
993
  def GetDeleteOpInput(self):
994
    """Delete an instance.
995

996
    """
997
    assert len(self.items) == 1
998
    return ({}, {
999
      "instance_name": self.items[0],
1000
      "ignore_failures": False,
1001
      "dry_run": self.dryRun(),
1002
      })
1003

    
1004

    
1005
class R_2_instances_name_info(baserlib.OpcodeResource):
1006
  """/2/instances/[instance_name]/info resource.
1007

1008
  """
1009
  GET_OPCODE = opcodes.OpInstanceQueryData
1010

    
1011
  def GetGetOpInput(self):
1012
    """Request detailed instance information.
1013

1014
    """
1015
    assert len(self.items) == 1
1016
    return ({}, {
1017
      "instances": [self.items[0]],
1018
      "static": bool(self._checkIntVariable("static", default=0)),
1019
      })
1020

    
1021

    
1022
class R_2_instances_name_reboot(baserlib.OpcodeResource):
1023
  """/2/instances/[instance_name]/reboot resource.
1024

1025
  Implements an instance reboot.
1026

1027
  """
1028
  POST_OPCODE = opcodes.OpInstanceReboot
1029

    
1030
  def GetPostOpInput(self):
1031
    """Reboot an instance.
1032

1033
    The URI takes type=[hard|soft|full] and
1034
    ignore_secondaries=[False|True] parameters.
1035

1036
    """
1037
    return ({}, {
1038
      "instance_name": self.items[0],
1039
      "reboot_type":
1040
        self.queryargs.get("type", [constants.INSTANCE_REBOOT_HARD])[0],
1041
      "ignore_secondaries": bool(self._checkIntVariable("ignore_secondaries")),
1042
      "dry_run": self.dryRun(),
1043
      "reason":
1044
        (constants.INSTANCE_REASON_SOURCE_RAPI,
1045
         self._checkStringVariable("reason_text",
1046
                                   default=constants.INSTANCE_REASON_REBOOT)),
1047
      })
1048

    
1049

    
1050
class R_2_instances_name_startup(baserlib.OpcodeResource):
1051
  """/2/instances/[instance_name]/startup resource.
1052

1053
  Implements an instance startup.
1054

1055
  """
1056
  PUT_OPCODE = opcodes.OpInstanceStartup
1057

    
1058
  def GetPutOpInput(self):
1059
    """Startup an instance.
1060

1061
    The URI takes force=[False|True] parameter to start the instance
1062
    if even if secondary disks are failing.
1063

1064
    """
1065
    return ({}, {
1066
      "instance_name": self.items[0],
1067
      "force": self.useForce(),
1068
      "dry_run": self.dryRun(),
1069
      "no_remember": bool(self._checkIntVariable("no_remember")),
1070
      })
1071

    
1072

    
1073
class R_2_instances_name_shutdown(baserlib.OpcodeResource):
1074
  """/2/instances/[instance_name]/shutdown resource.
1075

1076
  Implements an instance shutdown.
1077

1078
  """
1079
  PUT_OPCODE = opcodes.OpInstanceShutdown
1080

    
1081
  def GetPutOpInput(self):
1082
    """Shutdown an instance.
1083

1084
    """
1085
    return (self.request_body, {
1086
      "instance_name": self.items[0],
1087
      "no_remember": bool(self._checkIntVariable("no_remember")),
1088
      "dry_run": self.dryRun(),
1089
      })
1090

    
1091

    
1092
def _ParseInstanceReinstallRequest(name, data):
1093
  """Parses a request for reinstalling an instance.
1094

1095
  """
1096
  if not isinstance(data, dict):
1097
    raise http.HttpBadRequest("Invalid body contents, not a dictionary")
1098

    
1099
  ostype = baserlib.CheckParameter(data, "os", default=None)
1100
  start = baserlib.CheckParameter(data, "start", exptype=bool,
1101
                                  default=True)
1102
  osparams = baserlib.CheckParameter(data, "osparams", default=None)
1103

    
1104
  ops = [
1105
    opcodes.OpInstanceShutdown(instance_name=name),
1106
    opcodes.OpInstanceReinstall(instance_name=name, os_type=ostype,
1107
                                osparams=osparams),
1108
    ]
1109

    
1110
  if start:
1111
    ops.append(opcodes.OpInstanceStartup(instance_name=name, force=False))
1112

    
1113
  return ops
1114

    
1115

    
1116
class R_2_instances_name_reinstall(baserlib.OpcodeResource):
1117
  """/2/instances/[instance_name]/reinstall resource.
1118

1119
  Implements an instance reinstall.
1120

1121
  """
1122
  POST_OPCODE = opcodes.OpInstanceReinstall
1123

    
1124
  def POST(self):
1125
    """Reinstall an instance.
1126

1127
    The URI takes os=name and nostartup=[0|1] optional
1128
    parameters. By default, the instance will be started
1129
    automatically.
1130

1131
    """
1132
    if self.request_body:
1133
      if self.queryargs:
1134
        raise http.HttpBadRequest("Can't combine query and body parameters")
1135

    
1136
      body = self.request_body
1137
    elif self.queryargs:
1138
      # Legacy interface, do not modify/extend
1139
      body = {
1140
        "os": self._checkStringVariable("os"),
1141
        "start": not self._checkIntVariable("nostartup"),
1142
        }
1143
    else:
1144
      body = {}
1145

    
1146
    ops = _ParseInstanceReinstallRequest(self.items[0], body)
1147

    
1148
    return self.SubmitJob(ops)
1149

    
1150

    
1151
class R_2_instances_name_replace_disks(baserlib.OpcodeResource):
1152
  """/2/instances/[instance_name]/replace-disks resource.
1153

1154
  """
1155
  POST_OPCODE = opcodes.OpInstanceReplaceDisks
1156

    
1157
  def GetPostOpInput(self):
1158
    """Replaces disks on an instance.
1159

1160
    """
1161
    static = {
1162
      "instance_name": self.items[0],
1163
      }
1164

    
1165
    if self.request_body:
1166
      data = self.request_body
1167
    elif self.queryargs:
1168
      # Legacy interface, do not modify/extend
1169
      data = {
1170
        "remote_node": self._checkStringVariable("remote_node", default=None),
1171
        "mode": self._checkStringVariable("mode", default=None),
1172
        "disks": self._checkStringVariable("disks", default=None),
1173
        "iallocator": self._checkStringVariable("iallocator", default=None),
1174
        }
1175
    else:
1176
      data = {}
1177

    
1178
    # Parse disks
1179
    try:
1180
      raw_disks = data.pop("disks")
1181
    except KeyError:
1182
      pass
1183
    else:
1184
      if raw_disks:
1185
        if ht.TListOf(ht.TInt)(raw_disks): # pylint: disable=E1102
1186
          data["disks"] = raw_disks
1187
        else:
1188
          # Backwards compatibility for strings of the format "1, 2, 3"
1189
          try:
1190
            data["disks"] = [int(part) for part in raw_disks.split(",")]
1191
          except (TypeError, ValueError), err:
1192
            raise http.HttpBadRequest("Invalid disk index passed: %s" % err)
1193

    
1194
    return (data, static)
1195

    
1196

    
1197
class R_2_instances_name_activate_disks(baserlib.OpcodeResource):
1198
  """/2/instances/[instance_name]/activate-disks resource.
1199

1200
  """
1201
  PUT_OPCODE = opcodes.OpInstanceActivateDisks
1202

    
1203
  def GetPutOpInput(self):
1204
    """Activate disks for an instance.
1205

1206
    The URI might contain ignore_size to ignore current recorded size.
1207

1208
    """
1209
    return ({}, {
1210
      "instance_name": self.items[0],
1211
      "ignore_size": bool(self._checkIntVariable("ignore_size")),
1212
      })
1213

    
1214

    
1215
class R_2_instances_name_deactivate_disks(baserlib.OpcodeResource):
1216
  """/2/instances/[instance_name]/deactivate-disks resource.
1217

1218
  """
1219
  PUT_OPCODE = opcodes.OpInstanceDeactivateDisks
1220

    
1221
  def GetPutOpInput(self):
1222
    """Deactivate disks for an instance.
1223

1224
    """
1225
    return ({}, {
1226
      "instance_name": self.items[0],
1227
      })
1228

    
1229

    
1230
class R_2_instances_name_recreate_disks(baserlib.OpcodeResource):
1231
  """/2/instances/[instance_name]/recreate-disks resource.
1232

1233
  """
1234
  POST_OPCODE = opcodes.OpInstanceRecreateDisks
1235

    
1236
  def GetPostOpInput(self):
1237
    """Recreate disks for an instance.
1238

1239
    """
1240
    return ({}, {
1241
      "instance_name": self.items[0],
1242
      })
1243

    
1244

    
1245
class R_2_instances_name_prepare_export(baserlib.OpcodeResource):
1246
  """/2/instances/[instance_name]/prepare-export resource.
1247

1248
  """
1249
  PUT_OPCODE = opcodes.OpBackupPrepare
1250

    
1251
  def GetPutOpInput(self):
1252
    """Prepares an export for an instance.
1253

1254
    """
1255
    return ({}, {
1256
      "instance_name": self.items[0],
1257
      "mode": self._checkStringVariable("mode"),
1258
      })
1259

    
1260

    
1261
class R_2_instances_name_export(baserlib.OpcodeResource):
1262
  """/2/instances/[instance_name]/export resource.
1263

1264
  """
1265
  PUT_OPCODE = opcodes.OpBackupExport
1266
  PUT_RENAME = {
1267
    "destination": "target_node",
1268
    }
1269

    
1270
  def GetPutOpInput(self):
1271
    """Exports an instance.
1272

1273
    """
1274
    return (self.request_body, {
1275
      "instance_name": self.items[0],
1276
      })
1277

    
1278

    
1279
class R_2_instances_name_migrate(baserlib.OpcodeResource):
1280
  """/2/instances/[instance_name]/migrate resource.
1281

1282
  """
1283
  PUT_OPCODE = opcodes.OpInstanceMigrate
1284

    
1285
  def GetPutOpInput(self):
1286
    """Migrates an instance.
1287

1288
    """
1289
    return (self.request_body, {
1290
      "instance_name": self.items[0],
1291
      })
1292

    
1293

    
1294
class R_2_instances_name_failover(baserlib.OpcodeResource):
1295
  """/2/instances/[instance_name]/failover resource.
1296

1297
  """
1298
  PUT_OPCODE = opcodes.OpInstanceFailover
1299

    
1300
  def GetPutOpInput(self):
1301
    """Does a failover of an instance.
1302

1303
    """
1304
    return (self.request_body, {
1305
      "instance_name": self.items[0],
1306
      })
1307

    
1308

    
1309
class R_2_instances_name_rename(baserlib.OpcodeResource):
1310
  """/2/instances/[instance_name]/rename resource.
1311

1312
  """
1313
  PUT_OPCODE = opcodes.OpInstanceRename
1314

    
1315
  def GetPutOpInput(self):
1316
    """Changes the name of an instance.
1317

1318
    """
1319
    return (self.request_body, {
1320
      "instance_name": self.items[0],
1321
      })
1322

    
1323

    
1324
class R_2_instances_name_modify(baserlib.OpcodeResource):
1325
  """/2/instances/[instance_name]/modify resource.
1326

1327
  """
1328
  PUT_OPCODE = opcodes.OpInstanceSetParams
1329

    
1330
  def GetPutOpInput(self):
1331
    """Changes parameters of an instance.
1332

1333
    """
1334
    return (self.request_body, {
1335
      "instance_name": self.items[0],
1336
      })
1337

    
1338

    
1339
class R_2_instances_name_disk_grow(baserlib.OpcodeResource):
1340
  """/2/instances/[instance_name]/disk/[disk_index]/grow resource.
1341

1342
  """
1343
  POST_OPCODE = opcodes.OpInstanceGrowDisk
1344

    
1345
  def GetPostOpInput(self):
1346
    """Increases the size of an instance disk.
1347

1348
    """
1349
    return (self.request_body, {
1350
      "instance_name": self.items[0],
1351
      "disk": int(self.items[1]),
1352
      })
1353

    
1354

    
1355
class R_2_instances_name_console(baserlib.ResourceBase):
1356
  """/2/instances/[instance_name]/console resource.
1357

1358
  """
1359
  GET_ACCESS = [rapi.RAPI_ACCESS_WRITE, rapi.RAPI_ACCESS_READ]
1360
  GET_OPCODE = opcodes.OpInstanceConsole
1361

    
1362
  def GET(self):
1363
    """Request information for connecting to instance's console.
1364

1365
    @return: Serialized instance console description, see
1366
             L{objects.InstanceConsole}
1367

1368
    """
1369
    client = self.GetClient()
1370

    
1371
    ((console, ), ) = client.QueryInstances([self.items[0]], ["console"], False)
1372

    
1373
    if console is None:
1374
      raise http.HttpServiceUnavailable("Instance console unavailable")
1375

    
1376
    assert isinstance(console, dict)
1377
    return console
1378

    
1379

    
1380
def _GetQueryFields(args):
1381
  """Tries to extract C{fields} query parameter.
1382

1383
  @type args: dictionary
1384
  @rtype: list of string
1385
  @raise http.HttpBadRequest: When parameter can't be found
1386

1387
  """
1388
  try:
1389
    fields = args["fields"]
1390
  except KeyError:
1391
    raise http.HttpBadRequest("Missing 'fields' query argument")
1392

    
1393
  return _SplitQueryFields(fields[0])
1394

    
1395

    
1396
def _SplitQueryFields(fields):
1397
  """Splits fields as given for a query request.
1398

1399
  @type fields: string
1400
  @rtype: list of string
1401

1402
  """
1403
  return [i.strip() for i in fields.split(",")]
1404

    
1405

    
1406
class R_2_query(baserlib.ResourceBase):
1407
  """/2/query/[resource] resource.
1408

1409
  """
1410
  # Results might contain sensitive information
1411
  GET_ACCESS = [rapi.RAPI_ACCESS_WRITE, rapi.RAPI_ACCESS_READ]
1412
  PUT_ACCESS = GET_ACCESS
1413
  GET_OPCODE = opcodes.OpQuery
1414
  PUT_OPCODE = opcodes.OpQuery
1415

    
1416
  def _Query(self, fields, qfilter):
1417
    return self.GetClient().Query(self.items[0], fields, qfilter).ToDict()
1418

    
1419
  def GET(self):
1420
    """Returns resource information.
1421

1422
    @return: Query result, see L{objects.QueryResponse}
1423

1424
    """
1425
    return self._Query(_GetQueryFields(self.queryargs), None)
1426

    
1427
  def PUT(self):
1428
    """Submits job querying for resources.
1429

1430
    @return: Query result, see L{objects.QueryResponse}
1431

1432
    """
1433
    body = self.request_body
1434

    
1435
    baserlib.CheckType(body, dict, "Body contents")
1436

    
1437
    try:
1438
      fields = body["fields"]
1439
    except KeyError:
1440
      fields = _GetQueryFields(self.queryargs)
1441

    
1442
    qfilter = body.get("qfilter", None)
1443
    # TODO: remove this after 2.7
1444
    if qfilter is None:
1445
      qfilter = body.get("filter", None)
1446

    
1447
    return self._Query(fields, qfilter)
1448

    
1449

    
1450
class R_2_query_fields(baserlib.ResourceBase):
1451
  """/2/query/[resource]/fields resource.
1452

1453
  """
1454
  GET_OPCODE = opcodes.OpQueryFields
1455

    
1456
  def GET(self):
1457
    """Retrieves list of available fields for a resource.
1458

1459
    @return: List of serialized L{objects.QueryFieldDefinition}
1460

1461
    """
1462
    try:
1463
      raw_fields = self.queryargs["fields"]
1464
    except KeyError:
1465
      fields = None
1466
    else:
1467
      fields = _SplitQueryFields(raw_fields[0])
1468

    
1469
    return self.GetClient().QueryFields(self.items[0], fields).ToDict()
1470

    
1471

    
1472
class _R_Tags(baserlib.OpcodeResource):
1473
  """Quasiclass for tagging resources.
1474

1475
  Manages tags. When inheriting this class you must define the
1476
  TAG_LEVEL for it.
1477

1478
  """
1479
  TAG_LEVEL = None
1480
  GET_OPCODE = opcodes.OpTagsGet
1481
  PUT_OPCODE = opcodes.OpTagsSet
1482
  DELETE_OPCODE = opcodes.OpTagsDel
1483

    
1484
  def __init__(self, items, queryargs, req, **kwargs):
1485
    """A tag resource constructor.
1486

1487
    We have to override the default to sort out cluster naming case.
1488

1489
    """
1490
    baserlib.OpcodeResource.__init__(self, items, queryargs, req, **kwargs)
1491

    
1492
    if self.TAG_LEVEL == constants.TAG_CLUSTER:
1493
      self.name = None
1494
    else:
1495
      self.name = items[0]
1496

    
1497
  def GET(self):
1498
    """Returns a list of tags.
1499

1500
    Example: ["tag1", "tag2", "tag3"]
1501

1502
    """
1503
    kind = self.TAG_LEVEL
1504

    
1505
    if kind in (constants.TAG_INSTANCE,
1506
                constants.TAG_NODEGROUP,
1507
                constants.TAG_NODE):
1508
      if not self.name:
1509
        raise http.HttpBadRequest("Missing name on tag request")
1510

    
1511
      cl = self.GetClient(query=True)
1512
      tags = list(cl.QueryTags(kind, self.name))
1513

    
1514
    elif kind == constants.TAG_CLUSTER:
1515
      assert not self.name
1516
      # TODO: Use query API?
1517
      ssc = ssconf.SimpleStore()
1518
      tags = ssc.GetClusterTags()
1519

    
1520
    return list(tags)
1521

    
1522
  def GetPutOpInput(self):
1523
    """Add a set of tags.
1524

1525
    The request as a list of strings should be PUT to this URI. And
1526
    you'll have back a job id.
1527

1528
    """
1529
    return ({}, {
1530
      "kind": self.TAG_LEVEL,
1531
      "name": self.name,
1532
      "tags": self.queryargs.get("tag", []),
1533
      "dry_run": self.dryRun(),
1534
      })
1535

    
1536
  def GetDeleteOpInput(self):
1537
    """Delete a tag.
1538

1539
    In order to delete a set of tags, the DELETE
1540
    request should be addressed to URI like:
1541
    /tags?tag=[tag]&tag=[tag]
1542

1543
    """
1544
    # Re-use code
1545
    return self.GetPutOpInput()
1546

    
1547

    
1548
class R_2_instances_name_tags(_R_Tags):
1549
  """ /2/instances/[instance_name]/tags resource.
1550

1551
  Manages per-instance tags.
1552

1553
  """
1554
  TAG_LEVEL = constants.TAG_INSTANCE
1555

    
1556

    
1557
class R_2_nodes_name_tags(_R_Tags):
1558
  """ /2/nodes/[node_name]/tags resource.
1559

1560
  Manages per-node tags.
1561

1562
  """
1563
  TAG_LEVEL = constants.TAG_NODE
1564

    
1565

    
1566
class R_2_groups_name_tags(_R_Tags):
1567
  """ /2/groups/[group_name]/tags resource.
1568

1569
  Manages per-nodegroup tags.
1570

1571
  """
1572
  TAG_LEVEL = constants.TAG_NODEGROUP
1573

    
1574

    
1575
class R_2_networks_name_tags(_R_Tags):
1576
  """ /2/networks/[network_name]/tags resource.
1577

1578
  Manages per-network tags.
1579

1580
  """
1581
  TAG_LEVEL = constants.TAG_NETWORK
1582

    
1583

    
1584
class R_2_tags(_R_Tags):
1585
  """ /2/tags resource.
1586

1587
  Manages cluster tags.
1588

1589
  """
1590
  TAG_LEVEL = constants.TAG_CLUSTER