Skip to content

Commit

Permalink
Added name_generator. Added flows. Added Instance and InstanceHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
alexquali committed Sep 10, 2024
1 parent 9d866a1 commit 8f4cdca
Show file tree
Hide file tree
Showing 6 changed files with 537 additions and 241 deletions.
60 changes: 17 additions & 43 deletions cloudshell/cp/gcp/flows/deploy_instance/base_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@

from cloudshell.cp.core.flows import AbstractDeployFlow
from cloudshell.cp.core.request_actions import DeployVMRequestActions

from cloudshell.cp.core.rollback import RollbackCommandsManager

from cloudshell.cp.gcp.helpers.name_generator import GCPNameGenerator

from cloudshell.cp.gcp.actions.vm_details import VMDetailsActions
from cloudshell.cp.gcp.actions.vm_details_actions import VMDetailsActions
from cloudshell.cp.gcp.flows.deploy_instance.commands import DeployInstanceCommand


if TYPE_CHECKING:
from google.cloud.compute_v1.types import compute
from cloudshell.cp.gcp.handlers.instance import InstanceHandler
from cloudshell.cp.gcp.handlers.instance import Instance, InstanceHandler
from cloudshell.cp.gcp.models.deploy_app import BaseGCPDeployApp
from cloudshell.cp.gcp.resource_conf import GCPResourceConfig
from cloudshell.api.cloudshell_api import CloudShellAPISession, ReservationInfo
Expand All @@ -42,11 +38,6 @@ def __attrs_post_init__(self):
self._rollback_manager = RollbackCommandsManager(logger)
self.name_generator = GCPNameGenerator()

@abstractmethod
def _prepare_instance(self, instance_name, deploy_app) -> compute.Instance:
"""Create Instance to deploy."""
pass

def _prepare_vm_details_data(
self,
deployed_vm_id: int,
Expand Down Expand Up @@ -86,47 +77,30 @@ def _prepare_deploy_app_result(
deployedAppAttributes=self._prepare_app_attrs(deploy_app, deployed_vm_id),
)

def _create_instance(
self,
deploy_app: BaseGCPDeployApp,
# instance_name: str,
) -> str:
@abstractmethod
def _create_instance(self, deploy_app: BaseGCPDeployApp) -> Instance:
""""""

with self.cancellation_manager:
instance = self._prepare_instance(
# instance_name=instance_name,
deploy_app=deploy_app,
)

return DeployInstanceCommand(
instance_handler=self.instance_handler,
instance=instance,
rollback_manager=self._rollback_manager,
cancellation_manager=self.cancellation_manager,
).execute()
pass

def _deploy(self, request_actions: DeployVMRequestActions) -> DeployAppResult:
"""Deploy Proxmox Instance."""
# noinspection PyTypeChecker
deploy_app: BaseGCPDeployApp = request_actions.deploy_app

# instance_name = self.name_generator.instance(
# app_name=deploy_app.app_name,
# generate=deploy_app.autogenerated_name
# )
#
# logger.info(f"Generated name for the Instance: {instance_name}")
with self.cancellation_manager:
instance = self._create_instance(deploy_app=deploy_app)

with self._rollback_manager:
# logger.info(f"Creating Instance {instance_name}")
instance_name = self._create_instance(
deploy_app=deploy_app,
# instance_name=instance_name,
)

logger.info(f"Preparing Deploy App result for the {instance_name}")
logger.info(f"Creating Instance {instance.name}")
deployed_instance = DeployInstanceCommand(
instance=instance,
credentials=self.resource_config.credentials,
rollback_manager=self._rollback_manager,
cancellation_manager=self.cancellation_manager,
).execute()

logger.info(f"Preparing Deploy App result for the {deployed_instance.name}")
return self._prepare_deploy_app_result(
deploy_app=deploy_app,
instance_name=instance_name,
instance_name=deployed_instance.name,
)
21 changes: 21 additions & 0 deletions cloudshell/cp/gcp/flows/deploy_instance/from_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from cloudshell.cp.gcp.handlers.instance import Instance
from cloudshell.cp.gcp.flows.deploy_instance.base_flow import AbstractGCPDeployFlow

if TYPE_CHECKING:
from cloudshell.cp.gcp.models.deploy_app import InstanceFromMachineImageDeployApp


class GCPDeployInstanceFromImageFlow(AbstractGCPDeployFlow):
def _create_instance(
self,
deploy_app: InstanceFromMachineImageDeployApp,
) -> Instance:
"""Create Instance object based on Machine Image."""
return Instance(
deploy_app=deploy_app,
resource_config=self.resource_config
).from_machine_image()
21 changes: 21 additions & 0 deletions cloudshell/cp/gcp/flows/deploy_instance/from_scratch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from cloudshell.cp.gcp.handlers.instance import Instance
from cloudshell.cp.gcp.flows.deploy_instance.base_flow import AbstractGCPDeployFlow

if TYPE_CHECKING:
from cloudshell.cp.gcp.models.deploy_app import InstanceFromScratchDeployApp


class GCPDeployInstanceFromScratchFlow(AbstractGCPDeployFlow):
def _create_instance(
self,
deploy_app: InstanceFromScratchDeployApp,
) -> Instance:
"""Create Instance object based on provided attributes."""
return Instance(
deploy_app=deploy_app,
resource_config=self.resource_config
).from_scratch()
21 changes: 21 additions & 0 deletions cloudshell/cp/gcp/flows/deploy_instance/from_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from cloudshell.cp.gcp.handlers.instance import Instance
from cloudshell.cp.gcp.flows.deploy_instance.base_flow import AbstractGCPDeployFlow

if TYPE_CHECKING:
from cloudshell.cp.gcp.models.deploy_app import InstanceFromTemplateDeployApp


class GCPDeployInstanceFromTemplateFlow(AbstractGCPDeployFlow):
def _create_instance(
self,
deploy_app: InstanceFromTemplateDeployApp,
) -> Instance:
"""Create Instance object based on Template."""
return Instance(
deploy_app=deploy_app,
resource_config=self.resource_config
).from_template()
Loading

0 comments on commit 8f4cdca

Please sign in to comment.