Skip to content

Commit

Permalink
Fix reapplying NSGs (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilatomic authored Oct 12, 2024
2 parents a88438e + 1abe0bc commit 53b7b13
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 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
16 changes: 8 additions & 8 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 @@ -44,7 +44,7 @@ def render(self) -> dict:
"name": self.name,
"resource_group_name": self.rg,
"location": self.location,
"security_rule": [],
"security_rule": None,
"tags": self.tags,
}

Expand All @@ -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
4 changes: 2 additions & 2 deletions llamazure/tf/network_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_terraform_example(self):
"name": "acceptanceTestSecurityGroup1",
"location": "West Europe",
"resource_group_name": "example-resources",
"security_rule": [],
"security_rule": None,
"tags": {
"environment": "Production",
},
Expand Down Expand Up @@ -91,5 +91,5 @@ def test_example(self):

assert (
json.dumps(tf.render())
== '{"resource": {"azurerm_network_security_group": {"n": {"name": "n", "resource_group_name": "rg", "location": "l", "security_rule": [], "tags": {}}}}}'
== '{"resource": {"azurerm_network_security_group": {"n": {"name": "n", "resource_group_name": "rg", "location": "l", "security_rule": null, "tags": {}}}}}'
)

0 comments on commit 53b7b13

Please sign in to comment.