Skip to content

Commit

Permalink
feature: add RefList type for correct pluralisation when referencing …
Browse files Browse the repository at this point in the history
…lists in variables
  • Loading branch information
lilatomic committed Sep 15, 2024
1 parent d42e575 commit 3ff572d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
9 changes: 8 additions & 1 deletion llamazure/tf/changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# 0

## 0.1

### 0.1.0

- fix : fix for TF considering NSG rules as subresources
- feature : add RefList type for correct pluralisation when referencing variables that are lists

## 0.0

### 0.0.1

- feature: easily generate nsg rules
- feature : easily generate nsg rules
22 changes: 18 additions & 4 deletions llamazure/tf/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from abc import ABC, abstractmethod
from collections import defaultdict
from dataclasses import dataclass
from typing import Union


class TFResource(ABC):
Expand Down Expand Up @@ -51,9 +52,22 @@ def register(resource: TFResource):
}


def _pluralise(k: str, v: list[str], pluralise: str = "s") -> dict[str, str | list[str]]:
@dataclass(frozen=True)
class RefList:
"""A reference to a var containing a list"""

var: str


TFList = Union[list[str], RefList]


def _pluralise(k: str, v: TFList, pluralise: str = "s") -> dict[str, str | list[str]]:
"""Format the k-v pair, pluralising the k if necessary"""
if len(v) == 1:
return {k: v[0]}
if isinstance(v, RefList):
return {k + pluralise: [v.var]}
else:
return {k + pluralise: v}
if len(v) == 1:
return {k: v[0]}
else:
return {k + pluralise: v}
7 changes: 6 additions & 1 deletion llamazure/tf/models_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from llamazure.tf.models import _pluralise
from llamazure.tf.models import RefList, _pluralise


class TestPluralise:
Expand Down Expand Up @@ -31,3 +31,8 @@ def test_multiple_elements_with_es_suffix(self):
result = _pluralise("box", ["box", "fox"], pluralise="es")
expected = {"boxes": ["box", "fox"]}
assert result == expected

def test_reflist(self):
result = _pluralise("apple", RefList("ref"))
expected = {"apples": ["ref"]}
assert result == expected
14 changes: 7 additions & 7 deletions llamazure/tf/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from enum import Enum
from typing import Generic, TypeVar

from llamazure.tf.models import AnyTFResource, TFResource, _pluralise
from llamazure.tf.models import AnyTFResource, TFList, TFResource, _pluralise

T = TypeVar("T")

Expand Down Expand Up @@ -70,12 +70,12 @@ class NSGRule:
direction: Direction

protocol: str = "Tcp"
src_ports: list[str] = field(default_factory=lambda: ["*"])
src_addrs: list[str] = field(default_factory=lambda: ["*"])
src_sgids: list[str] = field(default_factory=lambda: [])
dst_ports: list[str] = field(default_factory=lambda: ["*"])
dst_addrs: list[str] = field(default_factory=lambda: ["*"])
dst_sgids: list[str] = field(default_factory=lambda: [])
src_ports: TFList = field(default_factory=lambda: ["*"])
src_addrs: TFList = field(default_factory=lambda: ["*"])
src_sgids: TFList = field(default_factory=lambda: [])
dst_ports: TFList = field(default_factory=lambda: ["*"])
dst_addrs: TFList = field(default_factory=lambda: ["*"])
dst_sgids: TFList = field(default_factory=lambda: [])

description: str = ""

Expand Down

0 comments on commit 3ff572d

Please sign in to comment.