Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.

Commit 60daa6f

Browse files
committed
Added new staticmethod to Registry.
1 parent d085196 commit 60daa6f

5 files changed

Lines changed: 151 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2424
* Added documentation to the `gcip.core.variables` module.
2525
* Added @properties to all public Job attributes.
2626
* New addons: *aws* to allow receiving AWS account id and region.
27+
* New `gcip.addons.container.registries.Registry.AWS()` allows getting an ECR URL to be used in pipeline.
2728

2829
### Changed
2930
* Normalize config_file_path in `gcip.addons.container.config.DockerClientConfig`

gcip/addons/aws/helper.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import boto3
44

5-
session = boto3.Session()
6-
sts = session.client("sts")
5+
sts = boto3.Session().client("sts")
76

87

98
def aws_account_id() -> str:
@@ -20,8 +19,7 @@ def aws_account_id() -> str:
2019
if os.environ.get("AWS_ACCOUNT_ID"):
2120
return os.environ["AWS_ACCOUNT_ID"]
2221

23-
sts_response = sts.get_caller_identity()
24-
return sts_response["Account"]
22+
return sts.get_caller_identity()["Account"]
2523

2624

2725
def aws_region() -> str:

gcip/addons/container/registries.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
__maintainer__ = "Daniel von Eßen"
77
__email__ = "daniel.von-essen@deutschebahn.com"
88

9+
from typing import Optional
10+
11+
from gcip.addons.aws.helper import (
12+
aws_region,
13+
aws_account_id,
14+
)
15+
916

1017
class Registry:
1118
"""
@@ -15,3 +22,29 @@ class Registry:
1522
DOCKER: str = "index.docker.io"
1623
QUAY: str = "quay.io"
1724
GCR: str = "gcr.io"
25+
26+
@staticmethod
27+
def AWS(*, account_id: Optional[str] = None, region: Optional[str] = None) -> str:
28+
"""Amazon Elastic Container Registry (ECR).
29+
30+
If neither `account_id` nor `region` is given, method tries to evaluate `account_id` and `region`
31+
with helper functions from `gcip.addons.aws.helper`. If one of the helper functions, do not
32+
resolve to an appropriate value they will throw a `ValueError` or `KeyError` exception.
33+
34+
Args:
35+
account_id (Optional[str]): AWS account id. Defaults internally to `gcip.addons.aws.helper.aws_account_id()`.
36+
region (Optional[str]): AWS region where the ECR repository lives in. Defaults internally to `gcip.addons.aws.helper.aws_region`.
37+
38+
Raises:
39+
ValueError: If no region was found in `gcip.addons.aws.helper.aws_region()`.
40+
ValueError: If aws account id to resolvable from `gcip.addons.aws.helper.aws_account_id()`.
41+
42+
Returns:
43+
str: Elastic Container Registry URL in format of **aws_account_id.dkr.ecr.region.amazonaws.com**
44+
"""
45+
if not account_id:
46+
account_id = aws_account_id()
47+
if not region:
48+
region = aws_region()
49+
50+
return f"{account_id}.dkr.ecr.{region}.amazonaws.com"

gcip/core/artifact.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# __author__ = "Thomas Steinbach"
2+
# __copyright__ = "Copyright 2020 DB Systel GmbH"
3+
# __credits__ = ["Daniel von Eßen"]
4+
# # SPDX-License-Identifier: Apache-2.0
5+
# __license__ = "Apache-2.0"
6+
# __maintainer__ = "Daniel von Eßen"
7+
# __email__ = "daniel.von-essen@deutschebahn.com"
8+
9+
# from typing import Any, Dict, List, Optional
10+
11+
# from gcip.core.rule import WhenStatement
12+
# from gcip.core.variables import PredefinedVariables
13+
14+
15+
# class Artifacts:
16+
# def __init__(
17+
# self,
18+
# paths: List[str],
19+
# *,
20+
# excludes: List[str] = [],
21+
# expire_in: Optional[str] = None,
22+
# expose_as: Optional[str] = None,
23+
# name: Optional[str] = None,
24+
# public: Optional[bool] = None,
25+
# untracked: Optional[bool] = None,
26+
# when: Optional[WhenStatement] = None,
27+
# ) -> None:
28+
# self._expire_in = expire_in
29+
# self._expose_as = expose_as
30+
# self._name = name if name else f"{PredefinedVariables.CI_JOB_NAME}-{PredefinedVariables.CI_COMMIT_REF_SLUG}"
31+
# self._public = public
32+
# self._untracked = untracked
33+
# self._when = when
34+
35+
# # Remove project path prefix from paths given.
36+
# # Prepend ./ to path to clearify that cache paths
37+
# # are relative to CI_PROJECT_DIR
38+
# self._paths = [path if not path.startswith(PredefinedVariables.CI_PROJECT_DIR) else path[len(PredefinedVariables.CI_PROJECT_DIR) :] for path in paths]
39+
# if len(excludes) > 0:
40+
# self._excludes = [
41+
# exclude if not exclude.startswith(PredefinedVariables.CI_PROJECT_DIR) else exclude[len(PredefinedVariables.CI_PROJECT_DIR) :]
42+
# for exclude in excludes
43+
# ]
44+
45+
# @property
46+
# def paths(self):
47+
# return self._paths
48+
49+
# @property
50+
# def excludes(self):
51+
# return self._excludes
52+
53+
# @property
54+
# def expire_in(self):
55+
# return self._expire_in
56+
57+
# @property
58+
# def expose_as(self):
59+
# return self._expose_as
60+
61+
# @property
62+
# def name(self):
63+
# return self._name
64+
65+
# @property
66+
# def public(self):
67+
# return self._public
68+
69+
# @property
70+
# def untracked(self):
71+
# return self._untracked
72+
73+
# @property
74+
# def when(self):
75+
# return self._when
76+
77+
# def render(self) -> Dict[str, Any]:
78+
# """Return a representation of this Cache object as dictionary with static values.
79+
80+
# The rendered representation is used by the gcip to dump it
81+
# in YAML format as part of the .gitlab-ci.yml pipeline.
82+
83+
# Returns:
84+
# Dict[str, Any]: A dictionary prepresenting the cache object in Gitlab CI.
85+
# """
86+
# rendered: Dict[str, Union[str, bool, List[str], Union[str, Dict[str, Union[List[str], str]]]]]
87+
# rendered = {
88+
# "paths": self.paths,
89+
# "name": self.name,
90+
# }
91+
# if self.excludes:
92+
# rendered["excludes"] = self.excludes
93+
# if self.expire_in:
94+
# rendered["expire_in"] = self.expire_in
95+
# if self.expose_as:
96+
# rendered["expose_as"] = self.expose_as
97+
# if self.expose_as:
98+
# rendered["expose_as"] = self._policy.value
99+
# if self.expose_as:
100+
# rendered["expose_as"] = self._policy.value
101+
# if self.expose_as:
102+
# rendered["expose_as"] = self._policy.value
103+
# rendered["key"] = self._cache_key.render()
104+
105+
# return rendered

tests/unit/test_addons_container_registries.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@
99
("DOCKER", "https://index.docker.io/v1/"),
1010
("GCR", "gcr.io"),
1111
("QUAY", "quay.io"),
12-
]
12+
],
1313
)
1414
def test_registries(registry, expected_url):
1515
getattr(Registry, registry) == expected_url
16+
17+
18+
def test_aws_registry(mocker, monkeypatch):
19+
monkeypatch.setenv("AWS_ACCOUNT_ID", "123456789012")
20+
monkeypatch.setenv("AWS_DEFAULT_REGION", "eu-central-1")
21+
mocker.patch("gcip.addons.aws.helper.aws_region", return_value="eu-central-1")
22+
mocker.patch("gcip.addons.aws.helper.aws_account_id", return_value="123456789012")
23+
aws_registry = Registry.AWS()
24+
assert "123456789012.dkr.ecr.eu-central-1.amazonaws.com" == aws_registry

0 commit comments

Comments
 (0)