From 82d0d1e3f33e72a23d6b783c6c4bd71c949bb990 Mon Sep 17 00:00:00 2001 From: Riccardo Binetti Date: Fri, 6 Dec 2019 11:23:18 +0100 Subject: [PATCH 1/3] client/pairing: add UnregistedDevice function Signed-off-by: Riccardo Binetti --- client/pairing.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/client/pairing.go b/client/pairing.go index 51b93e2..e9221cc 100644 --- a/client/pairing.go +++ b/client/pairing.go @@ -54,3 +54,17 @@ func (s *PairingService) RegisterDevice(realm string, deviceID string, token str return responseBody.Data.CredentialsSecret, nil } + +// UnregisterDevice resets the registration state of a device. This makes it possible to register it again. +// All data belonging to the device will be left as is in Astarte. +func (s *PairingService) UnregisterDevice(realm string, deviceID string, token string) error { + callURL, _ := url.Parse(s.pairingURL.String()) + callURL.Path = path.Join(callURL.Path, fmt.Sprintf("/v1/%s/agent/devices/%s", realm, deviceID)) + + err := s.client.genericJSONDataAPIDelete(callURL.String(), token, 204) + if err != nil { + return err + } + + return nil +} From 878d8e31006c8b02f17785137a7ad45216e57392 Mon Sep 17 00:00:00 2001 From: Riccardo Binetti Date: Fri, 6 Dec 2019 11:24:50 +0100 Subject: [PATCH 2/3] agent: add unregister subcommand Allow unregistering a device Signed-off-by: Riccardo Binetti --- cmd/pairing/agent.go | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/cmd/pairing/agent.go b/cmd/pairing/agent.go index ca4db15..7f0e3ed 100644 --- a/cmd/pairing/agent.go +++ b/cmd/pairing/agent.go @@ -40,11 +40,25 @@ This returns the credentials_secret that can be use to obtain device credentials RunE: agentRegisterF, } +var agentUnregisterCmd = &cobra.Command{ + Use: "unregister ", + Short: "Unregister a device", + Long: `Unregister a device, making it possible to register it again even after it has requested its credentials. + +All data belonging to the device will be kept as is in Astarte.`, + Example: ` astartectl pairing agent unregister 2TBn-jNESuuHamE2Zo1anA`, + Args: cobra.ExactArgs(1), + RunE: agentUnregisterF, +} + func init() { + agentUnregisterCmd.PersistentFlags().BoolP("non-interactive", "y", false, "Non-interactive mode. Will answer yes by default to all questions.") + PairingCmd.AddCommand(agentCmd) agentCmd.AddCommand( agentRegisterCmd, + agentUnregisterCmd, ) } @@ -70,3 +84,36 @@ func agentRegisterF(command *cobra.Command, args []string) error { fmt.Printf("will be associated permanently to the Device and it won't be changeable anymore.\n") return nil } + +func agentUnregisterF(command *cobra.Command, args []string) error { + deviceID := args[0] + if !utils.IsValidAstarteDeviceID(deviceID) { + return errors.New("Invalid device id") + } + + nonInteractive, err := command.Flags().GetBool("non-interactive") + if err != nil { + return err + } + + fmt.Printf("Will unregister device %s from realm %s.\n", deviceID, realm) + if !nonInteractive { + confirmation, err := utils.AskForConfirmation("Do you want to continue?") + if err != nil { + fmt.Println(err) + os.Exit(1) + } + if !confirmation { + return nil + } + } + + err = astarteAPIClient.Pairing.UnregisterDevice(realm, deviceID, pairingJwt) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + fmt.Println("ok") + return nil +} From b50715875bc31abc0ebd125438226ac2e220cf97 Mon Sep 17 00:00:00 2001 From: Riccardo Binetti Date: Fri, 6 Dec 2019 11:27:49 +0100 Subject: [PATCH 3/3] Update CHANGELOG Signed-off-by: Riccardo Binetti --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d66c90c..a384b7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [0.10.4] - Unreleased ### Added - Added the new cluster command, to manage remote, Kubernetes-based, clusters +- pairing: add unregister subcommand, allowing to register again a device that already requested its + credentials ### Fixed - Avoid flaky parsing when "value" is a path token (#48)