Skip to content

Commit 658d92b

Browse files
committed
Add example for the expose/unexpose methods
1 parent 466d820 commit 658d92b

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

examples/expose-application.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
This example:
3+
4+
1. Connects to the current model.
5+
2. Deploys a charm and waits until it reports itself active.
6+
3. Demonstrates exposing application endpoints to space and CIDR combinations.
7+
3. Demonstrates unexposing application endpoints.
8+
9+
NOTE: this test must be run against a 2.9 controller.
10+
"""
11+
from juju import loop
12+
from juju.model import Model
13+
from juju.application import ExposedEndpoint
14+
15+
import logging
16+
17+
async def main():
18+
model = Model()
19+
print('Connecting to model')
20+
# connect to current model with current user, per Juju CLI
21+
await model.connect()
22+
23+
try:
24+
print('Deploying ubuntu')
25+
application = await model.deploy(
26+
'cs:~jameinel/ubuntu-lite-7',
27+
application_name='ubuntu',
28+
series='trusty',
29+
channel='stable',
30+
)
31+
32+
print('Waiting for active')
33+
await model.block_until(
34+
lambda: all(unit.workload_status == 'active'
35+
for unit in application.units))
36+
37+
print('Expose all opened port ranges')
38+
await application.expose()
39+
40+
print('Expose all opened port ranges to the CIDRs that correspond to a list of spaces')
41+
await application.expose(exposed_endpoints={
42+
"": ExposedEndpoint(to_spaces=["alpha"])
43+
})
44+
45+
print('Expose all opened port ranges to a list of CIDRs')
46+
await application.expose(exposed_endpoints={
47+
"": ExposedEndpoint(to_cidrs=["10.0.0.0/24"])
48+
})
49+
50+
print('Expose all opened port ranges to a list of spaces and CIDRs')
51+
await application.expose(exposed_endpoints={
52+
"": ExposedEndpoint(to_spaces=["alpha"], to_cidrs=["10.0.0.0/24"])
53+
})
54+
55+
print('Expose individual endpoints to different space/CIDR combinations')
56+
await application.expose(exposed_endpoints={
57+
"": ExposedEndpoint(to_spaces=["alpha"], to_cidrs=["10.0.0.0/24"]),
58+
"ubuntu": ExposedEndpoint(to_cidrs=["10.42.42.0/24"])
59+
})
60+
61+
print('Unexpose individual endpoints (other endpoints remain exposed)')
62+
await application.unexpose(exposed_endpoints=["ubuntu"])
63+
64+
print('Unexpose application')
65+
await application.unexpose()
66+
67+
print('Removing ubuntu')
68+
await application.remove()
69+
finally:
70+
print('Disconnecting from model')
71+
await model.disconnect()
72+
73+
74+
if __name__ == '__main__':
75+
loop.run(main())

0 commit comments

Comments
 (0)