Skip to content

Commit

Permalink
Fix SSHKeyPairDefinition
Browse files Browse the repository at this point in the history
The base `ResourceDefinition` class requires the `config` property's
type annotation to not be a string (added cd9319b / a70af27),
but the `ssh_keypair.py` module activates postponed annotations via the
`from __future__ import annotations` syntax (added e48422a), which
turns all type annotations in the module into strings[1], breaking the
`SSHKeyPairDefinition` class when initialized.

Thus any defined `resources.sshKeyPairs` break `nixops` commands,
causing failures with the message:

```
TypeError: <class 'nixops.resources.ssh_keypair.SSHKeyPairDefinition'>.config's type annotation is not allowed to be a string, see: https://nixops.readthedocs.io/en/latest/plugins/authoring.html
```

A number of possible workarounds for this, but the simplest would seem
to just not activate PEP 563-style postponed annotations in
`ssh_keypair.py`.

[1] https://peps.python.org/pep-0563/
  • Loading branch information
doshitan committed Sep 5, 2022
1 parent dcafae5 commit b82e6e0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion nixops/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self, name: str, config: ResourceEval):
)
else:
raise TypeError(
f"{self.__class__}.config's type annotation is not allowed to be a string, see: https://nixops.readthedocs.io/en/latest/plugins/authoring.html"
f"{self.__class__}.config's type annotation is not allowed to be a string (or defined in a module using PEP 563 postponed annotations), see: https://nixops.readthedocs.io/en/latest/plugins/authoring.html"
)

if not issubclass(config_type, ResourceOptions):
Expand Down
8 changes: 3 additions & 5 deletions nixops/resources/ssh_keypair.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import annotations

# Automatic provisioning of SSH key pairs.
from typing import Type, Dict, Optional

Expand All @@ -14,11 +12,11 @@ class SSHKeyPairDefinition(nixops.resources.ResourceDefinition):
config: nixops.resources.ResourceOptions

@classmethod
def get_type(cls: Type[SSHKeyPairDefinition]) -> str:
def get_type(cls: Type["SSHKeyPairDefinition"]) -> str:
return "ssh-keypair"

@classmethod
def get_resource_type(cls: Type[SSHKeyPairDefinition]) -> str:
def get_resource_type(cls: Type["SSHKeyPairDefinition"]) -> str:
return "sshKeyPairs"

def __init__(self, name: str, config: nixops.resources.ResourceEval):
Expand All @@ -38,7 +36,7 @@ class SSHKeyPairState(nixops.resources.ResourceState[SSHKeyPairDefinition]):
private_key: Optional[str] = nixops.util.attr_property("privateKey", None)

@classmethod
def get_type(cls: Type[SSHKeyPairState]) -> str:
def get_type(cls: Type["SSHKeyPairState"]) -> str:
return "ssh-keypair"

def __init__(self, depl: "nixops.deployment.Deployment", name: str, id: RecordId):
Expand Down
5 changes: 5 additions & 0 deletions tests/functional/ssh-key-pair-resource.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
network = {};

resources.sshKeyPairs.ssh-key = {};
}
20 changes: 20 additions & 0 deletions tests/functional/test_ssh_key_pair_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from os import path

from tests.functional.generic_deployment_test import GenericDeploymentTest

from nixops.evaluation import NetworkFile

parent_dir = path.dirname(__file__)

ssh_key_pair_spec = "%s/ssh-key-pair-resource.nix" % (parent_dir)


class TestSSHKeyPairResource(GenericDeploymentTest):
def setup(self):
super(TestSSHKeyPairResource, self).setup()
self.depl.network_expr = NetworkFile(ssh_key_pair_spec)

def test_evaluate(self):
self.depl.evaluate()

assert "ssh-key" in self.depl.definitions

0 comments on commit b82e6e0

Please sign in to comment.