Add query for getting environments pending atat user creation
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
from sqlalchemy import text
|
||||||
from sqlalchemy.orm.exc import NoResultFound
|
from sqlalchemy.orm.exc import NoResultFound
|
||||||
|
|
||||||
from atst.database import db
|
from atst.database import db
|
||||||
@@ -104,8 +105,8 @@ class Environments(object):
|
|||||||
return environment
|
return environment
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_environments_pending_creation(cls, now) -> [str]:
|
def base_provision_query(cls, now):
|
||||||
query = (
|
return (
|
||||||
db.session.query(Environment.id)
|
db.session.query(Environment.id)
|
||||||
.join(Application)
|
.join(Application)
|
||||||
.join(Portfolio)
|
.join(Portfolio)
|
||||||
@@ -113,6 +114,18 @@ class Environments(object):
|
|||||||
.join(CLIN)
|
.join(CLIN)
|
||||||
.filter(CLIN.start_date <= now)
|
.filter(CLIN.start_date <= now)
|
||||||
.filter(CLIN.end_date > now)
|
.filter(CLIN.end_date > now)
|
||||||
.filter(Environment.cloud_id == None)
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_environments_pending_creation(cls, now) -> [str]:
|
||||||
|
query = cls.base_provision_query(now).filter(Environment.cloud_id == None)
|
||||||
|
return [environment_id for (environment_id,) in query.all()]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_environments_pending_atat_user_creation(cls, now) -> [str]:
|
||||||
|
query = (
|
||||||
|
cls.base_provision_query(now)
|
||||||
|
.filter(Environment.cloud_id != None)
|
||||||
|
.filter(Environment.root_user_info == text("'null'"))
|
||||||
)
|
)
|
||||||
return [environment_id for (environment_id,) in query.all()]
|
return [environment_id for (environment_id,) in query.all()]
|
||||||
|
@@ -126,3 +126,11 @@ def dispatch_create_environment(self):
|
|||||||
pendulum.now()
|
pendulum.now()
|
||||||
):
|
):
|
||||||
create_environment.delay(environment_id=environment_id, atat_user_id="TODO")
|
create_environment.delay(environment_id=environment_id, atat_user_id="TODO")
|
||||||
|
|
||||||
|
|
||||||
|
@celery.task(bind=True)
|
||||||
|
def dispatch_create_atat_admin_user(self):
|
||||||
|
for environment_id in Environments.get_environments_pending_atat_user_creation(
|
||||||
|
pendulum.now()
|
||||||
|
):
|
||||||
|
create_atat_admin_user.delay(environment_id=environment_id, atat_user_id="TODO")
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
import pytest
|
import pytest
|
||||||
import pendulum
|
import pendulum
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
from atst.domain.environments import Environments
|
from atst.domain.environments import Environments
|
||||||
from atst.domain.environment_roles import EnvironmentRoles
|
from atst.domain.environment_roles import EnvironmentRoles
|
||||||
@@ -136,18 +137,27 @@ def test_update_environment():
|
|||||||
assert environment.name == "name 2"
|
assert environment.name == "name 2"
|
||||||
|
|
||||||
|
|
||||||
class TestGetEnvironmentsPendingCreate:
|
class EnvQueryTest:
|
||||||
NOW = pendulum.now()
|
@property
|
||||||
YESTERDAY = NOW.subtract(days=1)
|
def NOW(self):
|
||||||
TOMORROW = NOW.add(days=1)
|
return pendulum.now()
|
||||||
|
|
||||||
def create_portfolio_with_clins(self, start_and_end_dates):
|
@property
|
||||||
|
def YESTERDAY(self):
|
||||||
|
return self.NOW.subtract(days=1)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def TOMORROW(self):
|
||||||
|
return self.NOW.add(days=1)
|
||||||
|
|
||||||
|
def create_portfolio_with_clins(self, start_and_end_dates, env_data=None):
|
||||||
|
env_data = env_data or {}
|
||||||
return PortfolioFactory.create(
|
return PortfolioFactory.create(
|
||||||
applications=[
|
applications=[
|
||||||
{
|
{
|
||||||
"name": "Mos Eisley",
|
"name": "Mos Eisley",
|
||||||
"description": "Where Han shot first",
|
"description": "Where Han shot first",
|
||||||
"environments": [{"name": "thebar"}],
|
"environments": [{"name": "thebar", **env_data}],
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
task_orders=[
|
task_orders=[
|
||||||
@@ -160,6 +170,8 @@ class TestGetEnvironmentsPendingCreate:
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetEnvironmentsPendingCreate(EnvQueryTest):
|
||||||
def test_with_expired_clins(self, session):
|
def test_with_expired_clins(self, session):
|
||||||
self.create_portfolio_with_clins([(self.YESTERDAY, self.YESTERDAY)])
|
self.create_portfolio_with_clins([(self.YESTERDAY, self.YESTERDAY)])
|
||||||
assert len(Environments.get_environments_pending_creation(self.NOW)) == 0
|
assert len(Environments.get_environments_pending_creation(self.NOW)) == 0
|
||||||
@@ -173,3 +185,32 @@ class TestGetEnvironmentsPendingCreate:
|
|||||||
def test_with_future_clins(self, session):
|
def test_with_future_clins(self, session):
|
||||||
self.create_portfolio_with_clins([(self.TOMORROW, self.TOMORROW)])
|
self.create_portfolio_with_clins([(self.TOMORROW, self.TOMORROW)])
|
||||||
assert len(Environments.get_environments_pending_creation(self.NOW)) == 0
|
assert len(Environments.get_environments_pending_creation(self.NOW)) == 0
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetEnvironmentsPendingAtatUserCreation(EnvQueryTest):
|
||||||
|
def test_with_provisioned_environment(self):
|
||||||
|
self.create_portfolio_with_clins(
|
||||||
|
[(self.YESTERDAY, self.TOMORROW)],
|
||||||
|
{"cloud_id": uuid4().hex, "root_user_info": {}},
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
len(Environments.get_environments_pending_atat_user_creation(self.NOW)) == 0
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_with_unprovisioned_environment(self):
|
||||||
|
self.create_portfolio_with_clins(
|
||||||
|
[(self.YESTERDAY, self.TOMORROW)],
|
||||||
|
{"cloud_id": uuid4().hex, "root_user_info": None},
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
len(Environments.get_environments_pending_atat_user_creation(self.NOW)) == 1
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_with_unprovisioned_expired_clins_environment(self):
|
||||||
|
self.create_portfolio_with_clins(
|
||||||
|
[(self.YESTERDAY, self.YESTERDAY)],
|
||||||
|
{"cloud_id": uuid4().hex, "root_user_info": None},
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
len(Environments.get_environments_pending_atat_user_creation(self.NOW)) == 0
|
||||||
|
)
|
||||||
|
Reference in New Issue
Block a user