Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for cloud spec #119

Merged
merged 26 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b02cf85
feat: add support for cloud spec
IronCore864 Apr 8, 2024
108b1ce
test: add e2e test for cloud spec
IronCore864 Apr 8, 2024
9d58637
chore: fix linting
IronCore864 Apr 8, 2024
65be11b
chore: fix linting
IronCore864 Apr 8, 2024
8a058d1
chore: refactor according to code review
IronCore864 Apr 9, 2024
56e56db
chore: refactor according to code review
IronCore864 Apr 9, 2024
0c25e09
Update scenario/mocking.py
IronCore864 Apr 9, 2024
da8dea3
Update scenario/state.py
IronCore864 Apr 9, 2024
6e81250
refactor: some updates according to code review and standup discussions
IronCore864 Apr 10, 2024
d738b94
chore: refactor according to code review and standup discussion
IronCore864 Apr 11, 2024
db1f54f
chore: refactor according to code review
IronCore864 Apr 12, 2024
abaaf64
chore: fix static check
IronCore864 Apr 12, 2024
3a9a78d
chore: remove testing code
IronCore864 Apr 12, 2024
115b5bc
chore: make CloudCredential.to_ops_cloud_credential a private function'
IronCore864 Apr 15, 2024
d618391
docs: update readme with cloudspec
IronCore864 Apr 15, 2024
b274440
chore: make to ops methods private
IronCore864 Apr 15, 2024
1bc5e5c
chore: change import ops to import specific things
IronCore864 Apr 15, 2024
8e7dd97
chore: fix linting and ut
IronCore864 Apr 15, 2024
82b109c
chore: rename to ops method after discussion
IronCore864 Apr 15, 2024
77913d1
chore: fixing scenario imports
IronCore864 Apr 15, 2024
389dd65
chore: fix readability issue
IronCore864 Apr 15, 2024
e1bb629
docs: update readme cloudspec code example
IronCore864 Apr 15, 2024
82e8e4c
chore: add a missing type
IronCore864 Apr 15, 2024
3e1acc9
Update scenario/consistency_checker.py
IronCore864 Apr 17, 2024
0739003
chore: updating comment for redacted according to review suggestions
IronCore864 May 29, 2024
c56e267
Merge branch 'cloud-spec' of github.com:IronCore864/ops-scenario into…
IronCore864 May 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions scenario/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

from scenario.logger import logger as scenario_logger
from scenario.state import (
CloudSpec,
JujuLogLine,
Mount,
Network,
Expand Down Expand Up @@ -631,6 +632,13 @@ def resource_get(self, resource_name: str) -> str:
f"resource {resource_name} not found in State. please pass it.",
)

def credential_get(self) -> CloudSpec:
if not self._state.cloud_spec:
raise ModelError(
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
"ERROR cloud spec is empty, initialise it with `scenario.State(cloud_spec=...)`",
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
)
return self._state.cloud_spec


class _MockPebbleClient(_TestingPebbleClient):
def __init__(
Expand Down
79 changes: 79 additions & 0 deletions scenario/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,83 @@ def copy(self) -> "Self":
return copy.deepcopy(self)


@dataclasses.dataclass(frozen=True)
class CloudCredential:
auth_type: str
"""Authentication type."""
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved

attributes: Dict[str, str] = dataclasses.field(default_factory=dict)
"""A dictionary containing cloud credentials.

For example, for AWS, it contains `access-key` and `secret-key`;
for Azure, `application-id`, `application-password` and `subscription-id`
can be found here.
"""

redacted: List[str] = dataclasses.field(default_factory=list)
"""A list of redacted secrets."""
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def from_dict(cls, d: Dict[str, Any]) -> "CloudCredential":
"""Create a new CloudCredential object from a dictionary."""
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
return cls(
auth_type=d["auth-type"],
attributes=d.get("attrs") or {},
redacted=d.get("redacted") or [],
)


@dataclasses.dataclass(frozen=True)
class CloudSpec:
type: str
"""Type of the cloud."""
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved

name: str
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
"""Juju cloud name."""

region: Optional[str] = None
PietroPasotti marked this conversation as resolved.
Show resolved Hide resolved
"""Region of the cloud."""

endpoint: Optional[str] = None
"""Endpoint of the cloud."""

identity_endpoint: Optional[str] = None
"""Identity endpoint of the cloud."""

storage_endpoint: Optional[str] = None
"""Storage endpoint of the cloud."""

credential: Optional[CloudCredential] = None
"""Cloud credentials with key-value attributes."""

ca_certificates: List[str] = dataclasses.field(default_factory=list)
"""A list of CA certificates."""

skip_tls_verify: bool = False
"""Whether to skip TLS verfication."""

is_controller_cloud: bool = False
"""If this is the cloud used by the controller, defaults to False."""
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def from_dict(cls, d: Dict[str, Any]) -> "CloudSpec":
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
"""Create a new CloudSpec object from a dict parsed from JSON."""
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
return cls(
type=d["type"],
name=d["name"],
region=d.get("region") or None,
endpoint=d.get("endpoint") or None,
identity_endpoint=d.get("identity-endpoint") or None,
storage_endpoint=d.get("storage-endpoint") or None,
credential=CloudCredential.from_dict(d["credential"])
if d.get("credential")
else None,
ca_certificates=d.get("cacertificates") or [],
skip_tls_verify=d.get("skip-tls-verify") or False,
is_controller_cloud=d.get("is-controller-cloud") or False,
)


@dataclasses.dataclass(frozen=True)
class Secret(_DCBase):
id: str
Expand Down Expand Up @@ -919,6 +996,8 @@ class State(_DCBase):
"""Status of the unit."""
workload_version: str = ""
"""Workload version."""
cloud_spec: Optional[CloudSpec] = None
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
"""Cloud specification information (metadata) including credentials."""

def __post_init__(self):
for name in ["app_status", "unit_status"]:
Expand Down
48 changes: 48 additions & 0 deletions tests/test_e2e/test_cloud_spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import ops
import pytest

import scenario
from scenario.state import CloudSpec


@pytest.fixture(scope="function")
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
def mycharm():
class MyCharm(ops.CharmBase):
def __init__(self, framework: ops.Framework):
super().__init__(framework)
for evt in self.on.events().values():
self.framework.observe(evt, self._on_event)

def _on_event(self, event):
pass

return MyCharm


def test_get_cloud_spec(mycharm):
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved
cloud_spec = CloudSpec.from_dict(
{
"name": "localhost",
"type": "lxd",
"endpoint": "https://127.0.0.1:8443",
"credential": {
"auth-type": "certificate",
"attrs": {
"client-cert": "foo",
"client-key": "bar",
"server-cert": "baz",
},
},
}
)
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved

ctx = scenario.Context(mycharm, meta={"name": "foo"})
with ctx.manager("start", scenario.State(cloud_spec=cloud_spec)) as mgr:
assert mgr.charm.model.get_cloud_spec() == cloud_spec
IronCore864 marked this conversation as resolved.
Show resolved Hide resolved


def test_get_cloud_spec(mycharm):
ctx = scenario.Context(mycharm, meta={"name": "foo"})
with ctx.manager("start", scenario.State()) as mgr:
with pytest.raises(ops.ModelError):
mgr.charm.model.get_cloud_spec()
Loading