-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_device_id.go
38 lines (32 loc) · 1.1 KB
/
get_device_id.go
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
package tailscale_gateway
import (
"context"
"github.com/awlsring/texit/internal/app/api/ports/gateway"
"github.com/awlsring/texit/internal/pkg/domain/tailnet"
"github.com/awlsring/texit/internal/pkg/logger"
"github.com/pkg/errors"
)
func (g *TailscaleGateway) GetDeviceId(ctx context.Context, tid tailnet.DeviceName) (tailnet.DeviceIdentifier, error) {
log := logger.FromContext(ctx)
log.Debug().Msgf("getting device id for device with name %s", tid.String())
log.Debug().Msg("listing devices")
devices, err := g.client.Devices(ctx)
if err != nil {
log.Error().Err(err).Msg("failed to list devices")
return "", err
}
log.Debug().Msg("searching for device")
for _, device := range devices {
if device.Hostname == tid.String() {
log.Debug().Msgf("device found. id: %s", device.ID)
deviceId, err := tailnet.DeviceIdentifierFromString(device.ID)
if err != nil {
log.Error().Err(err).Msg("failed to parse device id")
return "", err
}
return deviceId, nil
}
}
log.Error().Msgf("device %s not found", tid.String())
return "", errors.Wrap(gateway.ErrUnknownDevice, tid.String())
}