Skip to content

Commit

Permalink
[STREAM-218] Refactor profile creating
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk92 committed Oct 25, 2021
1 parent cbb94f6 commit 2a7e78a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 59 deletions.
2 changes: 1 addition & 1 deletion streamingcli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def project_init(project_name: str):


@project.command()
@click.argument('--docker-image-tag')
@click.argument('docker-image-tag')
@click.option('--profile',
help='Profile name to use')
@click.option('--vvp-url', 'ververica_url',
Expand Down
1 change: 0 additions & 1 deletion streamingcli/platform/ververica/deployment_adapter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
from typing import Optional

import click
Expand Down
54 changes: 54 additions & 0 deletions streamingcli/profile/profile_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from dataclasses import asdict, dataclass, field, replace
from pathlib import Path
from typing import Any, Dict, Optional, Type

from marshmallow import Schema
from marshmallow_dataclass import class_schema
from streamingcli.Config import DEFAULT_PROFILE_DIR
from yaml import SafeLoader, load, safe_dump


@dataclass(repr=True)
class ScliProfile:
profile_name: str
ververica_url: Optional[str] = field(default=None)
ververica_namespace: Optional[str] = field(default=None)
ververica_deployment_target: Optional[str] = field(default=None)
ververica_api_token: Optional[str] = field(default=None)
docker_registry_url: Optional[str] = field(default=None)

@dataclass
class ScliProfiles:
profiles: Dict[str, ScliProfile] = field(default_factory=dict)

class ProfileAdapter:

@staticmethod
def save_profile(scli_profile: ScliProfile):
profiles = ProfileAdapter.load_profiles()
profiles_dict = profiles.profiles
profiles_dict[scli_profile.profile_name] = scli_profile
content = safe_dump(asdict(replace(profiles, profiles = profiles_dict)))
with open(DEFAULT_PROFILE_DIR, "w") as file:
file.write(content)

@staticmethod
def get_profile(profile_name: str) -> Optional[ScliProfile]:
profiles_dict = ProfileAdapter.load_profiles().profiles
return profiles_dict.get(profile_name)

@staticmethod
def load_profiles() -> ScliProfiles:
profiles_path = Path(DEFAULT_PROFILE_DIR)

if (profiles_path.is_file() is False):
return ScliProfiles(profiles={})

with open(profiles_path, "r") as file:
content = file.read()
return ProfileAdapter.strict_load_yaml(content, ScliProfiles)

@staticmethod
def strict_load_yaml(yaml: str, loaded_type: Type[Any]):
schema = class_schema(loaded_type)
return schema().load(load(yaml, Loader=SafeLoader))
55 changes: 2 additions & 53 deletions streamingcli/profile/profile_command.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
from dataclasses import asdict, dataclass, field, replace
from pathlib import Path
from typing import Any, Dict, Optional, Type

import click
from marshmallow import Schema
from marshmallow_dataclass import class_schema
from streamingcli.Config import DEFAULT_PROFILE_DIR
from yaml import SafeLoader, load, safe_dump


@dataclass(repr=True)
class ScliProfile:
profile_name: str
ververica_url: Optional[str] = field(default=None)
ververica_namespace: Optional[str] = field(default=None)
ververica_deployment_target: Optional[str] = field(default=None)
ververica_api_token: Optional[str] = field(default=None)
docker_registry_url: Optional[str] = field(default=None)

@dataclass
class ScliProfiles:
profiles: Dict[str, ScliProfile] = field(default_factory=dict)
from streamingcli.profile.profile_adapter import ProfileAdapter, ScliProfile


class ProfileCommand:
Expand All @@ -40,35 +19,5 @@ def create_profile(profile_name: str,
docker_registry_url=docker_registry_url,
ververica_api_token=ververica_api_token
)
ProfileCommand.save_profile(scli_profile=scli_profile)
ProfileAdapter.save_profile(scli_profile=scli_profile)
click.echo(f"Scli profile {profile_name} saved")

@staticmethod
def save_profile(scli_profile: ScliProfile):
profiles = ProfileCommand.load_profiles()
profiles_dict = profiles.profiles
profiles_dict[scli_profile.profile_name] = scli_profile
content = safe_dump(asdict(replace(profiles, profiles = profiles_dict)))
with open(DEFAULT_PROFILE_DIR, "w") as file:
file.write(content)

@staticmethod
def get_profile(profile_name: str) -> Optional[ScliProfile]:
profiles_dict = ProfileCommand.load_profiles().profiles
return profiles_dict.get(profile_name)

@staticmethod
def load_profiles() -> ScliProfiles:
profiles_path = Path(DEFAULT_PROFILE_DIR)

if (profiles_path.is_file() is False):
return ScliProfiles(profiles={})

with open(profiles_path, "r") as file:
content = file.read()
return ProfileCommand.strict_load_yaml(content, ScliProfiles)

@staticmethod
def strict_load_yaml(yaml: str, loaded_type: Type[Any]):
schema = class_schema(loaded_type)
return schema().load(load(yaml, Loader=SafeLoader))
7 changes: 3 additions & 4 deletions streamingcli/project/deploy_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from streamingcli.Config import PROFILE_ENV_VARIABLE_NAME
from streamingcli.platform.ververica.deployment_adapter import \
DeploymentAdapter
from streamingcli.profile.profile_command import ProfileCommand, ScliProfile
from streamingcli.profile.profile_adapter import ProfileAdapter, ScliProfile
from streamingcli.project.local_project_config import LocalProjectConfigIO
from streamingcli.project.template_loader import TemplateLoader
from streamingcli.project.yaml_merger import YamlMerger
Expand Down Expand Up @@ -36,7 +36,7 @@ def deploy_project(docker_image_tag: str,
profile_name = ProjectDeployer.get_profile_name(profile_name=profile)

if profile_name is not None:
profile_data = ProfileCommand.get_profile(profile_name=profile_name)
profile_data = ProfileAdapter.get_profile(profile_name=profile_name)
else:
profile_data = ScliProfile(profile_name="temporary")

Expand Down Expand Up @@ -87,7 +87,6 @@ def update_profile_data(profile_data, ververica_url, ververica_namespace, verver
}

profile_data = replace(profile_data, **non_empty_deployment_params)
click.echo(profile_data)
return profile_data

@staticmethod
Expand Down Expand Up @@ -117,5 +116,5 @@ def generate_project_template(
project_name=project_name,
docker_registry_url=docker_registry_url,
docker_image_tag=docker_image_tag,
deployment_target_id=deployment_target_name
deployment_target_name=deployment_target_name
)

0 comments on commit 2a7e78a

Please sign in to comment.