Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional fields to json output when listing workers #287

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions pkg/cmd/worker/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/cmd/model"
"github.com/OctopusDeploy/cli/pkg/cmd/worker/shared"
ssh "github.com/OctopusDeploy/cli/pkg/cmd/worker/ssh/view"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/machinescommon"
Expand Down Expand Up @@ -55,6 +56,11 @@ func ListRun(opts *ListOptions) error {
HealthStatus string `json:"HealthStatus"`
StatusSummary string `json:"StatusSummary"`
WorkerPools []model.Entity `json:"WorkerPools"`
URI string `json:"URI"`
Version string `json:"Version,omitempty"`
Runtime string `json:"Runtime,omitempty"`
Account *model.Entity `json:"Account,omitempty"`
Proxy string `json:"Proxy"`
}

workerPoolMap, err := GetWorkerPoolMap(opts)
Expand All @@ -72,6 +78,11 @@ func ListRun(opts *ListOptions) error {
HealthStatus: item.HealthStatus,
StatusSummary: item.StatusSummary,
WorkerPools: resolveEntities(item.WorkerPoolIDs, workerPoolMap),
URI: getEndpointUri(item.Endpoint),
Version: getVersion(item.Endpoint),
Runtime: getRuntimeArchitecture(item.Endpoint),
Account: getAccount(opts, item.Endpoint),
Proxy: getProxy(opts, item.Endpoint),
}
},
Table: output.TableDefinition[*machines.Worker]{
Expand Down Expand Up @@ -104,6 +115,83 @@ func resolveEntities(keys []string, lookup map[string]string) []model.Entity {
return entities
}

func getEndpointUri(end machines.IEndpoint) string {
endpointUri := ""
switch end.GetCommunicationStyle() {
case "TentaclePassive":
endpoint := end.(*machines.ListeningTentacleEndpoint)
endpointUri = endpoint.URI.String()
case "TentacleActive":
endpoint := end.(*machines.PollingTentacleEndpoint)
endpointUri = endpoint.URI.String()
case "Ssh":
endpoint := end.(*machines.SSHEndpoint)
endpointUri = endpoint.URI.String()
}
return endpointUri
}

func getVersion(end machines.IEndpoint) string {
tentacleVersion := ""
switch end.GetCommunicationStyle() {
case "TentaclePassive":
endpoint := end.(*machines.ListeningTentacleEndpoint)
tentacleVersion = endpoint.TentacleVersionDetails.Version
case "TentacleActive":
endpoint := end.(*machines.PollingTentacleEndpoint)
tentacleVersion = endpoint.TentacleVersionDetails.Version
}
return tentacleVersion
}

func getRuntimeArchitecture(end machines.IEndpoint) string {
switch end.GetCommunicationStyle() {
case "Ssh":
endpoint := end.(*machines.SSHEndpoint)
return ssh.GetRuntimeArchitecture(endpoint)
}
return ""
}

func getAccount(opts *ListOptions, end machines.IEndpoint) *model.Entity {
accountId := ""
switch end.GetCommunicationStyle() {
case "Ssh":
endpoint := end.(*machines.SSHEndpoint)
accountId = endpoint.AccountID
}
if accountId != "" {
account, err := opts.Client.Accounts.GetByID(accountId)
if err != nil {
return nil
}
entity := &model.Entity{Id: account.GetID(), Name: account.GetName()}
return entity
}
return nil
}

func getProxy(opts *ListOptions, end machines.IEndpoint) string {
proxyId := ""
switch end.GetCommunicationStyle() {
case "TentaclePassive":
endpoint := end.(*machines.ListeningTentacleEndpoint)
proxyId = endpoint.ProxyID
case "Ssh":
endpoint := end.(*machines.SSHEndpoint)
proxyId = endpoint.ProxyID
}

if proxyId != "" {
proxy, err := opts.Client.Proxies.GetById(proxyId)
if err != nil {
return "None"
}
return proxy.GetName()
}
return "None"
}

func GetWorkerPoolMap(opts *ListOptions) (map[string]string, error) {
workerPoolMap := make(map[string]string)
allEnvs, err := opts.Client.WorkerPools.GetAll()
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/worker/ssh/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func contributeEndpoint(opts *shared.ViewOptions, workerEndpoint machines.IEndpo
endpoint := workerEndpoint.(*machines.SSHEndpoint)

data = append(data, output.NewDataRow("URI", endpoint.URI.String()))
data = append(data, output.NewDataRow("Runtime architecture", getRuntimeArchitecture(endpoint)))
data = append(data, output.NewDataRow("Runtime architecture", GetRuntimeArchitecture(endpoint)))
accountRows, err := shared.ContributeAccount(opts, endpoint.AccountID)
if err != nil {
return nil, err
Expand All @@ -60,7 +60,7 @@ func contributeEndpoint(opts *shared.ViewOptions, workerEndpoint machines.IEndpo
return data, nil
}

func getRuntimeArchitecture(endpoint *machines.SSHEndpoint) string {
func GetRuntimeArchitecture(endpoint *machines.SSHEndpoint) string {
if endpoint.DotNetCorePlatform == "" {
return "Mono"
}
Expand Down