From: Thomas Markwalder Date: Wed, 8 Jun 2022 13:34:10 +0000 (-0400) Subject: [#2430] PostgreSQL CB now supports class user-context X-Git-Tag: Kea-2.1.7~129 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fd89a7983a1cf48df785c64b26b446f2a1c843e4;p=thirdparty%2Fkea.git [#2430] PostgreSQL CB now supports class user-context src/bin/admin/tests/pgsql_tests.sh.in Updated pgsql_upgrade_11_0_to_12_0() src/hooks/dhcp/pgsql_cb/pgsql_cb_dhcp4.cc src/hooks/dhcp/pgsql_cb/pgsql_cb_dhcp6.cc src/hooks/dhcp/pgsql_cb/pgsql_query_macros_dhcp.h added user_context to necessary statements, bindings src/share/database/scripts/pgsql/dhcpdb_create.pgsql src/share/database/scripts/pgsql/upgrade_011_to_012.sh.in added user_context column to class tables --- diff --git a/src/bin/admin/tests/pgsql_tests.sh.in b/src/bin/admin/tests/pgsql_tests.sh.in index 40baa83ef8..3e41e7493b 100644 --- a/src/bin/admin/tests/pgsql_tests.sh.in +++ b/src/bin/admin/tests/pgsql_tests.sh.in @@ -481,6 +481,18 @@ pgsql_upgrade_11_0_to_12_0() { count=$(echo "${OUTPUT}" | grep -Eci 'UPDATE dhcp6_subnet SET shared_network_name = NULL') || true assert_eq 1 "${count}" "function func_dhcp6_shared_network_BDEL() is missing changed line. (expected count %d, returned %d)" + + # user_context should have been added to dhcp4_client_class + qry="select user_context from dhcp4_client_class limit 1;" + run_command \ + pgsql_execute "${qry}" + assert_eq 0 "${EXIT_CODE}" "${qry}. (expected status code %d, returned %d)" + + # user_context should have been added to dhcp6_client_class + qry="select user_context from dhcp6_client_class limit 1;" + run_command \ + pgsql_execute "${qry}" + assert_eq 0 "${EXIT_CODE}" "${qry}. (expected status code %d, returned %d)" } pgsql_upgrade_test() { diff --git a/src/hooks/dhcp/pgsql_cb/pgsql_cb_dhcp4.cc b/src/hooks/dhcp/pgsql_cb/pgsql_cb_dhcp4.cc index adc729260e..afaceced64 100644 --- a/src/hooks/dhcp/pgsql_cb/pgsql_cb_dhcp4.cc +++ b/src/hooks/dhcp/pgsql_cb/pgsql_cb_dhcp4.cc @@ -2258,12 +2258,20 @@ public: // modification_ts last_client_class->setModificationTime(worker.getTimestamp(12)); + // user_context at 13. + if (!worker.isColumnNull(13)) { + ElementPtr user_context = worker.getJSON(13); + if (user_context) { + last_client_class->setContext(user_context); + } + } + class_list.push_back(last_client_class); } - // Check for new server tags at 35. - if (!worker.isColumnNull(35)) { - std::string new_tag = worker.getString(35); + // Check for new server tags at 36. + if (!worker.isColumnNull(36)) { + std::string new_tag = worker.getString(36); if (last_tag != new_tag) { if (!new_tag.empty() && !last_client_class->hasServerTag(ServerTag(new_tag))) { last_client_class->setServerTag(new_tag); @@ -2273,23 +2281,23 @@ public: } } - // Parse client class specific option definition from 13 to 22. - if (!worker.isColumnNull(13) && - (last_option_def_id < worker.getBigInt(13))) { - last_option_def_id = worker.getBigInt(13); + // Parse client class specific option definition from 14 to 23. + if (!worker.isColumnNull(14) && + (last_option_def_id < worker.getBigInt(14))) { + last_option_def_id = worker.getBigInt(14); - auto def = processOptionDefRow(worker, 13); + auto def = processOptionDefRow(worker, 14); if (def) { last_client_class->getCfgOptionDef()->add(def); } } - // Parse client class specific option from 23 to 34. - if (!worker.isColumnNull(23) && - (last_option_id < worker.getBigInt(23))) { - last_option_id = worker.getBigInt(23); + // Parse client class specific option from 24 to 35. + if (!worker.isColumnNull(24) && + (last_option_id < worker.getBigInt(24))) { + last_option_id = worker.getBigInt(24); - OptionDescriptorPtr desc = processOptionRow(Option::V4, worker, 23); + OptionDescriptorPtr desc = processOptionRow(Option::V4, worker, 24); if (desc) { last_client_class->getCfgOption()->add(*desc, desc->space_name_); } @@ -2418,6 +2426,7 @@ public: } in_bindings.addTimestamp(client_class->getModificationTime()); + in_bindings.add(client_class->getContext()); PgSqlTransaction transaction(conn_); @@ -3559,7 +3568,7 @@ TaggedStatementArray tagged_statements = { { // Insert client class. { // PgSqlConfigBackendDHCPv4Impl::INSERT_CLIENT_CLASS4, - 12, + 13, { OID_VARCHAR, // 1 name OID_TEXT, // 2 test @@ -3572,7 +3581,8 @@ TaggedStatementArray tagged_statements = { { OID_INT8, // 9 max_valid_lifetime OID_BOOL, // 10 depend_on_known_directly OID_VARCHAR, // 11 follow_class_name - OID_TIMESTAMP // 12 modification_ts + OID_TIMESTAMP, // 12 modification_ts + OID_TEXT // 13 user_context cast as JSON }, "INSERT_CLIENT_CLASS4", "INSERT INTO dhcp4_client_class(" @@ -3587,9 +3597,10 @@ TaggedStatementArray tagged_statements = { { " max_valid_lifetime," " depend_on_known_directly," " follow_class_name," - " modification_ts" + " modification_ts, " + " user_context " ") VALUES (" - "$1, $2, cast($3 as inet), $4, $5, $6, $7, $8, $9, $10, $11, $12" + "$1, $2, cast($3 as inet), $4, $5, $6, $7, $8, $9, $10, $11, $12, cast($13 as JSON)" ")" }, @@ -3980,7 +3991,7 @@ TaggedStatementArray tagged_statements = { { // Update existing client class with specifying its position. { // PgSqlConfigBackendDHCPv4Impl::UPDATE_CLIENT_CLASS4, - 13, + 14, { OID_VARCHAR, // 1 name OID_TEXT, // 2 test @@ -3994,7 +4005,8 @@ TaggedStatementArray tagged_statements = { { OID_BOOL, // 10 depend_on_known_directly OID_VARCHAR, // 11 follow_class_name OID_TIMESTAMP, // 12 modification_ts - OID_VARCHAR // 13 name (of class to update) + OID_TEXT, // 13 user_context cast as JSON + OID_VARCHAR // 14 name (of class to update) }, "UPDATE_CLIENT_CLASS4", PGSQL_UPDATE_CLIENT_CLASS4("follow_class_name = $11,") @@ -4003,7 +4015,7 @@ TaggedStatementArray tagged_statements = { { // Update existing client class without specifying its position. { // PgSqlConfigBackendDHCPv4Impl::UPDATE_CLIENT_CLASS4_SAME_POSITION, - 13, + 14, { OID_VARCHAR, // 1 name OID_TEXT, // 2 test @@ -4017,7 +4029,8 @@ TaggedStatementArray tagged_statements = { { OID_BOOL, // 10 depend_on_known_directly OID_VARCHAR, // 11 follow_class_name OID_TIMESTAMP, // 12 modification_ts - OID_VARCHAR // 13 name (of class to update) + OID_TEXT, // 13 user_context cast as JSON + OID_VARCHAR // 14 name (of class to update) }, "UPDATE_CLIENT_CLASS4_SAME_POSITION", PGSQL_UPDATE_CLIENT_CLASS4("") diff --git a/src/hooks/dhcp/pgsql_cb/pgsql_cb_dhcp6.cc b/src/hooks/dhcp/pgsql_cb/pgsql_cb_dhcp6.cc index 54e959ae19..599a78cb2b 100644 --- a/src/hooks/dhcp/pgsql_cb/pgsql_cb_dhcp6.cc +++ b/src/hooks/dhcp/pgsql_cb/pgsql_cb_dhcp6.cc @@ -2579,18 +2579,26 @@ public: // modification_ts last_client_class->setModificationTime(worker.getTimestamp(9)); - // class specific option definition from 10 to 19. - // class specific option from 20 to 31. + // user_context at 10. + if (!worker.isColumnNull(10)) { + ElementPtr user_context = worker.getJSON(10); + if (user_context) { + last_client_class->setContext(user_context); + } + } + + // class specific option definition from 11 to 20. + // class specific option from 21 to 32. // preferred lifetime: default, min, max - last_client_class->setPreferred(worker.getTriplet(33, 34, 35)); + last_client_class->setPreferred(worker.getTriplet(34, 35, 36)); class_list.push_back(last_client_class); } - // Check for new server tags at 32. - if (!worker.isColumnNull(32)) { - std::string new_tag = worker.getString(32); + // Check for new server tags at 33. + if (!worker.isColumnNull(33)) { + std::string new_tag = worker.getString(33); if (last_tag != new_tag) { if (!new_tag.empty() && !last_client_class->hasServerTag(ServerTag(new_tag))) { last_client_class->setServerTag(new_tag); @@ -2600,23 +2608,23 @@ public: } } - // Parse client class specific option definition from 10 to 19. - if (!worker.isColumnNull(10) && - (last_option_def_id < worker.getBigInt(10))) { - last_option_def_id = worker.getBigInt(10); + // Parse client class specific option definition from 11 to 20. + if (!worker.isColumnNull(11) && + (last_option_def_id < worker.getBigInt(11))) { + last_option_def_id = worker.getBigInt(11); - auto def = processOptionDefRow(worker, 10); + auto def = processOptionDefRow(worker, 11); if (def) { last_client_class->getCfgOptionDef()->add(def); } } - // Parse client class specific option from 20 to 31. - if (!worker.isColumnNull(20) && - (last_option_id < worker.getBigInt(20))) { - last_option_id = worker.getBigInt(20); + // Parse client class specific option from 21 to 32. + if (!worker.isColumnNull(21) && + (last_option_id < worker.getBigInt(21))) { + last_option_id = worker.getBigInt(21); - OptionDescriptorPtr desc = processOptionRow(Option::V6, worker, 20); + OptionDescriptorPtr desc = processOptionRow(Option::V6, worker, 21); if (desc) { last_client_class->getCfgOption()->add(*desc, desc->space_name_); } @@ -2745,6 +2753,7 @@ public: in_bindings.add(client_class->getPreferred().getMin()); in_bindings.add(client_class->getPreferred().getMax()); in_bindings.addTimestamp(client_class->getModificationTime()); + in_bindings.add(client_class->getContext()); PgSqlTransaction transaction(conn_); @@ -3980,7 +3989,7 @@ TaggedStatementArray tagged_statements = { { // Insert client class. { // PgSqlConfigBackendDHCPv6Impl::INSERT_CLIENT_CLASS6, - 12, + 13, { OID_VARCHAR, // 1 name OID_TEXT, // 2 test @@ -3993,7 +4002,8 @@ TaggedStatementArray tagged_statements = { { OID_INT8, // 9 preferred_lifetime OID_INT8, // 10 min_preferred_lifetime OID_INT8, // 11 max_preferred_lifetime - OID_TIMESTAMP // 12 modification_ts + OID_TIMESTAMP, // 12 modification_ts + OID_TEXT // 13 user_context cast as JSON }, "INSERT_CLIENT_CLASS6", "INSERT INTO dhcp6_client_class(" @@ -4008,9 +4018,10 @@ TaggedStatementArray tagged_statements = { { " preferred_lifetime," " min_preferred_lifetime," " max_preferred_lifetime," - " modification_ts" + " modification_ts," + " user_context " ") VALUES (" - " $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12" + " $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, cast($13 as JSON)" ")" }, @@ -4423,7 +4434,7 @@ TaggedStatementArray tagged_statements = { { // Update existing client class with specifying its position. { // PgSqlConfigBackendDHCPv6Impl::UPDATE_CLIENT_CLASS6, - 13, + 14, { OID_VARCHAR, // 1 name OID_TEXT, // 2 test @@ -4437,7 +4448,8 @@ TaggedStatementArray tagged_statements = { { OID_INT8, // 10 min_preferred_lifetime OID_INT8, // 11 max_preferred_lifetime OID_TIMESTAMP, // 12 modification_ts - OID_VARCHAR // 13 name (of class to update) + OID_TEXT, // 13 user_conetx + OID_VARCHAR // 14 name (of class to update) }, "UPDATE_CLIENT_CLASS6", PGSQL_UPDATE_CLIENT_CLASS6("follow_class_name = $8,") @@ -4446,7 +4458,7 @@ TaggedStatementArray tagged_statements = { { // Update existing client class without specifying its position. { // PgSqlConfigBackendDHCPv6Impl::UPDATE_CLIENT_CLASS6_SAME_POSITION, - 13, + 14, { OID_VARCHAR, // 1 name OID_TEXT, // 2 test @@ -4460,7 +4472,8 @@ TaggedStatementArray tagged_statements = { { OID_INT8, // 10 min_preferred_lifetime OID_INT8, // 11 max_preferred_lifetime OID_TIMESTAMP, // 12 modification_ts - OID_VARCHAR // 13 name (of class to update) + OID_TEXT, // 13 user_conetx + OID_VARCHAR // 14 name (of class to update) }, "UPDATE_CLIENT_CLASS6_SAME_POSITION", PGSQL_UPDATE_CLIENT_CLASS6("") diff --git a/src/hooks/dhcp/pgsql_cb/pgsql_query_macros_dhcp.h b/src/hooks/dhcp/pgsql_cb/pgsql_query_macros_dhcp.h index 56e8e23b81..375d914d96 100644 --- a/src/hooks/dhcp/pgsql_cb/pgsql_query_macros_dhcp.h +++ b/src/hooks/dhcp/pgsql_cb/pgsql_query_macros_dhcp.h @@ -671,6 +671,7 @@ namespace { " c.depend_on_known_directly," \ " o.depend_on_known_indirectly, " \ " gmt_epoch(c.modification_ts) as modification_ts, " \ + " c.user_context," \ " d.id," \ " d.code," \ " d.name," \ @@ -733,6 +734,7 @@ namespace { " c.depend_on_known_directly," \ " o.depend_on_known_indirectly, " \ " gmt_epoch(c.modification_ts) as modification_ts, " \ + " c.user_context, " \ " d.id," \ " d.code," \ " d.name," \ @@ -1086,8 +1088,9 @@ namespace { " max_valid_lifetime = $9," \ " depend_on_known_directly = $10," \ follow_class_name_set \ - " modification_ts = $12 " \ - "WHERE name = $13" + " modification_ts = $12, " \ + " user_context = cast($13 as json)" \ + "WHERE name = $14" #endif #ifndef PGSQL_UPDATE_CLIENT_CLASS6 @@ -1104,8 +1107,9 @@ namespace { " preferred_lifetime = $9, " \ " min_preferred_lifetime = $10, " \ " max_preferred_lifetime = $11, " \ - " modification_ts = $12 " \ - "WHERE name = $13" + " modification_ts = $12, " \ + " user_context = cast($13 as json) " \ + "WHERE name = $14" #endif #ifndef PGSQL_UPDATE_SERVER diff --git a/src/share/database/scripts/pgsql/dhcpdb_create.pgsql b/src/share/database/scripts/pgsql/dhcpdb_create.pgsql index 29f8df7448..a087724096 100644 --- a/src/share/database/scripts/pgsql/dhcpdb_create.pgsql +++ b/src/share/database/scripts/pgsql/dhcpdb_create.pgsql @@ -4969,6 +4969,10 @@ END; $dhcp6_shared_network_BDEL$ LANGUAGE plpgsql; +-- Add user_context column to Client class tables. +ALTER TABLE dhcp4_client_class ADD COLUMN user_context JSON DEFAULT NULL; +ALTER TABLE dhcp6_client_class ADD COLUMN user_context JSON DEFAULT NULL; + -- Update the schema version number. UPDATE schema_version SET version = '12', minor = '0'; diff --git a/src/share/database/scripts/pgsql/upgrade_011_to_012.sh.in b/src/share/database/scripts/pgsql/upgrade_011_to_012.sh.in index ed5c1535e5..cc3fccaa4b 100644 --- a/src/share/database/scripts/pgsql/upgrade_011_to_012.sh.in +++ b/src/share/database/scripts/pgsql/upgrade_011_to_012.sh.in @@ -90,6 +90,10 @@ END; \$dhcp6_shared_network_BDEL\$ LANGUAGE plpgsql; +-- Add user_context column to Client class tables. +ALTER TABLE dhcp4_client_class ADD COLUMN user_context JSON DEFAULT NULL; +ALTER TABLE dhcp6_client_class ADD COLUMN user_context JSON DEFAULT NULL; + -- Update the schema version number. UPDATE schema_version SET version = '12', minor = '0';