~robert-ayres/charms/trusty/contrail-configuration/sslv3

« back to all changes in this revision

Viewing changes to hooks/contrail_configuration_hooks.py

  • Committer: Robert Ayres
  • Date: 2015-05-01 07:33:32 UTC
  • mfrom: (16.1.2 contrail-configuration)
  • Revision ID: robert.ayres@canonical.com-20150501073332-s1u8j1c39yny4ccs
Merge trunk changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
import sys
7
7
 
8
8
import json
 
9
import yaml
9
10
 
10
11
from charmhelpers.contrib.openstack.utils import configure_installation_source
11
12
 
36
37
from contrail_configuration_utils import (
37
38
    api_port,
38
39
    check_url,
 
40
    contrail_floating_ip_create,
 
41
    contrail_floating_ip_deactivate,
 
42
    contrail_floating_ip_delete,
 
43
    contrail_floating_ip_use,
39
44
    fix_permissions,
40
45
    provision_metadata,
41
46
    units,
129
134
 
130
135
@hooks.hook("config-changed")
131
136
def config_changed():
132
 
    pass
 
137
    configure_floating_ip_pools()
133
138
 
134
139
def config_get(key):
135
140
    try:
137
142
    except KeyError:
138
143
        return None
139
144
 
 
145
def configure_floating_ip_pools():
 
146
    floating_pools = config.get("floating-ip-pools")
 
147
    previous_floating_pools = config_get("floating-ip-pools-prev")
 
148
    if floating_pools != previous_floating_pools:
 
149
        pools = { (pool["project"],
 
150
                   pool["network"],
 
151
                   pool["pool-name"]): set(pool["target-projects"])
 
152
                  for pool in yaml.safe_load(floating_pools) } \
 
153
                if floating_pools else {}
 
154
        previous_pools = {}
 
155
        if previous_floating_pools:
 
156
            for pool in yaml.safe_load(previous_floating_pools):
 
157
                projects = pool["target-projects"]
 
158
                name = (pool["project"], pool["network"], pool["pool-name"])
 
159
                if name in pools:
 
160
                    previous_pools[name] = set(projects)
 
161
                else:
 
162
                    floating_ip_pool_delete(name, projects)
 
163
        for name, projects in pools.iteritems():
 
164
            if name not in previous_pools:
 
165
                floating_ip_pool_create(name, projects)
 
166
            else:
 
167
                floating_ip_pool_update(name, projects, previous_pools[name])
 
168
 
 
169
        config["floating-ip-pools-prev"] = floating_pools
 
170
 
140
171
@hooks.hook("contrail-api-relation-joined")
141
172
def contrail_api_joined():
142
173
    if config_get("contrail-api-configured"):
171
202
    write_ifmap_config()
172
203
    service_restart("ifmap-server")
173
204
 
 
205
def floating_ip_pool_create(name, projects):
 
206
    fq_network = "default-domain:" + ":".join(name[:2])
 
207
    contrail_floating_ip_create(fq_network, name[2])
 
208
 
 
209
    fq_pool_name = "default-domain:" + ":".join(name)
 
210
    for project in projects:
 
211
        fq_project = "default-domain:" + project
 
212
        contrail_floating_ip_use(fq_project, fq_pool_name)
 
213
 
 
214
def floating_ip_pool_delete(name, projects):
 
215
    fq_pool_name = "default-domain:" + ":".join(name)
 
216
    for project in projects:
 
217
        fq_project = "default-domain:" + project
 
218
        contrail_floating_ip_deactivate(fq_project, fq_pool_name)
 
219
 
 
220
    fq_network = "default-domain:" + ":".join(name[:2])
 
221
    contrail_floating_ip_delete(fq_network, name[2])
 
222
 
 
223
def floating_ip_pool_update(name, projects, previous_projects):
 
224
    fq_pool_name = "default-domain:" + ":".join(name)
 
225
    for project in (previous_projects - projects):
 
226
        fq_project = "default-domain:" + project
 
227
        contrail_floating_ip_deactivate(fq_project, fq_pool_name)
 
228
    for project in (projects - previous_projects):
 
229
        fq_project = "default-domain:" + project
 
230
        contrail_floating_ip_use(fq_project, fq_pool_name)
 
231
 
174
232
@hooks.hook("identity-admin-relation-changed")
175
233
def identity_admin_changed():
176
234
    if not relation_get("service_hostname"):