-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsagemaker_notebook.py
66 lines (58 loc) · 2.15 KB
/
sagemaker_notebook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
"""
This module hosts the class `sagemaker_notebook` used to retrieve Amazon
SageMaker notebook.
"""
import logging
import json
import pandas
from data_perimeter_helper.referential import (
config_adv
)
from data_perimeter_helper.referential.ResourceType import ResourceType
logger = logging.getLogger(__name__)
class sagemaker_notebook(ResourceType):
"""List Amazon SageMaker notebooks inventoried in AWS Config aggregator"""
def __init__(self):
super().__init__(
type_name="AWS::SageMaker::NotebookInstance",
unknown_value="SAGEMAKER_NOTEBOOK_NOT_IN_CONFIG_AGGREGATOR"
)
def populate(self, *args, **kwargs) -> pandas.DataFrame:
"""Retrieve all AWS Glue jobs inventoried in AWS Config aggregator
https://github.com/awslabs/aws-config-resource-schema/blob/master/config/properties/resource-types/AWS%3A%3AGlue%3A%3AJob.properties.json
:return: DataFrame with all Glue jobs
"""
config_query = f'''
SELECT
accountId,
arn,
configuration.DirectInternetAccess,
configuration.SubnetId,
configuration.SecurityGroupIds
WHERE
resourceType = '{self.type_name}'
'''
logger.debug("[-] Submitting Config advanced query")
results = config_adv.submit_config_advanced_query(
query=config_query,
transform_to_pandas=False,
)
logger.debug("[+] Submitting Config advanced query")
assert isinstance(results, list) # nosec: B101
if len(results) == 0:
return pandas.DataFrame()
logger.debug("[-] Converting results to DataFrame")
df = pandas.DataFrame(
[json.loads(_) for _ in results]
)
df_configuration = pandas.json_normalize(df['configuration']) # type: ignore
df = pandas.concat([df, df_configuration], axis=1)
# Dropping uneeded columns
df = df.drop(columns=['configuration'])
df = df.fillna(value="Not set")
logger.debug("[+] Converting results to DataFrame")
return df