Move portfolio state machine helpers directly to model file to prevent import issues.

Having `get_stage_csp_class` in the csp module meant that any file that interacted with that import path would throw an error in a REPL. This will allow importing of the Azure and Mock providers for interactive dev.
This commit is contained in:
tomdds 2020-01-26 15:17:53 -05:00
parent b28281d04e
commit 466a575229
4 changed files with 25 additions and 27 deletions

View File

@ -1,5 +1,3 @@
import importlib
from .cloud import MockCloudProvider from .cloud import MockCloudProvider
from .file_uploads import AzureUploader, MockUploader from .file_uploads import AzureUploader, MockUploader
from .reports import MockReportingProvider from .reports import MockReportingProvider
@ -31,20 +29,3 @@ def make_csp_provider(app, csp=None):
app.csp = MockCSP(app, test_mode=True) app.csp = MockCSP(app, test_mode=True)
else: else:
app.csp = MockCSP(app) app.csp = MockCSP(app)
def _stage_to_classname(stage):
return "".join(map(lambda word: word.capitalize(), stage.split("_")))
def get_stage_csp_class(stage, class_type):
"""
given a stage name and class_type return the class
class_type is either 'payload' or 'result'
"""
cls_name = f"{_stage_to_classname(stage)}CSP{class_type.capitalize()}"
try:
return getattr(importlib.import_module("atst.domain.csp.cloud"), cls_name)
except AttributeError:
print("could not import CSP Result class <%s>" % cls_name)

View File

@ -124,3 +124,4 @@ class CloudProviderInterface:
exception if an error occurs while creating a subscription. exception if an error occurs while creating a subscription.
""" """
raise NotImplementedError() raise NotImplementedError()

View File

@ -1,3 +1,5 @@
import importlib
from sqlalchemy import Column, ForeignKey, Enum as SQLAEnum from sqlalchemy import Column, ForeignKey, Enum as SQLAEnum
from sqlalchemy.orm import relationship, reconstructor from sqlalchemy.orm import relationship, reconstructor
from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.dialects.postgresql import UUID
@ -8,8 +10,7 @@ from transitions.extensions.states import add_state_features, Tags
from flask import current_app as app from flask import current_app as app
from atst.domain.csp.cloud import ConnectionException, UnknownServerException from atst.domain.csp.cloud.exceptions import ConnectionException, UnknownServerException
from atst.domain.csp import MockCSP, AzureCSP, get_stage_csp_class
from atst.database import db from atst.database import db
from atst.models.types import Id from atst.models.types import Id
from atst.models.base import Base from atst.models.base import Base
@ -17,6 +18,25 @@ import atst.models.mixins as mixins
from atst.models.mixins.state_machines import FSMStates, AzureStages, _build_transitions from atst.models.mixins.state_machines import FSMStates, AzureStages, _build_transitions
def _stage_to_classname(stage):
return "".join(map(lambda word: word.capitalize(), stage.split("_")))
def get_stage_csp_class(stage, class_type):
"""
given a stage name and class_type return the class
class_type is either 'payload' or 'result'
"""
cls_name = f"{_stage_to_classname(stage)}CSP{class_type.capitalize()}"
try:
return getattr(
importlib.import_module("atst.domain.csp.cloud.models"), cls_name
)
except AttributeError:
print("could not import CSP Result class <%s>" % cls_name)
@add_state_features(Tags) @add_state_features(Tags)
class StateMachineWithTags(Machine): class StateMachineWithTags(Machine):
pass pass
@ -138,11 +158,7 @@ class PortfolioStateMachine(
self.fail_stage(stage) self.fail_stage(stage)
# TODO: Determine best place to do this, maybe @reconstructor # TODO: Determine best place to do this, maybe @reconstructor
csp = event.kwargs.get("csp") self.csp = app.csp.cloud
if csp is not None:
self.csp = AzureCSP(app).cloud
else:
self.csp = MockCSP(app).cloud
try: try:
func_name = f"create_{stage}" func_name = f"create_{stage}"

View File

@ -10,7 +10,7 @@ from tests.factories import (
from atst.models import FSMStates, PortfolioStateMachine, TaskOrder from atst.models import FSMStates, PortfolioStateMachine, TaskOrder
from atst.models.mixins.state_machines import AzureStages, StageStates, compose_state from atst.models.mixins.state_machines import AzureStages, StageStates, compose_state
from atst.models.portfolio import Portfolio from atst.models.portfolio import Portfolio
from atst.domain.csp import get_stage_csp_class from atst.models.portfolio_state_machine import get_stage_csp_class
# TODO: Write failure case tests # TODO: Write failure case tests