Added Placement parser.
[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 MACHINE_SCOPE = "#"
10
11 from .client import client
12
13 def parse(directive):
14 """
15 Given a string in the format `scope:directive`, or simply `scope`
16 or `directive`, return a Placement object suitable for passing
17 back over the websocket API.
18
19 """
20 if directive == "":
21 # Handle null case
22 return None
23
24 if type(directive) in [dict, client.Placement]:
25 # We've been handed something that we can simply hand back to
26 # the api. (Forwards compatibility)
27 return directive
28
29 if ":" in directive:
30 # Planner has given us a scope and directive in string form
31 scope, directive = directive.split(":")
32 return client.Placement(scope=scope, directive=directive)
33
34 if directive.isdigit():
35 # Planner has given us a machine id (we rely on juju core to
36 # verify its validity.)
37 return client.Placement(scope=MACHINE_SCOPE, directive=directive)
38
39 # Planner has probably given us a container type. Leave it up to
40 # juju core to verify that it is valid.
41 return client.Placement(scope=directive)