Skip to content

Commit

Permalink
feat: Accept resource args as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
farbodahm committed Oct 15, 2024
1 parent e91e6dd commit 5dbff7e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
24 changes: 20 additions & 4 deletions examples/sparkle/__main__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from sparkle.application import Sparkle
from damavand.cloud.provider import AwsProvider
from damavand.factories import SparkControllerFactory
from damavand.cloud.aws.resources.glue_component import (
GlueComponentArgs,
GlueJobDefinition,
)

from applications.orders import CustomerOrders
from applications.products import Products


def main() -> None:

spark_factory = SparkControllerFactory(
provider=AwsProvider(
app_name="my-app",
Expand All @@ -14,12 +20,22 @@ def main() -> None:
tags={"env": "dev"},
)

applications: list[Sparkle] = [Products(), CustomerOrders()]
resource_args = GlueComponentArgs(
jobs=[
GlueJobDefinition(
name=app.config.app_name,
description=app.config.__doc__ or "",
script_location="__main__.py",
)
for app in applications
],
)

spark_controller = spark_factory.new(
name="my-spark",
applications=[
Products(),
CustomerOrders(),
],
applications=applications,
resource_args=resource_args,
)
# app_name = os.getenv("APP_NAME", "products-app") # Get app name on runtime

Expand Down
2 changes: 2 additions & 0 deletions src/damavand/base/controllers/base_controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from functools import cache
from typing import Any
from pulumi import Resource as PulumiResource
import pulumi

Expand Down Expand Up @@ -41,6 +42,7 @@ def __init__(
self,
name: str,
tags: dict[str, str] = {},
resource_args: Any = None,
**kwargs,
) -> None:
self.name = name
Expand Down
4 changes: 4 additions & 0 deletions src/damavand/base/controllers/spark.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Any

from damavand.base.controllers import ApplicationController
from damavand.base.controllers.base_controller import runtime
Expand All @@ -24,6 +25,8 @@ class SparkController(ApplicationController):
the list of Spark applications.
tags : dict[str, str]
the tags of the controller.
resource_args : Any
Any extra arguments for the underlying resource.
kwargs : dict
the extra arguments.
Expand All @@ -40,6 +43,7 @@ def __init__(
name,
applications: list[Sparkle],
tags: dict[str, str] = {},
resource_args: Any = None,
**kwargs,
) -> None:
ApplicationController.__init__(self, name, tags, **kwargs)
Expand Down
9 changes: 8 additions & 1 deletion src/damavand/base/factory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import field, dataclass
from typing import Optional, Generic, TypeVar
from typing import Optional, Generic, TypeVar, Any

from sparkle.application import Sparkle
from damavand.base.controllers import ApplicationController
Expand All @@ -22,6 +22,8 @@ class ApplicationControllerFactory(Generic[ControllerType]):
The cloud provider object.
tags : dict[str, str]
A set of default tags to be applied to all resources.
resource_args: any
Any extra arguments for the underlying resource.
Methods
-------
Expand All @@ -38,6 +40,7 @@ def new(
name: str,
applications: list[Sparkle],
id: Optional[str] = None,
resource_args: Any = None,
**kwargs,
) -> ControllerType:
match self.provider:
Expand All @@ -47,6 +50,7 @@ def new(
applications=applications,
region=self.provider.explicit_region,
tags=self.tags,
resource_args=resource_args,
**kwargs,
)

Expand All @@ -56,6 +60,7 @@ def new(
name=name,
applications=applications,
tags=self.tags,
resource_args=resource_args,
**kwargs,
)

Expand All @@ -69,6 +74,7 @@ def _new_aws_controller(
applications: list[Sparkle],
region: str,
tags: dict[str, str] = {},
resource_args: Any = None,
**kwargs,
) -> ControllerType:
raise NotImplementedError()
Expand All @@ -78,6 +84,7 @@ def _new_azure_controller(
name: str,
applications: list[Sparkle],
tags: dict[str, str] = {},
resource_args: Any = None,
**kwargs,
) -> ControllerType:
raise NotImplementedError()
20 changes: 5 additions & 15 deletions src/damavand/cloud/aws/controllers/spark.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import logging
from functools import cache
from typing import Any

import boto3
from pulumi import Resource as PulumiResource
from sparkle.application import Sparkle

from damavand.base.controllers import buildtime
from damavand.base.controllers.spark import SparkController
from damavand.cloud.aws.resources import GlueComponent, GlueComponentArgs
from damavand.cloud.aws.resources.glue_component import GlueJobDefinition
from damavand.cloud.aws.resources import GlueComponent
from damavand.errors import BuildtimeException


Expand All @@ -22,27 +22,17 @@ def __init__(
applications: list[Sparkle],
region: str,
tags: dict[str, str] = {},
resource_args: Any = None,
**kwargs,
) -> None:
super().__init__(name, applications, tags, **kwargs)
self._glue_client = boto3.client("glue", region_name=region)
self.resource_args = resource_args

@buildtime
@cache
def resource(self) -> PulumiResource:
if not self.applications:
raise BuildtimeException("No applications found to create Glue jobs.")

return GlueComponent(
name=self.name,
args=GlueComponentArgs(
jobs=[
GlueJobDefinition(
name=app.config.app_name,
description=app.config.__doc__ or "",
script_location="__main__.py",
)
for app in self.applications
],
),
)
return GlueComponent(name=self.name, args=self.resource_args)
4 changes: 4 additions & 0 deletions src/damavand/factories.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sparkle.application import Sparkle
from typing import Any
from damavand.base.controllers.spark import SparkController
from damavand.base.factory import ApplicationControllerFactory
from damavand.cloud.aws.controllers.spark import AwsSparkController
Expand All @@ -12,13 +13,15 @@ def _new_aws_controller(
applications: list[Sparkle],
region: str,
tags: dict[str, str] = {},
resource_args: Any = None,
**kwargs,
) -> SparkController:
return AwsSparkController(
name=name,
applications=applications,
region=region,
tags=tags,
resource_args=resource_args,
**kwargs,
)

Expand All @@ -27,6 +30,7 @@ def _new_azure_controller(
name: str,
applications: list[Sparkle],
tags: dict[str, str] = {},
resource_args: Any = None,
**kwargs,
) -> SparkController:
return AzureSparkController(
Expand Down

0 comments on commit 5dbff7e

Please sign in to comment.