Native charm support
[osm/N2VC.git] / modules / libjuju / 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 isinstance(directive, (list, tuple)):
26 results = []
27 for d in directive:
28 results.extend(parse(d))
29 return results
30
31 if isinstance(directive, (dict, client.Placement)):
32 # We've been handed something that we can simply hand back to
33 # the api. (Forwards compatibility)
34 return [directive]
35
36 # Juju 2.0 can't handle lxc containers.
37 directive = directive.replace('lxc', 'lxd')
38
39 if ":" in directive:
40 # Planner has given us a scope and directive in string form
41 scope, directive = directive.split(":")
42 return [client.Placement(scope=scope, directive=directive)]
43
44 if directive.isdigit():
45 # Planner has given us a machine id (we rely on juju core to
46 # verify its validity.)
47 return [client.Placement(scope=MACHINE_SCOPE, directive=directive)]
48
49 if "/" in directive:
50 # e.g. "0/lxd/0"
51 # https://github.com/juju/juju/blob/master/instance/placement_test.go#L29
52 return [
53 client.Placement(scope=MACHINE_SCOPE, directive=directive),
54 ]
55
56 # Planner has probably given us a container type. Leave it up to
57 # juju core to verify that it is valid.
58 return [client.Placement(scope=directive)]