func (h *handlerManager) DeleteApplication(ctx context.Context, in *pb.ApplicationIdentifier) (*empty.Empty, error) { if err := in.Validate(); err != nil { return nil, errors.Wrap(err, "Invalid Application Identifier") } ctx, claims, err := h.validateTTNAuthAppContext(ctx, in.AppId) if err != nil { return nil, err } if !claims.AppRight(in.AppId, rights.AppSettings) { return nil, errors.NewErrPermissionDenied(`No "settings" rights to application`) } _, err = h.handler.applications.Get(in.AppId) if err != nil { return nil, err } // Get and delete all devices for this application devices, err := h.handler.devices.ListForApp(in.AppId) if err != nil { return nil, err } for _, dev := range devices { _, err = h.deviceManager.DeleteDevice(ctx, &pb_lorawan.DeviceIdentifier{AppEui: &dev.AppEUI, DevEui: &dev.DevEUI}) if err != nil { return nil, errors.Wrap(errors.FromGRPCError(err), "Broker did not delete device") } err = h.handler.devices.Delete(dev.AppID, dev.DevID) if err != nil { return nil, err } } // Delete the Application err = h.handler.applications.Delete(in.AppId) if err != nil { return nil, err } token, _ := api.TokenFromContext(ctx) err = h.handler.Discovery.RemoveAppID(in.AppId, token) if err != nil { h.handler.Ctx.WithField("AppID", in.AppId).WithError(errors.FromGRPCError(err)).Warn("Could not unregister Application from Discovery") } return &empty.Empty{}, nil }
func (h *handlerManager) RegisterApplication(ctx context.Context, in *pb.ApplicationIdentifier) (*empty.Empty, error) { if err := in.Validate(); err != nil { return nil, errors.Wrap(err, "Invalid Application Identifier") } ctx, claims, err := h.validateTTNAuthAppContext(ctx, in.AppId) if err != nil { return nil, err } if !claims.AppRight(in.AppId, rights.AppSettings) { return nil, errors.NewErrPermissionDenied(`No "settings" rights to application`) } app, err := h.handler.applications.Get(in.AppId) if err != nil && errors.GetErrType(err) != errors.NotFound { return nil, err } if app != nil { return nil, errors.NewErrAlreadyExists("Application") } err = h.handler.applications.Set(&application.Application{ AppID: in.AppId, }) if err != nil { return nil, err } token, _ := api.TokenFromContext(ctx) err = h.handler.Discovery.AddAppID(in.AppId, token) if err != nil { h.handler.Ctx.WithField("AppID", in.AppId).WithError(err).Warn("Could not register Application with Discovery") } _, err = h.handler.ttnBrokerManager.RegisterApplicationHandler(ctx, &pb_broker.ApplicationHandlerRegistration{ AppId: in.AppId, HandlerId: h.handler.Identity.Id, }) if err != nil { h.handler.Ctx.WithField("AppID", in.AppId).WithError(err).Warn("Could not register Application with Broker") } return &empty.Empty{}, nil }
func (h *handlerManager) GetDevicesForApplication(ctx context.Context, in *pb.ApplicationIdentifier) (*pb.DeviceList, error) { if err := in.Validate(); err != nil { return nil, errors.Wrap(err, "Invalid Application Identifier") } ctx, claims, err := h.validateTTNAuthAppContext(ctx, in.AppId) if err != nil { return nil, err } if !claims.AppRight(in.AppId, rights.Devices) { return nil, errors.NewErrPermissionDenied(fmt.Sprintf(`No "devices" rights to application "%s"`, in.AppId)) } if _, err := h.handler.applications.Get(in.AppId); err != nil { return nil, errors.Wrap(err, "Application not registered to this Handler") } devices, err := h.handler.devices.ListForApp(in.AppId) if err != nil { return nil, err } res := &pb.DeviceList{Devices: []*pb.Device{}} for _, dev := range devices { res.Devices = append(res.Devices, &pb.Device{ AppId: dev.AppID, DevId: dev.DevID, Device: &pb.Device_LorawanDevice{LorawanDevice: &pb_lorawan.Device{ AppId: dev.AppID, AppEui: &dev.AppEUI, DevId: dev.DevID, DevEui: &dev.DevEUI, DevAddr: &dev.DevAddr, NwkSKey: &dev.NwkSKey, AppSKey: &dev.AppSKey, AppKey: &dev.AppKey, }}, }) } return res, nil }
func (h *handlerManager) GetApplication(ctx context.Context, in *pb.ApplicationIdentifier) (*pb.Application, error) { if err := in.Validate(); err != nil { return nil, errors.NewErrInvalidArgument("Application Identifier", err.Error()) } ctx, claims, err := h.validateTTNAuthAppContext(ctx, in.AppId) if err != nil { return nil, err } if !claims.AppRight(in.AppId, rights.AppSettings) { return nil, errors.NewErrPermissionDenied(`No "settings" rights to application`) } app, err := h.handler.applications.Get(in.AppId) if err != nil { return nil, err } return &pb.Application{ AppId: app.AppID, Decoder: app.Decoder, Converter: app.Converter, Validator: app.Validator, Encoder: app.Encoder, }, nil }