teradataml 20.0.0.4__py3-none-any.whl → 20.0.0.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of teradataml might be problematic. Click here for more details.

Files changed (107) hide show
  1. teradataml/LICENSE-3RD-PARTY.pdf +0 -0
  2. teradataml/README.md +86 -13
  3. teradataml/__init__.py +2 -1
  4. teradataml/_version.py +2 -2
  5. teradataml/analytics/analytic_function_executor.py +7 -12
  6. teradataml/analytics/json_parser/analytic_functions_argument.py +4 -0
  7. teradataml/analytics/sqle/__init__.py +16 -1
  8. teradataml/analytics/utils.py +15 -1
  9. teradataml/automl/__init__.py +290 -106
  10. teradataml/automl/autodataprep/__init__.py +471 -0
  11. teradataml/automl/data_preparation.py +29 -10
  12. teradataml/automl/data_transformation.py +11 -0
  13. teradataml/automl/feature_engineering.py +64 -4
  14. teradataml/automl/feature_exploration.py +639 -25
  15. teradataml/automl/model_training.py +1 -1
  16. teradataml/clients/auth_client.py +2 -2
  17. teradataml/common/constants.py +61 -26
  18. teradataml/common/messagecodes.py +2 -1
  19. teradataml/common/messages.py +5 -4
  20. teradataml/common/utils.py +255 -37
  21. teradataml/context/context.py +225 -87
  22. teradataml/data/apriori_example.json +22 -0
  23. teradataml/data/docs/sqle/docs_17_20/Apriori.py +138 -0
  24. teradataml/data/docs/sqle/docs_17_20/NERExtractor.py +121 -0
  25. teradataml/data/docs/sqle/docs_17_20/NGramSplitter.py +3 -3
  26. teradataml/data/docs/sqle/docs_17_20/SMOTE.py +212 -0
  27. teradataml/data/docs/sqle/docs_17_20/TextMorph.py +119 -0
  28. teradataml/data/docs/sqle/docs_17_20/TextParser.py +54 -3
  29. teradataml/data/docs/uaf/docs_17_20/ACF.py +1 -1
  30. teradataml/data/docs/uaf/docs_17_20/ArimaEstimate.py +2 -2
  31. teradataml/data/docs/uaf/docs_17_20/ArimaXEstimate.py +2 -2
  32. teradataml/data/docs/uaf/docs_17_20/DFFT.py +1 -1
  33. teradataml/data/docs/uaf/docs_17_20/DFFT2.py +1 -1
  34. teradataml/data/docs/uaf/docs_17_20/DFFT2Conv.py +1 -1
  35. teradataml/data/docs/uaf/docs_17_20/DFFTConv.py +1 -1
  36. teradataml/data/docs/uaf/docs_17_20/FilterFactory1d.py +4 -4
  37. teradataml/data/docs/uaf/docs_17_20/GenseriesSinusoids.py +2 -2
  38. teradataml/data/docs/uaf/docs_17_20/GoldfeldQuandt.py +2 -2
  39. teradataml/data/docs/uaf/docs_17_20/HoltWintersForecaster.py +6 -6
  40. teradataml/data/docs/uaf/docs_17_20/LineSpec.py +1 -1
  41. teradataml/data/docs/uaf/docs_17_20/LinearRegr.py +1 -1
  42. teradataml/data/docs/uaf/docs_17_20/Matrix2Image.py +4 -4
  43. teradataml/data/docs/uaf/docs_17_20/MultivarRegr.py +1 -1
  44. teradataml/data/docs/uaf/docs_17_20/PACF.py +1 -1
  45. teradataml/data/docs/uaf/docs_17_20/PowerSpec.py +2 -2
  46. teradataml/data/docs/uaf/docs_17_20/PowerTransform.py +3 -3
  47. teradataml/data/docs/uaf/docs_17_20/Resample.py +5 -5
  48. teradataml/data/docs/uaf/docs_17_20/SAX.py +3 -3
  49. teradataml/data/docs/uaf/docs_17_20/SignifPeriodicities.py +1 -1
  50. teradataml/data/docs/uaf/docs_17_20/SimpleExp.py +1 -1
  51. teradataml/data/docs/uaf/docs_17_20/Smoothma.py +3 -3
  52. teradataml/data/docs/uaf/docs_17_20/UNDIFF.py +1 -1
  53. teradataml/data/jsons/sqle/17.20/NGramSplitter.json +6 -6
  54. teradataml/data/jsons/sqle/17.20/TD_Apriori.json +181 -0
  55. teradataml/data/jsons/sqle/17.20/TD_NERExtractor.json +145 -0
  56. teradataml/data/jsons/sqle/17.20/TD_SMOTE.json +267 -0
  57. teradataml/data/jsons/sqle/17.20/TD_TextMorph.json +134 -0
  58. teradataml/data/jsons/sqle/17.20/TD_TextParser.json +114 -9
  59. teradataml/data/jsons/sqle/20.00/AI_AnalyzeSentiment.json +328 -0
  60. teradataml/data/jsons/sqle/20.00/AI_AskLLM.json +420 -0
  61. teradataml/data/jsons/sqle/20.00/AI_DetectLanguage.json +343 -0
  62. teradataml/data/jsons/sqle/20.00/AI_ExtractKeyPhrases.json +328 -0
  63. teradataml/data/jsons/sqle/20.00/AI_MaskPII.json +328 -0
  64. teradataml/data/jsons/sqle/20.00/AI_RecognizeEntities.json +328 -0
  65. teradataml/data/jsons/sqle/20.00/AI_RecognizePIIEntities.json +328 -0
  66. teradataml/data/jsons/sqle/20.00/AI_TextClassifier.json +359 -0
  67. teradataml/data/jsons/sqle/20.00/AI_TextEmbeddings.json +360 -0
  68. teradataml/data/jsons/sqle/20.00/AI_TextSummarize.json +343 -0
  69. teradataml/data/jsons/sqle/20.00/AI_TextTranslate.json +343 -0
  70. teradataml/data/jsons/sqle/20.00/TD_SMOTE.json +2 -2
  71. teradataml/data/jsons/sqle/20.00/TD_VectorDistance.json +1 -1
  72. teradataml/data/ner_dict.csv +8 -0
  73. teradataml/data/ner_input_eng.csv +7 -0
  74. teradataml/data/ner_rule.csv +5 -0
  75. teradataml/data/pos_input.csv +40 -0
  76. teradataml/data/tdnerextractor_example.json +14 -0
  77. teradataml/data/teradataml_example.json +13 -0
  78. teradataml/data/textmorph_example.json +5 -0
  79. teradataml/data/to_num_data.csv +4 -0
  80. teradataml/data/tochar_data.csv +5 -0
  81. teradataml/data/trans_dense.csv +16 -0
  82. teradataml/data/trans_sparse.csv +55 -0
  83. teradataml/dataframe/copy_to.py +37 -26
  84. teradataml/dataframe/data_transfer.py +61 -45
  85. teradataml/dataframe/dataframe.py +130 -50
  86. teradataml/dataframe/dataframe_utils.py +15 -2
  87. teradataml/dataframe/functions.py +109 -9
  88. teradataml/dataframe/sql.py +328 -76
  89. teradataml/dbutils/dbutils.py +33 -13
  90. teradataml/dbutils/filemgr.py +14 -10
  91. teradataml/lib/aed_0_1.dll +0 -0
  92. teradataml/opensource/_base.py +6 -157
  93. teradataml/options/configure.py +4 -5
  94. teradataml/scriptmgmt/UserEnv.py +305 -38
  95. teradataml/scriptmgmt/lls_utils.py +376 -130
  96. teradataml/store/__init__.py +1 -1
  97. teradataml/table_operators/Apply.py +16 -1
  98. teradataml/table_operators/Script.py +20 -1
  99. teradataml/table_operators/table_operator_util.py +58 -9
  100. teradataml/utils/dtypes.py +2 -1
  101. teradataml/utils/internal_buffer.py +22 -2
  102. teradataml/utils/validators.py +313 -57
  103. {teradataml-20.0.0.4.dist-info → teradataml-20.0.0.5.dist-info}/METADATA +89 -14
  104. {teradataml-20.0.0.4.dist-info → teradataml-20.0.0.5.dist-info}/RECORD +107 -77
  105. {teradataml-20.0.0.4.dist-info → teradataml-20.0.0.5.dist-info}/WHEEL +0 -0
  106. {teradataml-20.0.0.4.dist-info → teradataml-20.0.0.5.dist-info}/top_level.txt +0 -0
  107. {teradataml-20.0.0.4.dist-info → teradataml-20.0.0.5.dist-info}/zip-safe +0 -0
@@ -10,36 +10,40 @@ teradataml load library service wrappers.
10
10
  All teradataml wrappers to provide interface to load library service stored procedures
11
11
  from Open Analytics Framework.
12
12
  """
13
+ import base64
13
14
  import concurrent.futures
14
15
  import functools
15
16
  import json
16
17
  import operator
17
18
  import os
19
+ import warnings
20
+ from json.decoder import JSONDecodeError
21
+ from time import sleep, time
22
+ from urllib.parse import urlparse
18
23
 
19
24
  import pandas as pd
20
- import requests
21
25
 
22
- from json.decoder import JSONDecodeError
23
26
  from teradataml import configure
24
27
  from teradataml.context.context import _get_user, get_connection
25
- from teradataml.common.constants import HTTPRequest, AsyncStatusColumns
26
- from teradataml.common.deprecations import argument_deprecation
28
+ from teradataml.common.constants import HTTPRequest, AsyncStatusColumns, AuthMechs
29
+ from teradataml.clients.auth_client import _AuthWorkflow
30
+ from teradataml.clients.pkce_client import _DAWorkflow
31
+ from teradataml.common.constants import (AsyncOpStatus, AsyncStatusColumns,
32
+ HTTPRequest)
27
33
  from teradataml.common.exceptions import TeradataMlException
28
- from teradataml.common.messages import Messages
29
34
  from teradataml.common.messagecodes import MessageCodes
35
+ from teradataml.common.messages import Messages
30
36
  from teradataml.common.utils import UtilFuncs
31
- from teradataml.clients.pkce_client import _DAWorkflow
32
- from teradataml.clients.auth_client import _AuthWorkflow
37
+ from teradataml.context.context import _get_user, get_connection
38
+ from teradataml.scriptmgmt.UserEnv import (UserEnv, _AuthToken,
39
+ _get_auth_token, _get_ues_url,
40
+ _process_ues_response, _get_ccp_url)
41
+ from teradataml.telemetry_utils.queryband import collect_queryband
33
42
  from teradataml.utils.internal_buffer import _InternalBuffer
34
- from teradataml.scriptmgmt.UserEnv import UserEnv, _get_auth_token, \
35
- _process_ues_response, _get_ues_url, _AuthToken
36
- from teradataml.utils.validators import _Validators
37
- from time import time, sleep
38
- import warnings
39
- import webbrowser
40
- from urllib.parse import parse_qs, urlparse
41
43
  from teradataml.utils.utils import _async_run_id_info
42
- from teradataml.telemetry_utils.queryband import collect_queryband
44
+ from teradataml.utils.validators import _Validators
45
+
46
+
43
47
 
44
48
  @collect_queryband(queryband="LstBsEnv")
45
49
  def list_base_envs():
@@ -197,7 +201,7 @@ def list_user_envs(env_name=None, **kwargs):
197
201
  ... 'python_3.9',
198
202
  ... 'Sales team environment.',
199
203
  ... conda_env=True)
200
- Conda environment creation initiated
204
+ Conda environment creation initiated.
201
205
  User environment 'Sales_cond_env' created.
202
206
 
203
207
  # Example 1: List all available user environments.
@@ -303,7 +307,7 @@ def list_user_envs(env_name=None, **kwargs):
303
307
 
304
308
  try:
305
309
  response = UtilFuncs._http_request(_get_ues_url(), headers=_get_auth_token())
306
- # Below condition is special case handeling when remove_all_envs() used by user, remove_all_envs()
310
+ # Below condition is special case handling when remove_all_envs() used by user, remove_all_envs()
307
311
  # removes all the envs which result in a status_code 404 and due to which warnings provided in
308
312
  # list_user_envs() not appears.
309
313
  if response.status_code == 404 and "No user environments found." in response.text:
@@ -339,7 +343,7 @@ def list_user_envs(env_name=None, **kwargs):
339
343
  # Return the DataFrame if not empty.
340
344
  if len(pandas_df) > 0:
341
345
  return pandas_df
342
-
346
+
343
347
  print("No user environment(s) found.")
344
348
  except (TeradataMlException, RuntimeError):
345
349
  raise
@@ -451,8 +455,8 @@ def __create_envs(template):
451
455
  except Exception as lib_installation_failure:
452
456
  error_code = MessageCodes.FUNC_EXECUTION_FAILED
453
457
  error_msg = Messages.get_message(error_code,
454
- "'install_lib' request for enviornment: '{}'".format(env_name),
455
- '\n'+str(lib_installation_failure))
458
+ "'install_lib' request for enviornment: '{}'".format(env_name),
459
+ '\n' + str(lib_installation_failure))
456
460
  print(error_msg)
457
461
  errored = errored or True
458
462
  pass
@@ -747,9 +751,18 @@ def create_env(env_name=None, base_env=None, desc=None, template=None, conda_env
747
751
  >>> fraud_detection_env = create_env('Fraud_detection_conda',
748
752
  ... 'python_3.8',
749
753
  ... 'Fraud detection through time matching',
750
- conda_env=True)
751
- Conda environment creation initiated
752
- User environment 'Fraud_detection_conda' created.
754
+ ... conda_env=True)
755
+ Conda environment creation initiated.
756
+ User environment 'Fraud_detection_conda' created.
757
+
758
+ # Example 5: Create a Conda R 4.2 environment with given name and
759
+ # description in the Vantage.
760
+ >>> conda_r_env = create_env('conda_r_env',
761
+ ... 'r_4.2',
762
+ ... 'Conda R environment',
763
+ ... conda_env=True)
764
+ Conda environment creation initiated.
765
+ User environment 'conda_r_env' created.
753
766
  """
754
767
 
755
768
  # Either env_name or template can be used.
@@ -774,7 +787,7 @@ def create_env(env_name=None, base_env=None, desc=None, template=None, conda_env
774
787
  # Or if base_env is provided and not in the list of base envs.
775
788
  # Note: By default python base env is obtained.
776
789
  if configure.ues_url is not None and \
777
- get_connection() is not None:
790
+ get_connection() is not None:
778
791
  # Check if base_env is provided or not in the list of base envs.
779
792
 
780
793
  # Check if user requested for conda environment but do not specify the base_env.
@@ -787,11 +800,11 @@ def create_env(env_name=None, base_env=None, desc=None, template=None, conda_env
787
800
  # Check if base_env provided or not. If provided, check if it is available in
788
801
  # the list of base envs. If not available, set base_env to the default python base env.
789
802
  if not base_env or \
790
- base_env.lower() not in list_base_envs()['base_name'].str.lower().to_list():
803
+ base_env.lower() not in list_base_envs()['base_name'].str.lower().to_list():
791
804
  # Print warning message if base_env provided is not available.
792
805
  if base_env:
793
- print(f"Note: The specified base environment '{base_env}' is unavailable. "\
794
- "Using the default base environment as specified in the documentation.")
806
+ print(f"Note: The specified base environment '{base_env}' is unavailable. " \
807
+ "Using the default base environment as specified in the documentation.")
795
808
  # Set base_env to the default
796
809
  base_env = __get_default_base_env()
797
810
  if not desc:
@@ -804,21 +817,25 @@ def create_env(env_name=None, base_env=None, desc=None, template=None, conda_env
804
817
  response = UtilFuncs._http_request(
805
818
  _get_ues_url(conda_env=conda_env), HTTPRequest.POST, headers=_get_auth_token(), json=data)
806
819
 
807
- # UES reponse.
808
- resp = _process_ues_response(api_name="create_env", response=response)
820
+ # Validate UES response.
821
+ _process_ues_response(api_name="create_env", response=response)
809
822
 
810
823
  msg = "User environment '{}' created."
811
824
 
812
825
  if conda_env:
813
- print("Conda environment creation initiated")
826
+ print("Conda environment creation initiated.")
814
827
  # Get claim_id.
815
828
  claim_id = response.json().get("claim_id", "")
816
- # Poll the claim_id status.
829
+
830
+ # Since create_env() for conda environment is internally
831
+ # asynchronous but exposed as synchronous API, keep polling
832
+ # the status of underlying asynchronous operation until
833
+ # it is either successful or errored.
817
834
  __poll_claim_id_status(claim_id, "create_env")
818
835
  print(msg.format(env_name))
819
836
 
820
837
  # Return an instance of class UserEnv.
821
- return UserEnv(env_name, base_env, desc)
838
+ return UserEnv(env_name, base_env, desc, conda_env)
822
839
 
823
840
  except (TeradataMlException, RuntimeError):
824
841
  raise
@@ -855,7 +872,7 @@ def _async_run_status_open_af(claim_id):
855
872
  __get_claim_id_status('278381bf-e3b3-47ff-9ba5-c3b5d9007363')
856
873
  """
857
874
  # Get the claim id status.
858
- resp_data = __get_status(claim_id)
875
+ resp_data = _get_status(claim_id)
859
876
 
860
877
  desc = _async_run_id_info.get(claim_id, {}).get("description", "Unknown")
861
878
  get_details = lambda data: {AsyncStatusColumns.ADDITIONAL_DETAILS.value:
@@ -871,7 +888,7 @@ def _async_run_status_open_af(claim_id):
871
888
  return [get_details(sub_step) for sub_step in resp_data]
872
889
 
873
890
 
874
- def __get_status(claim_id):
891
+ def _get_status(claim_id):
875
892
  """
876
893
  DESCRIPTION:
877
894
  Internal function to get the status of a claim_id using
@@ -891,7 +908,7 @@ def __get_status(claim_id):
891
908
  None
892
909
 
893
910
  EXAMPLES:
894
- __get_status('278381bf-e3b3-47ff-9ba5-c3b5d9007363')
911
+ _get_status('278381bf-e3b3-47ff-9ba5-c3b5d9007363')
895
912
  """
896
913
  # Get the claim id status
897
914
  response = UtilFuncs._http_request(_get_ues_url(env_type="fm",
@@ -1030,6 +1047,9 @@ def __manage_envs(env_name=None, api_name="remove_env", **kwargs):
1030
1047
 
1031
1048
  try:
1032
1049
  # Get the ues url for corresponding API.
1050
+ # While deleting environment, endpoint UES URL for deleting
1051
+ # normal and conda environment is same, unlike creating
1052
+ # normal and conda environment.
1033
1053
  ues_url = _get_ues_url(env_name=env_name, api_name=api_name) if api_name == "remove_env" \
1034
1054
  else _get_ues_url(remove_all_envs=True, api_name=api_name)
1035
1055
 
@@ -1048,7 +1068,7 @@ def __manage_envs(env_name=None, api_name="remove_env", **kwargs):
1048
1068
  if api_name == "remove_env":
1049
1069
  msg = "{2}list_user_envs(). If environment is not removed, " \
1050
1070
  "check the status of asynchronous call using" \
1051
- " async_run_status('{1}') or get_env('{0}').status('{1}')".\
1071
+ " async_run_status('{1}') or get_env('{0}').status('{1}')". \
1052
1072
  format(env_name, claim_id, msg)
1053
1073
  else:
1054
1074
  msg = "{0}async_run_status('{1}')".format(msg, claim_id)
@@ -1093,30 +1113,37 @@ def __poll_claim_id_status(claim_id, api_name="remove_env"):
1093
1113
  Default Value: remove_env
1094
1114
  Types: str
1095
1115
 
1096
-
1097
-
1098
1116
  RETURNS:
1099
1117
  None.
1100
1118
 
1101
1119
  RAISES:
1102
- None.
1120
+ TeradataMlException
1103
1121
 
1104
1122
  EXAMPLES:
1105
1123
  __poll_claim_id_status('cf7245f0-e962-4451-addf-efa7e123998d')
1106
1124
  """
1125
+ err_details = None
1107
1126
  while True:
1108
1127
  sleep(2)
1109
1128
 
1110
1129
  # Poll the claim id to get the status.
1111
- resp_data = __get_status(claim_id)
1130
+ resp_data = _get_status(claim_id)
1112
1131
 
1113
1132
  # Breaking condition -
1114
1133
  # For create_env and remove_env: Check for the 'Finished' stage in the list of resp.
1115
1134
  # For remove_all_envs: above cond. and No user envs condition should break it .
1116
1135
  for data in resp_data:
1117
- if ("Finished" in data["stage"]) or \
1118
- (api_name in ["create_env", "remove_all_envs"] and "Errored" in data["stage"]):
1136
+ if AsyncOpStatus.FINISHED.value in data["stage"]:
1119
1137
  return
1138
+ elif AsyncOpStatus.ERRED.value in data["stage"]:
1139
+ err_details = data["details"]
1140
+ break
1141
+ if err_details:
1142
+ break
1143
+
1144
+ raise TeradataMlException(Messages.get_message(MessageCodes.FUNC_EXECUTION_FAILED,
1145
+ api_name, err_details),
1146
+ MessageCodes.FUNC_EXECUTION_FAILED)
1120
1147
 
1121
1148
 
1122
1149
  @collect_queryband(queryband="GtEnv")
@@ -1191,7 +1218,9 @@ def get_env(env_name):
1191
1218
  # Return an instance of class UserEnv.
1192
1219
  return UserEnv(userenv_row.env_name.values[0],
1193
1220
  userenv_row.base_env_name.values[0],
1194
- userenv_row.env_description.values[0])
1221
+ userenv_row.env_description.values[0],
1222
+ userenv_row.conda.values[0]
1223
+ )
1195
1224
  except (TeradataMlException, RuntimeError) as tdemsg:
1196
1225
  # TeradataMlException and RuntimeError are raised by list_user_envs.
1197
1226
  # list_user_envs should be replaced with get_env in the error
@@ -1446,7 +1475,7 @@ def _remove_all_envs(env_type, **kwargs):
1446
1475
  if env_type.capitalize() == "Py":
1447
1476
  env_type = ["Python", "python"]
1448
1477
  else:
1449
- env_type = ["R"]
1478
+ env_type = ["R", "r"]
1450
1479
  env_type_message = "R"
1451
1480
  asynchronous = kwargs.get("asynchronous", False)
1452
1481
 
@@ -1485,7 +1514,7 @@ def _remove_all_envs(env_type, **kwargs):
1485
1514
  if len(failed_envs) > 0:
1486
1515
  emsg = ""
1487
1516
  for env, tdemsg in failed_envs.items():
1488
- emsg += "\nUser environment '{0}' failed to remove. Reason: {1}"\
1517
+ emsg += "\nUser environment '{0}' failed to remove. Reason: {1}" \
1489
1518
  .format(env, tdemsg.args[0])
1490
1519
  msg_code = MessageCodes.FUNC_EXECUTION_FAILED
1491
1520
  error_msg = Messages.get_message(msg_code, "remove_all_envs()", emsg)
@@ -1598,23 +1627,94 @@ def get_user_env():
1598
1627
  return configure._default_user_env
1599
1628
 
1600
1629
 
1630
+ def _validate_jwt_token(base_url, token_data):
1631
+ """
1632
+ DESCRIPTION:
1633
+ Function to validate the authentication token generated using PAT and PEM file.
1634
+
1635
+ PARAMETERS:
1636
+ base_url:
1637
+ Required Argument.
1638
+ Specifies the endpoint URL for a given environment on VantageCloud Lake.
1639
+ Types: str
1640
+
1641
+ token_data:
1642
+ Required Argument.
1643
+ Specifies the JWT token to be authenticated.
1644
+
1645
+ RETURNS:
1646
+ Boolan flag representing validation status.
1647
+ * True: Indicates that token is valid.
1648
+ * None: Indicates that token is not validated.
1649
+
1650
+ RAISES:
1651
+ TeradataMlException
1652
+
1653
+ EXAMPLES:
1654
+ Example 1: Validate JWT token.
1655
+ >>> _validate_jwt_token(base_url, token_data)
1656
+
1657
+ """
1658
+ # Extract environment id from base_url.
1659
+ try:
1660
+ url_parser = urlparse(base_url)
1661
+ env_id = url_parser.path.split("accounts/")[1].split("/")[0]
1662
+ if not env_id:
1663
+ raise
1664
+ except Exception:
1665
+ raise TeradataMlException(Messages.get_message(MessageCodes.FUNC_EXECUTION_FAILED,
1666
+ "set_auth_token",
1667
+ "Use valid value for 'base_url'"),
1668
+ MessageCodes.FUNC_EXECUTION_FAILED)
1669
+
1670
+ valid_token = None
1671
+ try:
1672
+ response = UtilFuncs._http_request(url="{}/{}/{}/{}".format(_get_ccp_url(base_url),
1673
+ "api", "accounts", env_id),
1674
+ method_type=HTTPRequest.GET,
1675
+ headers={"Authorization": "Bearer {}".format(token_data)})
1676
+ if 200 <= response.status_code < 300: # Authorized access.
1677
+ valid_token = True
1678
+ elif 400 <= response.status_code < 500: # Unauthorized access.
1679
+ valid_token = False
1680
+ except:
1681
+ pass
1682
+
1683
+ if valid_token is False:
1684
+ raise TeradataMlException(Messages.get_message(MessageCodes.FUNC_EXECUTION_FAILED,
1685
+ "set_auth_token",
1686
+ "Use valid values for input arguments ['base_url',"
1687
+ " 'pat_token', 'pem_file']."),
1688
+ MessageCodes.FUNC_EXECUTION_FAILED)
1689
+ return valid_token
1690
+
1691
+
1601
1692
  @collect_queryband(queryband="StAthTkn")
1602
1693
  def set_auth_token(base_url=None, client_id=None, pat_token=None, pem_file=None, **kwargs):
1603
1694
  """
1604
1695
  DESCRIPTION:
1605
- Function to set the Authentication token to connect to User Environment Service
1606
- in VantageCloud Lake.
1607
- Note:
1608
- User must have a privilege to login with a NULL password to use set_auth_token().
1609
- Please refer to GRANT LOGON section in Teradata Documentation for more details.
1610
- If base_url and client_id are specified then authentication is through OAuth.
1611
- If base_url, pat_token, pem_file are specified then authentication is through PAT.
1612
- Refresh token still works but only for OAuth authentication.
1696
+ Function to set the authentication token required to access services running on
1697
+ Teradata Vantage.
1698
+ Notes:
1699
+ * User must have a privilege to login with a NULL password to use set_auth_token().
1700
+ Refer to GRANT LOGON section in Teradata Documentation for more details.
1701
+ * When "auth_mech" is not specified, arguments are used in the following combination
1702
+ to derive authentication mechanism.
1703
+ * If "base_url" and "client_id" are specified then token generation is done through OAuth.
1704
+ * If "base_url", "pat_token", "pem_file" are specified then token generation is done using PAT.
1705
+ * If "base_url", "username" and "password" are specified then authentication is done via
1706
+ Basic authentication mechanism using user credentials.
1707
+ * If "base_url" and "auth_token" are specified then readily available token is used.
1708
+ * If only "base_url" is specified then token generation is done through OAuth.
1709
+ * Refresh token works only for OAuth authentication.
1710
+ * Use the argument "kid" only when key used during the pem file generation is different
1711
+ from pem file name. For example, if you use the key as 'key1' while generating pem file
1712
+ and the name of the pem file is `key1(1).pem`, then pass value 'key1' to the argument "kid".
1613
1713
 
1614
1714
  PARAMETERS:
1615
1715
  base_url:
1616
1716
  Required Argument.
1617
- Specifies the CCP endpoint URL.
1717
+ Specifies the endpoint URL for a given environment on Teradata Vantage system.
1618
1718
  Types: str
1619
1719
 
1620
1720
  client_id:
@@ -1631,33 +1731,68 @@ def set_auth_token(base_url=None, client_id=None, pat_token=None, pem_file=None,
1631
1731
  pem_file:
1632
1732
  Required, if PAT authentication is to be used, optional otherwise.
1633
1733
  Specifies the path to private key file which is generated from VantageCloud Lake Console.
1734
+ Note:
1735
+ Teradata recommends not to change the name of the file generated from VantageCloud Lake
1736
+ Console. If the name of the file is changed, then authentication token generated from
1737
+ this function will not work.
1634
1738
  Types: str
1635
1739
 
1636
1740
  **kwargs:
1637
1741
  username:
1742
+ Optional Argument.
1638
1743
  Specifies the user for which authentication is to be requested.
1639
1744
  If not specified, then user associated with current connection is used.
1640
- Note:
1641
- 1. Use this option only if name of the database username has lower case letters.
1642
- 2. This option is used only for PAT and not for OAuth.
1745
+ Notes:
1746
+ * Use this option only if name of the database username has lowercase letters.
1747
+ * This option is used only for PAT and not for OAuth.
1643
1748
  Types: str
1644
1749
 
1645
1750
  expiration_time:
1646
- Specifies the expiration time of the token in seconds. After expiry time JWT token expires and
1647
- UserEnv methods does not work, user should regenerate the token.
1751
+ Optional Argument.
1752
+ Specifies the expiration time of the token in seconds. After expiry time, JWT
1753
+ token expires and UserEnv methods does not work, user should regenerate the token.
1648
1754
  Note:
1649
- This option is used only for PAT and not for OAuth.
1755
+ * This option is used only for PAT and not for OAuth.
1650
1756
  Default Value: 31536000
1651
1757
  Types: int
1652
1758
 
1653
1759
  auth_token:
1654
- Optional Parameter.
1655
- Specifies the authentication token to connect to VantageCloud Lake.
1760
+ Optional Argument.
1761
+ Specifies the authentication token required to access services running
1762
+ on Teradata Vantage.
1656
1763
  Notes:
1657
- * if "auth_token" is set through this function, then this function
1658
- should always be used only after create_context.
1659
- * use this option only if user has got JWT token and wants to set the same
1660
- instead of generating it again from this function.
1764
+ * If "auth_token" is set through this function, then this function
1765
+ should always be used only after create_context().
1766
+ * Use this option only if user has got JWT token and wants to set
1767
+ the same instead of generating it again from this function.
1768
+
1769
+ Types: str
1770
+
1771
+ kid:
1772
+ Optional Argument.
1773
+ Specifies the name of the key which is used while generating 'pem_file'.
1774
+ Types: str
1775
+
1776
+ password:
1777
+ Optional Argument.
1778
+ Specifies the password for database user to be used for Basic authentication.
1779
+ Types: str
1780
+
1781
+ auth_mech:
1782
+ Optional Argument.
1783
+ Specifies the mechanism to be used for generating authentication token.
1784
+ Note:
1785
+ * When "auth_mech" is provided, other arguments are used in the following
1786
+ combination as per value of "auth_mech":
1787
+ * OAuth: Token generation is done through OAuth by using client id
1788
+ which can be sepcified by user in "client_id" argument or
1789
+ can be derived internally from "base_url".
1790
+ * PAT : Token generation is done using "pat_token" and "pem_file".
1791
+ * BASIC: Authentication is done via Basic authentication mechanism
1792
+ using user credentials passed in "username" and "password"
1793
+ arguments.
1794
+ * JWT : Readily available token in "auth_token" argument is used.
1795
+ Permitted Values: "OAuth", "PAT", "BASIC", "JWT".
1661
1796
  Types: str
1662
1797
 
1663
1798
  RETURNS:
@@ -1671,10 +1806,14 @@ def set_auth_token(base_url=None, client_id=None, pat_token=None, pem_file=None,
1671
1806
  # Example 1: Set the Authentication token using default client_id.
1672
1807
  >>> import getpass
1673
1808
  >>> set_auth_token(base_url=getpass.getpass("ues_url : "))
1809
+ Authentication token is generated and set for the session.
1810
+ True
1674
1811
 
1675
1812
  # Example 2: Set the Authentication token by specifying the client_id.
1676
1813
  >>> set_auth_token(base_url=getpass.getpass("base_url : "),
1677
1814
  ... client_id=getpass.getpass("client_id : "))
1815
+ Authentication token is generated and set for the session.
1816
+ True
1678
1817
 
1679
1818
  # Example 3: Set the Authentication token by specifying the "pem_file" and "pat_token"
1680
1819
  # without specifying "username".
@@ -1682,6 +1821,7 @@ def set_auth_token(base_url=None, client_id=None, pat_token=None, pem_file=None,
1682
1821
  >>> set_auth_token(base_url=getpass.getpass("base_url : "),
1683
1822
  ... pat_token=getpass.getpass("pat_token : "),
1684
1823
  ... pem_file=getpass.getpass("pem_file : "))
1824
+ Authentication token is generated, authenticated and set for the session.
1685
1825
  True
1686
1826
 
1687
1827
  # Example 4: Set the Authentication token by specifying the "pem_file" and "pat_token"
@@ -1689,85 +1829,175 @@ def set_auth_token(base_url=None, client_id=None, pat_token=None, pem_file=None,
1689
1829
  >>> import getpass
1690
1830
  >>> set_auth_token(base_url=getpass.getpass("base_url : "),
1691
1831
  ... pat_token=getpass.getpass("pat_token : "),
1692
- ... pem_file=getpass.getpass("pem_file : "))
1693
- ... username = "alice")
1832
+ ... pem_file=getpass.getpass("pem_file : "),
1833
+ ... username=getpass.getpass("username : "))
1834
+ Authentication token is generated, authenticated and set for the session.
1835
+ True
1836
+
1837
+ # Example 5: Set the Authentication token by specifying the "pem_file" and "pat_token"
1838
+ # and "kid".
1839
+ >>> import getpass
1840
+ >>> set_auth_token(base_url=getpass.getpass("base_url : "),
1841
+ ... pat_token=getpass.getpass("pat_token : "),
1842
+ ... pem_file=getpass.getpass("pem_file : ")
1843
+ ... kid="key1")
1844
+ Authentication token is generated, authenticated and set for the session.
1845
+ True
1846
+
1847
+ # Example 6: Set the authentication token via Basic Authentication mechanism by
1848
+ # specifying the "base_url", "username" and "password".
1849
+ >>> import getpass
1850
+ >>> set_auth_token(base_url=getpass.getpass("base_url : "),
1851
+ ... username=getpass.getpass("username : "),
1852
+ ... password=getpass.getpass("password : "))
1853
+ Authentication token is generated and set for the session.
1854
+ True
1855
+
1856
+ # Example 7: Set the authentication token for by specifying "base_url" and
1857
+ # "auth_mech" as "OAuth".
1858
+ >>> import getpass
1859
+ >>> set_auth_token(base_url=getpass.getpass("base_url : "),
1860
+ ... auth_mech="OAuth")
1861
+ Authentication token is generated and set for the session.
1694
1862
  True
1863
+
1695
1864
  """
1865
+
1696
1866
  # Deriving global connection using get_connection().
1697
- con = get_connection()
1698
- if con is None:
1699
- raise TeradataMlException(Messages.get_message(MessageCodes.INVALID_CONTEXT_CONNECTION),
1867
+ if get_connection() is None:
1868
+ raise TeradataMlException(Messages.get_message(MessageCodes.INVALID_CONTEXT_CONNECTION),
1700
1869
  MessageCodes.INVALID_CONTEXT_CONNECTION)
1701
1870
 
1702
- # Getting the ues_url.
1703
- ues_url = kwargs.get("ues_url", None)
1871
+ # Remove keys from _InternalBuffer which are interrelated to base_url and authentication token.
1872
+ _InternalBuffer.remove_keys(['list_base_envs', 'default_base_env',
1873
+ 'vs_session_id', 'vs_header'])
1704
1874
 
1875
+ # ---------------------------------ARGUMENT VALIDATION------------------------------------------------------
1876
+ # STEP 1: Validate arguments for allowed types.
1877
+ # ----------------------------------------------------------------------------------------------------------
1705
1878
  __arg_info_matrix = []
1706
1879
  __arg_info_matrix.append(["base_url", base_url, True, (str), True])
1707
- __arg_info_matrix.append(["ues_url", ues_url, True, (str), True])
1708
1880
  __arg_info_matrix.append(["client_id", client_id, True, (str), True])
1709
1881
  __arg_info_matrix.append(["pat_token", pat_token, True, (str), True])
1710
1882
  __arg_info_matrix.append(["pem_file", pem_file, True, (str), True])
1711
1883
 
1712
- username = kwargs.get("username", None)
1884
+ # Get keyword arguments.
1885
+ ues_url = kwargs.get("ues_url", None)
1886
+ __arg_info_matrix.append(["ues_url", ues_url, True, (str), True])
1887
+
1888
+ username = kwargs.get("username", _get_user())
1713
1889
  __arg_info_matrix.append((["username", username, True, (str), True]))
1714
1890
 
1891
+ password = kwargs.get("password", None)
1892
+ __arg_info_matrix.append(["password", password, True, (str), True])
1893
+
1715
1894
  auth_token = kwargs.get("auth_token")
1716
1895
  __arg_info_matrix.append((["auth_token", auth_token, True, (str), True]))
1717
1896
 
1718
1897
  expiration_time = kwargs.get("expiration_time", 31536000)
1719
1898
  __arg_info_matrix.append((["expiration_time", expiration_time, True, (int), True]))
1720
1899
 
1900
+ kid = kwargs.get("kid")
1901
+ __arg_info_matrix.append((["kid", kid, True, (str), True]))
1902
+
1903
+ auth_mech = kwargs.get("auth_mech", None)
1904
+ __arg_info_matrix.append((["auth_mech", auth_mech, True, (str), True, [mech.value for mech in AuthMechs]]))
1905
+
1721
1906
  # Validate arguments.
1722
1907
  _Validators._validate_function_arguments(__arg_info_matrix)
1723
1908
 
1909
+ # ---------------------------------BASE_URL PROCESSING------------------------------------------------------
1910
+ # STEP 2: Process base_url/ues_url and set applicable config options.
1911
+ # ----------------------------------------------------------------------------------------------------------
1912
+
1724
1913
  # base_url should not end with 'open-analytics' or 'data-insights'
1725
1914
  if base_url:
1726
1915
  if base_url.endswith('open-analytics') or base_url.endswith('data-insights'):
1727
1916
  message = Messages.get_message(MessageCodes.ARG_NONE,
1728
- "base_url", "ending with 'data-insights' or 'open-analytics", None)
1917
+ "base_url", "ending with 'data-insights' or 'open-analytics", "")
1729
1918
  raise TeradataMlException(message, MessageCodes.ARG_NONE)
1730
1919
 
1731
1920
  # Set the vector_store_base_url. This should only be done if base_url is set.
1732
1921
  # In case ues_url is set, vector_store_base_url should not be set.
1733
1922
  configure._vector_store_base_url = f'{base_url}/data-insights'
1734
1923
 
1924
+ if ues_url:
1925
+ # If incorrectly formatted UES service URL is passed, set it to None
1926
+ # and let further validation raise error.
1927
+ if not (ues_url.endswith('open-analytics') or ues_url.endswith('user-environment-service/api/v1/')):
1928
+ ues_url = None
1929
+
1735
1930
  # If ues_url is provided, then use it as base_url.
1736
- base_url = kwargs.get("ues_url", base_url)
1931
+ base_url = ues_url if ues_url else base_url
1932
+
1933
+ if not (base_url or ues_url):
1934
+ raise TeradataMlException(Messages.get_message(MessageCodes.MISSING_ARGS, ["base_url"]),
1935
+ MessageCodes.MISSING_ARGS)
1737
1936
 
1738
1937
  # Set the OpenAF url.
1739
- # If ues_url is present use that otherwise generate it from base_url.
1938
+ # If ues_url is present, then use that otherwise generate it from base_url.
1740
1939
  configure.ues_url = ues_url if ues_url else f'{base_url}/open-analytics'
1741
1940
 
1742
- # If user pass Auth token, set it.
1743
- if auth_token:
1744
- _InternalBuffer.add(auth_token=_AuthToken(token=auth_token))
1745
- return True
1746
-
1747
- if client_id and any([pat_token, pem_file]):
1748
- message = Messages.get_message(MessageCodes.EITHER_THIS_OR_THAT_ARGUMENT,
1749
- "client_id", "pat_token' and 'pem_file")
1750
- raise TeradataMlException(message, MessageCodes.EITHER_THIS_OR_THAT_ARGUMENT)
1751
-
1752
- if client_id is None:
1753
- if (pat_token and pem_file is None) or (pem_file and pat_token is None):
1754
- message = Messages.get_message(MessageCodes.MUST_PASS_ARGUMENT,
1755
- "pat_token", "pem_file")
1756
- raise TeradataMlException(message, MessageCodes.MUST_PASS_ARGUMENT)
1757
-
1758
- # Check if pem file exists.
1759
- if pem_file is not None:
1760
- _Validators._validate_file_exists(pem_file)
1761
-
1762
- # Extract the base URL.
1941
+ # Extract the base URL and org id.
1763
1942
  url_parser = urlparse(base_url)
1764
1943
  parsed_base_url = "{}://{}".format(url_parser.scheme, url_parser.netloc)
1765
- netloc = url_parser.netloc.split('.')[0]
1766
-
1767
- # Check if the authentication is PAT based or OAuth.
1768
- if all(arg is None for arg in [pat_token, pem_file]):
1944
+ org_id = url_parser.netloc.split('.')[0]
1945
+
1946
+ # ---------------------------------TOKEN GENERATION------------------------------------------------------
1947
+ # STEP 3: Based on auth_mech, generate authentication token data and store in _InternalBuffer.
1948
+ # Note: auth_mech can be user-provided or can be derived from valid combination of supporting parameters.
1949
+ # --------------------------------------------------------------------------------------------------------
1950
+ if auth_mech:
1951
+ auth_mech = auth_mech.lower()
1952
+ if auth_mech == 'oauth':
1953
+ pat_token = pem_file = password = auth_token = None
1954
+ elif auth_mech == 'jwt':
1955
+ pat_token = pem_file = password = client_id = None
1956
+ elif auth_mech == 'basic':
1957
+ pat_token = pem_file = auth_token = client_id = None
1958
+ elif auth_mech == 'pat':
1959
+ password = client_id = auth_token = None
1960
+
1961
+ # Validate arguments for mutual exclusiveness.
1962
+ all_groups_none = \
1963
+ _Validators._validate_mutually_exclusive_argument_groups({"client_id": client_id},
1964
+ {"auth_token": auth_token},
1965
+ {"pat_token": pat_token,
1966
+ "pem_file": pem_file},
1967
+ {"password": password},
1968
+ return_all_falsy_status=True)
1969
+
1970
+ # Determine authentication mechanism from availability of supportive arguments.
1971
+ if auth_mech is None:
1972
+ if auth_token:
1973
+ auth_mech = 'jwt'
1974
+ elif any([pat_token, pem_file]):
1975
+ auth_mech = 'pat'
1976
+ elif password:
1977
+ # Authentication is done via Basic authentication mechanism
1978
+ # by passing 'basic' field in header.
1979
+ auth_mech = 'basic'
1980
+ # When all supporting arguments are None, default mechanism is OAuth.
1981
+ elif client_id or all_groups_none:
1982
+ auth_mech = 'oauth'
1983
+
1984
+ token_validated = False
1985
+ # Generate and use authentication data as per authentication mechanism.
1986
+ if auth_mech == 'jwt':
1987
+ if not auth_token:
1988
+ raise TeradataMlException(Messages.get_message(MessageCodes.MISSING_ARGS, ["auth_token"]),
1989
+ MessageCodes.MISSING_ARGS)
1990
+ # Validate JWT token if base_url points to CCP environment.
1991
+ # TODO: Uncomment when mechanism to validate JWT for AI-On-prem system is available.
1992
+ # if not ues_url:
1993
+ # token_validated = _validate_jwt_token(base_url, auth_token)
1994
+
1995
+ _InternalBuffer.add(auth_token=_AuthToken(token=auth_token,
1996
+ auth_type='bearer'))
1997
+ elif auth_mech == 'oauth':
1998
+ # TODO: Finalize need to set this flag to False in other scenarios.
1769
1999
  configure._oauth = True
1770
- client_id = "{}-oaf-device".format(netloc) if client_id is None else client_id
2000
+ client_id = "{}-oaf-device".format(org_id) if client_id is None else client_id
1771
2001
  da_wf = _DAWorkflow(parsed_base_url, client_id)
1772
2002
  token_data = da_wf._get_token_data()
1773
2003
 
@@ -1777,34 +2007,50 @@ def set_auth_token(base_url=None, client_id=None, pat_token=None, pem_file=None,
1777
2007
  configure._auth_token_expiry_time = time() + token_data["expires_in"] - 15
1778
2008
 
1779
2009
  # Store the jwt token in internal class attribute.
1780
- _InternalBuffer.add(auth_token=_AuthToken(token=token_data["access_token"]))
1781
-
1782
- else:
1783
- configure._oauth = False
1784
-
1785
- if username is None:
1786
- # If username is not specified then the database username associated with the current context will be
1787
- # considered.
1788
- username = _get_user()
1789
-
1790
- org_id = netloc
2010
+ _InternalBuffer.add(auth_token=_AuthToken(token=token_data["access_token"],
2011
+ auth_type='bearer'))
2012
+ elif auth_mech == 'pat':
2013
+ if any([pat_token, pem_file]):
2014
+ _Validators._validate_mutually_inclusive_n_arguments(pat_token=pat_token,
2015
+ pem_file=pem_file)
2016
+ else:
2017
+ raise TeradataMlException(Messages.get_message(MessageCodes.MISSING_ARGS, ["pat_token", "pem_file"]),
2018
+ MessageCodes.MISSING_ARGS)
2019
+
2020
+ # Check if pem file exists.
2021
+ if pem_file is not None:
2022
+ _Validators._validate_file_exists(pem_file)
2023
+
2024
+ # Generate JWT token.
2025
+ auth_wf = _AuthWorkflow({"base_url": parsed_base_url,
2026
+ "org_id": org_id,
2027
+ "pat_token": pat_token,
2028
+ "pem_file": pem_file,
2029
+ "username": username,
2030
+ "expiration_time": expiration_time,
2031
+ "kid": kid})
2032
+ token_data = auth_wf._proxy_jwt()
1791
2033
 
1792
- # Construct a dictionary to be passed to _AuthWorkflow().
1793
- state_dict = {}
1794
- state_dict["base_url"] = parsed_base_url
1795
- state_dict["org_id"] = org_id
1796
- state_dict["pat_token"] = pat_token
1797
- state_dict["pem_file"] = pem_file
1798
- state_dict["username"] = username
1799
- state_dict["expiration_time"] = expiration_time
2034
+ # Validate generated JWT token.
2035
+ token_validated = _validate_jwt_token(base_url, token_data)
1800
2036
 
1801
- auth_wf = _AuthWorkflow(state_dict)
1802
- token_data = auth_wf._proxy_jwt()
1803
2037
  # Store the jwt token in internal class attribute.
1804
- _InternalBuffer.add(auth_token=_AuthToken(token=token_data))
1805
- # If set_auth_token is triggered then it will be ccp_enabled = True.
1806
- # The function returns if we have just passed the auth_token, thus
1807
- # having ccp_enabled = False.
1808
- configure._ccp_enabled = True
2038
+ _InternalBuffer.add(auth_token=_AuthToken(token=token_data,
2039
+ auth_type='bearer'))
2040
+ elif auth_mech == 'basic':
2041
+ if not password:
2042
+ raise TeradataMlException(Messages.get_message(MessageCodes.MISSING_ARGS, ["password"]),
2043
+ MessageCodes.MISSING_ARGS)
2044
+ credentials = f"{username}:{password}"
2045
+ # Encode the credentials string using Base64.
2046
+ encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
2047
+ # Store the header data in internal class attribute.
2048
+ _InternalBuffer.add(auth_token=_AuthToken(token=encoded_credentials,
2049
+ auth_type='basic'))
2050
+
2051
+ if token_validated:
2052
+ print("Authentication token is generated, authenticated and set for the session.")
2053
+ else:
2054
+ print("Authentication token is generated and set for the session.")
1809
2055
 
1810
2056
  return True