Skip to content

Commit

Permalink
fix(provider): setted properties were not directly available
Browse files Browse the repository at this point in the history
providers arguments were not directly available due to design of pulumi.
This solution creates defautl wapper around pulumi providers to keep
contexts.
  • Loading branch information
kkiani committed Aug 2, 2024
1 parent 2d9d52c commit e62f95b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 18 deletions.
55 changes: 52 additions & 3 deletions src/damavand/cloud/provider.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
from typing import Union
from pulumi_azure_native import Provider as AzurermProvider
from pulumi_aws import Provider as AwsProvider
from typing import Optional, Union
from pulumi_azure_native import Provider as PulumiAzurermProvider
from pulumi_aws import Provider as PulumiAwsProvider
from pulumi import ResourceOptions


class BaseProvider:
def __init__(self, app_name: str) -> None:
self.app_name = app_name


class AwsProvider(BaseProvider, PulumiAwsProvider):
def __init__(
self,
app_name: str,
region: str,
opts: Optional[ResourceOptions] = None,
**kwargs,
) -> None:
"""Create a new AWS provider instance. For available options, see: https://www.pulumi.com/registry/packages/aws/api-docs/provider/#inputs"""

BaseProvider.__init__(
self,
app_name=app_name,
)
PulumiAwsProvider.__init__(
self,
resource_name=f"{app_name}-provider",
opts=opts,
region=region,
**kwargs,
)

self.__region = region

@property
def enforced_region(self) -> str:
"""Region that enforced to all AWS operations"""
return self.__region


class AzurermProvider(BaseProvider, PulumiAzurermProvider):
def __init__(self, app_name: str, **kwargs) -> None:
BaseProvider.__init__(
self,
app_name=app_name,
)
PulumiAzurermProvider.__init__(
self,
resource_name=f"{app_name}-provider",
**kwargs,
)


CloudProvider = Union[
Expand Down
43 changes: 28 additions & 15 deletions src/damavand/core.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import logging
from typing import Any, Optional, cast
from typing import Any, Optional
from rich.console import Console
import pulumi_aws as aws
import pulumi_azure_native as azurerm

from damavand import utils
from damavand.resource import BaseResource, BaseObjectStorage
Expand All @@ -21,9 +19,6 @@ def __init__(
provider: CloudProvider,
resources: list[BaseResource] = [],
) -> None:
if isinstance(provider, AwsProvider) and not provider.region:
raise ValueError("AWS provider must have a region set.")

self.app_name = app_name
self.provider = provider
self._resources = resources
Expand All @@ -39,8 +34,8 @@ def new_object_storage(self, name: str, tags: dict, **kwargs) -> BaseObjectStora
case AwsProvider():
resource = AwsBucket(
name,
region=cast(str, self.provider.region),
tags=tags,
region=self.provider.enforced_region,
tags={**self.all_tags, **tags},
**kwargs,
)
self._resources.append(resource)
Expand All @@ -53,31 +48,49 @@ def new_object_storage(self, name: str, tags: dict, **kwargs) -> BaseObjectStora

class CloudConnection:
@staticmethod
def from_aws_provider(app_name: str, region: str, **kwargs) -> "CloudConnection":
def from_aws_provider(
app_name: str, region: str, tags: dict[str, str], **kwargs
) -> "CloudConnection":
"""
Create a connection for AWS provider.
Check `AwsProvider` class for more information about the available arguments.
"""

provider = AwsProvider(
f"{app_name}-provider",
args=aws.ProviderArgs(region=region, **kwargs),
app_name=app_name,
region=region,
**kwargs,
)

return CloudConnection(ResourceFactory(app_name, provider))
return CloudConnection(
ResourceFactory(
app_name=app_name,
provider=provider,
tags=tags,
)
)

@staticmethod
def from_azure_provider(app_name: str, **kwargs) -> "CloudConnection":
def from_azure_provider(
app_name: str, tags: dict[str, str], **kwargs
) -> "CloudConnection":
"""
Create a connection for Azure provider.
Check `AzurermProvider` class for more information about the available arguments.
"""

provider = AzurermProvider(
f"{app_name}-provider", args=azurerm.ProviderArgs(**kwargs)
app_name=app_name,
)

return CloudConnection(ResourceFactory(app_name, provider))
return CloudConnection(
ResourceFactory(
app_name=app_name,
provider=provider,
tags=tags,
**kwargs,
)
)

def __init__(self, resource_factory: ResourceFactory) -> None:
self.resource_factory = resource_factory
Expand Down

0 comments on commit e62f95b

Please sign in to comment.