Merge ~ack/maas:1950827-3.1 into maas:3.1

Proposed by Alberto Donato
Status: Merged
Approved by: Alberto Donato
Approved revision: 1077c9ca94af21e324a8956c42a7ff0ea37f8d05
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~ack/maas:1950827-3.1
Merge into: maas:3.1
Diff against target: 50 lines (+13/-9)
2 files modified
src/maasserver/tests/test_vmhost.py (+1/-0)
src/maasserver/vmhost.py (+12/-9)
Reviewer Review Type Date Requested Status
Alberto Donato (community) Approve
Review via email: mp+411852@code.launchpad.net

Commit message

LP:1950827 urlparse doesn't work if scheme is missing

To post a comment you must log in.
Revision history for this message
Alberto Donato (ack) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/maasserver/tests/test_vmhost.py b/src/maasserver/tests/test_vmhost.py
2index 4850aed..a46f7a3 100644
3--- a/src/maasserver/tests/test_vmhost.py
4+++ b/src/maasserver/tests/test_vmhost.py
5@@ -281,6 +281,7 @@ class TestCleanPowerAddress(MAASServerTestCase):
6 def test_clean_power_address(self):
7 cases = [
8 ("10.0.0.1", "10.0.0.1:8443"),
9+ ("10.0.0.1:8443", "10.0.0.1:8443"),
10 ("[2001:db8::1]", "[2001:db8::1]:8443"),
11 ("lxd-1", "lxd-1:8443"),
12 ("lxd.cluster.com", "lxd.cluster.com:8443"),
13diff --git a/src/maasserver/vmhost.py b/src/maasserver/vmhost.py
14index dba36da..882e533 100644
15--- a/src/maasserver/vmhost.py
16+++ b/src/maasserver/vmhost.py
17@@ -137,21 +137,24 @@ def _clean_power_address(vmhost_address):
18 HTTPS or the user omits the protocol. This MUST be
19 generalized to support other services.
20 """
21- vmhost_url = urlparse(vmhost_address)
22-
23 lxd_schemes = ["https", ""]
24+
25+ # urlparse() doesn't work if scheme is missing
26+ if "://" not in vmhost_address:
27+ vmhost_address = f"{lxd_schemes[0]}://{vmhost_address}"
28+
29+ vmhost_url = urlparse(vmhost_address)
30 if vmhost_url.port:
31 port = f":{vmhost_url.port}"
32 else:
33 port = ":8443" if vmhost_url.scheme in lxd_schemes else ""
34
35- # parsing just an IP as a url results in the IP stored in path
36- host = vmhost_url.hostname or vmhost_url.path
37- new_path = vmhost_url.path if vmhost_url.hostname else ""
38- if valid_ipv6(host):
39- host = f"[{host}]"
40-
41- vmhost_url = vmhost_url._replace(netloc=f"{host}{port}", path=new_path)
42+ host = (
43+ f"[{vmhost_url.hostname}]"
44+ if valid_ipv6(vmhost_url.hostname)
45+ else vmhost_url.hostname
46+ )
47+ vmhost_url = vmhost_url._replace(netloc=f"{host}{port}")
48 vmhost_address = vmhost_url.geturl()
49
50 if vmhost_url.scheme in lxd_schemes:

Subscribers

People subscribed via source and target branches