forked from norcams/himlarcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_global_state.py
executable file
·66 lines (51 loc) · 1.7 KB
/
test_global_state.py
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
#!/usr/bin/env python
from himlarcli import tests as tests
tests.is_virtual_env()
from himlarcli.parser import Parser
from himlarcli.printer import Printer
from himlarcli import utils
#from himlarcli.cinder import Cinder
from himlarcli.nova import Nova
#from himlarcli.neutron import Neutron
from himlarcli.keystone import Keystone
from himlarcli.global_state import GlobalState
from himlarcli.global_state import Instance
parser = Parser()
options = parser.parse_args()
printer = Printer(options.format)
kc = Keystone(options.config, debug=options.debug)
kc.set_domain(options.domain)
kc.set_dry_run(options.dry_run)
logger = kc.get_logger()
# Region
if hasattr(options, 'region'):
regions = kc.find_regions(region_name=options.region)
else:
regions = kc.find_regions()
""" save a dict to database """
def action_save():
# start db connection
state = utils.get_client(GlobalState, options, logger)
for region in regions:
instance_data = {}
instance_data['user_id'] = 'fake user id'
instance_data['name'] = 'instance name'
instance_data['region'] = region
instance_data['type'] = 'test'
instance = Instance.create(instance_data)
state.add(instance)
# close db connection
state.close()
""" Purge all tables in db """
def action_purge():
# start db connection
state = utils.get_client(GlobalState, options, logger)
for region in regions:
state.purge('instance')
# close db connection
state.close()
# Run local function with the same name as the action (Note: - => _)
action = locals().get('action_' + options.action.replace('-', '_'))
if not action:
utils.sys_error("Function action_%s() not implemented" % options.action)
action()