mirror of https://github.com/hashicorp/boundary
Add a hosts create command (#25)
parent
17fffe7c23
commit
e3c7ca3070
@ -1,29 +1,36 @@
|
||||
package base
|
||||
|
||||
const (
|
||||
// flagNameAddress is the flag used in the base command to read in the
|
||||
// FlagNameAddress is the flag used in the base command to read in the
|
||||
// address of the Watchtower server.
|
||||
FlagNameAddress = "address"
|
||||
// flagnameCACert is the flag used in the base command to read in the CA
|
||||
// FlagNameOrg is the flag used in the base command to read in the org in
|
||||
// which to make a request.
|
||||
FlagNameOrg = "org"
|
||||
// FlagNameProject is the flag used in the base command to read in the
|
||||
// project in which to make a request.
|
||||
FlagNameProject = "project"
|
||||
// FlagnameCACert is the flag used in the base command to read in the CA
|
||||
// cert.
|
||||
FlagNameCACert = "ca-cert"
|
||||
// flagnameCAPath is the flag used in the base command to read in the CA
|
||||
// FlagnameCAPath is the flag used in the base command to read in the CA
|
||||
// cert path.
|
||||
FlagNameCAPath = "ca-path"
|
||||
//flagNameClientCert is the flag used in the base command to read in the
|
||||
//client key
|
||||
// FlagNameClientCert is the flag used in the base command to read in the
|
||||
// client key
|
||||
FlagNameClientKey = "client-key"
|
||||
//flagNameClientCert is the flag used in the base command to read in the
|
||||
//client cert
|
||||
// FlagNameClientCert is the flag used in the base command to read in the
|
||||
// client cert
|
||||
FlagNameClientCert = "client-cert"
|
||||
// flagNameTLSInsecure is the flag used in the base command to read in
|
||||
// FlagNameTLSInsecure is the flag used in the base command to read in
|
||||
// the option to ignore TLS certificate verification.
|
||||
FlagNameTLSInsecure = "tls-insecure"
|
||||
// flagTLSServerName is the flag used in the base command to read in
|
||||
// FlagTLSServerName is the flag used in the base command to read in
|
||||
// the TLS server name.
|
||||
FlagTLSServerName = "tls-server-name"
|
||||
)
|
||||
|
||||
const EnvWatchtowerCLINoColor = `WATCHTOWER_CLI_NO_COLOR`
|
||||
const EnvWatchtowerCLIFormat = `WATCHTOWER_CLI_FORMAT`
|
||||
|
||||
const (
|
||||
EnvWatchtowerCLINoColor = `WATCHTOWER_CLI_NO_COLOR`
|
||||
EnvWatchtowerCLIFormat = `WATCHTOWER_CLI_FORMAT`
|
||||
)
|
||||
|
||||
@ -0,0 +1,131 @@
|
||||
package hosts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/watchtower/api"
|
||||
"github.com/hashicorp/watchtower/api/hosts"
|
||||
"github.com/hashicorp/watchtower/internal/cmd/base"
|
||||
"github.com/kr/pretty"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
)
|
||||
|
||||
var _ cli.Command = (*CreateCommand)(nil)
|
||||
var _ cli.CommandAutocomplete = (*CreateCommand)(nil)
|
||||
|
||||
type CreateCommand struct {
|
||||
*base.Command
|
||||
|
||||
flagHost string
|
||||
flagName string
|
||||
flagCatalog string
|
||||
}
|
||||
|
||||
func (c *CreateCommand) Synopsis() string {
|
||||
return "Creates a host in the given host catalog"
|
||||
}
|
||||
|
||||
func (c *CreateCommand) Help() string {
|
||||
helpText := `
|
||||
Usage: watchtower hosts create
|
||||
|
||||
Creates a host in the given host catalog. This command will result in an
|
||||
error for any catalog that does not support manual host creation.
|
||||
|
||||
Example:
|
||||
|
||||
$ watchtower hosts create -catalog=<id> -host=<addr> -name=<name>
|
||||
|
||||
` + c.Flags().Help()
|
||||
|
||||
return strings.TrimSpace(helpText)
|
||||
}
|
||||
|
||||
func (c *CreateCommand) Flags() *base.FlagSets {
|
||||
set := c.FlagSet(base.FlagSetHTTP | base.FlagSetOutputFormat)
|
||||
|
||||
f := set.NewFlagSet("Command Options")
|
||||
|
||||
f.StringVar(&base.StringVar{
|
||||
Name: "host",
|
||||
Target: &c.flagHost,
|
||||
Completion: complete.PredictAnything,
|
||||
Usage: "The host's address; can be an IP address or DNS name",
|
||||
})
|
||||
|
||||
f.StringVar(&base.StringVar{
|
||||
Name: "name",
|
||||
Target: &c.flagName,
|
||||
Completion: complete.PredictAnything,
|
||||
Usage: "A friendly name for the host for display purposes",
|
||||
})
|
||||
|
||||
f.StringVar(&base.StringVar{
|
||||
Name: "catalog",
|
||||
Target: &c.flagCatalog,
|
||||
Completion: complete.PredictAnything,
|
||||
Usage: "The ID of the host catalog in which the host should be created",
|
||||
})
|
||||
|
||||
return set
|
||||
}
|
||||
|
||||
func (c *CreateCommand) AutocompleteArgs() complete.Predictor {
|
||||
return complete.PredictAnything
|
||||
}
|
||||
|
||||
func (c *CreateCommand) AutocompleteFlags() complete.Flags {
|
||||
return c.Flags().Completions()
|
||||
}
|
||||
|
||||
func (c *CreateCommand) Run(args []string) int {
|
||||
f := c.Flags()
|
||||
|
||||
if err := f.Parse(args); err != nil {
|
||||
c.UI.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
|
||||
switch {
|
||||
case c.flagCatalog == "":
|
||||
c.UI.Error("Catalog ID must be provided via -catalog")
|
||||
return 1
|
||||
case c.flagHost == "":
|
||||
c.UI.Error("Host address must be provided via -host")
|
||||
return 1
|
||||
}
|
||||
|
||||
client, err := c.Client()
|
||||
if err != nil {
|
||||
c.UI.Error(err.Error())
|
||||
return 2
|
||||
}
|
||||
|
||||
catalog := &hosts.HostCatalog{
|
||||
Client: client,
|
||||
Id: api.String(c.flagCatalog),
|
||||
}
|
||||
|
||||
host := &hosts.Host{
|
||||
FriendlyName: api.StringOrNil(c.flagName),
|
||||
Address: api.String(c.flagHost),
|
||||
}
|
||||
|
||||
var apiErr *api.Error
|
||||
host, apiErr, err = catalog.CreateHost(c.Context, host)
|
||||
|
||||
switch {
|
||||
case err != nil:
|
||||
c.UI.Error(fmt.Errorf("error creating host: %w", err).Error())
|
||||
return 2
|
||||
case apiErr != nil:
|
||||
c.UI.Error(pretty.Sprint(apiErr))
|
||||
return 2
|
||||
default:
|
||||
c.UI.Info(pretty.Sprint(host))
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
Loading…
Reference in new issue