Statistics
| Branch: | Tag: | Revision:

root / lib / rapi / client_utils.py @ bc826292

History | View | Annotate | Download (2.5 kB)

1
#
2
#
3

    
4
# Copyright (C) 2010 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
"""RAPI client utilities.
23

24
"""
25

    
26
from ganeti import constants
27
from ganeti import cli
28

    
29
from ganeti.rapi import client
30

    
31
# Local constant to avoid importing ganeti.http
32
HTTP_NOT_FOUND = 404
33

    
34

    
35
class RapiJobPollCb(cli.JobPollCbBase):
36
  def __init__(self, cl):
37
    """Initializes this class.
38

39
    @param cl: RAPI client instance
40

41
    """
42
    cli.JobPollCbBase.__init__(self)
43

    
44
    self.cl = cl
45

    
46
  def WaitForJobChangeOnce(self, job_id, fields,
47
                           prev_job_info, prev_log_serial):
48
    """Waits for changes on a job.
49

50
    """
51
    try:
52
      result = self.cl.WaitForJobChange(job_id, fields,
53
                                        prev_job_info, prev_log_serial)
54
    except client.GanetiApiError, err:
55
      if err.code == HTTP_NOT_FOUND:
56
        return None
57

    
58
      raise
59

    
60
    if result is None:
61
      return constants.JOB_NOTCHANGED
62

    
63
    return (result["job_info"], result["log_entries"])
64

    
65
  def QueryJobs(self, job_ids, fields):
66
    """Returns the given fields for the selected job IDs.
67

68
    @type job_ids: list of numbers
69
    @param job_ids: Job IDs
70
    @type fields: list of strings
71
    @param fields: Fields
72

73
    """
74
    if len(job_ids) != 1:
75
      raise NotImplementedError("Only one job supported at this time")
76

    
77
    try:
78
      result = self.cl.GetJobStatus(job_ids[0])
79
    except client.GanetiApiError, err:
80
      if err.code == HTTP_NOT_FOUND:
81
        return [None]
82

    
83
      raise
84

    
85
    return [[result[name] for name in fields], ]
86

    
87

    
88
def PollJob(rapi_client, job_id, reporter):
89
  """Function to poll for the result of a job.
90

91
  @param rapi_client: RAPI client instance
92
  @type job_id: number
93
  @param job_id: Job ID
94
  @type reporter: L{cli.JobPollReportCbBase}
95
  @param reporter: PollJob reporter instance
96

97
  """
98
  return cli.GenericPollJob(job_id, RapiJobPollCb(rapi_client), reporter)