Skip to content

Commit

Permalink
Merge pull request #275 from Sooyoung98/master
Browse files Browse the repository at this point in the history
feat: added metadata features to support search labels and data_types
  • Loading branch information
Sooyoung98 authored Apr 30, 2024
2 parents 6eba293 + 45f0a6f commit 65ee6bd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
46 changes: 34 additions & 12 deletions src/spaceone/inventory/plugin/collector/lib/metadata_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _separate_metadata(self):
return query_sets_meta, search_meta, table_meta, tabs_meta, widget_meta

def _generate_search(self, search_meta: dict) -> list:
return self._generate_fields(search_meta["fields"])
return self._generate_fields(search_meta["fields"], is_search=True)

def _generate_table(self, table_meta: dict) -> dict:
table_metadata = self._generate_default_dynamic_view(
Expand Down Expand Up @@ -91,9 +91,9 @@ def _generate_tabs(self, tabs_meta: list) -> dict:
del inner_tab_meta["root_path"]

if "sort" in inner_tab_meta:
inner_dynamic_view["options"][
"default_sort"
] = self._generate_sort(inner_tab_meta["sort"])
inner_dynamic_view["options"]["default_sort"] = (
self._generate_sort(inner_tab_meta["sort"])
)

if "root_path" in inner_tab_meta:
inner_dynamic_view["options"]["root_path"] = inner_tab_meta[
Expand Down Expand Up @@ -145,10 +145,13 @@ def _generate_sort(table: dict) -> dict:

return Sort(**sort_option).dict()

def _generate_fields(self, fields: list) -> list:
def _generate_fields(self, fields: list, is_search=False) -> list:
gen_fields = []
for field in fields:
if "type" not in field:
if "data_type" in field:
gen_fields.append(self._generate_data_type_field(field))

elif "type" not in field:
gen_fields.append(self._generate_text_field(field))

elif field["type"] == "text":
Expand Down Expand Up @@ -176,12 +179,18 @@ def _generate_fields(self, fields: list) -> list:
gen_fields.append(self._generate_image_field(field))

elif field["type"] == "enum":
gen_fields.append(self._generate_enum_field(field))
gen_fields.append(self._generate_enum_field(field, is_search))

elif field["type"] == "more":
gen_fields.append(self._generate_more_field(field))
return gen_fields

def _generate_data_type_field(self, field: dict) -> dict:
if "key" not in field:
field = self._add_key_name_fields(field)

return DataTypeField(**field).dict(exclude_none=True)

def _generate_text_field(self, field: dict) -> dict:
if "key" not in field:
field = self._add_key_name_fields(field)
Expand Down Expand Up @@ -346,7 +355,7 @@ def _generate_image_field(self, field: dict, is_enum: bool = False) -> dict:
else:
return EnumImageField(**field).dict(exclude_none=True)

def _generate_enum_field(self, field: dict) -> dict:
def _generate_enum_field(self, field: dict, is_search=False) -> dict:
if "key" not in field:
field = self._add_key_name_fields(field)

Expand Down Expand Up @@ -376,6 +385,12 @@ def _generate_enum_field(self, field: dict) -> dict:
key for key in enum.keys() if key not in enable_enum_options
][0]

if enum["type"] == "label":
enum["label"] = enum[main_key]
del enum[main_key]
del enum["type"]
enums[main_key] = enum

if enum["type"] == "badge":
enum["outline_color"] = enum[main_key]
del enum[main_key]
Expand Down Expand Up @@ -403,12 +418,19 @@ def _generate_enum_field(self, field: dict) -> dict:
enums[main_key] = self._generate_datetime_field(
field=enum, is_enum=True
)
if "options" in field:
field["options"].update(enums)

if is_search:
field["enums"] = enums
else:
field["options"] = enums
if "options" in field:
field["options"].update(enums)
else:
field["options"] = enums

del field["enums"]

del field["enums"]
if is_search:
return SearchEnumField(**field).dict(exclude_none=True)

return EnumField(**field).dict(exclude_none=True)

Expand Down
17 changes: 17 additions & 0 deletions src/spaceone/inventory/plugin/collector/model/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"EnumStateField",
"EnumDatetimeField",
"EnumImageField",
"DataTypeField",
"SearchEnumField",
]

BACKGROUND_COLORS = (
Expand Down Expand Up @@ -169,6 +171,13 @@ class MoreOptions(BaseModel):
layout: MoreOptionsLayout = None


class DataTypeField(BaseModel):
name: str
key: str
data_type: str
options: Union[dict, None] = None


class TextField(BaseModel):
name: str
key: str
Expand Down Expand Up @@ -232,6 +241,14 @@ class EnumField(BaseModel):
options: dict


class SearchEnumField(BaseModel):
name: str
key: str
type: str = "enum"
enums: dict
options: dict = None


class MoreField(BaseModel):
name: str
key: str
Expand Down

0 comments on commit 65ee6bd

Please sign in to comment.