* Renamed fakeRepo to mockRepo * Changing around TODOs to capture current plans with the code. * Remove the repo interface and use a real DB for tests. * Fixed a few tests. * Adding checks for properly formatted identifiers. * Updating code to account for recent merges. Relaxing some restrictions on what an id can look like. * Added small comment explaining why RegisterGrpcGatewayer interface exists. * Move project service up a call level. Long term I think it would make sense to define these services even higher up the call stack, potentially at the initialization of the controller server, near where the DB is initialized. * Making these fields align with those in the resources under hosts/... * Hard code the service initialization in the handler registration code. * Regenerate project related code proto code. * Removing type field for project. * Adds basic project handling in CLI/SDK (#47) * Adds SDK for creating and reading projects * Reorganizes a bit * Adds CLI for creating and reading projects * Fixes some some generation bugs * Adds redirect support for default org ID * Fix protobuf import references being moved. * Update client test to look for "orgs" and "projects" instead of "org" and "project" * Update expected segments test * Fix setAddr handling when default organization is in use * Set client in target after create or read Co-authored-by: Jim Lambert <jimlambrt@Jims-MBP-3.home> Co-authored-by: Jim <jlambert@hashicorp.com> Co-authored-by: Michael Gaffney <mgaffney@users.noreply.github.com> Co-authored-by: Jeff Mitchell <jeffrey.mitchell@gmail.com>
141 lines
3.1 KiB
Go
141 lines
3.1 KiB
Go
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
|
|
|
|
flagAddress string
|
|
flagName string
|
|
flagCatalogId string
|
|
flagDescription 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=<id> -address=<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: "address",
|
|
Target: &c.flagAddress,
|
|
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: "An optional name assigned to the host for display purposes",
|
|
})
|
|
|
|
f.StringVar(&base.StringVar{
|
|
Name: "catalog-id",
|
|
Target: &c.flagCatalogId,
|
|
Completion: complete.PredictAnything,
|
|
Usage: "The ID of the host catalog in which the host should be created",
|
|
})
|
|
|
|
f.StringVar(&base.StringVar{
|
|
Name: "description",
|
|
Target: &c.flagDescription,
|
|
Completion: complete.PredictNothing,
|
|
Usage: "An optional description assigned to the host for display purposes",
|
|
})
|
|
|
|
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.flagCatalogId == "":
|
|
c.UI.Error("Catalog ID must be provided via -catalog-id")
|
|
return 1
|
|
case c.flagAddress == "":
|
|
c.UI.Error("Host address must be provided via -address")
|
|
return 1
|
|
}
|
|
|
|
client, err := c.Client()
|
|
if err != nil {
|
|
c.UI.Error(err.Error())
|
|
return 2
|
|
}
|
|
|
|
catalog := &hosts.HostCatalog{
|
|
Client: client,
|
|
Id: c.flagCatalogId,
|
|
}
|
|
|
|
host := &hosts.Host{
|
|
Address: api.String(c.flagAddress),
|
|
Name: api.StringOrNil(c.flagName),
|
|
Description: api.StringOrNil(c.flagDescription),
|
|
}
|
|
|
|
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
|
|
}
|