Skip to content

Commit

Permalink
Made optional the need of passing a project when initializing Calm fo… (
Browse files Browse the repository at this point in the history
#252)

Made optional the need of passing a project when initializing Calm-DSL.

---------

Co-authored-by: Abhijeet Kaurav <[email protected]>
(cherry picked from commit 2a97bb69c2d10ac0ae03cf97d26e581a4d0a6222)
  • Loading branch information
dwivediprab authored and abhijeetkaurav1st committed Aug 8, 2023
1 parent a95e61a commit 32ea9e8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 26 deletions.
10 changes: 10 additions & 0 deletions calm/dsl/builtins/models/blueprint_payload.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import sys

from calm.dsl.config import get_context
from calm.dsl.constants import DSL_CONFIG
from calm.dsl.log import get_logging_handle

from .entity import EntityType, Entity
from .validator import PropertyValidator
from .blueprint import BlueprintType
from .simple_blueprint import SimpleBlueprintType
from .calm_ref import Ref

LOG = get_logging_handle(__name__)


# Blueprint Payload

Expand Down Expand Up @@ -61,6 +67,10 @@ def create_blueprint_payload(UserBlueprint, metadata={}):
# Project will be taken from config if not provided
if not metadata.get("project_reference", {}):
project_name = project_config["name"]
if project_name == DSL_CONFIG.EMPTY_PROJECT_NAME:
LOG.error(DSL_CONFIG.EMPTY_PROJECT_MESSAGE)
sys.exit("Invalid project configuration")

metadata["project_reference"] = Ref.Project(project_name)

# User will be taken from config if not provided
Expand Down
6 changes: 5 additions & 1 deletion calm/dsl/cli/bps.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
from calm.dsl.builtins import Brownfield as BF
from calm.dsl.providers import get_provider
from calm.dsl.providers.plugins.ahv_vm.main import AhvNew
from calm.dsl.constants import CACHE
from calm.dsl.constants import CACHE, DSL_CONFIG
from calm.dsl.log import get_logging_handle
from calm.dsl.builtins.models.calm_ref import Ref

Expand Down Expand Up @@ -326,6 +326,10 @@ def compile_blueprint(bp_file, brownfield_deployment_file=None):
]
else:
project_name = project_config["name"]
if project_name == DSL_CONFIG.EMPTY_PROJECT_NAME:
LOG.error(DSL_CONFIG.EMPTY_PROJECT_MESSAGE)
sys.exit("Invalid project configuration")

bp_payload["metadata"]["project_reference"] = Ref.Project(project_name)
else:
if isinstance(UserBlueprint, type(VmBlueprint)):
Expand Down
43 changes: 23 additions & 20 deletions calm/dsl/cli/init_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from calm.dsl.init import init_bp, init_runbook
from calm.dsl.providers import get_provider_types
from calm.dsl.store import Version
from calm.dsl.constants import POLICY, STRATOS
from calm.dsl.constants import POLICY, STRATOS, DSL_CONFIG

from .main import init, set
from calm.dsl.log import get_logging_handle
Expand Down Expand Up @@ -158,7 +158,9 @@ def set_server_details(
port = port or click.prompt("Port", default="9440")
username = username or click.prompt("Username", default="admin")
password = password or click.prompt("Password", default="", hide_input=True)
project_name = project_name or click.prompt("Project", default="default")
project_name = project_name or click.prompt(
"Project", default=DSL_CONFIG.EMPTY_PROJECT_NAME
)

# Default log-level
log_level = "INFO"
Expand Down Expand Up @@ -250,15 +252,15 @@ def set_server_details(
else:
LOG.debug("Stratos is not supported")
stratos_status = False

LOG.info("Verifying the project details")
project_name_uuid_map = client.project.get_name_uuid_map(
params={"filter": "name=={}".format(project_name)}
)
if not project_name_uuid_map:
LOG.error("Project '{}' not found !!!".format(project_name))
sys.exit(-1)
LOG.info("Project '{}' verified successfully".format(project_name))
if project_name != DSL_CONFIG.EMPTY_PROJECT_NAME:
LOG.info("Verifying the project details")
project_name_uuid_map = client.project.get_name_uuid_map(
params={"filter": "name=={}".format(project_name)}
)
if not project_name_uuid_map:
LOG.error("Project '{}' not found !!!".format(project_name))
sys.exit(-1)
LOG.info("Project '{}' verified successfully".format(project_name))

# Writing configuration to file
set_dsl_config(
Expand Down Expand Up @@ -455,7 +457,7 @@ def _set_config(
password = password or server_config["pc_password"]

project_config = ContextObj.get_project_config()
project_name = project_name or project_config.get("name") or "default"
project_name = project_name or project_config.get("name")

LOG.info("Checking if Calm is enabled on Server")

Expand Down Expand Up @@ -533,14 +535,15 @@ def _set_config(
LOG.debug("Stratos is not supported")
stratos_status = False

LOG.info("Verifying the project details")
project_name_uuid_map = client.project.get_name_uuid_map(
params={"filter": "name=={}".format(project_name)}
)
if not project_name_uuid_map:
LOG.error("Project '{}' not found !!!".format(project_name))
sys.exit(-1)
LOG.info("Project '{}' verified successfully".format(project_name))
if project_name != DSL_CONFIG.EMPTY_PROJECT_NAME:
LOG.info("Verifying the project details")
project_name_uuid_map = client.project.get_name_uuid_map(
params={"filter": "name=={}".format(project_name)}
)
if not project_name_uuid_map:
LOG.error("Project '{}' not found !!!".format(project_name))
sys.exit(-1)
LOG.info("Project '{}' verified successfully".format(project_name))

log_config = ContextObj.get_log_config()
log_level = log_level or log_config.get("level") or "INFO"
Expand Down
9 changes: 9 additions & 0 deletions calm/dsl/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from calm.dsl.log import get_logging_handle
from calm.dsl.config import get_context
from calm.dsl.store import Cache
from calm.dsl.constants import DSL_CONFIG

from .version_validator import validate_version
from .click_options import simple_verbosity_option, show_trace_option
Expand Down Expand Up @@ -75,6 +76,14 @@ def main(ctx, config_file, sync):
if config_file:
ContextObj = get_context()
ContextObj.update_config_file_context(config_file=config_file)

ContextObj = get_context()
project_config = ContextObj.get_project_config()
project_name = project_config.get("name")

if project_name == DSL_CONFIG.EMPTY_PROJECT_NAME:
LOG.warning(DSL_CONFIG.EMPTY_PROJECT_MESSAGE)

if sync:
Cache.sync()

Expand Down
6 changes: 5 additions & 1 deletion calm/dsl/cli/runbooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from calm.dsl.api import get_api_client
from calm.dsl.log import get_logging_handle
from calm.dsl.builtins.models.calm_ref import Ref
from calm.dsl.constants import CACHE
from calm.dsl.constants import CACHE, DSL_CONFIG
from calm.dsl.store import Cache
from calm.dsl.tools import get_module_from_file
from .utils import (
Expand Down Expand Up @@ -160,6 +160,10 @@ def compile_runbook(runbook_file):
]
else:
project_name = project_config["name"]
if project_name == DSL_CONFIG.EMPTY_PROJECT_NAME:
LOG.error(DSL_CONFIG.EMPTY_PROJECT_MESSAGE)
sys.exit("Invalid project configuration")

runbook_payload["metadata"]["project_reference"] = Ref.Project(project_name)

return runbook_payload
Expand Down
6 changes: 2 additions & 4 deletions calm/dsl/config/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .env_config import EnvConfig
from .config import get_config_handle
from calm.dsl.log import get_logging_handle
from calm.dsl.constants import DSL_CONFIG

LOG = get_logging_handle(__name__)

Expand Down Expand Up @@ -100,10 +101,7 @@ def get_project_config(self):

config = self.project_config
if not config.get("name"):
LOG.warning(
"Default project not found in config file or environment('CALM_DSL_DEFAULT_PROJECT' variable). Setting it to 'default' project"
)
config["name"] = "default"
config["name"] = DSL_CONFIG.EMPTY_PROJECT_NAME

return config

Expand Down
5 changes: 5 additions & 0 deletions calm/dsl/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,8 @@ class OPENAPI_TYPE:
GCP = "app_gcp_account"
CREDENTIAL_PROVIDER = "app_credential_provider_account"
CUSTOM_PROVIDER = "app_custom_provider_account"


class DSL_CONFIG:
EMPTY_PROJECT_NAME = "-"
EMPTY_PROJECT_MESSAGE = "Project configuration not available. Use command `calm set config -pj <project_name>` to set it."

0 comments on commit 32ea9e8

Please sign in to comment.