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
diff --git a/src/provisioningserver/drivers/power/proxmox.py b/src/provisioningserver/drivers/power/proxmox.py
index c0e6ca1..7e141e9 100644
--- a/src/provisioningserver/drivers/power/proxmox.py
+++ b/src/provisioningserver/drivers/power/proxmox.py
@@ -5,11 +5,13 @@
55
6from io import BytesIO6from io import BytesIO
7import json7import json
8from urllib.parse import urlencode, urlparse
89
9from twisted.internet.defer import inlineCallbacks, succeed10from twisted.internet.defer import inlineCallbacks, succeed
10from twisted.web.client import FileBodyProducer11from twisted.web.client import FileBodyProducer
1112
12from provisioningserver.drivers import (13from provisioningserver.drivers import (
14 IP_EXTRACTOR_PATTERNS,
13 make_ip_extractor,15 make_ip_extractor,
14 make_setting_field,16 make_setting_field,
15 SETTING_SCOPE,17 SETTING_SCOPE,
@@ -62,16 +64,30 @@ class ProxmoxPowerDriver(WebhookPowerDriver):
62 ),64 ),
63 ]65 ]
6466
65 ip_extractor = make_ip_extractor("power_address")67 ip_extractor = make_ip_extractor(
68 "power_address", IP_EXTRACTOR_PATTERNS.URL
69 )
6670
67 def _get_url(self, context, endpoint, params=None):71 def _get_url(self, context, endpoint, params=None):
68 uri = f"https://{context['power_address']}:8006/api2/json/{endpoint}"72 url = urlparse(context["power_address"])
73 if not url.scheme:
74 # When the scheme is not included in the power address
75 # urlparse puts the url into path.
76 url = url._replace(scheme="https", netloc=url.path, path="")
77 if not url.port:
78 if url.netloc:
79 url = url._replace(netloc="%s:8006" % url.netloc)
80 else:
81 # Similar to above, we need to swap netloc and path.
82 url = url._replace(netloc="%s:8006" % url.path, path="")
69 if params:83 if params:
70 uri = "%s?%s" % (84 query = urlencode(params)
71 uri,85 else:
72 "&".join([f"{key}={value}" for key, value in params.items()]),86 query = ""
73 )87 url = url._replace(
74 return uri.encode()88 path="/api2/json/%s" % endpoint, query=query, fragment=""
89 )
90 return url.geturl().encode()
7591
76 def _login(self, system_id, context):92 def _login(self, system_id, context):
77 power_token_name = context.get("power_token_name")93 power_token_name = context.get("power_token_name")
diff --git a/src/provisioningserver/drivers/power/tests/test_proxmox.py b/src/provisioningserver/drivers/power/tests/test_proxmox.py
index 7c32b78..881ac5f 100644
--- a/src/provisioningserver/drivers/power/tests/test_proxmox.py
+++ b/src/provisioningserver/drivers/power/tests/test_proxmox.py
@@ -52,6 +52,26 @@ class TestProxmoxPowerDriver(MAASTestCase):
52 ),52 ),
53 )53 )
5454
55 def test_get_url_funky_params(self):
56 power_address = factory.make_name("power_address")
57 endpoint = factory.make_name("endpoint")
58 params = {"test": "? /"}
59 params_str = "test=%3F+%2F"
60 self.assertEqual(
61 f"https://{power_address}:8006/api2/json/{endpoint}?{params_str}".encode(),
62 self.proxmox._get_url(
63 {"power_address": power_address}, endpoint, params
64 ),
65 )
66
67 def test_get_url_allows_custom_port(self):
68 power_address = "%s:443" % factory.make_name("power_address")
69 endpoint = factory.make_name("endpoint")
70 self.assertEqual(
71 f"https://{power_address}/api2/json/{endpoint}".encode(),
72 self.proxmox._get_url({"power_address": power_address}, endpoint),
73 )
74
55 @inlineCallbacks75 @inlineCallbacks
56 def test_login(self):76 def test_login(self):
57 system_id = factory.make_name("system_id")77 system_id = factory.make_name("system_id")

Subscribers

People subscribed via source and target branches