Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 1 | from storm.common import rest_client |
| 2 | import json |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 3 | import storm.config |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 4 | import time |
| 5 | |
| 6 | |
| 7 | class ImagesClient(object): |
| 8 | |
| 9 | def __init__(self, username, key, auth_url, tenant_name=None): |
| 10 | self.client = rest_client.RestClient(username, key, |
| 11 | auth_url, tenant_name) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 12 | self.config = storm.config.StormConfig() |
| 13 | self.build_interval = self.config.nova.build_interval |
| 14 | self.build_timeout = self.config.nova.build_timeout |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 15 | self.headers = {'Content-Type': 'application/json', |
| 16 | 'Accept': 'application/json'} |
| 17 | |
| 18 | def create_image(self, server_id, name, meta=None): |
| 19 | """Creates an image of the original server""" |
| 20 | |
| 21 | post_body = { |
| 22 | 'createImage': { |
| 23 | 'name': name, |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | if meta != None: |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 28 | post_body['createImage']['metadata'] = meta |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 29 | |
| 30 | post_body = json.dumps(post_body) |
| 31 | resp, body = self.client.post('servers/%s/action' % |
| 32 | str(server_id), post_body, self.headers) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 33 | return resp, body |
| 34 | |
| 35 | def list_images(self, params=None): |
| 36 | """Returns a list of all images filtered by any parameters""" |
| 37 | url = 'images' |
| 38 | if params != None: |
| 39 | param_list = [] |
| 40 | for param, value in params.iteritems(): |
| 41 | param_list.append("%s=%s&" % (param, value)) |
| 42 | |
| 43 | url = "images?" + "".join(param_list) |
| 44 | |
| 45 | resp, body = self.client.get(url) |
| 46 | body = json.loads(body) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 47 | return resp, body['images'] |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 48 | |
| 49 | def list_images_with_detail(self, params=None): |
| 50 | """Returns a detailed list of images filtered by any parameters""" |
| 51 | url = 'images/detail' |
| 52 | if params != None: |
| 53 | param_list = [] |
| 54 | for param, value in params.iteritems(): |
| 55 | param_list.append("%s=%s&" % (param, value)) |
| 56 | |
| 57 | url = "images/detail?" + "".join(param_list) |
| 58 | |
| 59 | resp, body = self.client.get(url) |
| 60 | body = json.loads(body) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 61 | return resp, body['images'] |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 62 | |
| 63 | def get_image(self, image_id): |
| 64 | """Returns the details of a single image""" |
| 65 | resp, body = self.client.get("images/%s" % str(image_id)) |
| 66 | body = json.loads(body) |
| 67 | return resp, body['image'] |
| 68 | |
| 69 | def delete_image(self, image_id): |
| 70 | """Deletes the provided image""" |
| 71 | return self.client.delete("images/%s" % str(image_id)) |
| 72 | |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 73 | def wait_for_image_exists(self, image_id): |
| 74 | resp, body = self.client.get("images/%s" % str(image_id)) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 75 | start = int(time.time()) |
| 76 | |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 77 | while resp.status != 200: |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 78 | time.sleep(self.build_interval) |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 79 | resp, body = self.client.get("images/%s" % str(image_id)) |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 80 | |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 81 | if int(time.time()) - start >= self.build_timeout: |
| 82 | raise exceptions.BuildErrorException |
| 83 | |
| 84 | def wait_for_image_status(self, image_id, status): |
| 85 | """Waits for an image to reach a given status.""" |
| 86 | resp, image = self.get_image(image_id) |
| 87 | start = int(time.time()) |
| 88 | |
| 89 | while image['status'] != status: |
| 90 | time.sleep(self.build_interval) |
| 91 | resp, image = self.get_image(image_id) |
| 92 | |
| 93 | if image['status'] == 'ERROR': |
Daryl Walleck | e5b83d4 | 2011-11-10 14:39:02 -0600 | [diff] [blame] | 94 | raise exceptions.TimeoutException |
| 95 | |
| 96 | if int(time.time()) - start >= self.build_timeout: |
| 97 | raise exceptions.BuildErrorException |
Daryl Walleck | 73a9e7a | 2011-11-15 17:43:31 -0600 | [diff] [blame] | 98 | |
| 99 | def list_image_metadata(self, image_id): |
| 100 | resp, body = self.client.get("images/%s/metadata" % str(image_id)) |
| 101 | body = json.loads(body) |
| 102 | return resp, body |
| 103 | |
| 104 | def set_image_metadata(self, image_id, meta): |
| 105 | post_body = json.dumps({'metadata': meta}) |
| 106 | resp, body = self.client.put('images/%s/metadata' % |
| 107 | str(image_id), post_body, self.headers) |
| 108 | body = json.loads(body) |
| 109 | return resp, body |
| 110 | |
| 111 | def update_image_metadata(self, image_id, meta): |
| 112 | post_body = json.dumps({'metadata': meta}) |
| 113 | resp, body = self.client.post('images/%s/metadata' % |
| 114 | str(image_id), post_body, self.headers) |
| 115 | body = json.loads(body) |
| 116 | return resp, body |
| 117 | |
| 118 | def get_image_metadata_item(self, image_id, key): |
| 119 | resp, body = self.client.get("images/%s/metadata/%s" % |
| 120 | (str(image_id), key)) |
| 121 | body = json.loads(body) |
| 122 | return resp, body |
| 123 | |
| 124 | def set_image_metadata_item(self, image_id, key, meta): |
| 125 | post_body = json.dumps({'meta': meta}) |
| 126 | resp, body = self.client.put('images/%s/metdata/%s' % |
| 127 | (str(image_id), key), |
| 128 | post_body, self.headers) |
| 129 | body = json.loads(body) |
| 130 | return resp, body |
| 131 | |
| 132 | def delete_image_metadata_item(self, image_id, key): |
| 133 | resp, body = self.client.delete("images/%s/metadata/%s" % |
| 134 | (str(image_id), key)) |
| 135 | return resp, body |