Fixes: mircostrategy test connection (#18889)

This commit is contained in:
Suman Maharana 2024-12-03 21:43:15 +05:30 committed by GitHub
parent e582cb4d9c
commit 11b0a9c376
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 51 additions and 25 deletions

View File

@ -37,12 +37,13 @@ from metadata.utils.logger import ingestion_logger
logger = ingestion_logger()
API_VERSION = "MicroStrategyLibrary/api"
HEADERS = {"Content-Type": "application/json"}
APPLICATION_TYPE = 35
class MicroStrategyClient:
"""
Client Handling API communication with Metabase
Client Handling API communication with MicroStrategy
"""
def _get_base_url(self, path=None):
@ -56,18 +57,43 @@ class MicroStrategyClient:
):
self.config = config
self.auth_params: AuthHeaderCookie = self._get_auth_header_and_cookies()
self.auth_params = self._get_auth_header_and_cookies()
client_config = ClientConfig(
base_url=clean_uri(str(self.config.hostPort)),
api_version=API_VERSION,
extra_headers=self.auth_params.auth_header,
extra_headers=self.auth_params.auth_header if self.auth_params else None,
allow_redirects=True,
cookies=self.auth_params.auth_cookies,
cookies=self.auth_params.auth_cookies if self.auth_params else None,
)
self.client = REST(client_config)
self._set_api_session()
def get_auth_params(self) -> AuthHeaderCookie:
"""
Test whether we can fetch auth_token from the api
"""
data = {
"username": self.config.username,
"password": self.config.password.get_secret_value(),
"loginMode": int(self.config.loginMode),
"applicationType": APPLICATION_TYPE,
}
response = requests.post(
url=self._get_base_url("auth/login"), json=data, headers=HEADERS, timeout=60
)
response.raise_for_status()
if (
not response.ok
or response.status_code != 204
or "X-MSTR-AuthToken" not in response.headers
):
raise SourceConnectionException(
f"Failed to Fetch Token, please validate your credentials and login_mode : {response.text}"
)
return AuthHeaderCookie(
auth_header=response.headers, auth_cookies=response.cookies
)
def _get_auth_header_and_cookies(self) -> Optional[AuthHeaderCookie]:
"""
@ -77,35 +103,25 @@ class MicroStrategyClient:
https://demo.microstrategy.com/MicroStrategyLibrary/api-docs/index.html#/Authentication/postLogin
"""
try:
data = {
"username": self.config.username,
"password": self.config.password.get_secret_value(),
"loginMode": self.config.loginMode,
"applicationType": APPLICATION_TYPE,
}
response = requests.post(
url=self._get_base_url("auth/login"), data=data, timeout=60
)
if not response:
raise SourceConnectionException()
return AuthHeaderCookie(
auth_header=response.headers, auth_cookies=response.cookies
)
auth_data = self.get_auth_params()
if auth_data:
self._set_api_session(auth_data)
return auth_data
except Exception as exc:
logger.debug(traceback.format_exc())
logger.error(
f"Failed to fetch the auth header and cookies due to [{exc}], please validate credentials"
f"Failed to fetch the auth header and cookies due to : [{exc}]"
)
return None
def _set_api_session(self) -> bool:
def _set_api_session(self, auth_data: AuthHeaderCookie) -> bool:
"""
Set the user api session to active this will keep the connection alive
"""
api_session = requests.put(
url=self._get_base_url("sessions"),
headers=self.auth_params.auth_header,
cookies=self.auth_params.auth_cookies,
headers=auth_data.auth_header,
cookies=auth_data.auth_cookies,
timeout=60,
)
if api_session.ok:

View File

@ -46,7 +46,10 @@ def test_connection(
of a metadata workflow or during an Automation Workflow
"""
test_fn = {"GetProjects": client.get_projects_list}
test_fn = {
"GetToken": client.get_auth_params,
"GetProjects": client.get_projects_list,
}
return test_connection_steps(
metadata=metadata,

View File

@ -3,6 +3,13 @@
"displayName": "MicroStrategy Test Connection",
"description": "This Test Connection validates the access against the server and basic metadata extraction of dashboards and charts.",
"steps": [
{
"name": "GetToken",
"description": "Fetch the Auth Token",
"errorMessage": "Failed to fetch auth token, please validate the credentials or if user has access to fetch token or if you are using the correct login mode",
"shortCircuit": true,
"mandatory": true
},
{
"name": "GetProjects",
"description": "List all the projects available to the user",

View File

@ -27,6 +27,6 @@ $$
$$section
### Login Mode $(id="loginMode")
Login Mode for Microstrategy's REST API connection. You can authenticate with one of the following authentication modes: `Standard (1)`, `Anonymous (8)`. Default will be `Anonymous (8)`.
Login Mode for Microstrategy's REST API connection. You can authenticate with one of the following authentication modes: `Standard (1)`, `Anonymous (8)`. Default will be `Standard (1)`.
If you're using demo account for Microstrategy, it will be needed to authenticate through loginMode `8`.
$$