Files
threshold/internal/cmd/commands/scopes/project_read.go
T
c0c6a9b0b8 Handler project (#44)
* 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>
2020-05-12 17:35:48 -07:00

106 lines
2.1 KiB
Go

package scopes
import (
"fmt"
"strings"
"github.com/hashicorp/watchtower/api"
"github.com/hashicorp/watchtower/api/scopes"
"github.com/hashicorp/watchtower/internal/cmd/base"
"github.com/kr/pretty"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
var _ cli.Command = (*ReadProjectCommand)(nil)
var _ cli.CommandAutocomplete = (*ReadProjectCommand)(nil)
type ReadProjectCommand struct {
*base.Command
flagId string
}
func (c *ReadProjectCommand) Synopsis() string {
return "Reads a project's data"
}
func (c *ReadProjectCommand) Help() string {
helpText := `
Usage: watchtower projects read
Returns information about a project specified by the ID. It is an error if
the project is not within the organization specified via the "org-id"
parameter or the associated environment variable.
Example:
$ watchtower projects read -org=<org_id> -id=<project_id>
` + c.Flags().Help()
return strings.TrimSpace(helpText)
}
func (c *ReadProjectCommand) Flags() *base.FlagSets {
set := c.FlagSet(base.FlagSetHTTP | base.FlagSetOutputFormat)
f := set.NewFlagSet("Command Options")
f.StringVar(&base.StringVar{
Name: "id",
Target: &c.flagId,
Completion: complete.PredictNothing,
Usage: "The ID of the project to read",
})
return set
}
func (c *ReadProjectCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictAnything
}
func (c *ReadProjectCommand) AutocompleteFlags() complete.Flags {
return c.Flags().Completions()
}
func (c *ReadProjectCommand) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
return 1
}
client, err := c.Client()
if err != nil {
c.UI.Error(err.Error())
return 2
}
org := &scopes.Organization{
Client: client,
}
project := &scopes.Project{
Id: c.flagId,
}
var apiErr *api.Error
project, apiErr, err = org.ReadProject(c.Context, project)
switch {
case err != nil:
c.UI.Error(fmt.Errorf("error reading project: %w", err).Error())
return 2
case apiErr != nil:
c.UI.Error(pretty.Sprint(apiErr))
return 2
default:
c.UI.Info(pretty.Sprint(project))
}
return 0
}