4 # Copyright 2017 RIFT.IO Inc
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 gi
.require_version('RwcalYang', '1.0')
22 from gi
.repository
import RwcalYang
26 class ImageUtils(object):
28 Utility class for image operations
30 def __init__(self
, driver
):
34 driver: object of OpenstackDriver()
39 def make_image_args(self
, image
):
41 Function to create kwargs required for glance_image_create API
44 image: Protobuf GI object for RwcalYang.ImageInfoItem()
47 A kwargs dictionary for glance operation
50 kwargs
['name'] = image
.name
52 kwargs
['disk_format'] = image
.disk_format
53 if image
.container_format
:
54 kwargs
['container_format'] = image
.container_format
57 def create_image_handle(self
, image
):
59 Function to create a image-file handle
62 image: Protobuf GI object for RwcalYang.ImageInfoItem()
65 An object of _io.BufferedReader (file handle)
68 if image
.has_field("fileno"):
69 new_fileno
= os
.dup(image
.fileno
)
70 hdl
= os
.fdopen(new_fileno
, 'rb')
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
))
78 def parse_cloud_image_info(self
, image_info
):
80 Parse image_info dictionary (return by python-client) and put values in GI object for image
83 image_info : A dictionary object return by glanceclient library listing image attributes
86 Protobuf GI Object of type RwcalYang.ImageInfoItem()
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']
100 image
.state
= 'inactive'
101 if 'status' in image_info
and image_info
['status']:
102 if image_info
['status'] == 'active':
103 image
.state
= 'active'