Skip to content

Commit 31276df

Browse files
aiordacheulyssessouza
authored andcommitted
Make orchestrator field optional
Signed-off-by: aiordache <[email protected]>
1 parent 105efa0 commit 31276df

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

docker/context/api.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class ContextAPI(object):
1414
Contains methods for context management:
1515
create, list, remove, get, inspect.
1616
"""
17-
DEFAULT_CONTEXT = Context("default")
17+
DEFAULT_CONTEXT = Context("default", "swarm")
1818

1919
@classmethod
2020
def create_context(
21-
cls, name, orchestrator="swarm", host=None, tls_cfg=None,
21+
cls, name, orchestrator=None, host=None, tls_cfg=None,
2222
default_namespace=None, skip_tls_verify=False):
2323
"""Creates a new context.
2424
Returns:
@@ -38,9 +38,7 @@ def create_context(
3838
>>> print(ctx.Metadata)
3939
{
4040
"Name": "test",
41-
"Metadata": {
42-
"StackOrchestrator": "swarm"
43-
},
41+
"Metadata": {},
4442
"Endpoints": {
4543
"docker": {
4644
"Host": "unix:///var/run/docker.sock",
@@ -57,7 +55,9 @@ def create_context(
5755
ctx = Context.load_context(name)
5856
if ctx:
5957
raise errors.ContextAlreadyExists(name)
60-
endpoint = "docker" if orchestrator == "swarm" else orchestrator
58+
endpoint = "docker"
59+
if orchestrator and orchestrator != "swarm":
60+
endpoint = orchestrator
6161
ctx = Context(name, orchestrator)
6262
ctx.set_endpoint(
6363
endpoint, host, tls_cfg,
@@ -79,9 +79,7 @@ def get_context(cls, name=None):
7979
>>> print(ctx.Metadata)
8080
{
8181
"Name": "test",
82-
"Metadata": {
83-
"StackOrchestrator": "swarm"
84-
},
82+
"Metadata": {},
8583
"Endpoints": {
8684
"docker": {
8785
"Host": "unix:///var/run/docker.sock",

docker/context/context.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212
class Context:
1313
"""A context."""
14-
def __init__(self, name, orchestrator="swarm", host=None, endpoints=None,
14+
def __init__(self, name, orchestrator=None, host=None, endpoints=None,
1515
tls=False):
1616
if not name:
1717
raise Exception("Name not provided")
1818
self.name = name
1919
self.orchestrator = orchestrator
2020
if not endpoints:
2121
default_endpoint = "docker" if (
22-
orchestrator == "swarm"
22+
not orchestrator or orchestrator == "swarm"
2323
) else orchestrator
2424
self.endpoints = {
2525
default_endpoint: {
@@ -85,7 +85,8 @@ def _load_meta(cls, name):
8585
context {} : {}""".format(name, e))
8686

8787
return (
88-
metadata["Name"], metadata["Metadata"]["StackOrchestrator"],
88+
metadata["Name"],
89+
metadata["Metadata"].get("StackOrchestrator", None),
8990
metadata["Endpoints"])
9091
return None, None, None
9192

@@ -162,7 +163,7 @@ def Name(self):
162163

163164
@property
164165
def Host(self):
165-
if self.orchestrator == "swarm":
166+
if not self.orchestrator or self.orchestrator == "swarm":
166167
return self.endpoints["docker"]["Host"]
167168
return self.endpoints[self.orchestrator]["Host"]
168169

@@ -172,18 +173,19 @@ def Orchestrator(self):
172173

173174
@property
174175
def Metadata(self):
176+
meta = {}
177+
if self.orchestrator:
178+
meta = {"StackOrchestrator": self.orchestrator}
175179
return {
176180
"Name": self.name,
177-
"Metadata": {
178-
"StackOrchestrator": self.orchestrator
179-
},
181+
"Metadata": meta,
180182
"Endpoints": self.endpoints
181183
}
182184

183185
@property
184186
def TLSConfig(self):
185187
key = self.orchestrator
186-
if key == "swarm":
188+
if not key or key == "swarm":
187189
key = "docker"
188190
if key in self.tls_cfg.keys():
189191
return self.tls_cfg[key]

0 commit comments

Comments
 (0)