|
6 | 6 | from infrahub.core.node import Node |
7 | 7 | from infrahub.core.node.resource_manager.ip_address_pool import CoreIPAddressPool |
8 | 8 | from infrahub.core.node.resource_manager.ip_prefix_pool import CoreIPPrefixPool |
| 9 | +from infrahub.core.node.resource_manager.number_pool import CoreNumberPool |
9 | 10 | from infrahub.core.schema import SchemaRoot |
| 11 | +from infrahub.core.schema.attribute_parameters import NumberPoolParameters |
10 | 12 | from infrahub.core.schema.schema_branch import SchemaBranch |
11 | 13 | from infrahub.database import InfrahubDatabase |
12 | 14 | from infrahub.graphql.initialization import prepare_graphql_params |
13 | 15 | from tests.helpers.graphql import graphql |
14 | | -from tests.helpers.schema import TICKET, load_schema |
| 16 | +from tests.helpers.schema import SNOW_TICKET_SCHEMA, TICKET, load_schema |
15 | 17 |
|
16 | 18 |
|
17 | 19 | @pytest.fixture |
@@ -662,6 +664,34 @@ async def test_address_pool_get_resource_with_prefix_length( |
662 | 664 | """ |
663 | 665 |
|
664 | 666 |
|
| 667 | +DELETE_NUMBER_POOL = """ |
| 668 | +mutation DeleteNumberPool( |
| 669 | + $id: String!, |
| 670 | + ) { |
| 671 | + CoreNumberPoolDelete( |
| 672 | + data: { |
| 673 | + id: $id, |
| 674 | + } |
| 675 | + ) { |
| 676 | + ok |
| 677 | + } |
| 678 | +} |
| 679 | +""" |
| 680 | + |
| 681 | + |
| 682 | +QUERY_NUMBER_POOL = """ |
| 683 | +query NumberPool( |
| 684 | + $id: ID!, |
| 685 | + ) { |
| 686 | + CoreNumberPool( |
| 687 | + ids: [$id] |
| 688 | + ) { |
| 689 | + count |
| 690 | + } |
| 691 | +} |
| 692 | +""" |
| 693 | + |
| 694 | + |
665 | 695 | async def test_test_number_pool_creation_errors( |
666 | 696 | db: InfrahubDatabase, default_branch: Branch, register_core_models_schema |
667 | 697 | ): |
@@ -812,3 +842,117 @@ async def test_test_number_pool_update(db: InfrahubDatabase, default_branch: Bra |
812 | 842 | assert "start_range can't be larger than end_range" in str(update_invalid_range.errors[0]) |
813 | 843 | assert update_ok.data |
814 | 844 | assert not update_ok.errors |
| 845 | + |
| 846 | + # Validate that we can delete a number pool that isn't tied to an attribute of kind NumberPool |
| 847 | + delete_ok = await graphql( |
| 848 | + schema=gql_params.schema, |
| 849 | + source=DELETE_NUMBER_POOL, |
| 850 | + context_value=gql_params.context, |
| 851 | + root_value=None, |
| 852 | + variable_values={ |
| 853 | + "id": pool_id, |
| 854 | + }, |
| 855 | + ) |
| 856 | + assert not delete_ok.errors |
| 857 | + assert delete_ok.data |
| 858 | + assert delete_ok.data["CoreNumberPoolDelete"]["ok"] |
| 859 | + |
| 860 | + query_after_delete = await graphql( |
| 861 | + schema=gql_params.schema, |
| 862 | + source=QUERY_NUMBER_POOL, |
| 863 | + context_value=gql_params.context, |
| 864 | + root_value=None, |
| 865 | + variable_values={ |
| 866 | + "id": pool_id, |
| 867 | + }, |
| 868 | + ) |
| 869 | + assert not query_after_delete.errors |
| 870 | + assert query_after_delete.data |
| 871 | + assert query_after_delete.data["CoreNumberPool"]["count"] == 0 |
| 872 | + |
| 873 | + |
| 874 | +async def test_delete_number_pool_in_use_by_numberpool_attribute( |
| 875 | + db: InfrahubDatabase, default_branch: Branch, register_core_models_schema: None |
| 876 | +) -> None: |
| 877 | + await load_schema(db=db, schema=SNOW_TICKET_SCHEMA) |
| 878 | + gql_params = await prepare_graphql_params(db=db, include_subscription=False, branch=default_branch) |
| 879 | + node_schema = registry.schema.get(name="SnowTask", branch=default_branch) |
| 880 | + number_pool_attribute = node_schema.get_attribute(name="number") |
| 881 | + assert isinstance(number_pool_attribute.parameters, NumberPoolParameters) |
| 882 | + registry.node[InfrahubKind.NUMBERPOOL] = CoreNumberPool |
| 883 | + query_before_creation = await graphql( |
| 884 | + schema=gql_params.schema, |
| 885 | + source=QUERY_NUMBER_POOL, |
| 886 | + context_value=gql_params.context, |
| 887 | + root_value=None, |
| 888 | + variable_values={ |
| 889 | + "id": number_pool_attribute.parameters.number_pool_id, |
| 890 | + }, |
| 891 | + ) |
| 892 | + |
| 893 | + assert not query_before_creation.errors |
| 894 | + assert query_before_creation.data |
| 895 | + assert query_before_creation.data["CoreNumberPool"]["count"] == 0 |
| 896 | + |
| 897 | + create_snow_incident_mutation = """ |
| 898 | + mutation CreateSnowIncident( |
| 899 | + $title: String!, |
| 900 | + ) { |
| 901 | + SnowIncidentCreate( |
| 902 | + data: { |
| 903 | + title: {value: $title}, |
| 904 | + } |
| 905 | + ) { |
| 906 | + object { |
| 907 | + title { |
| 908 | + value |
| 909 | + } |
| 910 | + number { |
| 911 | + value |
| 912 | + source { |
| 913 | + id |
| 914 | + } |
| 915 | + } |
| 916 | + identifier { |
| 917 | + value |
| 918 | + } |
| 919 | + } |
| 920 | + } |
| 921 | + } |
| 922 | + """ |
| 923 | + |
| 924 | + create_snow_incident = await graphql( |
| 925 | + schema=gql_params.schema, |
| 926 | + source=create_snow_incident_mutation, |
| 927 | + context_value=gql_params.context, |
| 928 | + root_value=None, |
| 929 | + variable_values={ |
| 930 | + "title": "Printer is saying PC load Letter", |
| 931 | + }, |
| 932 | + ) |
| 933 | + |
| 934 | + assert not create_snow_incident.errors |
| 935 | + assert create_snow_incident.data |
| 936 | + assert ( |
| 937 | + create_snow_incident.data["SnowIncidentCreate"]["object"]["title"]["value"] |
| 938 | + == "Printer is saying PC load Letter" |
| 939 | + ) |
| 940 | + assert create_snow_incident.data["SnowIncidentCreate"]["object"]["number"]["value"] == 1 |
| 941 | + assert ( |
| 942 | + create_snow_incident.data["SnowIncidentCreate"]["object"]["number"]["source"]["id"] |
| 943 | + == number_pool_attribute.parameters.number_pool_id |
| 944 | + ) |
| 945 | + assert create_snow_incident.data["SnowIncidentCreate"]["object"]["identifier"]["value"] == "INC1" |
| 946 | + |
| 947 | + delete_fail = await graphql( |
| 948 | + schema=gql_params.schema, |
| 949 | + source=DELETE_NUMBER_POOL, |
| 950 | + context_value=gql_params.context, |
| 951 | + root_value=None, |
| 952 | + variable_values={ |
| 953 | + "id": number_pool_attribute.parameters.number_pool_id, |
| 954 | + }, |
| 955 | + ) |
| 956 | + |
| 957 | + assert delete_fail.errors |
| 958 | + assert "Unable to delete number pool SnowTask.number is in use (branches: main)" in str(delete_fail.errors) |
0 commit comments