CAL refactoring
[osm/SO.git] / rwcal / plugins / vala / rwcal_openstack / rift / rwcal / openstack / utils / image.py
1 #!/usr/bin/python
2
3 #
4 # Copyright 2017 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 import os
19 import gi
20
21 gi.require_version('RwcalYang', '1.0')
22 from gi.repository import RwcalYang
23
24
25
26 class ImageUtils(object):
27 """
28 Utility class for image operations
29 """
30 def __init__(self, driver):
31 """
32 Constructor for class
33 Arguments:
34 driver: object of OpenstackDriver()
35 """
36 self._driver = driver
37 self.log = driver.log
38
39 def make_image_args(self, image):
40 """
41 Function to create kwargs required for glance_image_create API
42
43 Arguments:
44 image: Protobuf GI object for RwcalYang.ImageInfoItem()
45
46 Returns:
47 A kwargs dictionary for glance operation
48 """
49 kwargs = dict()
50 kwargs['name'] = image.name
51 if image.disk_format:
52 kwargs['disk_format'] = image.disk_format
53 if image.container_format:
54 kwargs['container_format'] = image.container_format
55 return kwargs
56
57 def create_image_handle(self, image):
58 """
59 Function to create a image-file handle
60
61 Arguments:
62 image: Protobuf GI object for RwcalYang.ImageInfoItem()
63
64 Returns:
65 An object of _io.BufferedReader (file handle)
66 """
67 try:
68 if image.has_field("fileno"):
69 new_fileno = os.dup(image.fileno)
70 hdl = os.fdopen(new_fileno, 'rb')
71 else:
72 hdl = open(image.location, "rb")
73 except Exception as e:
74 self.log.exception("Could not open file for upload. Exception received: %s", str(e))
75 raise
76 return hdl
77
78 def parse_cloud_image_info(self, image_info):
79 """
80 Parse image_info dictionary (return by python-client) and put values in GI object for image
81
82 Arguments:
83 image_info : A dictionary object return by glanceclient library listing image attributes
84
85 Returns:
86 Protobuf GI Object of type RwcalYang.ImageInfoItem()
87 """
88 image = RwcalYang.ImageInfoItem()
89 if 'name' in image_info and image_info['name']:
90 image.name = image_info['name']
91 if 'id' in image_info and image_info['id']:
92 image.id = image_info['id']
93 if 'checksum' in image_info and image_info['checksum']:
94 image.checksum = image_info['checksum']
95 if 'disk_format' in image_info and image_info['disk_format']:
96 image.disk_format = image_info['disk_format']
97 if 'container_format' in image_info and image_info['container_format']:
98 image.container_format = image_info['container_format']
99
100 image.state = 'inactive'
101 if 'status' in image_info and image_info['status']:
102 if image_info['status'] == 'active':
103 image.state = 'active'
104
105 return image
106
107