Skip to content

Commit

Permalink
feat: destinations list status/last seen
Browse files Browse the repository at this point in the history
  • Loading branch information
mxyng committed Jul 6, 2022
1 parent b432fe0 commit 6b6cfaa
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
3 changes: 2 additions & 1 deletion api/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type Destination struct {
Resources []string `json:"resources"`
Roles []string `json:"roles"`

LastSeen Time `json:"lastSeen"`
LastSeen Time `json:"lastSeen"`
Connected bool `json:"connected"`
}

type DestinationConnection struct {
Expand Down
22 changes: 18 additions & 4 deletions internal/cmd/destinations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"github.com/infrahq/infra/internal/logging"
)

const (
DestinationStatusConnected = "Connected"
DestinationStatusDisconnected = "Disconnected"
)

func newDestinationsCmd(cli *CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "destinations",
Expand Down Expand Up @@ -58,15 +63,24 @@ func newDestinationsListCmd(cli *CLI) *cobra.Command {
cli.Output(string(jsonOutput))
default:
type row struct {
Name string `header:"NAME"`
URL string `header:"URL"`
Name string `header:"NAME"`
URL string `header:"URL"`
Status string `header:"STATUS"`
LastSeen string `header:"LAST SEEN"`
}

var rows []row
for _, d := range destinations.Items {
status := DestinationStatusDisconnected
if d.Connected {
status = DestinationStatusConnected
}

rows = append(rows, row{
Name: d.Name,
URL: d.Connection.URL,
Name: d.Name,
URL: d.Connection.URL,
Status: status,
LastSeen: HumanTime(d.LastSeen.Time(), "never"),
})
}
if len(rows) > 0 {
Expand Down
1 change: 1 addition & 0 deletions internal/server/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,7 @@ func TestAPI_CreateDestination(t *testing.T) {
"url": "cluster.production.example",
"ca": "-----BEGIN CERTIFICATE-----\nok\n-----END CERTIFICATE-----\n"
},
"connected": false,
"lastSeen": null,
"resources": ["res1", "res2"],
"roles": ["role1", "role2"],
Expand Down
8 changes: 8 additions & 0 deletions internal/server/models/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ type Destination struct {
}

func (d *Destination) ToAPI() *api.Destination {
connected := false
// TODO: this should be configurable
// https://github.com/infrahq/infra/issues/2505
if time.Since(d.LastSeenAt) < 5*time.Minute {
connected = true
}

return &api.Destination{
ID: d.ID,
Created: api.Time(d.CreatedAt),
Expand All @@ -34,5 +41,6 @@ func (d *Destination) ToAPI() *api.Destination {
Resources: d.Resources,
Roles: d.Roles,
LastSeen: api.Time(d.LastSeenAt),
Connected: connected,
}
}
6 changes: 6 additions & 0 deletions internal/server/testdata/openapi3.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
},
"Destination": {
"properties": {
"connected": {
"type": "boolean"
},
"connection": {
"properties": {
"ca": {
Expand Down Expand Up @@ -332,6 +335,9 @@
"items": {
"items": {
"properties": {
"connected": {
"type": "boolean"
},
"connection": {
"properties": {
"ca": {
Expand Down

0 comments on commit 6b6cfaa

Please sign in to comment.