-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdid_trunk_assignment.py
More file actions
108 lines (93 loc) · 3.57 KB
/
Copy pathdid_trunk_assignment.py
File metadata and controls
108 lines (93 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
Demonstrates exclusive trunk/trunk group assignment on DIDs.
Assigning a trunk auto-nullifies the trunk group and vice versa.
Usage: DIDWW_API_KEY=xxx python examples/did_trunk_assignment.py
"""
import uuid
from client_factory import create_client
from didww.enums import Codec, TransportProtocol
from didww.query_params import QueryParams
from didww.resources.did import Did
from didww.resources.voice_in_trunk import VoiceInTrunk
from didww.resources.voice_in_trunk_group import VoiceInTrunkGroup
from didww.resources.configuration.sip import SipConfiguration
client = create_client()
suffix = uuid.uuid4().hex[:8]
include_params = QueryParams().include("voice_in_trunk", "voice_in_trunk_group")
def print_did_assignment(did_id):
result = client.dids().find(did_id, include_params).data
trunk = result.voice_in_trunk
group = result.voice_in_trunk_group
print(f" trunk = {trunk.id if trunk else 'null'}")
print(f" group = {group.id if group else 'null'}")
# Get a DID
did_params = QueryParams().include("voice_in_trunk", "voice_in_trunk_group").page(number=1, size=1)
dids = client.dids().list(did_params).data
if not dids:
raise RuntimeError("No DIDs found. Order a DID first.")
did = dids[0]
print(f"Using DID: {did.number} ({did.id})")
print(f" Emergency enabled: {did.emergency_enabled}")
# Get a POP
pop = client.pops().list().data[0]
# Create a trunk
sip = SipConfiguration()
sip.host = "sip.example.com"
sip.port = 5060
sip.codec_ids = [Codec.PCMU, Codec.PCMA]
sip.transport_protocol_id = TransportProtocol.UDP
trunk = VoiceInTrunk()
trunk.name = f"Assignment Trunk {suffix}"
trunk.configuration = sip
trunk.pop = pop
trunk = client.voice_in_trunks().create(trunk).data
print(f"Created trunk: {trunk.id}")
# Create a trunk group
group = VoiceInTrunkGroup()
group.name = f"Assignment Group {suffix}"
group.capacity_limit = 10
group = client.voice_in_trunk_groups().create(group).data
print(f"Created trunk group: {group.id}")
try:
# 1. Assign trunk to DID (auto-nullifies trunk group)
update1 = Did.build(did.id)
update1.voice_in_trunk = VoiceInTrunk.build(trunk.id)
client.dids().update(update1)
print("\n1. Assigned trunk:")
print_did_assignment(did.id)
# 2. Assign trunk group to DID (auto-nullifies trunk)
update2 = Did.build(did.id)
update2.voice_in_trunk_group = VoiceInTrunkGroup.build(group.id)
client.dids().update(update2)
print("\n2. Assigned trunk group:")
print_did_assignment(did.id)
# 3. Re-assign trunk (auto-nullifies trunk group again)
update3 = Did.build(did.id)
update3.voice_in_trunk = VoiceInTrunk.build(trunk.id)
client.dids().update(update3)
print("\n3. Re-assigned trunk:")
print_did_assignment(did.id)
# 4. Update description only (trunk stays assigned)
update4 = Did.build(did.id)
update4.description = "DID with trunk assigned"
client.dids().update(update4)
print("\n4. Updated description only (trunk stays):")
print_did_assignment(did.id)
finally:
# Cleanup: reassign DID to group (frees trunk), delete trunk, then group
try:
cleanup = Did.build(did.id)
cleanup.voice_in_trunk_group = VoiceInTrunkGroup.build(group.id)
client.dids().update(cleanup)
except Exception:
pass
try:
client.voice_in_trunks().delete(trunk.id)
print("\nDeleted trunk")
except Exception as e:
print(f"\nTrunk cleanup: {e}")
try:
client.voice_in_trunk_groups().delete(group.id)
print("Deleted trunk group")
except Exception as e:
print(f"Trunk group still assigned to DID (expected): {e}")