4 # Copyright (C) 2010 Google Inc.
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.
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.
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
22 """RAPI client utilities.
26 from ganeti import constants
27 from ganeti import cli
29 from ganeti.rapi import client
31 # Local constant to avoid importing ganeti.http
35 class RapiJobPollCb(cli.JobPollCbBase):
36 def __init__(self, cl):
37 """Initializes this class.
39 @param cl: RAPI client instance
42 cli.JobPollCbBase.__init__(self)
46 def WaitForJobChangeOnce(self, job_id, fields,
47 prev_job_info, prev_log_serial):
48 """Waits for changes on a job.
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:
61 return constants.JOB_NOTCHANGED
63 return (result["job_info"], result["log_entries"])
65 def QueryJobs(self, job_ids, fields):
66 """Returns the given fields for the selected job IDs.
68 @type job_ids: list of numbers
69 @param job_ids: Job IDs
70 @type fields: list of strings
75 raise NotImplementedError("Only one job supported at this time")
78 result = self.cl.GetJobStatus(job_ids[0])
79 except client.GanetiApiError, err:
80 if err.code == HTTP_NOT_FOUND:
85 return [[result[name] for name in fields], ]
88 def PollJob(rapi_client, job_id, reporter):
89 """Function to poll for the result of a job.
91 @param rapi_client: RAPI client instance
94 @type reporter: L{cli.JobPollReportCbBase}
95 @param reporter: PollJob reporter instance
98 return cli.GenericPollJob(job_id, RapiJobPollCb(rapi_client), reporter)