Skip to content

Commit

Permalink
Added changes for making connection param configurable via command (#173
Browse files Browse the repository at this point in the history
)

(cherry picked from commit c9380a3257a7624f0c3e7c358584086bc20de4be)
  • Loading branch information
abhijeetkaurav1st committed Mar 6, 2023
1 parent aaacb8c commit 57e67b4
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 11 deletions.
20 changes: 15 additions & 5 deletions calm/dsl/api/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from requests.packages.urllib3.util.retry import Retry

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

urllib3.disable_warnings()
LOG = get_logging_handle(__name__)
Expand Down Expand Up @@ -91,7 +92,6 @@ def __init__(
base_url="",
response_processor=None,
session_headers=None,
retries_enabled=True,
**kwargs,
):
"""Generic client to connect to server.
Expand All @@ -110,7 +110,6 @@ def __init__(
session_headers (dict): session headers dict
auth_type (str): auth type that needs to be used by the client
auth (tuple): authentication
retries_enabled (bool): Flag to perform retries (default: false)
Returns:
Raises:
"""
Expand All @@ -126,7 +125,6 @@ def __init__(
self.scheme = scheme
self.auth_type = auth_type
self.response_processor = response_processor
self.retries_enabled = retries_enabled

def connect(self):
"""Connect to api server, create http session pool.
Expand All @@ -137,7 +135,9 @@ def connect(self):
Raises:
"""

if self.retries_enabled:
context = get_context()
connection_config = context.get_connection_config()
if connection_config["retries_enabled"]:
retry_strategy = Retry(
total=3,
status_forcelist=[429, 500, 502, 503, 504],
Expand Down Expand Up @@ -194,9 +194,9 @@ def _call(
verify=True,
headers=None,
files=None,
timeout=(5, 30), # (connection timeout, read timeout)
ignore_error=False,
warning_msg="",
**kwargs,
):
"""Private method for making http request to calm
Expand All @@ -206,9 +206,19 @@ def _call(
cookies (dict): cookies that need to be forwarded.
request_json (dict): request data
request_params (dict): request params
timeout (touple): (connection timeout, read timeout)
Returns:
(tuple (requests.Response, dict)): Response
"""
timeout = kwargs.get("timeout", None)
if not timeout:
context = get_context()
connection_config = context.get_connection_config()
timeout = (
connection_config["connection_timeout"],
connection_config["read_timeout"],
)

if request_params is None:
request_params = {}

Expand Down
41 changes: 41 additions & 0 deletions calm/dsl/cli/init_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
get_default_config_file,
get_default_db_file,
get_default_local_dir,
get_default_connection_config,
init_context,
)
from calm.dsl.db import init_db_handle
Expand Down Expand Up @@ -159,6 +160,12 @@ def set_server_details(
# Default log-level
log_level = "INFO"

# Default connection params
default_connection_config = get_default_connection_config()
retries_enabled = default_connection_config["retries_enabled"]
connection_timeout = default_connection_config["connection_timeout"]
read_timeout = default_connection_config["read_timeout"]

# Do not prompt for init config variables, Take default values for init.ini file
config_file = config_file or get_default_config_file()
local_dir = local_dir or get_default_local_dir()
Expand Down Expand Up @@ -199,6 +206,9 @@ def set_server_details(
config_file=config_file,
db_location=db_file,
local_dir=local_dir,
retries_enabled=retries_enabled,
connection_timeout=connection_timeout,
read_timeout=read_timeout,
)

# Updating context for using latest config data
Expand Down Expand Up @@ -325,6 +335,24 @@ def init_dsl_runbook(runbook_name, dir_name):
help="Path to local directory for storing secrets",
)
@click.option("--log_level", "-l", default=None, help="Default log level")
@click.option(
"--retries-enabled/--retries-disabled",
"-re/-rd",
default=None,
help="Retries enabled/disabled",
)
@click.option(
"--connection-timeout",
"-ct",
type=int,
help="connection timeout",
)
@click.option(
"--read-timeout",
"-rt",
type=int,
help="read timeout",
)
@click.argument("config_file", required=False)
def _set_config(
host,
Expand All @@ -336,6 +364,9 @@ def _set_config(
log_level,
config_file,
local_dir,
retries_enabled,
connection_timeout,
read_timeout,
):
"""writes the configuration to config files i.e. config.ini and init.ini
Expand Down Expand Up @@ -393,6 +424,13 @@ def _set_config(
db_location = db_location or init_config["DB"]["location"]
local_dir = local_dir or init_config["LOCAL_DIR"]["location"]

# Get connection config
connection_config = ContextObj.get_connection_config()
if retries_enabled is None: # Not supplied in command
retries_enabled = connection_config["retries_enabled"]
connection_timeout = connection_timeout or connection_config["connection_timeout"]
read_timeout = read_timeout or connection_config["read_timeout"]

# Set the dsl configuration
set_dsl_config(
host=host,
Expand All @@ -404,6 +442,9 @@ def _set_config(
log_level=log_level,
local_dir=local_dir,
config_file=config_file,
retries_enabled=retries_enabled,
connection_timeout=connection_timeout,
read_timeout=read_timeout,
)
LOG.info("Configuration changed successfully")

Expand Down
3 changes: 2 additions & 1 deletion calm/dsl/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .config import get_config_handle, set_dsl_config
from .context import get_context, init_context
from .context import get_context, init_context, get_default_connection_config
from .init_config import (
get_default_config_file,
get_default_db_file,
Expand All @@ -15,4 +15,5 @@
"get_default_config_file",
"get_default_db_file",
"get_default_local_dir",
"get_default_connection_config",
]
9 changes: 7 additions & 2 deletions calm/dsl/config/config.ini.jinja2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% macro ConfigTemplate(ip, port, username, password, project_name, db_location, log_level) -%}
{% macro ConfigTemplate(ip, port, username, password, project_name, db_location, log_level, retries_enabled, connection_timeout, read_timeout) -%}

[SERVER]
pc_ip = {{ip}}
Expand All @@ -12,8 +12,13 @@ name = {{project_name}}
[LOG]
level = {{log_level}}

[CONNECTION]
retries_enabled = {{retries_enabled}}
connection_timeout = {{connection_timeout}}
read_timeout = {{read_timeout}}

[CATEGORIES]
{%- endmacro %}


{{ConfigTemplate(ip, port, username, password, project_name, db_location, log_level)}}
{{ConfigTemplate(ip, port, username, password, project_name, db_location, log_level, retries_enabled, connection_timeout, read_timeout)}}
60 changes: 58 additions & 2 deletions calm/dsl/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def __init__(self, config_file):
config_obj[section][k] = v

self._CONFIG = config_obj
self._CONFIG_PARSER_OBJECT = config

def get_server_config(self):
"""returns server config"""
Expand Down Expand Up @@ -63,6 +64,25 @@ def get_categories_config(self):
else:
return {}

def get_connection_config(self):
"""returns connection config"""

connection_config = {}
if "CONNECTION" in self._CONFIG_PARSER_OBJECT:
for k, v in self._CONFIG_PARSER_OBJECT.items("CONNECTION"):
if k == "retries_enabled":
connection_config[k] = self._CONFIG_PARSER_OBJECT[
"CONNECTION"
].getboolean(k)
elif k in ["connection_timeout", "read_timeout"]:
connection_config[k] = self._CONFIG_PARSER_OBJECT[
"CONNECTION"
].getint(k)
else:
connection_config[k] = v

return connection_config


class ConfigHandle:
def __init__(self, config_file=None):
Expand All @@ -78,6 +98,7 @@ def __init__(self, config_file=None):
self.project_config = config_obj.get_project_config()
self.log_config = config_obj.get_log_config()
self.categories_config = config_obj.get_categories_config()
self.connection_config = config_obj.get_connection_config()

def get_server_config(self):
"""returns server configuration"""
Expand All @@ -99,6 +120,11 @@ def get_categories_config(self):

return self.categories_config

def get_connection_config(self):
"""returns connection config"""

return self.connection_config

@classmethod
def get_init_config(cls):

Expand All @@ -114,6 +140,9 @@ def _render_config_template(
password,
project_name,
log_level,
retries_enabled,
connection_timeout,
read_timeout,
schema_file="config.ini.jinja2",
):
"""renders the config template"""
Expand All @@ -128,19 +157,40 @@ def _render_config_template(
password=password,
project_name=project_name,
log_level=log_level,
retries_enabled=retries_enabled,
connection_timeout=connection_timeout,
read_timeout=read_timeout,
)
return text.strip() + os.linesep

@classmethod
def update_config_file(
cls, config_file, host, port, username, password, project_name, log_level
cls,
config_file,
host,
port,
username,
password,
project_name,
log_level,
retries_enabled,
connection_timeout,
read_timeout,
):
"""Updates the config file data"""

LOG.debug("Rendering configuration template")
make_file_dir(config_file)
text = cls._render_config_template(
host, port, username, password, project_name, log_level
host,
port,
username,
password,
project_name,
log_level,
retries_enabled,
connection_timeout,
read_timeout,
)

LOG.debug("Writing configuration to '{}'".format(config_file))
Expand All @@ -164,6 +214,9 @@ def set_dsl_config(
db_location,
local_dir,
config_file,
retries_enabled,
connection_timeout,
read_timeout,
):

"""
Expand All @@ -188,4 +241,7 @@ def set_dsl_config(
password=password,
project_name=project_name,
log_level=log_level,
retries_enabled=retries_enabled,
connection_timeout=connection_timeout,
read_timeout=read_timeout,
)
Loading

0 comments on commit 57e67b4

Please sign in to comment.