@@ -703,20 +703,34 @@ func createExtClient(w http.ResponseWriter, r *http.Request) {
703
703
logic .ReturnErrorResponse (w , r , logic .FormatError (err , "internal" ))
704
704
return
705
705
}
706
- for _ , extclient := range extclients {
707
- // if device id is sent, then make sure extclient with the same device id
708
- // does not exist.
709
- if customExtClient .DeviceID != "" && extclient .DeviceID == customExtClient .DeviceID &&
710
- extclient .OwnerID == caller .UserName && nodeid == extclient .IngressGatewayID {
711
- err = errors .New ("remote client config already exists on the gateway" )
712
- slog .Error ("failed to create extclient" , "user" , userName , "error" , err )
713
- logic .ReturnErrorResponse (w , r , logic .FormatError (err , "badrequest" ))
714
- return
706
+
707
+ // if device id is sent, we don't want to create another extclient for the same user
708
+ // and gw, with the same device id.
709
+ if customExtClient .DeviceID != "" {
710
+ // let's first confirm that none of the user's extclients for this gw have device id.
711
+ for _ , extclient := range extclients {
712
+ if extclient .DeviceID == customExtClient .DeviceID &&
713
+ extclient .OwnerID == caller .UserName && nodeid == extclient .IngressGatewayID {
714
+ err = errors .New ("remote client config already exists on the gateway" )
715
+ slog .Error ("failed to create extclient" , "user" , userName , "error" , err )
716
+ logic .ReturnErrorResponse (w , r , logic .FormatError (err , "badrequest" ))
717
+ return
718
+ }
715
719
}
720
+ }
716
721
722
+ for _ , extclient := range extclients {
717
723
if extclient .RemoteAccessClientID != "" &&
718
- extclient .RemoteAccessClientID == customExtClient .RemoteAccessClientID && extclient .OwnerID == caller .UserName && nodeid == extclient .IngressGatewayID {
719
- // extclient on the gw already exists for the remote access client
724
+ extclient .RemoteAccessClientID == customExtClient .RemoteAccessClientID &&
725
+ extclient .OwnerID == caller .UserName && nodeid == extclient .IngressGatewayID {
726
+ if customExtClient .DeviceID != "" && extclient .DeviceID == "" {
727
+ // This extclient doesn’t include a device ID (and neither do the others).
728
+ // We patch it by assigning the device ID from the incoming request.
729
+ // When clients see that the config already exists, they will fetch
730
+ // the one with their device ID. And we will return this one.
731
+ extclient .DeviceID = customExtClient .DeviceID
732
+ _ = logic .SaveExtClient (& extclient )
733
+ }
720
734
err = errors .New ("remote client config already exists on the gateway" )
721
735
slog .Error ("failed to create extclient" , "user" , userName , "error" , err )
722
736
logic .ReturnErrorResponse (w , r , logic .FormatError (err , "badrequest" ))
0 commit comments