Skip to content

Commit

Permalink
Merge pull request #16 from ImMin5/master
Browse files Browse the repository at this point in the history
Add class variable at region manager for using ResourceManager feat: add history api for Asset
  • Loading branch information
ImMin5 authored Dec 11, 2024
2 parents 3edf832 + c1dbe92 commit fa5bfe4
Show file tree
Hide file tree
Showing 13 changed files with 616 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ def collect(
yield self._convert_resource_data(resource_data)

@staticmethod
def _convert_match_rule(resource_data: dict, resource_type: str) -> dict:
def _convert_match_rule_from_resource_data(
resource_data: dict,
) -> dict:
resource_type = resource_data.get("resource_type")
for rule_values in resource_data.get("match_rules", {}).values():
for index, rule_value in enumerate(rule_values):
if rule_value == "cloud_service_id":
Expand All @@ -67,78 +70,88 @@ def _convert_match_rule(resource_data: dict, resource_type: str) -> dict:
rule_values[index] = "asset_type_id"
return resource_data

def _convert_resource_data(self, resource_data: dict) -> dict:

if "resource" in resource_data and "metadata" in resource_data["resource"]:
del resource_data["resource"]["metadata"]

_LOGGER.debug(
f"[_convert_resource_data] before convert resource_data: {resource_data}"
)

@staticmethod
def _convert_resource_type_from_resource_data(resource_data: dict) -> dict:
resource_type = resource_data.get("resource_type")
if resource_type == "inventory.Region":
pass

if resource_type in ["inventory.CloudService", "inventory.CloudServiceType"]:
if resource_type == "inventory.CloudService":
resource_data["resource_type"] = "inventory.Asset"
resource_data["resource"]["asset_type_id"] = resource_data[
"resource"
].get("asset_type")
# resource_data["resource"]["asset_group_id"] = resource_data[
# "resource"
# ].get("cloud_service_group")
elif resource_type == "inventory.CloudServiceType":
resource_data["resource_type"] = "inventory.AssetType"

return resource_data

@staticmethod
def _convert_resource_in_resource_data(resource_data: dict) -> dict:
if _resource := resource_data.get("resource"):
resource_type = resource_data.get("resource_type")
if resource_type == "inventory.Region":
_resource["region_id"] = (
f"{_resource['provider']}-{_resource['region_code']}"
)

# convert match rule
resource_data = self._convert_match_rule(resource_data, resource_type)
elif resource_type == "inventory.Metric":
metric_resource_type = _resource.get("resource_type")
_resource["resource_type"] = metric_resource_type.replace(
"CloudService", "Asset"
)

# convert keywords [instance_size, instance_type, ]
if _resource := resource_data.get("resource"):
# del _resource["metadata"]
elif resource_type == "inventory.Asset":
del _resource["metadata"]

if "instance_size" in _resource:
_resource["data"]["instance_size"] = _resource.pop("instance_size")
_resource["data"]["instance_size"] = _resource.get("instance_size")
if "instance_type" in _resource:
_resource["data"]["instance_type"] = _resource.pop("instance_type")
_resource["data"]["instance_type"] = _resource.get("instance_type")

if "region_code" in _resource:
_resource["region_id"] = (
f"{_resource['provider']}-{_resource['region_code']}"
)

if resource_type == "inventory.Asset":
asset_type_id = f"{_resource['provider']}-{_resource['cloud_service_group']}-{_resource['cloud_service_type']}"
_resource["asset_type_id"] = asset_type_id
resource_data["asset_type_id"] = asset_type_id

if "reference" in _resource:
_resource["resource_id"] = _resource["reference"].get(
"resource_id"
)
_resource["external_link"] = _resource["reference"].get(
"external_link"
)

elif resource_type == "inventory.AssetType":

asset_type_id = f"at-{_resource['provider']}-{_resource['group']}-{_resource['name']}"
asset_groups = [
f"ag-{_resource['provider']}-{_resource['group']}",
f"ag-{_resource['provider']}",
]
_resource["asset_type_id"] = asset_type_id
resource_data["asset_type_id"] = asset_type_id
resource_data["asset_groups"] = asset_groups

resource_data["icon"] = resource_data.get("tags", {}).get(
"spaceone:icon", ""
asset_type_id = f"{_resource['provider']}-{_resource['cloud_service_group']}-{_resource['cloud_service_type']}"
_resource["asset_type_id"] = asset_type_id

if "reference" in _resource:
_resource["resource_id"] = _resource["reference"].get("resource_id")
_resource["external_link"] = _resource["reference"].get(
"external_link"
)

resource_data["asset_type_id"] = asset_type_id

elif resource_type == "inventory.AssetType":

asset_type_id = f"at-{_resource['provider']}-{_resource['group']}-{_resource['name']}"
asset_groups = [
f"ag-{_resource['provider']}-{_resource['group']}",
f"ag-{_resource['provider']}",
]
_resource["asset_type_id"] = asset_type_id
resource_data["asset_type_id"] = asset_type_id
resource_data["asset_groups"] = asset_groups

resource_data["icon"] = resource_data.get("tags", {}).get(
"spaceone:icon", ""
)
return resource_data

def _convert_resource_data(self, resource_data: dict) -> dict:

_LOGGER.debug(
f"[_convert_resource_data] before convert resource_data: {resource_data}"
)

# 1. convert resource_type
resource_data = self._convert_resource_type_from_resource_data(resource_data)

# 2. convert match rule
resource_data = self._convert_match_rule_from_resource_data(resource_data)

# 3. convert resource in resource_data
resource_data = self._convert_resource_in_resource_data(resource_data)

_LOGGER.debug(f"[_convert_resource_data] resource_data: {resource_data}")

return resource_data
3 changes: 3 additions & 0 deletions src/spaceone/inventory_v2/interface/grpc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from spaceone.core.pygrpc.server import GRPCServer

from .asset import Asset
from .region import Region
from .collector import Collector
from .job import Job
Expand All @@ -22,3 +24,4 @@
app.add_service(Metric)
app.add_service(MetricData)
app.add_service(MetricExample)
app.add_service(Asset)
94 changes: 94 additions & 0 deletions src/spaceone/inventory_v2/interface/grpc/asset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from spaceone.api.inventory_v2.v1 import asset_pb2, asset_pb2_grpc
from spaceone.core.pygrpc import BaseAPI

from spaceone.inventory_v2.service import AssetService


class Asset(BaseAPI, asset_pb2_grpc.AssetServicer):
pb2 = asset_pb2
pb2_grpc = asset_pb2_grpc

def create(self, request, context):
params, metadata = self.parse_request(request, context)
asset_svc = AssetService(metadata)
response: dict = asset_svc.create(params)
return self.dict_to_message(response)

def update(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service(
"CloudServiceService", metadata
) as cloud_svc_service:
return self.locator.get_info(
"CloudServiceInfo", cloud_svc_service.update(params)
)

def pin_data(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service(
"CloudServiceService", metadata
) as cloud_svc_service:
return self.locator.get_info(
"CloudServiceInfo", cloud_svc_service.pin_data(params)
)

def delete(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service(
"CloudServiceService", metadata
) as cloud_svc_service:
cloud_svc_service.delete(params)
return self.locator.get_info("EmptyInfo")

def get(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service(
"CloudServiceService", metadata
) as cloud_svc_service:
return self.locator.get_info(
"CloudServiceInfo", cloud_svc_service.get(params)
)

def list(self, request, context):
params, metadata = self.parse_request(request, context)
asset_svc = AssetService(metadata)
response: dict = asset_svc.list(params)
return self.dict_to_message(response)

def export(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service(
"CloudServiceService", metadata
) as cloud_svc_service:
return self.locator.get_info("ExportInfo", cloud_svc_service.export(params))

def history(self, request, context):
params, metadata = self.parse_request(request, context)
asset_svc = AssetService(metadata)
response: dict = asset_svc.history(params)
return self.dict_to_message(response)

def analyze(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service(
"CloudServiceService", metadata
) as cloud_svc_service:
return self.locator.get_info(
"AnalyzeInfo", cloud_svc_service.analyze(params)
)

def stat(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service(
"CloudServiceService", metadata
) as cloud_svc_service:
return self.locator.get_info(
"StatisticsInfo", cloud_svc_service.stat(params)
)
1 change: 0 additions & 1 deletion src/spaceone/inventory_v2/manager/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from spaceone.inventory_v2.manager.asset_manager import AssetManager
from spaceone.inventory_v2.manager.asset_type_manager import AssetTypeManager
from spaceone.inventory_v2.lib.resource_manager import ResourceManager
from spaceone.inventory_v2.manager.region_manager import RegionManager
from spaceone.inventory_v2.manager.collecting_manager import CollectingManager
from spaceone.inventory_v2.manager.namespace_group_manager import NamespaceGroupManager
Expand Down
40 changes: 22 additions & 18 deletions src/spaceone/inventory_v2/manager/asset_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from spaceone.inventory_v2.lib.resource_manager import ResourceManager
from spaceone.inventory_v2.manager.identity_manager import IdentityManager
from spaceone.inventory_v2.model.asset.database import Asset
from spaceone.inventory_v2.model.asset.database import Asset, History

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -45,6 +45,7 @@ class AssetManager(BaseManager, ResourceManager):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.asset_model = Asset
self.asset_history_model = History

def create_asset(self, params: dict) -> Asset:
def _rollback(vo: Asset):
Expand Down Expand Up @@ -77,11 +78,11 @@ def delete_cloud_service_by_vo(asset_vo: Asset) -> None:
asset_vo.delete()

def get_asset(
self,
asset_id: str,
domain_id: str,
workspace_id: str = None,
user_projects: list = None,
self,
asset_id: str,
domain_id: str,
workspace_id: str = None,
user_projects: list = None,
):
conditions = {"asset_id": asset_id, "domain_id": domain_id}

Expand All @@ -94,12 +95,12 @@ def get_asset(
return self.asset_model.get(**conditions)

def list_assets(
self,
query: dict,
target: str = None,
change_filter: bool = False,
domain_id: str = None,
reference_filter: dict = None,
self,
query: dict,
target: str = None,
change_filter: bool = False,
domain_id: str = None,
reference_filter: dict = None,
) -> Tuple[QuerySet, int]:
if change_filter:
query = self._change_filter_tags(query)
Expand All @@ -115,12 +116,12 @@ def list_assets(
)

def analyze_assets(
self,
query: dict,
change_filter: bool = False,
domain_id: str = None,
reference_filter: dict = None,
):
self,
query: dict,
change_filter: bool = False,
domain_id: str = None,
reference_filter: dict = None,
) -> dict:
if change_filter:
query = self._change_filter_tags(query)
query = self._change_filter_project_group_id(query, domain_id)
Expand All @@ -130,6 +131,9 @@ def analyze_assets(

return self.asset_model.analyze(**query, reference_filter=reference_filter)

def list_histories(self, query: dict) -> Tuple[QuerySet, int]:
return self.asset_history_model.query(**query)

def _change_filter_tags(self, query: dict) -> dict:
change_filter = []

Expand Down
2 changes: 0 additions & 2 deletions src/spaceone/inventory_v2/manager/collecting_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ def collecting_resources(self, params: dict) -> bool:
secret_data = params["secret_data"]
plugin_info = params["plugin_info"]

print(secret_info)

if is_sub_task:
_LOGGER.debug(
f"[collecting_resources] start sub task: {job_task_id} "
Expand Down
Loading

0 comments on commit fa5bfe4

Please sign in to comment.