Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame^] | 1 | from storm.common import rest_client |
| 2 | import json |
| 3 | import time |
| 4 | |
| 5 | |
| 6 | class ImagesClient(object): |
| 7 | |
| 8 | def __init__(self, username, key, auth_url, tenant_name=None): |
| 9 | self.client = rest_client.RestClient(username, key, |
| 10 | auth_url, tenant_name) |
| 11 | self.headers = {'Content-Type': 'application/json', |
| 12 | 'Accept': 'application/json'} |
| 13 | |
| 14 | def create_image(self, server_id, name, meta=None): |
| 15 | """Creates an image of the original server""" |
| 16 | |
| 17 | post_body = { |
| 18 | 'createImage': { |
| 19 | 'name': name, |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | if meta != None: |
| 24 | post_body['metadata'] = meta |
| 25 | |
| 26 | post_body = json.dumps(post_body) |
| 27 | resp, body = self.client.post('servers/%s/action' % |
| 28 | str(server_id), post_body, self.headers) |
| 29 | body = json.loads(body) |
| 30 | return resp, body |
| 31 | |
| 32 | def list_images(self, params=None): |
| 33 | """Returns a list of all images filtered by any parameters""" |
| 34 | url = 'images' |
| 35 | if params != None: |
| 36 | param_list = [] |
| 37 | for param, value in params.iteritems(): |
| 38 | param_list.append("%s=%s&" % (param, value)) |
| 39 | |
| 40 | url = "images?" + "".join(param_list) |
| 41 | |
| 42 | resp, body = self.client.get(url) |
| 43 | body = json.loads(body) |
| 44 | return resp, body |
| 45 | |
| 46 | def list_images_with_detail(self, params=None): |
| 47 | """Returns a detailed list of images filtered by any parameters""" |
| 48 | url = 'images/detail' |
| 49 | if params != None: |
| 50 | param_list = [] |
| 51 | for param, value in params.iteritems(): |
| 52 | param_list.append("%s=%s&" % (param, value)) |
| 53 | |
| 54 | url = "images/detail?" + "".join(param_list) |
| 55 | |
| 56 | resp, body = self.client.get(url) |
| 57 | body = json.loads(body) |
| 58 | return resp, body |
| 59 | |
| 60 | def get_image(self, image_id): |
| 61 | """Returns the details of a single image""" |
| 62 | resp, body = self.client.get("images/%s" % str(image_id)) |
| 63 | body = json.loads(body) |
| 64 | return resp, body['image'] |
| 65 | |
| 66 | def delete_image(self, image_id): |
| 67 | """Deletes the provided image""" |
| 68 | return self.client.delete("images/%s" % str(image_id)) |
| 69 | |
| 70 | def wait_for_image_status(self, image_id, status): |
| 71 | """Waits for an image to reach a given status""" |
| 72 | resp, body = self.get_image(image_id) |
| 73 | image_status = body['image']['status'] |
| 74 | start = int(time.time()) |
| 75 | |
| 76 | while image_status != status: |
| 77 | time.sleep(self.build_interval) |
| 78 | resp, body = self.get_image(image_id) |
| 79 | image_status = body['image']['status'] |
| 80 | |
| 81 | if image_status == 'ERROR': |
| 82 | raise exceptions.TimeoutException |
| 83 | |
| 84 | if int(time.time()) - start >= self.build_timeout: |
| 85 | raise exceptions.BuildErrorException |