Newer
Older
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from charmhelpers.core.hookenv import (
action_get,
action_fail,
action_set,
status_set,
log,
config,
)
from charms.reactive import (
clear_flag,
set_flag,
when,
when_not,
)
import charms.osm.ns
import json
import traceback
@when_not('ns.installed')
def install_ns():
set_flag('ns.installed')
status_set('active', 'Ready!')
@when('actions.add-user')
def action_add_user():
"""Add a user to the database."""
err = ''
output = ''
try:
username = action_get('username')
bw = action_get('bw')
qos = action_get('qos')
tariff = action_get('tariff')
# Get the configuration, which should contain the juju username and
# password. The endpoint and model will be discovered automatically
cfg = config()
client = charms.osm.ns.NetworkService(
user=cfg['juju-username'],
secret=cfg['juju-password'],
)
user_id = add_user(client, username, tariff)
if user_id > 0:
success = set_policy(client, user_id, bw, qos)
else:
log("user_id is 0; add_user failed.")
log("Output from charm: {}".format(output))
except Exception as err:
log(str(err))
log(str(traceback.format_exc()))
action_fail(str(err))
else:
action_set({
'user-id': user_id,
'policy-set': success,
})
finally:
clear_flag('actions.add-user')
def add_user(client, username, tariff):
"""Add a user to the database and return the id."""
cfg = config()
ns_config_info = json.loads(cfg['ns_config_info'])
log(ns_config_info)
application = ns_config_info['osm-config-mapping']['{}.{}.{}'.format(
'1', # member-vnf-index
'userVM', # vdu_id
'0', # vdu_count_index
)]
output = client.ExecutePrimitiveGetOutput(
# The name of the application for adding a user
application,
# The name of the action to call
"add-user",
# The parameter(s) required by the above charm and action
params={
'username': username,
'tariff': tariff,
},
# How long to wait (in seconds) for the action to finish
timeout=500
)
# Get the output from the `add-user` function
user_id = int(output['user-id'])
return user_id
def set_policy(client, user_id, bw, qos):
"""Set the policy for a user."""
success = False
cfg = config()
ns_config_info = json.loads(cfg['ns_config_info'])
application = ns_config_info['osm-config-mapping']['{}.{}.{}'.format(
'2', # member-vnf-index
'policyVM', # vdu_id
'0', # vdu_count_index
)]
success = client.ExecutePrimitiveGetOutput(
# The name of the application for policy management
application,
# The name of the action to call
"set-policy",
# The parameter(s) required by the above charm and action
params={
'user_id': user_id,
'bw': bw,
'qos': qos,
},
# How long to wait (in seconds) for the action to finish
timeout=500
)
# Get the output from the `add-user` function
return success