Merge ~pwlars/testflinger-cli:cli-cancel-job into testflinger-cli:master

Proposed by Paul Larson
Status: Merged
Approved by: Paul Larson
Approved revision: 169f966d86f7e4b330794be372365d795aa066c8
Merged at revision: 1e74c7a1ce6ff59de1c4aebee065138671d30295
Proposed branch: ~pwlars/testflinger-cli:cli-cancel-job
Merge into: testflinger-cli:master
Diff against target: 69 lines (+42/-1)
2 files modified
testflinger-cli (+28/-0)
testflinger_cli/__init__.py (+14/-1)
Reviewer Review Type Date Requested Status
Maciej Kisielewski (community) Approve
Review via email: mp+364179@code.launchpad.net

Description of the change

Once the recent changes proposed for testflinger-server land, we can safely set the job state to something else. This is one example of that, but it doesn't do much until the agent actually looks for it to happen and does something like stopping execution.

To post a comment you must log in.
Revision history for this message
Maciej Kisielewski (kissiel) wrote :

One question inline

review: Needs Information
Revision history for this message
Paul Larson (pwlars) wrote :

400 is bad request, and it was the closest match I could find. If you have a better one then I'm happy to change it, but it would also require a server-side change. The difference here is that the 204 would be sent if it doesn't find the job ID you specified. The 400 would be if you told it to look for something that doesn't even look like a valid job ID (ie. Not resembling a UUID)

Revision history for this message
Maciej Kisielewski (kissiel) wrote :

Right. I interpreted "Invalid job" in your message as of invalid value (similar to what you handle in 204) instead of 'type'. Makes more sense to me now, thanks.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/testflinger-cli b/testflinger-cli
2index 0f8b92c..7142e04 100755
3--- a/testflinger-cli
4+++ b/testflinger-cli
5@@ -67,6 +67,34 @@ def status(ctx, job_id):
6
7
8 @cli.command()
9+@click.argument('job_id', nargs=1)
10+@click.pass_context
11+def cancel(ctx, job_id):
12+ conn = ctx.obj['conn']
13+ try:
14+ job_state = conn.get_status(job_id)
15+ except testflinger_cli.HTTPError as e:
16+ if e.status == 204:
17+ print('Job {} not found. Check the job id to be sure it is '
18+ 'correct.'.format(job_id))
19+ elif e.status == 400:
20+ print('Invalid job id specified. Check the job id to be sure it '
21+ 'is correct.')
22+ if e.status == 404:
23+ print('Received 404 error from server. Are you sure this '
24+ 'is a testflinger server?')
25+ sys.exit(1)
26+ except Exception:
27+ print('Error communicating with server, check connection and retry')
28+ sys.exit(1)
29+ if job_state in ('complete', 'cancelled'):
30+ print('Job {} is already in {} state and cannot be cancelled.'.format(
31+ job_id, job_state))
32+ sys.exit(1)
33+ conn.post_job_state(job_id, 'cancelled')
34+
35+
36+@cli.command()
37 @click.argument('filename', nargs=1)
38 @click.option('--quiet', '-q', is_flag=True)
39 @click.pass_context
40diff --git a/testflinger_cli/__init__.py b/testflinger_cli/__init__.py
41index 96ff880..1866b63 100644
42--- a/testflinger_cli/__init__.py
43+++ b/testflinger_cli/__init__.py
44@@ -78,12 +78,25 @@ class Client():
45 ID for the test job
46 :return:
47 String containing the job_state for the specified ID
48- (waiting, setup, provision, test, complete)
49+ (waiting, setup, provision, test, reserved, released,
50+ cancelled, complete)
51 """
52 endpoint = '/v1/result/{}'.format(job_id)
53 data = json.loads(self.get(endpoint))
54 return data.get('job_state')
55
56+ def post_job_state(self, job_id, state):
57+ """Post the status of a test job
58+
59+ :param job_id:
60+ ID for the test job
61+ :param state:
62+ Job state to set for the specified job
63+ """
64+ endpoint = '/v1/result/{}'.format(job_id)
65+ data = dict(job_state=state)
66+ self.put(endpoint, data)
67+
68 def submit_job(self, job_data):
69 """Submit a test job to the testflinger server
70

Subscribers

People subscribed via source and target branches