Skip to content

Commit e4f5905

Browse files
Find query using charmhub
The following adds the ability to query the charmhub repository. It uses the controller to query the charmhub repository so is really easy for clients to also use the same logic/code path as the go CLI.
1 parent 6fab2ee commit e4f5905

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

examples/charmhub_find.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Example to show how to connect to the current model and search the charm-hub
3+
repository for charms.
4+
"""
5+
import logging
6+
7+
from juju import loop
8+
from juju.model import Model
9+
10+
log = logging.getLogger(__name__)
11+
12+
13+
async def main():
14+
model = Model()
15+
try:
16+
# connect to the current model with the current user, per the Juju CLI
17+
await model.connect()
18+
19+
# do a partial query so that we get more results.
20+
charms = await model.charmhub.find("kuber")
21+
22+
print("Bundle\tName")
23+
for resp in charms.result:
24+
print("{}\t{}".format("N" if resp.type_ == "charm" else "Y", resp.name))
25+
finally:
26+
if model.is_connected():
27+
print('Disconnecting from model')
28+
await model.disconnect()
29+
30+
31+
if __name__ == '__main__':
32+
logging.basicConfig(level=logging.INFO)
33+
loop.run(main())

juju/charmhub.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,12 @@ async def info(self, name, channel=None):
2424
facade = self._facade()
2525
return await facade.Info(tag="application-{}".format(name), channel=channel)
2626

27+
async def find(self, query):
28+
"""find queries the CharmHub store for available charms or bundles.
29+
30+
"""
31+
facade = self._facade()
32+
return await facade.Find(query=query)
33+
2734
def _facade(self):
2835
return client.CharmHubFacade.from_connection(self.model.connection())

tests/integration/test_charmhub.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,27 @@ async def test_info_not_found(event_loop):
3333
assert e.message == "No charm or bundle with name 'badnameforapp'."
3434
else:
3535
raise
36+
37+
38+
@base.bootstrapped
39+
@pytest.mark.asyncio
40+
async def test_find(event_loop):
41+
async with base.CleanModel() as model:
42+
result = await model.charmhub.find("kube")
43+
44+
assert len(result.result) > 0
45+
for resp in result.result:
46+
assert resp.name != ""
47+
assert resp.type_ in ["charm", "bundle"]
48+
49+
50+
@base.bootstrapped
51+
@pytest.mark.asyncio
52+
async def test_find_all(event_loop):
53+
async with base.CleanModel() as model:
54+
result = await model.charmhub.find("")
55+
56+
assert len(result.result) > 0
57+
for resp in result.result:
58+
assert resp.name != ""
59+
assert resp.type_ in ["charm", "bundle"]

0 commit comments

Comments
 (0)