Skip to content

Commit

Permalink
fix: rename id to version for secretvalue parent references (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
jyecusch authored Dec 4, 2024
1 parent a5be9fa commit f10852b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
22 changes: 17 additions & 5 deletions nitric/resources/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from dataclasses import dataclass
from typing import List, Literal, Union
import warnings

from grpclib import GRPCError
from grpclib.client import Channel
Expand Down Expand Up @@ -77,7 +78,7 @@ def version(self, version: str) -> SecretVersionRef:
Can be used to retrieve the secret value associated with the version.
"""
return SecretVersionRef(secret=self, id=version)
return SecretVersionRef(secret=self, version=version)

def latest(self) -> SecretVersionRef:
"""
Expand All @@ -90,15 +91,26 @@ def latest(self) -> SecretVersionRef:


def _secret_version_to_wire(version: SecretVersionRef) -> VersionMessage:
return VersionMessage(SecretMessage(name=version.secret.name), version=version.id)
return VersionMessage(SecretMessage(name=version.secret.name), version=version.version)


@dataclass(frozen=True)
class SecretVersionRef:
"""A reference to a version of a secret, used to access the value of the version."""

secret: SecretRef
id: str
version: str

@property
def id(self) -> SecretVersionRef:
"""Get the version of this secret value."""
warnings.warn(
"id is deprecated and will be removed in a future version. "
"Use version instead.",
DeprecationWarning,
stacklevel=2
)
return self.version

async def access(self) -> SecretValue:
"""Return the value stored in this version of the secret."""
Expand All @@ -115,8 +127,8 @@ async def access(self) -> SecretValue:
# with a fixed version id.
static_version = (
self
if response.secret_version.version == self.id
else SecretVersionRef(secret=self.secret, id=response.secret_version.version)
if response.secret_version.version == self.version
else SecretVersionRef(secret=self.secret, version=response.secret_version.version)
)

return SecretValue(version=static_version, value=response.value)
Expand Down
6 changes: 3 additions & 3 deletions tests/resources/test_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def test_put(self):
)

# Check the returned value
assert result.id == "test-version"
assert result.version == "test-version"
assert result.secret.name == "test-secret"

async def test_put_string(self):
Expand All @@ -78,7 +78,7 @@ async def test_latest(self):
version = SecretRef("test-secret").latest()

assert version.secret.name == "test-secret"
assert version.id == "latest"
assert version.version == "latest"

async def test_access(self):
mock_access = AsyncMock()
Expand All @@ -100,7 +100,7 @@ async def test_access(self):
)

# Check the returned value
assert result.version.id == "response-version"
assert result.version.version == "response-version"
assert result.value == b"super secret value"

async def test_value_to_string(self):
Expand Down

0 comments on commit f10852b

Please sign in to comment.