|
| 1 | +"""Amazon QuickSight Get Module.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from typing import Callable, List, Optional |
| 5 | + |
| 6 | +import boto3 # type: ignore |
| 7 | + |
| 8 | +from awswrangler import exceptions |
| 9 | +from awswrangler.quicksight import _list |
| 10 | + |
| 11 | +_logger: logging.Logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +def _get_ids( |
| 15 | + name: str, |
| 16 | + func: Callable, |
| 17 | + attr_name: str, |
| 18 | + account_id: Optional[str] = None, |
| 19 | + boto3_session: Optional[boto3.Session] = None, |
| 20 | +) -> List[str]: |
| 21 | + ids: List[str] = [] |
| 22 | + for item in func(account_id=account_id, boto3_session=boto3_session): |
| 23 | + if item["Name"] == name: |
| 24 | + ids.append(item[attr_name]) |
| 25 | + return ids |
| 26 | + |
| 27 | + |
| 28 | +def _get_id( |
| 29 | + name: str, |
| 30 | + func: Callable, |
| 31 | + attr_name: str, |
| 32 | + account_id: Optional[str] = None, |
| 33 | + boto3_session: Optional[boto3.Session] = None, |
| 34 | +) -> str: |
| 35 | + ids: List[str] = _get_ids( |
| 36 | + name=name, func=func, attr_name=attr_name, account_id=account_id, boto3_session=boto3_session |
| 37 | + ) |
| 38 | + if len(ids) == 0: |
| 39 | + raise exceptions.InvalidArgument(f"There is no {attr_name} related with name {name}") |
| 40 | + if len(ids) > 1: |
| 41 | + raise exceptions.InvalidArgument( |
| 42 | + f"There is {len(ids)} {attr_name} with name {name}. " |
| 43 | + f"Please pass the id argument to specify " |
| 44 | + f"which one you would like to describe." |
| 45 | + ) |
| 46 | + return ids[0] |
| 47 | + |
| 48 | + |
| 49 | +def get_dashboard_ids( |
| 50 | + name: str, account_id: Optional[str] = None, boto3_session: Optional[boto3.Session] = None |
| 51 | +) -> List[str]: |
| 52 | + """Get QuickSight dashboard IDs given a name. |
| 53 | +
|
| 54 | + Note |
| 55 | + ---- |
| 56 | + This function returns a list of ID because Quicksight accepts duplicated dashboard names, |
| 57 | + so you may have more than 1 ID for a given name. |
| 58 | +
|
| 59 | + Parameters |
| 60 | + ---------- |
| 61 | + name : str |
| 62 | + Dashboard name. |
| 63 | + account_id : str, optional |
| 64 | + If None, the account ID will be inferred from your boto3 session. |
| 65 | + boto3_session : boto3.Session(), optional |
| 66 | + Boto3 Session. The default boto3 session will be used if boto3_session receive None. |
| 67 | +
|
| 68 | + Returns |
| 69 | + ------- |
| 70 | + List[str] |
| 71 | + Dashboad IDs. |
| 72 | +
|
| 73 | + Examples |
| 74 | + -------- |
| 75 | + >>> import awswrangler as wr |
| 76 | + >>> ids = wr.quicksight.get_dashboard_ids(name="...") |
| 77 | + """ |
| 78 | + return _get_ids( |
| 79 | + name=name, |
| 80 | + func=_list.list_dashboards, |
| 81 | + attr_name="DashboardId", |
| 82 | + account_id=account_id, |
| 83 | + boto3_session=boto3_session, |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +def get_dashboard_id(name: str, account_id: Optional[str] = None, boto3_session: Optional[boto3.Session] = None) -> str: |
| 88 | + """Get QuickSight dashboard ID given a name and fails if there is more than 1 ID associated with this name. |
| 89 | +
|
| 90 | + Parameters |
| 91 | + ---------- |
| 92 | + name : str |
| 93 | + Dashboard name. |
| 94 | + account_id : str, optional |
| 95 | + If None, the account ID will be inferred from your boto3 session. |
| 96 | + boto3_session : boto3.Session(), optional |
| 97 | + Boto3 Session. The default boto3 session will be used if boto3_session receive None. |
| 98 | +
|
| 99 | + Returns |
| 100 | + ------- |
| 101 | + str |
| 102 | + Dashboad ID. |
| 103 | +
|
| 104 | + Examples |
| 105 | + -------- |
| 106 | + >>> import awswrangler as wr |
| 107 | + >>> my_id = wr.quicksight.get_dashboard_id(name="...") |
| 108 | + """ |
| 109 | + return _get_id( |
| 110 | + name=name, |
| 111 | + func=_list.list_dashboards, |
| 112 | + attr_name="DashboardId", |
| 113 | + account_id=account_id, |
| 114 | + boto3_session=boto3_session, |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +def get_dataset_ids( |
| 119 | + name: str, account_id: Optional[str] = None, boto3_session: Optional[boto3.Session] = None |
| 120 | +) -> List[str]: |
| 121 | + """Get QuickSight dataset IDs given a name. |
| 122 | +
|
| 123 | + Note |
| 124 | + ---- |
| 125 | + This function returns a list of ID because Quicksight accepts duplicated datasets names, |
| 126 | + so you may have more than 1 ID for a given name. |
| 127 | +
|
| 128 | + Parameters |
| 129 | + ---------- |
| 130 | + name : str |
| 131 | + Dataset name. |
| 132 | + account_id : str, optional |
| 133 | + If None, the account ID will be inferred from your boto3 session. |
| 134 | + boto3_session : boto3.Session(), optional |
| 135 | + Boto3 Session. The default boto3 session will be used if boto3_session receive None. |
| 136 | +
|
| 137 | + Returns |
| 138 | + ------- |
| 139 | + List[str] |
| 140 | + Datasets IDs. |
| 141 | +
|
| 142 | + Examples |
| 143 | + -------- |
| 144 | + >>> import awswrangler as wr |
| 145 | + >>> ids = wr.quicksight.get_dataset_ids(name="...") |
| 146 | + """ |
| 147 | + return _get_ids( |
| 148 | + name=name, func=_list.list_datasets, attr_name="DataSetId", account_id=account_id, boto3_session=boto3_session |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +def get_dataset_id(name: str, account_id: Optional[str] = None, boto3_session: Optional[boto3.Session] = None) -> str: |
| 153 | + """Get QuickSight Dataset ID given a name and fails if there is more than 1 ID associated with this name. |
| 154 | +
|
| 155 | + Parameters |
| 156 | + ---------- |
| 157 | + name : str |
| 158 | + Dataset name. |
| 159 | + account_id : str, optional |
| 160 | + If None, the account ID will be inferred from your boto3 session. |
| 161 | + boto3_session : boto3.Session(), optional |
| 162 | + Boto3 Session. The default boto3 session will be used if boto3_session receive None. |
| 163 | +
|
| 164 | + Returns |
| 165 | + ------- |
| 166 | + str |
| 167 | + Dataset ID. |
| 168 | +
|
| 169 | + Examples |
| 170 | + -------- |
| 171 | + >>> import awswrangler as wr |
| 172 | + >>> my_id = wr.quicksight.get_dataset_id(name="...") |
| 173 | + """ |
| 174 | + return _get_id( |
| 175 | + name=name, func=_list.list_datasets, attr_name="DataSetId", account_id=account_id, boto3_session=boto3_session |
| 176 | + ) |
| 177 | + |
| 178 | + |
| 179 | +def get_data_source_ids( |
| 180 | + name: str, account_id: Optional[str] = None, boto3_session: Optional[boto3.Session] = None |
| 181 | +) -> List[str]: |
| 182 | + """Get QuickSight data source IDs given a name. |
| 183 | +
|
| 184 | + Note |
| 185 | + ---- |
| 186 | + This function returns a list of ID because Quicksight accepts duplicated data source names, |
| 187 | + so you may have more than 1 ID for a given name. |
| 188 | +
|
| 189 | + Parameters |
| 190 | + ---------- |
| 191 | + name : str |
| 192 | + Data source name. |
| 193 | + account_id : str, optional |
| 194 | + If None, the account ID will be inferred from your boto3 session. |
| 195 | + boto3_session : boto3.Session(), optional |
| 196 | + Boto3 Session. The default boto3 session will be used if boto3_session receive None. |
| 197 | +
|
| 198 | + Returns |
| 199 | + ------- |
| 200 | + List[str] |
| 201 | + Data source IDs. |
| 202 | +
|
| 203 | + Examples |
| 204 | + -------- |
| 205 | + >>> import awswrangler as wr |
| 206 | + >>> ids = wr.quicksight.get_data_source_ids(name="...") |
| 207 | + """ |
| 208 | + return _get_ids( |
| 209 | + name=name, |
| 210 | + func=_list.list_data_sources, |
| 211 | + attr_name="DataSourceId", |
| 212 | + account_id=account_id, |
| 213 | + boto3_session=boto3_session, |
| 214 | + ) |
| 215 | + |
| 216 | + |
| 217 | +def get_data_source_id( |
| 218 | + name: str, account_id: Optional[str] = None, boto3_session: Optional[boto3.Session] = None |
| 219 | +) -> str: |
| 220 | + """Get QuickSight data source ID given a name and fails if there is more than 1 ID associated with this name. |
| 221 | +
|
| 222 | + Parameters |
| 223 | + ---------- |
| 224 | + name : str |
| 225 | + Data source name. |
| 226 | + account_id : str, optional |
| 227 | + If None, the account ID will be inferred from your boto3 session. |
| 228 | + boto3_session : boto3.Session(), optional |
| 229 | + Boto3 Session. The default boto3 session will be used if boto3_session receive None. |
| 230 | +
|
| 231 | + Returns |
| 232 | + ------- |
| 233 | + str |
| 234 | + Dataset ID. |
| 235 | +
|
| 236 | + Examples |
| 237 | + -------- |
| 238 | + >>> import awswrangler as wr |
| 239 | + >>> my_id = wr.quicksight.get_data_source_id(name="...") |
| 240 | + """ |
| 241 | + return _get_id( |
| 242 | + name=name, |
| 243 | + func=_list.list_data_sources, |
| 244 | + attr_name="DataSourceId", |
| 245 | + account_id=account_id, |
| 246 | + boto3_session=boto3_session, |
| 247 | + ) |
| 248 | + |
| 249 | + |
| 250 | +def get_template_ids( |
| 251 | + name: str, account_id: Optional[str] = None, boto3_session: Optional[boto3.Session] = None |
| 252 | +) -> List[str]: |
| 253 | + """Get QuickSight template IDs given a name. |
| 254 | +
|
| 255 | + Note |
| 256 | + ---- |
| 257 | + This function returns a list of ID because Quicksight accepts duplicated templates names, |
| 258 | + so you may have more than 1 ID for a given name. |
| 259 | +
|
| 260 | + Parameters |
| 261 | + ---------- |
| 262 | + name : str |
| 263 | + Template name. |
| 264 | + account_id : str, optional |
| 265 | + If None, the account ID will be inferred from your boto3 session. |
| 266 | + boto3_session : boto3.Session(), optional |
| 267 | + Boto3 Session. The default boto3 session will be used if boto3_session receive None. |
| 268 | +
|
| 269 | + Returns |
| 270 | + ------- |
| 271 | + List[str] |
| 272 | + Tamplate IDs. |
| 273 | +
|
| 274 | + Examples |
| 275 | + -------- |
| 276 | + >>> import awswrangler as wr |
| 277 | + >>> ids = wr.quicksight.get_template_ids(name="...") |
| 278 | + """ |
| 279 | + return _get_ids( |
| 280 | + name=name, func=_list.list_templates, attr_name="TemplateId", account_id=account_id, boto3_session=boto3_session |
| 281 | + ) |
| 282 | + |
| 283 | + |
| 284 | +def get_template_id(name: str, account_id: Optional[str] = None, boto3_session: Optional[boto3.Session] = None) -> str: |
| 285 | + """Get QuickSight template ID given a name and fails if there is more than 1 ID associated with this name. |
| 286 | +
|
| 287 | + Parameters |
| 288 | + ---------- |
| 289 | + name : str |
| 290 | + Template name. |
| 291 | + account_id : str, optional |
| 292 | + If None, the account ID will be inferred from your boto3 session. |
| 293 | + boto3_session : boto3.Session(), optional |
| 294 | + Boto3 Session. The default boto3 session will be used if boto3_session receive None. |
| 295 | +
|
| 296 | + Returns |
| 297 | + ------- |
| 298 | + str |
| 299 | + Template ID. |
| 300 | +
|
| 301 | + Examples |
| 302 | + -------- |
| 303 | + >>> import awswrangler as wr |
| 304 | + >>> my_id = wr.quicksight.get_template_id(name="...") |
| 305 | + """ |
| 306 | + return _get_id( |
| 307 | + name=name, func=_list.list_templates, attr_name="TemplateId", account_id=account_id, boto3_session=boto3_session |
| 308 | + ) |
| 309 | + |
| 310 | + |
| 311 | +def get_data_source_arns( |
| 312 | + name: str, account_id: Optional[str] = None, boto3_session: Optional[boto3.Session] = None |
| 313 | +) -> List[str]: |
| 314 | + """Get QuickSight Data source ARNs given a name. |
| 315 | +
|
| 316 | + Note |
| 317 | + ---- |
| 318 | + This function returns a list of ARNs because Quicksight accepts duplicated data source names, |
| 319 | + so you may have more than 1 ARN for a given name. |
| 320 | +
|
| 321 | + Parameters |
| 322 | + ---------- |
| 323 | + name : str |
| 324 | + Data source name. |
| 325 | + account_id : str, optional |
| 326 | + If None, the account ID will be inferred from your boto3 session. |
| 327 | + boto3_session : boto3.Session(), optional |
| 328 | + Boto3 Session. The default boto3 session will be used if boto3_session receive None. |
| 329 | +
|
| 330 | + Returns |
| 331 | + ------- |
| 332 | + List[str] |
| 333 | + Data source ARNs. |
| 334 | +
|
| 335 | + Examples |
| 336 | + -------- |
| 337 | + >>> import awswrangler as wr |
| 338 | + >>> arns = wr.quicksight.get_data_source_arns(name="...") |
| 339 | + """ |
| 340 | + arns: List[str] = [] |
| 341 | + for source in _list.list_data_sources(account_id=account_id, boto3_session=boto3_session): |
| 342 | + if source["Name"] == name: |
| 343 | + arns.append(source["Arn"]) |
| 344 | + return arns |
| 345 | + |
| 346 | + |
| 347 | +def get_data_source_arn( |
| 348 | + name: str, account_id: Optional[str] = None, boto3_session: Optional[boto3.Session] = None |
| 349 | +) -> str: |
| 350 | + """Get QuickSight data source ARN given a name and fails if there is more than 1 ARN associated with this name. |
| 351 | +
|
| 352 | + Note |
| 353 | + ---- |
| 354 | + This function returns a list of ARNs because Quicksight accepts duplicated data source names, |
| 355 | + so you may have more than 1 ARN for a given name. |
| 356 | +
|
| 357 | + Parameters |
| 358 | + ---------- |
| 359 | + name : str |
| 360 | + Data source name. |
| 361 | + account_id : str, optional |
| 362 | + If None, the account ID will be inferred from your boto3 session. |
| 363 | + boto3_session : boto3.Session(), optional |
| 364 | + Boto3 Session. The default boto3 session will be used if boto3_session receive None. |
| 365 | +
|
| 366 | + Returns |
| 367 | + ------- |
| 368 | + str |
| 369 | + Data source ARN. |
| 370 | +
|
| 371 | + Examples |
| 372 | + -------- |
| 373 | + >>> import awswrangler as wr |
| 374 | + >>> arn = wr.quicksight.get_data_source_arn("...") |
| 375 | + """ |
| 376 | + arns: List[str] = get_data_source_arns(name=name, account_id=account_id, boto3_session=boto3_session) |
| 377 | + if len(arns) == 0: |
| 378 | + raise exceptions.InvalidArgument(f"There is not data source with name {name}") |
| 379 | + if len(arns) > 1: |
| 380 | + raise exceptions.InvalidArgument( |
| 381 | + f"There is more than 1 data source with name {name}. " |
| 382 | + f"Please pass the data_source_arn argument to specify " |
| 383 | + f"which one you would like to describe." |
| 384 | + ) |
| 385 | + return arns[0] |
0 commit comments