Skip to content

Commit

Permalink
feat: add optional installation_request_id parameter to registration … (
Browse files Browse the repository at this point in the history
#282)

…messages
  • Loading branch information
wck0 authored Oct 23, 2024
1 parent 74b2638 commit d9d7418
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 1 deletion.
8 changes: 7 additions & 1 deletion landscape/client/broker/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Configuration class for the broker."""

import os

from landscape.client.deployment import Configuration
Expand Down Expand Up @@ -101,6 +100,13 @@ def make_parser(self):
"managed by Landscape, in which case set it to be the uid that "
"Landscape assigned to the host machine.",
)
parser.add_argument(
"--installation-request-id",
help="Only set this value if this computer is a child instance "
"managed by Landscape, in which case set it to be the request id "
"that Landscape assigned to the installation activity for the "
"host machine.",
)

return parser

Expand Down
4 changes: 4 additions & 0 deletions landscape/client/broker/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Identity:
tags = config_property("tags")
access_group = config_property("access_group")
hostagent_uid = config_property("hostagent_uid")
installation_request_id = config_property("installation_request_id")

def __init__(self, config, persist):
self._config = config
Expand Down Expand Up @@ -192,6 +193,7 @@ def _handle_pre_exchange(self):
group = identity.access_group
registration_key = identity.registration_key
hostagent_uid = identity.hostagent_uid
installation_request_id = identity.installation_request_id

self._message_store.delete_all_messages()

Expand Down Expand Up @@ -221,6 +223,8 @@ def _handle_pre_exchange(self):
message["access_group"] = group
if hostagent_uid:
message["hostagent_uid"] = hostagent_uid
if installation_request_id:
message["installation_request_id"] = installation_request_id

server_api = self._message_store.get_server_api()
# If we have juju data to send and if the server is recent enough to
Expand Down
29 changes: 29 additions & 0 deletions landscape/client/broker/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,32 @@ def test_missing_hostagent_uid_is_none(self):
configuration.load(["--config", filename, "--url", "whatever"])

self.assertIsNone(configuration.hostagent_uid)

def test_installation_request_id_handling(self):
"""
The 'installation_request_id' value specified in the configuration
file is passed through.
"""
filename = self.makeFile(
"[client]\ninstallation_request_id = installed-according-to-plan",
)

configuration = BrokerConfiguration()
configuration.load(["--config", filename, "--url", "whatever"])

self.assertEqual(
configuration.installation_request_id,
"installed-according-to-plan",
)

def test_missing_installation_request_id_is_none(self):
"""
Test that if we don't explicitly pass a installation_request_id,
then this value is None.
"""
filename = self.makeFile("[client]\n")

configuration = BrokerConfiguration()
configuration.load(["--config", filename, "--url", "whatever"])

self.assertIsNone(configuration.installation_request_id)
41 changes: 41 additions & 0 deletions landscape/client/broker/tests/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,47 @@ def test_queue_message_on_exchange_with_none_hostagent_uid(self):
messages = self.mstore.get_pending_messages()
self.assertNotIn("hostagent_uid", messages[0])

def test_queue_message_on_exchange_with_installation_request_id(self):
"""
If the admin has defined a installation_request_id for this computer,
we send it to the server.
"""
self.mstore.set_accepted_types(["register"])
self.mstore.set_server_api(b"3.3")
self.config.account_name = "account_name"
self.config.installation_request_id = "installed-according-to-plan"
self.config.tags = "server,london"
self.reactor.fire("pre-exchange")
messages = self.mstore.get_pending_messages()
self.assertEqual(
"installed-according-to-plan",
messages[0]["installation_request_id"],
)

def test_queue_message_on_exchange_and_empty_installation_request_id(self):
"""
If the installation_request_id is "", then the outgoing message
does not define an "installation_request_id" key.
"""
self.mstore.set_accepted_types(["register"])
self.mstore.set_server_api(b"3.3")
self.config.installation_request_id = ""
self.reactor.fire("pre-exchange")
messages = self.mstore.get_pending_messages()
self.assertNotIn("installation_request_id", messages[0])

def test_queue_message_on_exchange_with_none_installation_request_id(self):
"""
If the installation_request_id is None, then the outgoing message
does not define an "installation_request_id" key.
"""
self.mstore.set_accepted_types(["register"])
self.mstore.set_server_api(b"3.3")
self.config.installation_request_id = None
self.reactor.fire("pre-exchange")
messages = self.mstore.get_pending_messages()
self.assertNotIn("installation_request_id", messages[0])

def test_queueing_registration_message_resets_message_store(self):
"""
When a registration message is queued, the store is reset
Expand Down
2 changes: 2 additions & 0 deletions landscape/client/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ClientRegistrationInfo:

container_info: Optional[str] = None
hostagent_uid: Optional[str] = None
installation_request_id: Optional[str] = None
hostname: Optional[str] = None
juju_info: None = None # We don't send Juju info currently.
registration_password: Optional[str] = None
Expand All @@ -54,6 +55,7 @@ def from_identity(
identity.computer_title,
container_info=get_container_info(),
hostagent_uid=identity.hostagent_uid,
installation_request_id=identity.installation_request_id,
hostname=get_fqdn(),
registration_password=identity.registration_key,
tags=identity.tags,
Expand Down
2 changes: 2 additions & 0 deletions landscape/message_schemas/server_bound.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@
"clone_secure_id": Any(Unicode(), Constant(None)),
"ubuntu_pro_info": Unicode(),
"hostagent_uid": Unicode(),
"installation_request_id": Unicode(),
},
api=b"3.3",
optional=[
Expand All @@ -375,6 +376,7 @@
"clone_secure_id",
"ubuntu_pro_info",
"hostagent_uid",
"installation_request_id",
],
)

Expand Down

0 comments on commit d9d7418

Please sign in to comment.