summaryrefslogtreecommitdiffstats
path: root/deploy/post/glance.py
blob: 7ea4b51d29775b44a5015ba11cfd176212845f55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
##############################################################################
# Copyright (c) 2017 ZTE Coreporation and others.
# feng.xiaowei@zte.com.cn
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import os

import glanceclient

import keystoneauth


class Glance(keystoneauth.Keystoneauth):
    def __init__(self, version='2', openrc=None):
        super(Glance, self).__init__(openrc)
        self.client = glanceclient.Client(version, session=self.session)
        self.controller = self.client.images

    def create(self, name, path,
               disk_format="qcow2",
               container_format="bare",
               visibility="public"):
        if not os.path.isfile(path):
            raise Exception('Error: file {} not exist'.format(path))
        image = self.controller.create(name=name,
                                       visibility=visibility,
                                       disk_format=disk_format,
                                       container_format=container_format)
        id = image.id
        with open(path) as data:
            self.controller.upload(id, data)
        return id

    def get_by_name(self, name):
        for image in self.list():
            if image.name == name:
                return image.id

        return None

    def list(self):
        return self.controller.list()