561bc40a9df0c0189aa694740590212653bba78a
[osm/N2VC.git] / juju / placement.py
1 #
2 # This module allows us to parse a machine placement directive into a
3 # Placement object suitable for passing through the websocket API.
4 #
5 # Once https://bugs.launchpad.net/juju/+bug/1645480 is addressed, this
6 # module should be deprecated.
7 #
8
9 from .client import client
10
11 MACHINE_SCOPE = "#"
12
13
14 def parse(directive):
15 """
16 Given a string in the format `scope:directive`, or simply `scope`
17 or `directive`, return a Placement object suitable for passing
18 back over the websocket API.
19
20 """
21 if not directive:
22 # Handle null case
23 return None
24
25 if type(directive) in [dict, client.Placement]:
26 # We've been handed something that we can simply hand back to
27 # the api. (Forwards compatibility)
28 return [directive]
29
30 # Juju 2.0 can't handle lxc containers.
31 directive = directive.replace('lxc', 'lxd')
32
33 if ":" in directive:
34 # Planner has given us a scope and directive in string form
35 scope, directive = directive.split(":")
36 return [client.Placement(scope=scope, directive=directive)]
37
38 if directive.isdigit():
39 # Planner has given us a machine id (we rely on juju core to
40 # verify its validity.)
41 return [client.Placement(scope=MACHINE_SCOPE, directive=directive)]
42
43 if "/" in directive:
44 machine, container, container_num = directive.split("/")
45 return [
46 client.Placement(scope=MACHINE_SCOPE, directive=machine),
47 client.Placement(scope=container, directive=container_num)
48 ]
49
50 # Planner has probably given us a container type. Leave it up to
51 # juju core to verify that it is valid.
52 return [client.Placement(scope=directive)]