MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / registerGateway

Method registerGateway

pkg/basicstation/cups/update_info.go:64–133  ·  view source on GitHub ↗

registerGateway creates a new gateway for the default owner. It also creates the necessary CUPS and LNS credentials. `TargetCUPSURI` is set in order to make the gateway connect once again to this CUPS but using auth and then receive the LNS credentials.

(ctx context.Context, req UpdateInfoRequest)

Source from the content-addressed store, hash-verified

62// registerGateway creates a new gateway for the default owner. It also creates the necessary CUPS and LNS credentials.
63// `TargetCUPSURI` is set in order to make the gateway connect once again to this CUPS but using auth and then receive the LNS credentials.
64func (s *Server) registerGateway(ctx context.Context, req UpdateInfoRequest) (*ttnpb.Gateway, error) {
65 logger := log.FromContext(ctx)
66 ids := &ttnpb.GatewayIdentifiers{
67 GatewayId: fmt.Sprintf("eui-%s", strings.ToLower(req.Router.EUI64.String())),
68 Eui: req.Router.EUI64.Bytes(),
69 }
70 logger = logger.WithField("gateway_uid", unique.ID(ctx, ids))
71 registry, err := s.getRegistry(ctx, ids)
72 if err != nil {
73 return nil, err
74 }
75 auth := s.defaultOwnerAuth(ctx)
76 gtw, err := registry.Create(ctx, &ttnpb.CreateGatewayRequest{
77 Gateway: &ttnpb.Gateway{
78 Ids: ids,
79 GatewayServerAddress: s.defaultLNSURI,
80 },
81 Collaborator: s.defaultOwner,
82 }, auth)
83 if err != nil {
84 return nil, err
85 }
86 logger.Info("Created new gateway")
87 accessRegistry, err := s.getAccess(ctx, gtw.GetIds())
88 if err != nil {
89 return nil, err
90 }
91 cupsKey, err := accessRegistry.CreateAPIKey(ctx, &ttnpb.CreateGatewayAPIKeyRequest{
92 GatewayIds: gtw.GetIds(),
93 Name: fmt.Sprintf("CUPS Key, generated %s", time.Now().UTC().Format(time.RFC3339)),
94 Rights: []ttnpb.Right{
95 ttnpb.Right_RIGHT_GATEWAY_INFO,
96 ttnpb.Right_RIGHT_GATEWAY_SETTINGS_BASIC,
97 ttnpb.Right_RIGHT_GATEWAY_READ_SECRETS,
98 },
99 }, auth)
100 if err != nil {
101 return nil, err
102 }
103 logger.WithField("api_key_id", cupsKey.Id).Info("Created gateway API key for CUPS")
104 lnsKey, err := accessRegistry.CreateAPIKey(ctx, &ttnpb.CreateGatewayAPIKeyRequest{
105 GatewayIds: gtw.GetIds(),
106 Name: fmt.Sprintf("LNS Key, generated %s", time.Now().UTC().Format(time.RFC3339)),
107 Rights: []ttnpb.Right{
108 ttnpb.Right_RIGHT_GATEWAY_INFO,
109 ttnpb.Right_RIGHT_GATEWAY_LINK,
110 },
111 }, auth)
112 if err != nil {
113 return nil, err
114 }
115 _, err = registry.Update(ctx, &ttnpb.UpdateGatewayRequest{
116 Gateway: &ttnpb.Gateway{
117 Ids: ids,
118 LbsLnsSecret: &ttnpb.Secret{
119 Value: []byte(lnsKey.Key),
120 },
121 TargetCupsUri: req.CUPSURI,

Callers 1

updateInfoMethod · 0.95

Calls 15

getRegistryMethod · 0.95
getAccessMethod · 0.95
FromContextFunction · 0.92
IDFunction · 0.92
FieldMaskFunction · 0.92
StringMethod · 0.65
BytesMethod · 0.65
WithFieldMethod · 0.65
CreateMethod · 0.65
InfoMethod · 0.65
CreateAPIKeyMethod · 0.65
FormatMethod · 0.65

Tested by

no test coverage detected