-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from pehala/types
Add Routes and Secrets objects
- Loading branch information
Showing
5 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""Module containing helper classes for OpenShift objects (C)RUD""" | ||
from typing import TYPE_CHECKING, List | ||
|
||
if TYPE_CHECKING: | ||
# pylint: disable=cyclic-import | ||
from testsuite.openshift.client import OpenShiftClient | ||
|
||
|
||
class RemoteMapping: | ||
"""Dict-like interface to different types of OpenShift Objects""" | ||
|
||
def __init__(self, client: 'OpenShiftClient', resource_name: str): | ||
self._client = client | ||
self._resource_name = resource_name | ||
|
||
def fetch_resource(self, name, *cmd_args: str, auto_raise: bool = True): | ||
"""Executes command and returns output in yaml format""" | ||
args: List[str] = [] | ||
args.extend([self._resource_name, name, "-o", "json", "--ignore-not-found=true"]) | ||
args.extend(cmd_args) | ||
return self._client.do_action("get", *args, auto_raise=auto_raise, parse_output=True) | ||
|
||
def __iter__(self): | ||
"""Return iterator for requested resource""" | ||
data = self._client.do_action("get", self._resource_name, parse_output=True) | ||
return iter(data["items"]) | ||
|
||
def __getitem__(self, name): | ||
"""Return requested resource as APIObject""" | ||
res = self.fetch_resource(name) | ||
if res is None: | ||
raise KeyError() | ||
return res | ||
|
||
def __contains__(self, name): | ||
res = self.fetch_resource(name) | ||
return res is not None | ||
|
||
def __delitem__(self, name): | ||
if name not in self: | ||
raise KeyError() | ||
self._client.do_action("delete", self._resource_name, name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""Module containing classes for manipulation with OpenShift routes""" | ||
from testsuite.openshift.types import RemoteMapping | ||
|
||
|
||
class Routes(RemoteMapping): | ||
"""Dict-like interface to OpenShift routes""" | ||
|
||
def __init__(self, client) -> None: | ||
super().__init__(client, "route") | ||
|
||
def expose(self, name, service, hostname=None): | ||
"""Expose containers internally as services or externally via routes. | ||
Returns requested route in yaml format. | ||
""" | ||
extra_args = [] | ||
if hostname is not None: | ||
extra_args.append(f"--hostname={hostname}") | ||
return self._client.do_action("expose", "service", f"--name={name}", "-o", "json", | ||
service, *extra_args, parse_output=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Module containing classes for manipulation with OpenShift secrets""" | ||
import base64 | ||
|
||
from testsuite.openshift.types import RemoteMapping | ||
|
||
|
||
class Secrets(RemoteMapping): | ||
"""Dict-like interface to openshift secrets""" | ||
|
||
def __init__(self, client): | ||
super().__init__(client, "secret") | ||
|
||
def __getitem__(self, name: str): | ||
"""Return requested secret in yaml format""" | ||
|
||
# pylint: disable=too-few-public-methods | ||
class _DecodedSecrets: | ||
def __init__(self, data): | ||
self._data = data | ||
|
||
def __getitem__(self, name): | ||
return base64.b64decode(self._data[name]).decode("utf-8") | ||
|
||
def __contains__(self, name): | ||
return name in self._data | ||
|
||
return _DecodedSecrets(super().__getitem__(name).model.data) |