Skip to content

Commit

Permalink
feat: return environments in DTAP order.
Browse files Browse the repository at this point in the history
When you retrieve the environments you used to get a random order. With this feature you will have the ability to customize the order. By default we follow the DTAP order as in Development, Testing, Acceptance and Production.
  • Loading branch information
Nr18 committed Jul 16, 2023
1 parent 65af507 commit aa20ba2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
8 changes: 7 additions & 1 deletion docs/content/2-configuration/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ options. You can do this via 2 environment variable:
| **Name** | **Default Value** | **Description** |
|--------------------------|-------------------|---------------------------------------------------------------------------------------------------------------|
| PATTERN_WORKLOAD_NAME | `.*?-(.*)-.*` | The first match is used as the workload name. |
| PATTERN_ENVIRONMENT_NAME | `.*-.*-(.*)` | The first match is used as the environment name. For example: development, testing, acceptance or production. |
| PATTERN_ENVIRONMENT_NAME | `.*-.*-(.*)` | The first match is used as the environment name. For example: development, testing, acceptance or production. |

## Other environment variable

| **Name** | **Default Value** | **Description** |
|--------------------|-------------------|----------------------------------------------------------------------------------------------------------------------------------------|
| ENVIRONMENT_WEIGHT | dev,test,acc,prod | The order that the environments are returned, we match using a startswith. If the environment does not match it will be returned last. |

## AWS Policies

Expand Down
6 changes: 5 additions & 1 deletion landingzone_organization/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from dataclasses import dataclass
from typing import Optional

from landingzone_organization.filtering import resolve_account_environment
from landingzone_organization.filtering import resolve_account_environment, resolve_account_weight


@dataclass
Expand All @@ -18,3 +18,7 @@ class Account:
@property
def environment(self) -> Optional[str]:
return resolve_account_environment(self.name)

@property
def weight(self) -> int:
return resolve_account_weight(self.name)
9 changes: 8 additions & 1 deletion landingzone_organization/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import re
from typing import Optional


PATTERN_WORKLOAD_NAME = os.environ.get("PATTERN_WORKLOAD_NAME", ".*?-(.*)-.*")
PATTERN_ENVIRONMENT_NAME = os.environ.get("PATTERN_ENVIRONMENT_NAME", ".*-.*-(.*)")
ENVIRONMENT_WEIGHT = os.environ.get("ENVIRONMENT_WEIGHT", "dev,test,acc,prod")


def match_workload_pattern(name: str) -> bool:
Expand All @@ -21,3 +21,10 @@ def resolve_workload_name(name: str) -> Optional[str]:
def resolve_account_environment(name: str) -> Optional[str]:
match = re.match(PATTERN_ENVIRONMENT_NAME, name)
return match.group(1) if match else None


def resolve_account_weight(name: str) -> int:
order = ENVIRONMENT_WEIGHT.split(",")
item = next(filter(lambda x: name.startswith(x), order), None)

return order.index(item) if item else len(order)
9 changes: 4 additions & 5 deletions landingzone_organization/workload.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations
import re
from typing import List, Optional, Set
from typing import List, Optional

from landingzone_organization.account import Account

Expand All @@ -16,11 +15,11 @@ def name(self) -> str:

@property
def accounts(self) -> List[Account]:
return self.__accounts
return sorted(self.__accounts, key=lambda x: x.weight)

@property
def environments(self) -> Set[str]:
return set(map(lambda account: str(account.environment), self.accounts))
def environments(self) -> List[str]:
return list(map(lambda account: str(account.environment), self.accounts))

def by_environment(self, name: str) -> Optional[Account]:
def match(account: Account):
Expand Down
19 changes: 8 additions & 11 deletions tests/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,44 @@

def test_workloads(organization: Organization) -> None:
workloads = organization.workloads(["Workloads"])
assert workloads.environments == {
"development",
assert workloads.environments == {"development",
"testing",
"acceptance",
"production",
}
"production"}

assert len(workloads) == 4
assert workloads.names == {"workload-1", "workload-2", "workload-3", "workload-4"}

workload1 = workloads.by_name("workload-1")
assert len(workload1.accounts) == 4
assert workload1.environments == {
"development",
assert workload1.environments == ["development",
"testing",
"acceptance",
"production",
}
"production"]
assert workload1.by_environment("development").name == "test-workload-1-development"
assert workload1.by_environment("testing").name == "test-workload-1-testing"
assert workload1.by_environment("acceptance").name == "test-workload-1-acceptance"
assert workload1.by_environment("production").name == "test-workload-1-production"

workload2 = workloads.by_name("workload-2")
assert len(workload2.accounts) == 3
assert workload2.environments == {"development", "testing", "acceptance"}
assert workload2.environments == ["development", "testing", "acceptance"]
assert workload2.by_environment("development").name == "test-workload-2-development"
assert workload2.by_environment("testing").name == "test-workload-2-testing"
assert workload2.by_environment("acceptance").name == "test-workload-2-acceptance"
assert workload2.by_environment("production") is None

workload3 = workloads.by_name("workload-3")
assert len(workload3.accounts) == 2
assert workload3.environments == {"development", "testing"}
assert workload3.environments == ["development", "testing"]
assert workload3.by_environment("development").name == "test-workload-3-development"
assert workload3.by_environment("testing").name == "test-workload-3-testing"
assert workload3.by_environment("acceptance") is None
assert workload3.by_environment("production") is None

workload4 = workloads.by_name("workload-4")
assert len(workload4.accounts) == 1
assert workload4.environments == {"development"}
assert workload4.environments == ["development"]
assert workload4.by_environment("development").name == "test-workload-4-development"
assert workload4.by_environment("testing") is None
assert workload4.by_environment("acceptance") is None
Expand Down
5 changes: 5 additions & 0 deletions tests/test_workload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


def test_workload_environment_order(organization) -> None:
workload = organization.workloads(["Workloads"]).by_name("workload-1")
assert ["development", "testing", "acceptance", "production"] == workload.environments

0 comments on commit aa20ba2

Please sign in to comment.