def get_resources(self) -> List[BucketResource]:
        print("** S3 START **")
        resources = []
        start_time = time.time()

        try:
            # init cloud service type
            for cst in CLOUD_SERVICE_TYPES:
                resources.append(cst)

            # merge data
            for data, name in self.request_data():
                # This is Global API, yet set up its region for bucket
                if getattr(data, 'set_cloudwatch', None):
                    data.cloudwatch = CloudWatchModel(data.set_cloudwatch())

                bucket_resource = {
                    'name': name,
                    'data': data,
                    'reference': ReferenceModel(data.reference())
                }

                if data.get('region_name'):
                    bucket_resource.update({
                        'region_code': data.get('region_name'),
                    })

                resources.append(self.response_schema({'resource': BucketResource(bucket_resource)}))

        except Exception as e:
            print(f'[ERROR {self.service_name}] {e}')

        print(f' S3 Finished {time.time() - start_time} Seconds')
        return resources
    def get_resources(self) -> List[HostedZoneResource]:
        print("** Route53 START **")
        resources = []
        start_time = time.time()

        try:
            # init cloud service type
            for cst in CLOUD_SERVICE_TYPES:
                resources.append(cst)

            # merge data
            for data in self.request_data():
                if getattr(data, 'set_cloudwatch', None):
                    data.cloudwatch = CloudWatchModel(data.set_cloudwatch())

                resources.append(
                    self.response_schema({
                        'resource':
                        HostedZoneResource({
                            'data':
                            data,
                            'reference':
                            ReferenceModel(data.reference()),
                            'region_code':
                            'global'
                        })
                    }))
        except Exception as e:
            print(f'[ERROR {self.service_name}] {e}')

        print(f' Route53 Finished {time.time() - start_time} Seconds')
        return resources
    def get_resources(self):
        _LOGGER.debug("[get_resources] START: Cloudfront")
        resources = []
        start_time = time.time()

        resources.extend(self.set_service_code_in_cloud_service_type())

        try:
            for data in self.request_data():
                if getattr(data, 'resource_type', None) and data.resource_type == 'inventory.ErrorResource':
                    # Error Resource
                    resources.append(data)
                else:
                    if getattr(data, 'set_cloudwatch', None):
                        data.cloudwatch = CloudWatchModel(data.set_cloudwatch())
    
                    resources.append(self.response_schema(
                        {'resource': DistributionResource({
                            'name': data.domain_name,
                            'data': data,
                            'account': self.account_id,
                            'reference': ReferenceModel(data.reference()),
                            'region_code': 'global'})
                        }))
        except Exception as e:
            resource_id = ''
            resources.append(self.generate_error('global', resource_id, e))

        _LOGGER.debug(f'[get_resources] Cloud Front Finished {time.time() - start_time} Seconds')
        return resources
    def get_resources(self):
        print("** Cloud Front START **")
        resources = []
        start_time = time.time()

        for cst in CLOUD_SERVICE_TYPES:
            resources.append(cst)

        try:
            for data in self.request_data():
                # print(f"[ CloudFront DATA ]")
                if getattr(data, 'set_cloudwatch', None):
                    data.cloudwatch = CloudWatchModel(data.set_cloudwatch())

                resources.append(self.response_schema(
                    {'resource': DistributionResource({
                        'name': data.domain_name,
                        'data': data,
                        'reference': ReferenceModel(data.reference()),
                        'region_code': 'global'})
                    }))
        except Exception as e:
            print(f'[ERROR {self.service_name}] {e}')

        print(f' Cloud Front Finished {time.time() - start_time} Seconds')
        return resources
    def get_resources(self):
        print("** RDS START **")
        resources = []
        start_time = time.time()

        collect_resources = [
            {
                'request_method': self.instance_data,
                'resource': InstanceResource,
                'response_schema': InstanceResponse
            },
            {
                'request_method': self.snapshot_request_data,
                'resource': SnapshotResource,
                'response_schema': SnapshotResponse
            },
            {
                'request_method': self.subnet_group_request_data,
                'resource': SubnetGroupResource,
                'response_schema': SubnetGroupResponse
            },
            {
                'request_method': self.parameter_group_request_data,
                'resource': ParameterGroupResource,
                'response_schema': ParameterGroupResponse
            },
            {
                'request_method': self.option_group_request_data,
                'resource': OptionGroupResource,
                'response_schema': OptionGroupResponse
            }
        ]

        # init cloud service type
        for cst in CLOUD_SERVICE_TYPES:
            resources.append(cst)

        for region_name in self.region_names:
            # print(f'[ {region_name} ]')
            self.reset_region(region_name)

            # For Database
            for database_vo, resource in self.db_cluster_data(region_name):
                if getattr(database_vo, 'set_cloudwatch', None):
                    database_vo.cloudwatch = CloudWatchModel(database_vo.set_cloudwatch(region_name))

                resources.append(DatabaseResponse(
                    {'resource': resource(
                        {'data': database_vo,
                         'tags': [{'key':tag.key, 'value': tag.value} for tag in database_vo.tags],
                         'region_code': region_name,
                         'reference': ReferenceModel(database_vo.reference(region_name))})}
                ))

            # For All except Database
            for collect_resource in collect_resources:
                resources.extend(self.collect_data_by_region(self.service_name, region_name, collect_resource))
        print(f' RDS Finished {time.time() - start_time} Seconds')
        return resources
    def get_resources(self):
        _LOGGER.debug("[get_resources] START: ElastiCache")
        resources = []
        start_time = time.time()

        resources.extend(self.set_service_code_in_cloud_service_type())

        for region_name in self.region_names:
            self.reset_region(region_name)
            cache_clusters = [cluster for cluster in self.describe_clusters()]

            for memcached_vo in self.get_memcached_data(
                    region_name, cache_clusters):
                if getattr(
                        memcached_vo, 'resource_type', None
                ) and memcached_vo.resource_type == 'inventory.ErrorResource':
                    # Error Resource
                    resources.append(memcached_vo)
                else:
                    if getattr(memcached_vo, 'set_cloudwatch', None):
                        memcached_vo.cloudwatch = CloudWatchModel(
                            memcached_vo.set_cloudwatch(region_name))

                    resources.append(
                        MemcachedResponse({'resource': memcached_vo}))

            for redis_vo in self.get_redis_data(region_name, cache_clusters):
                if getattr(
                        redis_vo, 'resource_type', None
                ) and redis_vo.resource_type == 'inventory.ErrorResource':
                    # Error Resource
                    resources.append(redis_vo)
                else:
                    if getattr(redis_vo, 'set_cloudwatch', None):
                        redis_vo.cloudwatch = CloudWatchModel(
                            redis_vo.set_cloudwatch(region_name))

                    resources.append(RedisResponse({'resource': redis_vo}))

        _LOGGER.debug(
            f'[get_resources] FINISHED: ElastiCache ({time.time() - start_time} sec)'
        )
        return resources
Beispiel #7
0
    def collect_data_by_region(self, service_name, region_name, collect_resource_info):
        '''
        collect_resource_info = {
                'request_method': self.request_something_like_data,
                'resource': ResourceClass,
                'response_schema': ResponseClass,
                'kwargs': {}
        }
        '''
        resources = []
        additional_data = ['name', 'type', 'size', 'launched_at']

        try:
            for collected_dict in collect_resource_info['request_method'](region_name, **collect_resource_info.get('kwargs', {})):
                data = collected_dict['data']
    
                if getattr(data, 'resource_type', None) and data.resource_type == 'inventory.ErrorResource':
                    # Error Resource
                    resources.append(data)
                else:
                    # Cloud Service Resource
                    if getattr(data, 'set_cloudwatch', None):
                        data.cloudwatch = CloudWatchModel(data.set_cloudwatch(region_name))
    
                    resource_dict = {
                        'data': data,
                        'account': collected_dict.get('account'),
                        'instance_size': float(collected_dict.get('instance_size', 0)),
                        'instance_type': collected_dict.get('instance_type', ''),
                        'launched_at': str(collected_dict.get('launched_at', '')),
                        'tags': self.get_resource_tags(getattr(data, 'tags', [])),
                        'region_code': region_name,
                        'reference': ReferenceModel(data.reference(region_name))
                    }

                    for add_field in additional_data:
                        if add_field in collected_dict:
                            resource_dict.update({add_field: collected_dict[add_field]})
    
                    resources.append(collect_resource_info['response_schema'](
                        {'resource': collect_resource_info['resource'](resource_dict)}))
        except Exception as e:
            resource_id = ''
            error_resource_response = self.generate_error(region_name, resource_id, e)
            resources.append(error_resource_response)
            
        return resources
    def collect_data_by_region(self, service_name, region_name,
                               collect_resource_info):
        '''
        collect_resource_info = {
                'request_method': self.request_something_like_data,
                'resource': ResourceClass,
                'response_schema': ResponseClass,
                'kwargs': {}
        }
        '''

        resources = []

        try:
            for data, name in collect_resource_info['request_method'](
                    region_name, **collect_resource_info.get('kwargs', {})):
                if getattr(data, 'set_cloudwatch', None):
                    data.cloudwatch = CloudWatchModel(
                        data.set_cloudwatch(region_name))

                resources.append(collect_resource_info['response_schema']({
                    'resource':
                    collect_resource_info['resource']({
                        'name':
                        name,
                        'data':
                        data,
                        'tags':
                        self.get_resource_tags(getattr(data, 'tags', [])),
                        'region_code':
                        region_name,
                        'reference':
                        ReferenceModel(data.reference(region_name))
                    })
                }))

        except Exception as e:
            print(f'[ERROR {service_name}] REGION : {region_name} {e}')

        return resources
Beispiel #9
0
    def get_resources(self) -> List[BucketResource]:
        _LOGGER.debug("[get_resources] START: S3")
        resources = []
        start_time = time.time()

        try:
            resources.extend(self.set_service_code_in_cloud_service_type())

            # merge data
            for data in self.request_data():
                if getattr(data, 'resource_type', None) and data.resource_type == 'inventory.ErrorResource':
                    # Error Resource
                    resources.append(data)
                else:
                    # This is Global API, yet set up its region for bucket
                    if getattr(data, 'set_cloudwatch', None):
                        data.cloudwatch = CloudWatchModel(data.set_cloudwatch())
    
                    bucket_resource = {
                        'name': data.name,
                        'data': data,
                        'account': self.account_id,
                        'instance_size': float(data.size),
                        'reference': ReferenceModel(data.reference())
                    }
    
                    if data.get('region_name'):
                        bucket_resource.update({
                            'region_code': data.get('region_name'),
                        })
    
                    resources.append(self.response_schema({'resource': BucketResource(bucket_resource)}))
        except Exception as e:
            resource_id = ''
            resources.append(self.generate_error('global', resource_id, e))

        _LOGGER.debug(f'[get_resources] FINISHED: S3 ({time.time() - start_time} sec)')
        return resources
    def get_resources(self):
        _LOGGER.debug("[get_resources] START: RDS")
        resources = []
        start_time = time.time()

        collect_resources = [{
            'request_method': self.instance_request_data,
            'resource': InstanceResource,
            'response_schema': InstanceResponse
        }, {
            'request_method': self.snapshot_request_data,
            'resource': SnapshotResource,
            'response_schema': SnapshotResponse
        }, {
            'request_method': self.subnet_group_request_data,
            'resource': SubnetGroupResource,
            'response_schema': SubnetGroupResponse
        }, {
            'request_method': self.parameter_group_request_data,
            'resource': ParameterGroupResource,
            'response_schema': ParameterGroupResponse
        }, {
            'request_method': self.option_group_request_data,
            'resource': OptionGroupResource,
            'response_schema': OptionGroupResponse
        }]

        resources.extend(self.set_service_code_in_cloud_service_type())

        for region_name in self.region_names:
            self.reset_region(region_name)

            try:
                # For Database
                for database_vo, resource, identifier in self.db_cluster_data(
                        region_name):
                    if getattr(
                            database_vo, 'resource_type', None
                    ) and database_vo.resource_type == 'inventory.ErrorResource':
                        # Error Resource
                        resources.append(database_vo)
                    else:
                        if getattr(database_vo, 'set_cloudwatch', None):
                            database_vo.cloudwatch = CloudWatchModel(
                                database_vo.set_cloudwatch(region_name))

                        resources.append(
                            DatabaseResponse({
                                'resource':
                                resource({
                                    'name':
                                    identifier,
                                    'data':
                                    database_vo,
                                    'instance_type':
                                    database_vo.engine,
                                    'tags': [{
                                        'key': tag.key,
                                        'value': tag.value
                                    } for tag in database_vo.tags],
                                    'region_code':
                                    region_name,
                                    'account':
                                    self.account_id,
                                    'reference':
                                    ReferenceModel(
                                        database_vo.reference(region_name))
                                })
                            }))
            except Exception as e:
                resource_id = ''
                resources.append(
                    self.generate_error(region_name, resource_id, e))

            # For All except Database
            for collect_resource in collect_resources:
                resources.extend(
                    self.collect_data_by_region(self.service_name, region_name,
                                                collect_resource))

        _LOGGER.debug(
            f'[get_resources] FINISHED: RDS ({time.time() - start_time} sec)')
        return resources