This commit is contained in:
Felix Fontein 2025-02-03 16:33:34 +01:00
parent 230922bf08
commit 3c86a9e9b8
4 changed files with 15 additions and 24 deletions

View File

@ -1,14 +0,0 @@
# -*- coding: utf-8 -*-
# This code is part of the Ansible collection community.docker, but is an independent component.
# This particular file, and this file only, is based on the Docker SDK for Python (https://github.com/docker/docker-py/)
#
# Copyright (c) 2016-2025 Docker, Inc.
#
# It is licensed under the Apache 2.0 license (see LICENSES/Apache-2.0.txt in this collection)
# SPDX-License-Identifier: Apache-2.0
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from .api import ContextAPI
from .context import Context

View File

@ -13,6 +13,8 @@ __metaclass__ = type
import json
import os
from ansible.module_utils.six import raise_from
from .. import errors
from .config import (
@ -128,9 +130,9 @@ class ContextAPI(object):
data = json.load(f)
names.append(data["Name"])
except Exception as e:
raise errors.ContextException(
f"Failed to load metafile {filepath}: {e}",
) from e
raise_from(errors.ContextException(
"Failed to load metafile {filepath}: {e}".format(filepath=filepath, e=e),
), e)
contexts = [cls.DEFAULT_CONTEXT]
for name in names:
@ -154,7 +156,7 @@ class ContextAPI(object):
err = write_context_name_to_docker_config(name)
if err:
raise errors.ContextException(
f'Failed to set current context: {err}')
'Failed to set current context: {err}'.format(err=err))
@classmethod
def remove_context(cls, name):

View File

@ -14,6 +14,8 @@ import json
import os
from shutil import copyfile, rmtree
from ansible.module_utils.six import raise_from
from ..errors import ContextException
from ..tls import TLSConfig
@ -59,7 +61,7 @@ class Context(object):
if not isinstance(v, dict):
# unknown format
raise ContextException(
f"Unknown endpoint format for context {name}: {v}",
"Unknown endpoint format for context {name}: {v}".format(name=name, v=v),
)
self.endpoints[k] = v
@ -113,9 +115,9 @@ class Context(object):
metadata = json.load(f)
except (OSError, KeyError, ValueError) as e:
# unknown format
raise Exception(
f"Detected corrupted meta file for context {name} : {e}"
) from e
raise_from(Exception(
"Detected corrupted meta file for context {name} : {e}".format(name=name, e=e)
), e)
# for docker endpoints, set defaults for
# Host and SkipTLSVerify fields
@ -189,7 +191,7 @@ class Context(object):
rmtree(self.tls_path)
def __repr__(self):
return f"<{self.__class__.__name__}: '{self.name}'>"
return "<{classname}: '{name}'>".format(classname=self.__class__.__name__, name=self.name)
def __str__(self):
return json.dumps(self.__call__(), indent=2)

View File

@ -20,7 +20,8 @@ from ansible_collections.community.docker.plugins.module_utils._api.constants im
DEFAULT_UNIX_SOCKET,
IS_WINDOWS_PLATFORM,
)
from ansible_collections.community.docker.plugins.module_utils._api.context import Context, ContextAPI
from ansible_collections.community.docker.plugins.module_utils._api.context.api import ContextAPI
from ansible_collections.community.docker.plugins.module_utils._api.context.context import Context
class BaseContextTest(unittest.TestCase):