Skip to content

Commit

Permalink
Merge pull request #52 from rbino/pairing-unregister
Browse files Browse the repository at this point in the history
Add `pairing agent unregister` subcommand
  • Loading branch information
drf authored Dec 6, 2019
2 parents 07de008 + b507158 commit a1055c7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions client/pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
47 changes: 47 additions & 0 deletions cmd/pairing/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <device_id>",
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,
)
}

Expand All @@ -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
}

0 comments on commit a1055c7

Please sign in to comment.