Merge ~adam-collard/maas:lp1914165-2.9 into maas:2.9

Proposed by Adam Collard
Status: Merged
Approved by: Adam Collard
Approved revision: d052382a8db9948e51084f25618dadc5151aea4d
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~adam-collard/maas:lp1914165-2.9
Merge into: maas:2.9
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
Review via email: mp+397425@code.launchpad.net

Commit message

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

Cherry-pick ccda539eee75c82d10bb32d1a6dff2cb48f954df

To post a comment you must log in.
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