-
Couldn't load subscription status.
- Fork 6
IHS-152 Allow unsetting one-to-one relationship #515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
64b4621
ba3fcad
1d11420
ff67612
0cdf642
4880a12
b49f915
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Allow unsetting optional relationship of cardinality one by setting its value to `None` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -234,15 +234,10 @@ def _generate_input_data( # noqa: C901 | |
|
|
||
| rel: RelatedNodeBase | RelationshipManagerBase = getattr(self, item_name) | ||
|
|
||
| # BLOCKED by https://github.com/opsmill/infrahub/issues/330 | ||
| # if ( | ||
| # item is None | ||
| # and item_name in self._relationships | ||
| # and self._schema.get_relationship(item_name).cardinality == "one" | ||
| # ): | ||
| # data[item_name] = None | ||
| # continue | ||
| # el | ||
| if rel_schema.cardinality == RelationshipCardinality.ONE and rel_schema.optional and not rel.initialized: | ||
| data[item_name] = None | ||
| continue | ||
|
|
||
| if rel is None or not rel.initialized: | ||
| continue | ||
|
|
||
|
|
@@ -315,7 +310,16 @@ def _strip_unmodified_dict(data: dict, original_data: dict, variables: dict, ite | |
| variables.pop(variable_key) | ||
|
|
||
| # TODO: I do not feel _great_ about this | ||
| if not data_item and data_item != [] and item in data: | ||
| # -> I don't even know who you are (but this is not great indeed) -- gmazoyer (quoting Thanos) | ||
| original_data_item = original_data.get(item) | ||
| original_data_item_is_none = original_data_item is None | ||
| if isinstance(original_data_item, dict): | ||
| if "node" in original_data_item: | ||
| original_data_item_is_none = original_data_item["node"] is None | ||
| elif "id" not in original_data_item: | ||
| original_data_item_is_none = True | ||
|
|
||
| if item in data and (data_item in ({}, []) or (data_item is None and original_data_item_is_none)): | ||
| data.pop(item) | ||
|
|
||
| def _strip_unmodified(self, data: dict, variables: dict) -> tuple[dict, dict]: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The whole "strip unmodified" code feels difficult to read. I think we probably want to take on the burden of trying to improve it to make it easier to maintain. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, we should probably look at the schema of the node as an initial starting point when refactoring and figure out what we want this to look like. |
||
|
|
@@ -324,7 +328,9 @@ def _strip_unmodified(self, data: dict, variables: dict) -> tuple[dict, dict]: | |
| relationship_property = getattr(self, relationship) | ||
| if not relationship_property or relationship not in data: | ||
| continue | ||
| if not relationship_property.initialized: | ||
| if not relationship_property.initialized and ( | ||
| not isinstance(relationship_property, RelatedNodeBase) or not relationship_property.schema.optional | ||
| ): | ||
| data.pop(relationship) | ||
| elif isinstance(relationship_property, RelationshipManagerBase) and not relationship_property.has_update: | ||
| data.pop(relationship) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1330,7 +1330,12 @@ async def test_create_input_data(client, location_schema: NodeSchemaAPI, client_ | |
| node = InfrahubNodeSync(client=client, schema=location_schema, data=data) | ||
|
|
||
| assert node._generate_input_data()["data"] == { | ||
| "data": {"name": {"value": "JFK1"}, "description": {"value": "JFK Airport"}, "type": {"value": "SITE"}} | ||
| "data": { | ||
| "name": {"value": "JFK1"}, | ||
| "description": {"value": "JFK Airport"}, | ||
| "type": {"value": "SITE"}, | ||
| "primary_tag": None, | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -1577,7 +1582,7 @@ async def test_create_input_data_with_IPHost_attribute(client, ipaddress_schema, | |
| ip_address = InfrahubNodeSync(client=client, schema=ipaddress_schema, data=data) | ||
|
|
||
| assert ip_address._generate_input_data()["data"] == { | ||
| "data": {"address": {"value": "1.1.1.1/24", "is_protected": True}} | ||
| "data": {"address": {"value": "1.1.1.1/24", "is_protected": True}, "interface": None} | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -1591,7 +1596,7 @@ async def test_create_input_data_with_IPNetwork_attribute(client, ipnetwork_sche | |
| ip_network = InfrahubNodeSync(client=client, schema=ipnetwork_schema, data=data) | ||
|
|
||
| assert ip_network._generate_input_data()["data"] == { | ||
| "data": {"network": {"value": "1.1.1.0/24", "is_protected": True}} | ||
| "data": {"network": {"value": "1.1.1.0/24", "is_protected": True}, "site": None} | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -1789,7 +1794,7 @@ async def test_update_input_data_empty_relationship( | |
| "data": { | ||
| "id": "llllllll-llll-llll-llll-llllllllllll", | ||
| "name": {"value": "DFW"}, | ||
| # "primary_tag": None, | ||
| "primary_tag": None, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think having these should be alright. This does not seem to have a negative impact. |
||
| "tags": [], | ||
| "type": {"value": "SITE"}, | ||
| }, | ||
|
|
@@ -1798,7 +1803,7 @@ async def test_update_input_data_empty_relationship( | |
| "data": { | ||
| "id": "llllllll-llll-llll-llll-llllllllllll", | ||
| "name": {"is_protected": True, "is_visible": True, "value": "DFW"}, | ||
| # "primary_tag": None, | ||
| "primary_tag": None, | ||
| "tags": [], | ||
| "type": {"is_protected": True, "is_visible": True, "value": "SITE"}, | ||
| }, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.