Merge ~ltrager/maas:lp1914165 into maas:master

Proposed by Lee Trager
Status: Merged
Approved by: Adam Collard
Approved revision: ea9848c4dbd6ce1003212ad0696f899e644246f5
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~ltrager/maas:lp1914165
Merge into: maas:master
Diff against target: 87 lines (+43/-7)
2 files modified
src/provisioningserver/drivers/power/proxmox.py (+23/-7)
src/provisioningserver/drivers/power/tests/test_proxmox.py (+20/-0)
Reviewer Review Type Date Requested Status
Adam Collard Approve
MAAS Lander Approve
Review via email: mp+397333@code.launchpad.net

Commit message

LP: #1914165 - Allow a custom port to be used with the Proxmox driver.

To post a comment you must log in.
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b lp1914165 lp:~ltrager/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 3be0738b110274f8f97dc20607f9905239f82a9e

review: Approve
Revision history for this message
Adam Collard (adam-collard) wrote :

The query parameters need URL encoding - see https://paste.ubuntu.com/p/9mBnzYsK9g/

review: Needs Fixing
~ltrager/maas:lp1914165 updated
bf94255... by Lee Trager

Merge branch 'master' into lp1914165

ea9848c... by Lee Trager

Apply patch from adam-collard

Revision history for this message
Lee Trager (ltrager) wrote :

Thanks for the patch!

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b lp1914165 lp:~ltrager/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: ea9848c4dbd6ce1003212ad0696f899e644246f5

review: Approve
Revision history for this message
Adam Collard (adam-collard) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/provisioningserver/drivers/power/proxmox.py b/src/provisioningserver/drivers/power/proxmox.py
2index c0e6ca1..7e141e9 100644
3--- a/src/provisioningserver/drivers/power/proxmox.py
4+++ b/src/provisioningserver/drivers/power/proxmox.py
5@@ -5,11 +5,13 @@
6
7 from io import BytesIO
8 import json
9+from urllib.parse import urlencode, urlparse
10
11 from twisted.internet.defer import inlineCallbacks, succeed
12 from twisted.web.client import FileBodyProducer
13
14 from provisioningserver.drivers import (
15+ IP_EXTRACTOR_PATTERNS,
16 make_ip_extractor,
17 make_setting_field,
18 SETTING_SCOPE,
19@@ -62,16 +64,30 @@ class ProxmoxPowerDriver(WebhookPowerDriver):
20 ),
21 ]
22
23- ip_extractor = make_ip_extractor("power_address")
24+ ip_extractor = make_ip_extractor(
25+ "power_address", IP_EXTRACTOR_PATTERNS.URL
26+ )
27
28 def _get_url(self, context, endpoint, params=None):
29- uri = f"https://{context['power_address']}:8006/api2/json/{endpoint}"
30+ url = urlparse(context["power_address"])
31+ if not url.scheme:
32+ # When the scheme is not included in the power address
33+ # urlparse puts the url into path.
34+ url = url._replace(scheme="https", netloc=url.path, path="")
35+ if not url.port:
36+ if url.netloc:
37+ url = url._replace(netloc="%s:8006" % url.netloc)
38+ else:
39+ # Similar to above, we need to swap netloc and path.
40+ url = url._replace(netloc="%s:8006" % url.path, path="")
41 if params:
42- uri = "%s?%s" % (
43- uri,
44- "&".join([f"{key}={value}" for key, value in params.items()]),
45- )
46- return uri.encode()
47+ query = urlencode(params)
48+ else:
49+ query = ""
50+ url = url._replace(
51+ path="/api2/json/%s" % endpoint, query=query, fragment=""
52+ )
53+ return url.geturl().encode()
54
55 def _login(self, system_id, context):
56 power_token_name = context.get("power_token_name")
57diff --git a/src/provisioningserver/drivers/power/tests/test_proxmox.py b/src/provisioningserver/drivers/power/tests/test_proxmox.py
58index 7c32b78..881ac5f 100644
59--- a/src/provisioningserver/drivers/power/tests/test_proxmox.py
60+++ b/src/provisioningserver/drivers/power/tests/test_proxmox.py
61@@ -52,6 +52,26 @@ class TestProxmoxPowerDriver(MAASTestCase):
62 ),
63 )
64
65+ def test_get_url_funky_params(self):
66+ power_address = factory.make_name("power_address")
67+ endpoint = factory.make_name("endpoint")
68+ params = {"test": "? /"}
69+ params_str = "test=%3F+%2F"
70+ self.assertEqual(
71+ f"https://{power_address}:8006/api2/json/{endpoint}?{params_str}".encode(),
72+ self.proxmox._get_url(
73+ {"power_address": power_address}, endpoint, params
74+ ),
75+ )
76+
77+ def test_get_url_allows_custom_port(self):
78+ power_address = "%s:443" % factory.make_name("power_address")
79+ endpoint = factory.make_name("endpoint")
80+ self.assertEqual(
81+ f"https://{power_address}/api2/json/{endpoint}".encode(),
82+ self.proxmox._get_url({"power_address": power_address}, endpoint),
83+ )
84+
85 @inlineCallbacks
86 def test_login(self):
87 system_id = factory.make_name("system_id")

Subscribers

People subscribed via source and target branches