|
6 | 6 | :license: MIT, see LICENSE for more details. |
7 | 7 | """ |
8 | 8 |
|
| 9 | +from SoftLayer import exceptions |
9 | 10 | from SoftLayer import utils |
10 | 11 |
|
11 | 12 | IMAGE_MASK = ('id,accountId,name,globalIdentifier,blockDevices,parentId,' |
@@ -187,3 +188,52 @@ def export_image_to_uri(self, image_id, uri, ibm_api_key=None): |
187 | 188 | }, id=image_id) |
188 | 189 | else: |
189 | 190 | return self.vgbdtg.copyToExternalSource({'uri': uri}, id=image_id) |
| 191 | + |
| 192 | + def add_locations(self, image_id, location_names): |
| 193 | + """Add available locations to an archive image template. |
| 194 | +
|
| 195 | + :param int image_id: The ID of the image |
| 196 | + :param location_names: Locations for the Image. |
| 197 | + """ |
| 198 | + locations = self.get_locations_list(image_id, location_names) |
| 199 | + return self.vgbdtg.addLocations(locations, id=image_id) |
| 200 | + |
| 201 | + def remove_locations(self, image_id, location_names): |
| 202 | + """Remove available locations from an archive image template. |
| 203 | +
|
| 204 | + :param int image_id: The ID of the image |
| 205 | + :param location_names: Locations for the Image. |
| 206 | + """ |
| 207 | + locations = self.get_locations_list(image_id, location_names) |
| 208 | + return self.vgbdtg.removeLocations(locations, id=image_id) |
| 209 | + |
| 210 | + def get_storage_locations(self, image_id): |
| 211 | + """Get available locations for public image storage. |
| 212 | +
|
| 213 | + :param int image_id: The ID of the image |
| 214 | + """ |
| 215 | + return self.vgbdtg.getStorageLocations(id=image_id) |
| 216 | + |
| 217 | + def get_locations_list(self, image_id, location_names): |
| 218 | + """Converts a list of location names to a list of locations. |
| 219 | +
|
| 220 | + :param int image_id: The ID of the image. |
| 221 | + :param list location_names: A list of location names strings. |
| 222 | + :returns: A list of locations associated with the given location names in the image. |
| 223 | + """ |
| 224 | + locations = self.get_storage_locations(image_id) |
| 225 | + locations_ids = [] |
| 226 | + matching_location = {} |
| 227 | + output_error = "Location {} does not exist for available locations for image {}" |
| 228 | + |
| 229 | + for location_name in location_names: |
| 230 | + for location in locations: |
| 231 | + if location_name == location.get('name'): |
| 232 | + matching_location = location |
| 233 | + break |
| 234 | + if matching_location.get('id') is None: |
| 235 | + raise exceptions.SoftLayerError(output_error.format(location_name, image_id)) |
| 236 | + |
| 237 | + locations_ids.append(matching_location) |
| 238 | + |
| 239 | + return locations_ids |
0 commit comments