Skip to content

Commit 8f4ce65

Browse files
Changing up how dispatch rules are assigned to numbers
1 parent ed4c9d9 commit 8f4ce65

File tree

1 file changed

+90
-8
lines changed

1 file changed

+90
-8
lines changed

cmd/lk/phone_number.go

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,53 @@ func createPhoneNumberClient(ctx context.Context, cmd *cli.Command) (*lksdk.Phon
161161
return lksdk.NewPhoneNumberClient(project.URL, project.APIKey, project.APISecret, withDefaultClientOpts(project)...), nil
162162
}
163163

164+
// appendPhoneNumberToDispatchRule appends a phone number ID to the trunk_ids of a dispatch rule
165+
func appendPhoneNumberToDispatchRule(ctx context.Context, cmd *cli.Command, dispatchRuleID, phoneNumberID string) error {
166+
_, err := requireProject(ctx, cmd)
167+
if err != nil {
168+
return fmt.Errorf("failed to get project: %w", err)
169+
}
170+
171+
sipClient := lksdk.NewSIPClient(project.URL, project.APIKey, project.APISecret, withDefaultClientOpts(project)...)
172+
173+
// Get the current dispatch rule to check if phone number ID is already in trunk_ids
174+
rules, err := sipClient.GetSIPDispatchRulesByIDs(ctx, []string{dispatchRuleID})
175+
if err != nil {
176+
return fmt.Errorf("failed to get dispatch rule: %w", err)
177+
}
178+
if len(rules) == 0 {
179+
return fmt.Errorf("dispatch rule %s not found", dispatchRuleID)
180+
}
181+
currentRule := rules[0]
182+
183+
// Check if phone number ID is already in trunk_ids
184+
for _, trunkID := range currentRule.TrunkIds {
185+
if trunkID == phoneNumberID {
186+
// Already in the list, no need to update
187+
return nil
188+
}
189+
}
190+
191+
// Append phone number ID to trunk_ids using Update action
192+
updateReq := &livekit.UpdateSIPDispatchRuleRequest{
193+
SipDispatchRuleId: dispatchRuleID,
194+
Action: &livekit.UpdateSIPDispatchRuleRequest_Update{
195+
Update: &livekit.SIPDispatchRuleUpdate{
196+
TrunkIds: &livekit.ListUpdate{
197+
Add: []string{phoneNumberID},
198+
},
199+
},
200+
},
201+
}
202+
203+
_, err = sipClient.UpdateSIPDispatchRule(ctx, updateReq)
204+
if err != nil {
205+
return fmt.Errorf("failed to update dispatch rule: %w", err)
206+
}
207+
208+
return nil
209+
}
210+
164211
func searchPhoneNumbers(ctx context.Context, cmd *cli.Command) error {
165212
client, err := createPhoneNumberClient(ctx, cmd)
166213
if err != nil {
@@ -226,26 +273,44 @@ func purchasePhoneNumbers(ctx context.Context, cmd *cli.Command) error {
226273
return fmt.Errorf("at least one phone number must be provided")
227274
}
228275

276+
dispatchRuleID := cmd.String("sip-dispatch-rule-id")
277+
229278
req := &livekit.PurchasePhoneNumberRequest{
230279
PhoneNumbers: phoneNumbers,
231280
}
232-
if val := cmd.String("sip-dispatch-rule-id"); val != "" {
233-
req.SipDispatchRuleId = &val
234-
}
235281

236282
resp, err := client.PurchasePhoneNumber(ctx, req)
237283
if err != nil {
238284
return err
239285
}
240286

287+
// If dispatch rule ID was provided, append each purchased phone number ID to the dispatch rule's trunk_ids
288+
dispatchRuleAdded := make(map[string]bool)
289+
if dispatchRuleID != "" {
290+
for _, phoneNumber := range resp.PhoneNumbers {
291+
if err := appendPhoneNumberToDispatchRule(ctx, cmd, dispatchRuleID, phoneNumber.Id); err != nil {
292+
// Log error but don't fail the purchase operation
293+
fmt.Fprintf(cmd.ErrWriter, "Warning: failed to add phone number %s to dispatch rule %s: %v\n", phoneNumber.Id, dispatchRuleID, err)
294+
} else {
295+
dispatchRuleAdded[phoneNumber.Id] = true
296+
}
297+
}
298+
}
299+
241300
if cmd.Bool("json") {
242301
util.PrintJSON(resp)
243302
return nil
244303
}
245304

246305
fmt.Printf("Successfully purchased %d phone numbers:\n", len(resp.PhoneNumbers))
247306
for _, phoneNumber := range resp.PhoneNumbers {
248-
fmt.Printf(" %s (%s) - %s\n", phoneNumber.E164Format, phoneNumber.Id, strings.TrimPrefix(phoneNumber.Status.String(), "PHONE_NUMBER_STATUS_"))
307+
ruleInfo := ""
308+
if dispatchRuleAdded[phoneNumber.Id] && dispatchRuleID != "" {
309+
ruleInfo = fmt.Sprintf(" (SIP Dispatch Rule: %s)", dispatchRuleID)
310+
} else if phoneNumber.SipDispatchRuleId != "" {
311+
ruleInfo = fmt.Sprintf(" (SIP Dispatch Rule: %s)", phoneNumber.SipDispatchRuleId)
312+
}
313+
fmt.Printf(" %s (%s) - %s%s\n", phoneNumber.E164Format, phoneNumber.Id, strings.TrimPrefix(phoneNumber.Status.String(), "PHONE_NUMBER_STATUS_"), ruleInfo)
249314
}
250315

251316
return nil
@@ -376,21 +441,32 @@ func updatePhoneNumber(ctx context.Context, cmd *cli.Command) error {
376441
return fmt.Errorf("only one of --id or --number can be provided")
377442
}
378443

444+
dispatchRuleID := cmd.String("sip-dispatch-rule-id")
445+
379446
req := &livekit.UpdatePhoneNumberRequest{}
380447
if id != "" {
381448
req.Id = &id
382449
} else {
383450
req.PhoneNumber = &phoneNumber
384451
}
385-
if val := cmd.String("sip-dispatch-rule-id"); val != "" {
386-
req.SipDispatchRuleId = &val
387-
}
388452

389453
resp, err := client.UpdatePhoneNumber(ctx, req)
390454
if err != nil {
391455
return err
392456
}
393457

458+
// If dispatch rule ID was provided, append the phone number ID to the dispatch rule's trunk_ids
459+
dispatchRuleAdded := false
460+
if dispatchRuleID != "" {
461+
phoneNumberID := resp.PhoneNumber.Id
462+
if err := appendPhoneNumberToDispatchRule(ctx, cmd, dispatchRuleID, phoneNumberID); err != nil {
463+
// Log error but don't fail the update operation
464+
fmt.Fprintf(cmd.ErrWriter, "Warning: failed to add phone number %s to dispatch rule %s: %v\n", phoneNumberID, dispatchRuleID, err)
465+
} else {
466+
dispatchRuleAdded = true
467+
}
468+
}
469+
394470
if cmd.Bool("json") {
395471
util.PrintJSON(resp)
396472
return nil
@@ -401,7 +477,13 @@ func updatePhoneNumber(ctx context.Context, cmd *cli.Command) error {
401477
fmt.Printf(" ID: %s\n", item.Id)
402478
fmt.Printf(" E164 Format: %s\n", item.E164Format)
403479
fmt.Printf(" Status: %s\n", strings.TrimPrefix(item.Status.String(), "PHONE_NUMBER_STATUS_"))
404-
fmt.Printf(" SIP Dispatch Rule: %s\n", item.SipDispatchRuleId)
480+
481+
// Show dispatch rule ID if it was provided and successfully added, or if it's in the response
482+
displayRuleID := item.SipDispatchRuleId
483+
if dispatchRuleAdded && dispatchRuleID != "" {
484+
displayRuleID = dispatchRuleID
485+
}
486+
fmt.Printf(" SIP Dispatch Rule: %s\n", displayRuleID)
405487

406488
return nil
407489
}

0 commit comments

Comments
 (0)