Esempio n. 1
0
func init() {
	maker := func() client.Crudder { return &client.User{} }
	singularName := "user"
	tree := makeCommandTree(singularName, maker)
	tree.AddCommand(
		&cobra.Command{
			Use:   "password [id] to [password]",
			Short: "Set a users password to [password]",
			Run: func(c *cobra.Command, args []string) {
				if len(args) != 3 {
					log.Fatalf("%v requires 2 arguments", c.UseLine())
				}
				obj := &client.User{}
				if err := client.Fetch(obj, args[0]); err != nil {
					log.Fatalln("Unable to fetch user from the server", err)
				}
				token, err := obj.StartPasswordReset()
				if err != nil {
					log.Fatalln("Unable to get password reset token", err)
				}
				if err := obj.CompletePasswordReset(token, args[2]); err != nil {
					log.Fatalln("Unable to change user password", err)
				}
				fmt.Println(prettyJSON(obj))
			},
		},
		&cobra.Command{
			Use:   "import [json]",
			Short: "Import a user.  Will create a new user and set the password in one shot",
			Run: func(c *cobra.Command, args []string) {
				if len(args) != 1 {
					log.Fatalf("%v requires 1 argument", c.UseLine())
				}
				obj := &client.User{}
				if err := client.CreateJSON(obj, []byte(args[0])); err != nil {
					if _, err := obj.Id(); err != nil {
						log.Fatalln("User has no name", err)
					}
					if err := client.Read(obj); err != nil {
						log.Fatalln("Unable to create or fetch user", err)
					}
					if err := client.UpdateJSON(obj, []byte(args[0])); err != nil {
						log.Fatalln("Unable to update user", err)
					}
				}
				rest := make(map[string]interface{})
				if err := json.Unmarshal([]byte(args[0]), &rest); err != nil {
					log.Fatalln("Problem unmarshalling new user", err)
				}
				password, ok := rest["password"]
				if ok {
					realPassword, ok := password.(string)
					if !ok {
						log.Fatal("Password not a string!")
					}
					token, err := obj.StartPasswordReset()
					if err != nil {
						log.Fatalln("Unable to get password reset token", err)
					}
					if err := obj.CompletePasswordReset(token, realPassword); err != nil {
						log.Fatalln("Unable to change user password", err)
					}
				}
				fmt.Println(prettyJSON(obj))
			},
		},
	)
	app.AddCommand(tree)
}
Esempio n. 2
0
func init() {
	maker := func() client.Crudder { return &client.Network{} }
	network := makeCommandTree("network", maker)
	cmds := []*cobra.Command{
		&cobra.Command{
			Use:   "import [json]",
			Short: "Import a network + optional ranges and routers into Crowbar",
			Run: func(c *cobra.Command, args []string) {
				if len(args) != 1 {
					log.Fatalf("%v requires 1 argument\n", c.UseLine())
				}
				obj := &client.Network{}
				if err := client.Import(obj, []byte(args[0])); err != nil {
					log.Fatalf("Unable to import network: %v\n", err)
				}
				fmt.Println(prettyJSON(obj))
			},
		},
		&cobra.Command{
			Use:   "add [id] to [nodeId]",
			Short: "Add a Network to a Node.  This will implicitly allocate an address from the default host ranges in the network.",
			Run: func(c *cobra.Command, args []string) {
				if len(args) != 3 {
					log.Fatalf("%v requires 2 arguments\n", c.UseLine())
				}
				node := &client.Node{}
				network := &client.Network{}
				if err := client.Fetch(node, args[2]); err != nil {
					log.Fatalf("Unable to fetch node\n%v\n", err)
				}
				if err := client.Fetch(network, args[0]); err != nil {
					log.Fatalf("Unable to fetch network\n%v\n", err)
				}
				ranges, err := network.AutoRanges(node)
				if err != nil {
					log.Fatalln("Failed to get auto ranges for network", err)
				}
				res := make([]*client.NetworkAllocation, len(ranges))
				unwind := true
				defer func() {
					if unwind {
						for _, allocation := range res {
							if allocation != nil {
								client.Destroy(allocation)
							}
						}
					}
				}()

				for i, netRange := range ranges {
					alloc := &client.NetworkAllocation{}
					alloc.NodeID = null.IntFrom(node.ID)
					alloc.NetworkID = null.IntFrom(network.ID)
					alloc.NetworkRangeID = null.IntFrom(netRange.ID)

					if err := client.BaseCreate(alloc); err != nil {
						log.Fatalf("Unable to create new NetworkAllocation: %v", err)
					}
					res[i] = alloc
				}
				unwind = false
				fmt.Println(prettyJSON(res))
			},
		},
		&cobra.Command{
			Use:   "alloc [id] on [nodeId] from [rangeId] hint [address]",
			Short: "Add a Network to a Node with an address allocated from a specific Range.",
			Run: func(c *cobra.Command, args []string) {
				if len(args) != 5 && len(args) != 7 {
					log.Fatalf("%v requires 3 or 4 arguments", c.UseLine())
				}
				node := &client.Node{}
				if err := client.Fetch(node, args[0]); err != nil {
					log.Fatalln("Unable to fetch node from Crowbar\n%v\n", err)
				}
				netRange := &client.NetworkRange{}
				if err := client.SetId(netRange, args[4]); err != nil {
					vals := map[string]interface{}{}
					vals["name"] = args[4]
					network := &client.Network{}
					if err := client.Fetch(network, args[2]); err != nil {
						log.Fatalln("Unable to fetch network from Crowbar\n%v\n", err)
					}
					vals["network_id"] = network.ID
					netRanges := []*client.NetworkRange{}
					if err := client.Match(netRange.ApiName(), vals, &netRanges); err != nil {
						log.Fatalf("Unable to fetch ranges matching %#v\n%v\n", netRange, err)
					}
					if len(netRanges) != 1 {
						log.Fatalln("Supplied network range information is ambiguous")
					}
					netRange = netRanges[0]
				} else {
					if err := client.Read(netRange); err != nil {
						log.Fatalln("Unable to fetch network range\n%v\n", err)
					}
				}
				alloc := &client.NetworkAllocation{}
				alloc.NetworkID = null.IntFrom(netRange.NetworkID)
				alloc.NetworkRangeID = null.IntFrom(netRange.ID)
				alloc.NodeID = null.IntFrom(node.ID)
				if len(args) == 7 {
					alloc.Address = args[6]
				}
				if err := client.BaseCreate(alloc); err != nil {
					log.Fatalf("Unable to add node to network\n%v\n", err)
				}
				fmt.Println(prettyJSON(alloc))
			},
		},
	}
	network.AddCommand(cmds...)
	app.AddCommand(network)
	maker = func() client.Crudder { return &client.NetworkRange{} }
	networkrange := makeCommandTree("networkrange", maker)
	app.AddCommand(networkrange)
	maker = func() client.Crudder { return &client.NetworkAllocation{} }
	app.AddCommand(makeCommandTree("networkallocation", maker))
	maker = func() client.Crudder { return &client.NetworkRouter{} }
	app.AddCommand(makeCommandTree("networkrouter", maker))
}