File: heat.py

package info (click to toggle)
python-os-collect-config 14.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 360 kB
  • sloc: python: 3,089; makefile: 19
file content (102 lines) | stat: -rw-r--r-- 3,936 bytes parent folder | download
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from heatclient import client as heatclient
from keystoneclient.v3 import client as keystoneclient
from oslo_config import cfg
from oslo_log import log

from os_collect_config import exc
from os_collect_config import keystone
from os_collect_config import merger

CONF = cfg.CONF
logger = log.getLogger(__name__)

opts = [
    cfg.StrOpt('user-id',
               help='User ID for API authentication'),
    cfg.StrOpt('password',
               help='Password for API authentication'),
    cfg.StrOpt('project-id',
               help='ID of project for API authentication'),
    cfg.StrOpt('auth-url',
               help='URL for API authentication'),
    cfg.StrOpt('stack-id',
               help='ID of the stack this deployment belongs to'),
    cfg.StrOpt('resource-name',
               help='Name of resource in the stack to be polled'),
    cfg.StrOpt('region-name',
               help='Region Name for extracting Heat endpoint'),
]
name = 'heat'


class Collector:
    def __init__(self,
                 keystoneclient=keystoneclient,
                 heatclient=heatclient,
                 discover_class=None):
        self.keystoneclient = keystoneclient
        self.heatclient = heatclient
        self.discover_class = discover_class

    def collect(self):
        if CONF.heat.auth_url is None:
            logger.info('No auth_url configured.')
            raise exc.HeatMetadataNotConfigured
        if CONF.heat.password is None:
            logger.info('No password configured.')
            raise exc.HeatMetadataNotConfigured
        if CONF.heat.project_id is None:
            logger.info('No project_id configured.')
            raise exc.HeatMetadataNotConfigured
        if CONF.heat.user_id is None:
            logger.info('No user_id configured.')
            raise exc.HeatMetadataNotConfigured
        if CONF.heat.stack_id is None:
            logger.info('No stack_id configured.')
            raise exc.HeatMetadataNotConfigured
        if CONF.heat.resource_name is None:
            logger.info('No resource_name configured.')
            raise exc.HeatMetadataNotConfigured
        # NOTE(flwang): To be compatible with old versions, we won't throw
        # error here if there is no region name.

        try:
            ks = keystone.Keystone(
                auth_url=CONF.heat.auth_url,
                user_id=CONF.heat.user_id,
                password=CONF.heat.password,
                project_id=CONF.heat.project_id,
                keystoneclient=self.keystoneclient,
                discover_class=self.discover_class).client
            kwargs = {'service_type': 'orchestration',
                      'endpoint_type': 'publicURL'}
            if CONF.heat.region_name:
                kwargs['region_name'] = CONF.heat.region_name
            endpoint = ks.service_catalog.url_for(**kwargs)
            logger.debug('Fetching metadata from %s' % endpoint)
            heat = self.heatclient.Client(
                '1', endpoint, token=ks.auth_token)
            r = heat.resources.metadata(CONF.heat.stack_id,
                                        CONF.heat.resource_name)

            final_list = merger.merged_list_from_content(
                r, cfg.CONF.deployment_key, name)
            return final_list

        except Exception as e:
            logger.warn(str(e))
            raise exc.HeatMetadataNotAvailable