diff --git a/softpack_core/schemas/environment.py b/softpack_core/schemas/environment.py index cd2b169..3498010 100644 --- a/softpack_core/schemas/environment.py +++ b/softpack_core/schemas/environment.py @@ -234,9 +234,23 @@ def create(cls, env: EnvironmentInput) -> CreateResponse: # type: ignore if result is not None: return result - response = cls.create_new_env(env, Artifacts.built_by_softpack_file) - if not isinstance(response, CreateEnvironmentSuccess): - return response + name = env.name + version = 1 + + while True: + env.name = name + "-" + str(version) + response = cls.create_new_env( + env, Artifacts.built_by_softpack_file + ) + if isinstance(response, CreateEnvironmentSuccess): + break + + if not isinstance(response, EnvironmentAlreadyExistsError): + return response + + version += 1 + + env.name = name # TODO: remove hard-coding of URL. # Send build request @@ -244,6 +258,7 @@ def create(cls, env: EnvironmentInput) -> CreateResponse: # type: ignore "http://0.0.0.0:7080/environments/build", json={ "name": f"{env.path}/{env.name}", + "version": str(version), "model": { "description": env.description, "packages": [f"{pkg.name}" for pkg in env.packages], @@ -308,56 +323,6 @@ def create_new_env( message="Successfully created environment in artifacts repo" ) - @classmethod - def update( - cls, - env: EnvironmentInput, - current_path: str, - current_name: str, - ) -> UpdateResponse: # type: ignore - """Update an Environment. - - Args: - env: Details of the updated environment - path: The path of the current environment - name: The name of the current environment - - Returns: - A message confirming the success or failure of the operation. - """ - result = env.validate() - if result is not None: - return result - - if current_name == "" or current_path == "": - return InvalidInputError(message="current values must be supplied") - - if env.path != current_path or env.name != current_name: - return InvalidInputError( - message=("change of name or path not currently supported") - ) - - result2 = cls.check_env_exists(Path(current_path, current_name)) - if result2 is not None: - return result2 - - httpx.post( - "http://0.0.0.0:7080/environments/build", - json={ - "name": f"{env.path}/{env.name}", - "model": { - "description": env.description, - "packages": [pkg.name for pkg in env.packages or []], - }, - }, - ) - - # TODO: validate the post worked - - return UpdateEnvironmentSuccess( - message="Successfully updated environment" - ) - @classmethod def check_env_exists( cls, path: Path @@ -632,7 +597,6 @@ class Mutation: """GraphQL mutation schema.""" createEnvironment: CreateResponse = Environment.create # type: ignore - updateEnvironment: UpdateResponse = Environment.update # type: ignore deleteEnvironment: DeleteResponse = Environment.delete # type: ignore # writeArtifact: WriteArtifactResponse = ( # type: ignore # Environment.write_artifact diff --git a/tests/integration/test_environment.py b/tests/integration/test_environment.py index 11678e6..314bfbc 100644 --- a/tests/integration/test_environment.py +++ b/tests/integration/test_environment.py @@ -51,13 +51,21 @@ def test_create(httpx_post, testable_env_input: EnvironmentInput) -> None: path = Path( Environment.artifacts.environments_root, testable_env_input.path, - testable_env_input.name, + testable_env_input.name + "-1", Environment.artifacts.built_by_softpack_file, ) assert file_in_remote(path) result = Environment.create(testable_env_input) - assert isinstance(result, EnvironmentAlreadyExistsError) + assert isinstance(result, CreateEnvironmentSuccess) + + path = Path( + Environment.artifacts.environments_root, + testable_env_input.path, + testable_env_input.name + "-2", + Environment.artifacts.built_by_softpack_file, + ) + assert file_in_remote(path) orig_name = testable_env_input.name testable_env_input.name = "" @@ -79,6 +87,7 @@ def builder_called_correctly( "http://0.0.0.0:7080/environments/build", json={ "name": f"{testable_env_input.path}/{testable_env_input.name}", + "version": "1", "model": { "description": testable_env_input.description, "packages": [ @@ -89,42 +98,6 @@ def builder_called_correctly( ) -def test_update(httpx_post, testable_env_input) -> None: - result = Environment.create(testable_env_input) - assert isinstance(result, CreateEnvironmentSuccess) - httpx_post.assert_called_once() - - testable_env_input.description = "updated description" - result = Environment.update( - testable_env_input, - testable_env_input.path, - testable_env_input.name, - ) - assert isinstance(result, UpdateEnvironmentSuccess) - - builder_called_correctly(httpx_post, testable_env_input) - - result = Environment.update( - testable_env_input, "invalid/path", "invalid_name" - ) - assert isinstance(result, InvalidInputError) - - testable_env_input.name = "" - result = Environment.update( - testable_env_input, - testable_env_input.path, - testable_env_input.name, - ) - assert isinstance(result, InvalidInputError) - - testable_env_input.name = "invalid_name" - testable_env_input.path = "invalid/path" - result = Environment.update( - testable_env_input, "invalid/path", "invalid_name" - ) - assert isinstance(result, EnvironmentNotFoundError) - - def test_delete(httpx_post, testable_env_input) -> None: result = Environment.delete( testable_env_input.name, testable_env_input.path @@ -138,13 +111,13 @@ def test_delete(httpx_post, testable_env_input) -> None: path = Path( Environment.artifacts.environments_root, testable_env_input.path, - testable_env_input.name, + testable_env_input.name + "-1", Artifacts.built_by_softpack_file, ) assert file_in_remote(path) result = Environment.delete( - testable_env_input.name, testable_env_input.path + testable_env_input.name + "-1", testable_env_input.path ) assert isinstance(result, DeleteEnvironmentSuccess) @@ -157,7 +130,7 @@ async def test_write_artifact(httpx_post, testable_env_input): result = await Environment.write_artifact( file=upload, - folder_path=f"{testable_env_input.path}/{testable_env_input.name}", + folder_path=f"{testable_env_input.path}/{testable_env_input.name}-1", file_name=upload.filename, ) assert isinstance(result, InvalidInputError) @@ -168,7 +141,7 @@ async def test_write_artifact(httpx_post, testable_env_input): result = await Environment.write_artifact( file=upload, - folder_path=f"{testable_env_input.path}/{testable_env_input.name}", + folder_path=f"{testable_env_input.path}/{testable_env_input.name}-1", file_name=upload.filename, ) assert isinstance(result, WriteArtifactSuccess) @@ -176,7 +149,7 @@ async def test_write_artifact(httpx_post, testable_env_input): path = Path( Environment.artifacts.environments_root, testable_env_input.path, - testable_env_input.name, + testable_env_input.name + "-1", upload.filename, ) assert file_in_remote(path) @@ -209,12 +182,12 @@ async def test_states(httpx_post, testable_env_input): result = await Environment.write_artifact( file=upload, - folder_path=f"{testable_env_input.path}/{testable_env_input.name}", + folder_path=f"{testable_env_input.path}/{testable_env_input.name}-1", file_name=upload.filename, ) assert isinstance(result, WriteArtifactSuccess) - env = get_env_from_iter(testable_env_input.name) + env = get_env_from_iter(testable_env_input.name + "-1") assert env is not None assert any(p.name == "zlib" for p in env.packages) assert any(p.version == "v1.1" for p in env.packages) @@ -227,12 +200,12 @@ async def test_states(httpx_post, testable_env_input): result = await Environment.write_artifact( file=upload, - folder_path=f"{testable_env_input.path}/{testable_env_input.name}", + folder_path=f"{testable_env_input.path}/{testable_env_input.name}-1", file_name=upload.filename, ) assert isinstance(result, WriteArtifactSuccess) - env = get_env_from_iter(testable_env_input.name) + env = get_env_from_iter(testable_env_input.name + "-1") assert env is not None assert env.type == Artifacts.built_by_softpack assert env.state == State.ready