From 75d0e18e4a2993de5551becd81886f1916778240 Mon Sep 17 00:00:00 2001 From: Denys Smirnov Date: Thu, 20 Mar 2025 18:37:33 +0200 Subject: [PATCH] Implement SIP update API. (#3141) --- go.mod | 2 +- go.sum | 4 +-- pkg/service/sip.go | 88 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 88 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 22bea598..5d56a4e9 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/jxskiss/base62 v1.1.0 github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1 github.com/livekit/mediatransportutil v0.0.0-20250310153736-45596af895b6 - github.com/livekit/protocol v1.35.1-0.20250319124948-ce72bd5bb7dc + github.com/livekit/protocol v1.35.1-0.20250320161708-6d044a0462b3 github.com/livekit/psrpc v0.6.1-0.20250205181828-a0beed2e4126 github.com/mackerelio/go-osstat v0.2.5 github.com/magefile/mage v1.15.0 diff --git a/go.sum b/go.sum index 2e834ff8..519bba7c 100644 --- a/go.sum +++ b/go.sum @@ -170,8 +170,8 @@ github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1 h1:jm09419p0lqTkD github.com/livekit/mageutil v0.0.0-20230125210925-54e8a70427c1/go.mod h1:Rs3MhFwutWhGwmY1VQsygw28z5bWcnEYmS1OG9OxjOQ= github.com/livekit/mediatransportutil v0.0.0-20250310153736-45596af895b6 h1:6ZhtnY9I9knfm3ieIPpznQSEU2rDECO8yliW/ANLQ7U= github.com/livekit/mediatransportutil v0.0.0-20250310153736-45596af895b6/go.mod h1:36s+wwmU3O40IAhE+MjBWP3W71QRiEE9SfooSBvtBqY= -github.com/livekit/protocol v1.35.1-0.20250319124948-ce72bd5bb7dc h1:RApnDTBKEI1rSMPtz39TC6fuh2Oh+7lmwCtEujgiZCc= -github.com/livekit/protocol v1.35.1-0.20250319124948-ce72bd5bb7dc/go.mod h1:WrT/CYRxtMNOVUjnIPm5OjWtEkmreffTeE1PRZwlRg4= +github.com/livekit/protocol v1.35.1-0.20250320161708-6d044a0462b3 h1:RsVnIuxnj3wRzWILhpnguyrq3vrK6Cccb762GpLH3Xg= +github.com/livekit/protocol v1.35.1-0.20250320161708-6d044a0462b3/go.mod h1:WrT/CYRxtMNOVUjnIPm5OjWtEkmreffTeE1PRZwlRg4= github.com/livekit/psrpc v0.6.1-0.20250205181828-a0beed2e4126 h1:fzuYpAQbCid7ySPpQWWePfQOWUrs8x6dJ0T3Wl07n+Y= github.com/livekit/psrpc v0.6.1-0.20250205181828-a0beed2e4126/go.mod h1:X5WtEZ7OnEs72Fi5/J+i0on3964F1aynQpCalcgMqRo= github.com/mackerelio/go-osstat v0.2.5 h1:+MqTbZUhoIt4m8qzkVoXUJg1EuifwlAJSk4Yl2GXh+o= diff --git a/pkg/service/sip.go b/pkg/service/sip.go index 6ca394db..a2d8f458 100644 --- a/pkg/service/sip.go +++ b/pkg/service/sip.go @@ -16,6 +16,7 @@ package service import ( "context" + "errors" "time" "github.com/dennwc/iters" @@ -188,7 +189,39 @@ func (s *SIPService) UpdateSIPInboundTrunk(ctx context.Context, req *livekit.Upd "trunkID", req.SipTrunkId, ) - return nil, twirp.NewError(twirp.Unimplemented, "not implemented") + // Validate all trunks including the new one first. + info, err := s.store.LoadSIPInboundTrunk(ctx, req.SipTrunkId) + if err != nil { + return nil, err + } + switch a := req.Action.(type) { + default: + return nil, errors.New("missing or unsupported action") + case livekit.UpdateSIPInboundTrunkRequestAction: + if err = a.Apply(info); err != nil { + return nil, err + } + } + + it, err := ListSIPInboundTrunk(ctx, s.store, &livekit.ListSIPInboundTrunkRequest{ + Numbers: info.Numbers, + }) + if err != nil { + return nil, err + } + defer it.Close() + if err = sip.ValidateTrunksIter(it, sip.WithTrunkReplace(func(t *livekit.SIPInboundTrunkInfo) *livekit.SIPInboundTrunkInfo { + if req.SipTrunkId == t.SipTrunkId { + return info // updated one + } + return t + })); err != nil { + return nil, err + } + if err := s.store.StoreSIPInboundTrunk(ctx, info); err != nil { + return nil, err + } + return info, nil } func (s *SIPService) UpdateSIPOutboundTrunk(ctx context.Context, req *livekit.UpdateSIPOutboundTrunkRequest) (*livekit.SIPOutboundTrunkInfo, error) { @@ -207,7 +240,23 @@ func (s *SIPService) UpdateSIPOutboundTrunk(ctx context.Context, req *livekit.Up "trunkID", req.SipTrunkId, ) - return nil, twirp.NewError(twirp.Unimplemented, "not implemented") + info, err := s.store.LoadSIPOutboundTrunk(ctx, req.SipTrunkId) + if err != nil { + return nil, err + } + switch a := req.Action.(type) { + default: + return nil, errors.New("missing or unsupported action") + case livekit.UpdateSIPOutboundTrunkRequestAction: + if err = a.Apply(info); err != nil { + return nil, err + } + } + // No additional validation needed for outbound. + if err := s.store.StoreSIPOutboundTrunk(ctx, info); err != nil { + return nil, err + } + return info, nil } func (s *SIPService) GetSIPInboundTrunk(ctx context.Context, req *livekit.GetSIPInboundTrunkRequest) (*livekit.GetSIPInboundTrunkResponse, error) { @@ -406,7 +455,40 @@ func (s *SIPService) UpdateSIPDispatchRule(ctx context.Context, req *livekit.Upd "ruleID", req.SipDispatchRuleId, ) - return nil, twirp.NewError(twirp.Unimplemented, "not implemented") + // Validate all trunks including the new one first. + info, err := s.store.LoadSIPDispatchRule(ctx, req.SipDispatchRuleId) + if err != nil { + return nil, err + } + switch a := req.Action.(type) { + default: + return nil, errors.New("missing or unsupported action") + case livekit.UpdateSIPDispatchRuleRequestAction: + if err = a.Apply(info); err != nil { + return nil, err + } + } + + it, err := ListSIPDispatchRule(ctx, s.store, &livekit.ListSIPDispatchRuleRequest{ + TrunkIds: info.TrunkIds, + }) + if err != nil { + return nil, err + } + defer it.Close() + if _, err = sip.ValidateDispatchRulesIter(it, sip.WithDispatchRuleReplace(func(t *livekit.SIPDispatchRuleInfo) *livekit.SIPDispatchRuleInfo { + if req.SipDispatchRuleId == t.SipDispatchRuleId { + return info // updated one + } + return t + })); err != nil { + return nil, err + } + + if err := s.store.StoreSIPDispatchRule(ctx, info); err != nil { + return nil, err + } + return info, nil } func ListSIPDispatchRule(ctx context.Context, s SIPStore, req *livekit.ListSIPDispatchRuleRequest, add ...*livekit.SIPDispatchRuleInfo) (iters.Iter[*livekit.SIPDispatchRuleInfo], error) {