2
0

delete.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. Copyright © 2021 NAME HERE <EMAIL ADDRESS>
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "fmt"
  16. "context"
  17. "github.com/gravitl/netmaker/wcagent/functions"
  18. nodepb "github.com/gravitl/netmaker/grpc"
  19. "github.com/spf13/cobra"
  20. "google.golang.org/grpc/metadata"
  21. "google.golang.org/grpc"
  22. )
  23. var deleteCmd = &cobra.Command{
  24. Use: "delete",
  25. Short: "Delete a Nodeby its MAC",
  26. Long: `Delete a node post by it's macaddress in mongodb.
  27. If no node is found with the MAC it will return a 'Not Found' error`,
  28. SilenceUsage: true,
  29. RunE: func(cmd *cobra.Command, args []string) error {
  30. macaddress, err := cmd.Flags().GetString("macaddress")
  31. if err != nil {
  32. return err
  33. }
  34. req := &nodepb.DeleteNodeReq{
  35. Macaddress: macaddress,
  36. }
  37. ctx := context.Background()
  38. ctx, err = functions.SetJWT(client)
  39. if err != nil {
  40. return err
  41. }
  42. var header metadata.MD
  43. _, err = client.DeleteNode(ctx, req, grpc.Header(&header))
  44. if err != nil {
  45. return err
  46. }
  47. fmt.Printf("Succesfully deleted the node with macaddress %s\n", macaddress)
  48. return nil
  49. },
  50. }
  51. func init() {
  52. deleteCmd.Flags().StringP("macaddress", "m", "", "The macaddress of the node")
  53. deleteCmd.MarkFlagRequired("macaddress")
  54. rootCmd.AddCommand(deleteCmd)
  55. }