Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to pass in ENV to workstation #3

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "cloud-workstation"
version = "0.1.3"
version = "0.1.4"
requires-python = ">=3.8"
readme = "README.md"
description = ""
Expand Down
11 changes: 11 additions & 0 deletions src/workstation/cli/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ def common_options(func): # noqa: D103
type=str,
metavar="<str>",
)
@click.option(
"--envs",
"-e",
"envs",
damienrj marked this conversation as resolved.
Show resolved Hide resolved
type=(str, str),
multiple=True,
help="Environment variables to set at runtime.",
metavar="<key value>",
)
@click.pass_context
def create(
context: click.Context,
Expand All @@ -190,6 +199,7 @@ def create(
project: Optional[str],
proxy: Optional[str],
no_proxy: Optional[str],
envs: Tuple[Tuple[str, str]],
**kwargs,
):
"""Create a workstation."""
Expand Down Expand Up @@ -227,6 +237,7 @@ def create(
location=location,
proxy=proxy,
no_proxy=no_proxy,
envs=envs,
)

config_manager.write_ssh_config(
Expand Down
15 changes: 14 additions & 1 deletion src/workstation/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from typing import Dict, List, Optional
from typing import Dict, List, Optional, Tuple

from google.api_core.exceptions import AlreadyExists
from google.api_core.operation import Operation
Expand Down Expand Up @@ -107,6 +107,7 @@ def create_workstation(
user: str,
proxy: Optional[str] = None,
no_proxy: Optional[str] = None,
envs: Optional[Tuple[Tuple[str, str]]] = None,
damienrj marked this conversation as resolved.
Show resolved Hide resolved
) -> Workstation:
"""
Create a new workstation with the specified configuration.
Expand Down Expand Up @@ -151,6 +152,18 @@ def create_workstation(
env["no_proxy"] = no_proxy
env["NO_PROXY"] = no_proxy

if envs:
user_envs = dict(envs)
# ensure that no duplicate keys are added to env
for key, value in user_envs.items():
if key not in env:
env[key] = value
damienrj marked this conversation as resolved.
Show resolved Hide resolved
else:
logger.warning(
f"Environment variable {key} already exists in the environment, skipping"
)


request = workstations_v1beta.CreateWorkstationRequest(
parent=f"projects/{project}/locations/{location}/workstationClusters/{cluster}/workstationConfigs/{config}",
workstation_id=name,
Expand Down
1 change: 0 additions & 1 deletion tests/test_ConfigManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from pathlib import Path

import pytest

from workstation.config import ConfigManager


Expand Down
35 changes: 35 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import getpass
from unittest.mock import patch

from workstation.core import create_workstation


@patch("workstation.core.workstations_v1beta.WorkstationsClient")
@patch("workstation.core.config_manager.write_configuration")
def test_create_workstation_env_dict(mock_write_configuration, mock_workstations_client):
mock_client_instance = mock_workstations_client.return_value
mock_operation = mock_client_instance.create_workstation.return_value
mock_operation.result.return_value = {}

envs = (("KEY1", "VALUE1"), ("KEY2", "VALUE2"))
create_workstation(
project="test-project",
location="us-central1",
cluster="default-cluster",
config="default-config",
name="test-workstation",
account="test-account",
user="test-user",
envs=envs,
)

expected_env = {
"LDAP": "test-user",
"ACCOUNT": "test-account",
"KEY1": "VALUE1",
"KEY2": "VALUE2",
}

assert mock_client_instance.create_workstation.call_count == 1
_, kwargs = mock_client_instance.create_workstation.call_args
assert kwargs["request"].workstation.env == expected_env
4 changes: 3 additions & 1 deletion tests/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import pytest
from click.testing import CliRunner

from workstation.cli import crud


Expand Down Expand Up @@ -119,3 +118,6 @@ def test_list(mock_get_gcloud_config, mock_check_gcloud_auth, mock_list_workstat
"]\n"
)
assert result.output == expected_json_output



1 change: 0 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest
from pytest_mock import MockerFixture

from workstation.utils import get_instance_assignment, process_entry


Expand Down
Loading