Skip to content

Commit

Permalink
Merge pull request #2578 from aiordache/fix_context_load_without_orch
Browse files Browse the repository at this point in the history
Make context orchestrator field optional
  • Loading branch information
ulyssessouza committed Jun 2, 2020
2 parents 71adba9 + 7133916 commit a527086
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
16 changes: 7 additions & 9 deletions docker/context/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class ContextAPI(object):
Contains methods for context management:
create, list, remove, get, inspect.
"""
DEFAULT_CONTEXT = Context("default")
DEFAULT_CONTEXT = Context("default", "swarm")

@classmethod
def create_context(
cls, name, orchestrator="swarm", host=None, tls_cfg=None,
cls, name, orchestrator=None, host=None, tls_cfg=None,
default_namespace=None, skip_tls_verify=False):
"""Creates a new context.
Returns:
Expand All @@ -38,9 +38,7 @@ def create_context(
>>> print(ctx.Metadata)
{
"Name": "test",
"Metadata": {
"StackOrchestrator": "swarm"
},
"Metadata": {},
"Endpoints": {
"docker": {
"Host": "unix:///var/run/docker.sock",
Expand All @@ -57,7 +55,9 @@ def create_context(
ctx = Context.load_context(name)
if ctx:
raise errors.ContextAlreadyExists(name)
endpoint = "docker" if orchestrator == "swarm" else orchestrator
endpoint = "docker"
if orchestrator and orchestrator != "swarm":
endpoint = orchestrator
ctx = Context(name, orchestrator)
ctx.set_endpoint(
endpoint, host, tls_cfg,
Expand All @@ -79,9 +79,7 @@ def get_context(cls, name=None):
>>> print(ctx.Metadata)
{
"Name": "test",
"Metadata": {
"StackOrchestrator": "swarm"
},
"Metadata": {},
"Endpoints": {
"docker": {
"Host": "unix:///var/run/docker.sock",
Expand Down
18 changes: 10 additions & 8 deletions docker/context/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

class Context:
"""A context."""
def __init__(self, name, orchestrator="swarm", host=None, endpoints=None,
def __init__(self, name, orchestrator=None, host=None, endpoints=None,
tls=False):
if not name:
raise Exception("Name not provided")
self.name = name
self.orchestrator = orchestrator
if not endpoints:
default_endpoint = "docker" if (
orchestrator == "swarm"
not orchestrator or orchestrator == "swarm"
) else orchestrator
self.endpoints = {
default_endpoint: {
Expand Down Expand Up @@ -85,7 +85,8 @@ def _load_meta(cls, name):
context {} : {}""".format(name, e))

return (
metadata["Name"], metadata["Metadata"]["StackOrchestrator"],
metadata["Name"],
metadata["Metadata"].get("StackOrchestrator", None),
metadata["Endpoints"])
return None, None, None

Expand Down Expand Up @@ -162,7 +163,7 @@ def Name(self):

@property
def Host(self):
if self.orchestrator == "swarm":
if not self.orchestrator or self.orchestrator == "swarm":
return self.endpoints["docker"]["Host"]
return self.endpoints[self.orchestrator]["Host"]

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

@property
def Metadata(self):
meta = {}
if self.orchestrator:
meta = {"StackOrchestrator": self.orchestrator}
return {
"Name": self.name,
"Metadata": {
"StackOrchestrator": self.orchestrator
},
"Metadata": meta,
"Endpoints": self.endpoints
}

@property
def TLSConfig(self):
key = self.orchestrator
if key == "swarm":
if not key or key == "swarm":
key = "docker"
if key in self.tls_cfg.keys():
return self.tls_cfg[key]
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/context_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ def test_context_remove(self):
ContextAPI.remove_context("test")
with pytest.raises(errors.ContextNotFound):
ContextAPI.inspect_context("test")

def test_load_context_without_orchestrator(self):
ContextAPI.create_context("test")
ctx = ContextAPI.get_context("test")
assert ctx
assert ctx.Name == "test"
assert ctx.Orchestrator is None

0 comments on commit a527086

Please sign in to comment.