Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2008 from hashicorp/deployListUsesUIAPI
Browse files Browse the repository at this point in the history
`deployment list` command uses new UI API
  • Loading branch information
izaaklauer authored Aug 11, 2021
2 parents 3385c3c + 69ff364 commit f75353c
Show file tree
Hide file tree
Showing 7 changed files with 3,756 additions and 3,439 deletions.
5 changes: 5 additions & 0 deletions ci/integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ cd ci/sinatra || exit 1

"$WP" release

# Smoke test list methods
"$WP" deployment list
"$WP" deployment list -V
"$WP" deployment list -json

## Let things get going.
sleep 10

Expand Down
199 changes: 114 additions & 85 deletions internal/cli/deployment_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"time"

"github.com/hashicorp/waypoint/internal/version"

"github.com/dustin/go-humanize"
"github.com/golang/protobuf/ptypes"
"github.com/posener/complete"
Expand Down Expand Up @@ -95,34 +97,38 @@ func (c *DeploymentListCommand) Run(args []string) int {
}

// List builds
resp, err := client.ListDeployments(c.Ctx, &pb.ListDeploymentsRequest{
resp, err := client.UI_ListDeployments(c.Ctx, &pb.UI_ListDeploymentsRequest{
Application: app.Ref(),
Workspace: wsRef,
PhysicalState: phyState,
Status: c.filterFlags.statusFilters(),
Order: c.filterFlags.orderOp(),
LoadDetails: pb.Deployment_BUILD,
})
if err != nil {
c.project.UI.Output(clierrors.Humanize(err), terminal.WithErrorStyle())
return ErrSentinel
}
sort.Sort(serversort.DeploymentCompleteDesc(resp.Deployments))
if s, ok := status.FromError(err); ok {
if s.Code() == codes.Unimplemented {
var serverVersion string
serverVersionResp := c.project.ServerVersion()
if serverVersionResp != nil {
serverVersion = serverVersionResp.Version
}

// get status reports
statusReportsResp, err := client.ListStatusReports(ctx, &pb.ListStatusReportsRequest{
Application: app.Ref(),
Workspace: wsRef,
})
var clientVersion string
clientVersionResp := version.GetVersion()
if clientVersionResp != nil {
clientVersion = clientVersionResp.Version
}

if status.Code(err) == codes.NotFound || status.Code(err) == codes.Unimplemented {
err = nil
statusReportsResp = nil
}
if err != nil {
app.UI.Output(clierrors.Humanize(err), terminal.WithErrorStyle())
c.project.UI.Output(
fmt.Sprintf("This CLI version %q is incompatible with the current server %q - missing UI_ListDeployments method. Upgrade your server to v0.5.0 or higher or downgrade your CLI to v0.4 or older.", clientVersion, serverVersion),
terminal.WithErrorStyle(),
)
return ErrSentinel
}
}
c.project.UI.Output(clierrors.Humanize(err), terminal.WithErrorStyle())
return ErrSentinel
}
sort.Sort(serversort.DeploymentBundleCompleteDesc(resp.Deployments))

if c.flagJson {
return c.displayJson(resp.Deployments)
Expand All @@ -140,7 +146,8 @@ func (c *DeploymentListCommand) Run(args []string) int {

const bullet = "●"

for _, b := range resp.Deployments {
for _, deployBundle := range resp.Deployments {
b := deployBundle.Deployment
// Determine our bullet
status := ""
statusColor := ""
Expand Down Expand Up @@ -181,26 +188,23 @@ func (c *DeploymentListCommand) Run(args []string) int {

// Add status report information if we have any
statusReportComplete := "n/a"
for _, statusReport := range statusReportsResp.StatusReports {
if deploymentTargetId, ok := statusReport.TargetId.(*pb.StatusReport_DeploymentId); ok {
if deploymentTargetId.DeploymentId == b.Id {
switch statusReport.Health.HealthStatus {
case "READY":
statusReportComplete = "✔"
case "ALIVE":
statusReportComplete = "✔"
case "DOWN":
statusReportComplete = "✖"
case "PARTIAL":
statusReportComplete = "●"
case "UNKNOWN":
statusReportComplete = "?"
}
if deployBundle.LatestStatusReport != nil {
statusReport := deployBundle.LatestStatusReport
switch statusReport.Health.HealthStatus {
case "READY":
statusReportComplete = "✔"
case "ALIVE":
statusReportComplete = "✔"
case "DOWN":
statusReportComplete = "✖"
case "PARTIAL":
statusReportComplete = "●"
case "UNKNOWN":
statusReportComplete = "?"
}

if t, err := ptypes.Timestamp(statusReport.GeneratedTime); err == nil {
statusReportComplete = fmt.Sprintf("%s - %s", statusReportComplete, humanize.Time(t))
}
}
if t, err := ptypes.Timestamp(statusReport.GeneratedTime); err == nil {
statusReportComplete = fmt.Sprintf("%s - %s", statusReportComplete, humanize.Time(t))
}
}

Expand All @@ -211,27 +215,33 @@ func (c *DeploymentListCommand) Run(args []string) int {

if user, ok := b.Labels["common/user"]; ok {
details = append(details, "user:"+user)
} else if user, ok := b.Preload.Build.Labels["common/user"]; ok {
details = append(details, "build-user:"+user)
}
} else if deployBundle.Build != nil {
build := deployBundle.Build
// labels have been set, safe to use them

if bp, ok := b.Preload.Build.Labels["common/languages"]; ok {
details = append(details, niceLanguages(bp))
}
if user, ok := build.Labels["common/user"]; ok {
details = append(details, "build-user:"+user)
}
if bp, ok := build.Labels["common/languages"]; ok {
details = append(details, niceLanguages(bp))
}

if img, ok := b.Preload.Build.Labels["common/image-id"]; ok {
img = shortImg(img)
if img, ok := build.Labels["common/image-id"]; ok {
img = shortImg(img)

details = append(details, "image:"+img)
details = append(details, "image:"+img)
}
}

artDetails := fmt.Sprintf("artifact:%s", c.flagId.FormatId(b.Preload.Artifact.Sequence, b.Preload.Artifact.Id))
if len(details) == 0 {
details = append(details, artDetails)
} else if c.flagVerbose {
details = append(details,
artDetails,
fmt.Sprintf("build:%s", c.flagId.FormatId(b.Preload.Build.Sequence, b.Preload.Build.Id)))
if deployBundle.Artifact != nil {
artDetails := fmt.Sprintf("artifact:%s", c.flagId.FormatId(deployBundle.Artifact.Sequence, deployBundle.Artifact.Id))
if len(details) == 0 {
details = append(details, artDetails)
} else if c.flagVerbose && deployBundle.Build != nil {
details = append(details,
artDetails,
fmt.Sprintf("build:%s", c.flagId.FormatId(deployBundle.Build.Sequence, deployBundle.Build.Id)))
}
}

if c.flagVerbose {
Expand All @@ -247,42 +257,50 @@ func (c *DeploymentListCommand) Run(args []string) int {
extraDetails = append(extraDetails, fmt.Sprintf("deployment.%s:%s", k, val))
}

for k, val := range b.Preload.Artifact.Labels {
if strings.HasPrefix(k, "waypoint/") {
continue
}
if deployBundle.Artifact != nil {
for k, val := range deployBundle.Artifact.Labels {
if strings.HasPrefix(k, "waypoint/") {
continue
}

if len(val) > 30 {
val = val[:30] + "..."
}
if len(val) > 30 {
val = val[:30] + "..."
}

extraDetails = append(extraDetails, fmt.Sprintf("artifact.%s:%s", k, val))
extraDetails = append(extraDetails, fmt.Sprintf("artifact.%s:%s", k, val))
}
}

for k, val := range b.Preload.Build.Labels {
if strings.HasPrefix(k, "waypoint/") {
continue
}
if deployBundle.Build != nil {
for k, val := range deployBundle.Build.Labels {
if strings.HasPrefix(k, "waypoint/") {
continue
}

if len(val) > 30 {
val = val[:30] + "..."
}
if len(val) > 30 {
val = val[:30] + "..."
}

extraDetails = append(extraDetails, fmt.Sprintf("build.%s:%s", k, val))
extraDetails = append(extraDetails, fmt.Sprintf("build.%s:%s", k, val))
}
}

sort.Strings(extraDetails)
}

sort.Strings(details)
var firstDetails string
if len(details) > 0 {
firstDetails = details[0]
}

var columns []string

columns = []string{
status,
c.flagId.FormatId(b.Sequence, b.Id),
b.Component.Name,
details[0],
firstDetails,
startTime,
completeTime,
statusReportComplete,
Expand All @@ -292,8 +310,8 @@ func (c *DeploymentListCommand) Run(args []string) int {
url := "n/a"
if b.Url != "" {
url = b.Url
} else if b.Preload.DeployUrl != "" {
url = b.Preload.DeployUrl
} else if deployBundle.DeployUrl != "" {
url = deployBundle.DeployUrl
}
columns = append(columns, url)
}
Expand All @@ -305,7 +323,7 @@ func (c *DeploymentListCommand) Run(args []string) int {
},
)

if len(details[1:]) > 0 {
if len(details) > 1 {
for _, dr := range details[1:] {
tbl.Rich([]string{"", "", "", dr}, nil)
}
Expand All @@ -329,22 +347,23 @@ func (c *DeploymentListCommand) Run(args []string) int {
return 0
}

func (c *DeploymentListCommand) displayJson(deployments []*pb.Deployment) error {
func (c *DeploymentListCommand) displayJson(deployments []*pb.UI_DeploymentBundle) error {
var output []map[string]interface{}

for _, dep := range deployments {
i := map[string]interface{}{}

i["id"] = dep.Id
i["sequence"] = dep.Sequence
i["application"] = dep.Application
i["labels"] = dep.Labels
i["component"] = dep.Component.Name
i["physical_state"] = dep.State.String()
i["status"] = c.statusJson(dep.Status)
i["workspace"] = dep.Workspace.Workspace
i["artifact"] = c.artifactJson(dep.Preload.Artifact)
i["build"] = c.buildJson(dep.Preload.Build)
i["id"] = dep.Deployment.Id
i["sequence"] = dep.Deployment.Sequence
i["application"] = dep.Deployment.Application
i["labels"] = dep.Deployment.Labels
i["component"] = dep.Deployment.Component.Name
i["physical_state"] = dep.Deployment.State.String()
i["status"] = c.statusJson(dep.Deployment.Status)
i["workspace"] = dep.Deployment.Workspace.Workspace
i["artifact"] = c.artifactJson(dep.Artifact)
i["build"] = c.buildJson(dep.Build)
i["latestStatusReport"] = dep.LatestStatusReport

output = append(output, i)
}
Expand All @@ -360,6 +379,10 @@ func (c *DeploymentListCommand) displayJson(deployments []*pb.Deployment) error
}

func (c *DeploymentListCommand) artifactJson(art *pb.PushedArtifact) interface{} {
if art == nil {
return nil
}

i := map[string]interface{}{}

i["id"] = art.Id
Expand All @@ -371,6 +394,9 @@ func (c *DeploymentListCommand) artifactJson(art *pb.PushedArtifact) interface{}
}

func (c *DeploymentListCommand) statusJson(status *pb.Status) interface{} {
if status == nil {
return nil
}
i := map[string]interface{}{}

i["state"] = status.State.String()
Expand All @@ -381,6 +407,9 @@ func (c *DeploymentListCommand) statusJson(status *pb.Status) interface{} {
}

func (c *DeploymentListCommand) buildJson(b *pb.Build) interface{} {
if b == nil {
return nil
}
i := map[string]interface{}{}

i["id"] = b.Id
Expand Down
Loading

0 comments on commit f75353c

Please sign in to comment.