update.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. nodepb "github.com/gravitl/netmaker/grpc"
  18. "github.com/spf13/cobra"
  19. "github.com/gravitl/netmaker/wcagent/functions"
  20. "google.golang.org/grpc/metadata"
  21. "google.golang.org/grpc"
  22. )
  23. var updateCmd = &cobra.Command{
  24. Use: "update",
  25. Short: "Find a Node by its Macaddress",
  26. Long: `Find a node by it's mongoDB Unique macaddressentifier.
  27. If no node is found for the Macaddress it will return a 'Not Found' error`,
  28. SilenceUsage: true,
  29. RunE: func(cmd *cobra.Command, args []string) error {
  30. // Get the flags from CLI
  31. nodegroup, err := cmd.Flags().GetString("nodegroup")
  32. password, err := cmd.Flags().GetString("password")
  33. macaddress, err := cmd.Flags().GetString("macaddress")
  34. name, err := cmd.Flags().GetString("name")
  35. listenport, err := cmd.Flags().GetInt32("listenport")
  36. publickey, err := cmd.Flags().GetString("publickey")
  37. endpoint, err := cmd.Flags().GetString("endpoint")
  38. // Create an UpdateNodeRequest
  39. node := &nodepb.Node{
  40. Password: password,
  41. Macaddress: macaddress,
  42. Nodegroup: nodegroup,
  43. Listenport: listenport,
  44. Publickey: publickey,
  45. Name: name,
  46. Endpoint: endpoint,
  47. }
  48. req := &nodepb.UpdateNodeReq{
  49. Node: node,
  50. }
  51. ctx := context.Background()
  52. ctx, err = functions.SetJWT(client)
  53. if err != nil {
  54. return err
  55. }
  56. var header metadata.MD
  57. res, err := client.UpdateNode(ctx, req, grpc.Header(&header))
  58. if err != nil {
  59. return err
  60. }
  61. fmt.Println(res)
  62. return nil
  63. },
  64. }
  65. func init() {
  66. updateCmd.Flags().StringP("name", "n", "", "The node name")
  67. updateCmd.Flags().StringP("listenport", "l", "", "The wireguard port")
  68. updateCmd.Flags().StringP("endpoint", "e", "", "The public IP")
  69. updateCmd.Flags().StringP("macaddress", "m", "", "The local macaddress")
  70. updateCmd.Flags().StringP("password", "p", "", "The password")
  71. updateCmd.Flags().StringP("nodegroup", "g", "", "The group this will be added to")
  72. updateCmd.Flags().StringP("publickey", "k", "", "The wireguard public key")
  73. updateCmd.MarkFlagRequired("nodegroup")
  74. updateCmd.MarkFlagRequired("password")
  75. updateCmd.MarkFlagRequired("macaddress")
  76. rootCmd.AddCommand(updateCmd)
  77. }