Skip to content

Commit 94a3677

Browse files
authored
Merge pull request #724 from cderici/charmhub-url-from-model-config
#724 #### Description Before this change, the api host address for the charmhub was hard-coded. This change allows fetching the api address from the model config. #### QA Steps ``` tox -e integration -- tests/integration/test_charmhub.py ```
2 parents 00cb2c0 + 7fa113f commit 94a3677

5 files changed

Lines changed: 14 additions & 9 deletions

File tree

juju/bundle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ async def run(self, context):
619619
self.application, charm, overrides=self.resources)
620620
elif Schema.CHARM_HUB.matches(url.schema):
621621
c_hub = charmhub.CharmHub(context.model)
622-
id_, _ = c_hub.get_charm_id(url.name)
622+
id_, _ = await c_hub.get_charm_id(url.name)
623623
origin.id_ = id_
624624
resources = await context.model._add_charmhub_resources(
625625
self.application, charm, origin, overrides=self.resources)

juju/charmhub.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,22 @@ def request_charmhub_with_retry(self, url, retries):
1818
jasyncio.sleep(5)
1919
raise JujuError("Got {} from {}".format(_response.status_code, url))
2020

21-
def get_charm_id(self, charm_name):
21+
async def get_charm_id(self, charm_name):
2222
conn, headers, path_prefix = self.model.connection().https_connection()
2323

24-
url = "https://api.snapcraft.io/v2/charms/info/{}".format(charm_name)
24+
model_conf = await self.model.get_config()
25+
charmhub_url = model_conf['charmhub-url']
26+
url = "{}/v2/charms/info/{}".format(charmhub_url.value, charm_name)
2527
_response = self.request_charmhub_with_retry(url, 5)
2628
response = json.loads(_response.text)
2729
return response['id'], response['name']
2830

29-
def is_subordinate(self, charm_name):
31+
async def is_subordinate(self, charm_name):
3032
conn, headers, path_prefix = self.model.connection().https_connection()
3133

32-
url = "https://api.snapcraft.io/v2/charms/info/{}?fields=default-release.revision.subordinate".format(charm_name)
34+
model_conf = await self.model.get_config()
35+
charmhub_url = model_conf['charmhub-url']
36+
url = "{}/v2/charms/info/{}?fields=default-release.revision.subordinate".format(charmhub_url.value, charm_name)
3337
_response = self.request_charmhub_with_retry(url, 5)
3438
response = json.loads(_response.text)
3539
return 'subordinate' in response['default-release']['revision']

juju/model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1774,7 +1774,8 @@ async def deploy(
17741774
resources = await self._add_charmhub_resources(res.app_name,
17751775
identifier,
17761776
add_charm_res.charm_origin)
1777-
if self.charmhub.is_subordinate(url.name):
1777+
is_sub = await self.charmhub.is_subordinate(url.name)
1778+
if is_sub:
17781779
if num_units > 1:
17791780
raise JujuError("cannot use num_units with subordinate application")
17801781
num_units = 0

tests/integration/test_charmhub.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@pytest.mark.asyncio
1010
async def test_info(event_loop):
1111
async with base.CleanModel() as model:
12-
_, name = model.charmhub.get_charm_id("hello-juju")
12+
_, name = await model.charmhub.get_charm_id("hello-juju")
1313
assert name == "hello-juju"
1414

1515

tests/unit/test_bundle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ async def test_run_with_charmhub_charm(self, event_loop):
307307
context.trusted = False
308308
context.model = model
309309

310-
info_func = mock.Mock(return_value=["12345", "name"])
310+
info_func = base.AsyncMock(return_value=["12345", "name"])
311311

312312
with patch.object(charmhub.CharmHub, 'get_charm_id', info_func):
313313
result = await change.run(context)
@@ -356,7 +356,7 @@ async def test_run_with_charmhub_charm_no_channel(self, event_loop):
356356
context.trusted = False
357357
context.model = model
358358

359-
info_func = mock.Mock(return_value=["12345", "name"])
359+
info_func = base.AsyncMock(return_value=["12345", "name"])
360360

361361
with patch.object(charmhub.CharmHub, 'get_charm_id', info_func):
362362
result = await change.run(context)

0 commit comments

Comments
 (0)