~ionutbalutoiu/charms/trusty/neutron-api/next

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/openstack/amulet/utils.py

  • Committer: James Page
  • Date: 2014-06-24 11:05:17 UTC
  • Revision ID: james.page@ubuntu.com-20140624110517-7chb3mwca2mtnmlp
Resync helpers, add standard targets to Makefile

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import logging
 
2
 
 
3
import glanceclient.v1.client as glance_client
 
4
import keystoneclient.v2_0 as keystone_client
 
5
import novaclient.v1_1.client as nova_client
 
6
 
 
7
from charmhelpers.contrib.amulet.utils import (
 
8
    AmuletUtils
 
9
)
 
10
 
 
11
DEBUG = logging.DEBUG
 
12
ERROR = logging.ERROR
 
13
 
 
14
 
 
15
class OpenStackAmuletUtils(AmuletUtils):
 
16
    """This class inherits from AmuletUtils and has additional support
 
17
       that is specifically for use by OpenStack charms."""
 
18
 
 
19
    def __init__(self, log_level=ERROR):
 
20
        """Initialize the deployment environment."""
 
21
        super(OpenStackAmuletUtils, self).__init__(log_level)
 
22
 
 
23
    def validate_endpoint_data(self, endpoints, admin_port, internal_port,
 
24
                               public_port, expected):
 
25
        """Validate actual endpoint data vs expected endpoint data. The ports
 
26
           are used to find the matching endpoint."""
 
27
        found = False
 
28
        for ep in endpoints:
 
29
            self.log.debug('endpoint: {}'.format(repr(ep)))
 
30
            if admin_port in ep.adminurl and internal_port in ep.internalurl \
 
31
               and public_port in ep.publicurl:
 
32
                found = True
 
33
                actual = {'id': ep.id,
 
34
                          'region': ep.region,
 
35
                          'adminurl': ep.adminurl,
 
36
                          'internalurl': ep.internalurl,
 
37
                          'publicurl': ep.publicurl,
 
38
                          'service_id': ep.service_id}
 
39
                ret = self._validate_dict_data(expected, actual)
 
40
                if ret:
 
41
                    return 'unexpected endpoint data - {}'.format(ret)
 
42
 
 
43
        if not found:
 
44
            return 'endpoint not found'
 
45
 
 
46
    def validate_svc_catalog_endpoint_data(self, expected, actual):
 
47
        """Validate a list of actual service catalog endpoints vs a list of
 
48
           expected service catalog endpoints."""
 
49
        self.log.debug('actual: {}'.format(repr(actual)))
 
50
        for k, v in expected.iteritems():
 
51
            if k in actual:
 
52
                ret = self._validate_dict_data(expected[k][0], actual[k][0])
 
53
                if ret:
 
54
                    return self.endpoint_error(k, ret)
 
55
            else:
 
56
                return "endpoint {} does not exist".format(k)
 
57
        return ret
 
58
 
 
59
    def validate_tenant_data(self, expected, actual):
 
60
        """Validate a list of actual tenant data vs list of expected tenant
 
61
           data."""
 
62
        self.log.debug('actual: {}'.format(repr(actual)))
 
63
        for e in expected:
 
64
            found = False
 
65
            for act in actual:
 
66
                a = {'enabled': act.enabled, 'description': act.description,
 
67
                     'name': act.name, 'id': act.id}
 
68
                if e['name'] == a['name']:
 
69
                    found = True
 
70
                    ret = self._validate_dict_data(e, a)
 
71
                    if ret:
 
72
                        return "unexpected tenant data - {}".format(ret)
 
73
            if not found:
 
74
                return "tenant {} does not exist".format(e.name)
 
75
        return ret
 
76
 
 
77
    def validate_role_data(self, expected, actual):
 
78
        """Validate a list of actual role data vs a list of expected role
 
79
           data."""
 
80
        self.log.debug('actual: {}'.format(repr(actual)))
 
81
        for e in expected:
 
82
            found = False
 
83
            for act in actual:
 
84
                a = {'name': act.name, 'id': act.id}
 
85
                if e['name'] == a['name']:
 
86
                    found = True
 
87
                    ret = self._validate_dict_data(e, a)
 
88
                    if ret:
 
89
                        return "unexpected role data - {}".format(ret)
 
90
            if not found:
 
91
                return "role {} does not exist".format(e.name)
 
92
        return ret
 
93
 
 
94
    def validate_user_data(self, expected, actual):
 
95
        """Validate a list of actual user data vs a list of expected user
 
96
           data."""
 
97
        self.log.debug('actual: {}'.format(repr(actual)))
 
98
        for e in expected:
 
99
            found = False
 
100
            for act in actual:
 
101
                a = {'enabled': act.enabled, 'name': act.name,
 
102
                     'email': act.email, 'tenantId': act.tenantId,
 
103
                     'id': act.id}
 
104
                if e['name'] == a['name']:
 
105
                    found = True
 
106
                    ret = self._validate_dict_data(e, a)
 
107
                    if ret:
 
108
                        return "unexpected user data - {}".format(ret)
 
109
            if not found:
 
110
                return "user {} does not exist".format(e.name)
 
111
        return ret
 
112
 
 
113
    def validate_flavor_data(self, expected, actual):
 
114
        """Validate a list of actual flavors vs a list of expected flavors."""
 
115
        self.log.debug('actual: {}'.format(repr(actual)))
 
116
        act = [a.name for a in actual]
 
117
        return self._validate_list_data(expected, act)
 
118
 
 
119
    def tenant_exists(self, keystone, tenant):
 
120
        """Return True if tenant exists"""
 
121
        return tenant in [t.name for t in keystone.tenants.list()]
 
122
 
 
123
    def authenticate_keystone_admin(self, keystone_sentry, user, password,
 
124
                                    tenant):
 
125
        """Authenticates admin user with the keystone admin endpoint."""
 
126
        service_ip = \
 
127
            keystone_sentry.relation('shared-db',
 
128
                                     'mysql:shared-db')['private-address']
 
129
        ep = "http://{}:35357/v2.0".format(service_ip.strip().decode('utf-8'))
 
130
        return keystone_client.Client(username=user, password=password,
 
131
                                      tenant_name=tenant, auth_url=ep)
 
132
 
 
133
    def authenticate_keystone_user(self, keystone, user, password, tenant):
 
134
        """Authenticates a regular user with the keystone public endpoint."""
 
135
        ep = keystone.service_catalog.url_for(service_type='identity',
 
136
                                              endpoint_type='publicURL')
 
137
        return keystone_client.Client(username=user, password=password,
 
138
                                      tenant_name=tenant, auth_url=ep)
 
139
 
 
140
    def authenticate_glance_admin(self, keystone):
 
141
        """Authenticates admin user with glance."""
 
142
        ep = keystone.service_catalog.url_for(service_type='image',
 
143
                                              endpoint_type='adminURL')
 
144
        return glance_client.Client(ep, token=keystone.auth_token)
 
145
 
 
146
    def authenticate_nova_user(self, keystone, user, password, tenant):
 
147
        """Authenticates a regular user with nova-api."""
 
148
        ep = keystone.service_catalog.url_for(service_type='identity',
 
149
                                              endpoint_type='publicURL')
 
150
        return nova_client.Client(username=user, api_key=password,
 
151
                                  project_id=tenant, auth_url=ep)