Skip to content

Commit 2f6c489

Browse files
committed
fix(server): display both public and private addresses in create output
1 parent b3fef37 commit 2f6c489

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Improved errors relating to argument resolver failures
1414
- Print version info, instead of missing credentials error, when runnning `upctl version` without credentials
1515
- Disable colors when outputting in JSON or YAML format
16+
- Display both public and private addresses in `server create` output
1617

1718
## [1.1.3] - 2022-02-24
1819
### Changed

internal/commands/server/create.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -371,17 +371,37 @@ func (s *createCommand) ExecuteWithoutArguments(exec commands.Executor) (output.
371371

372372
return output.MarshaledWithHumanDetails{Value: res, Details: []output.DetailRow{
373373
{Title: "UUID", Value: res.UUID, Colour: ui.DefaultUUUIDColours},
374-
{Title: "IP Addresses", Value: res.IPAddresses, Format: formatIPAddresses},
374+
{Title: "IP Addresses", Value: res, Format: formatIPAddresses},
375375
}}, nil
376376
}
377377

378378
func formatIPAddresses(val interface{}) (text.Colors, string, error) {
379-
if ipAddresses, ok := val.(upcloud.IPAddressSlice); ok {
380-
strs := make([]string, len(ipAddresses))
381-
for i, ipa := range ipAddresses {
382-
strs[i] = ipa.Address
379+
server, ok := val.(*upcloud.ServerDetails)
380+
if !ok {
381+
return nil, fmt.Sprint(val), nil
382+
}
383+
384+
// Store addresses in map keys to avoid duplicate addresses
385+
addresses := make(map[string]bool)
386+
387+
// Get public addressses from ip_addresses list
388+
// Public and utility interfaces created by default (no --network parameters) are only listed here
389+
for _, ipa := range server.IPAddresses {
390+
addresses[ipa.Address] = true
391+
}
392+
393+
// Get public and private addresses from networking.interfaces list
394+
// Public and utility interfaces created with --network are also listed here
395+
for _, iface := range server.Networking.Interfaces {
396+
for _, ipa := range iface.IPAddresses {
397+
addresses[ipa.Address] = true
383398
}
384-
return nil, strings.Join(strs, ",\n"), nil
385399
}
386-
return nil, fmt.Sprint(val), nil
400+
401+
var strs []string
402+
for ipa := range addresses {
403+
strs = append(strs, ipa)
404+
}
405+
406+
return nil, strings.Join(strs, ",\n"), nil
387407
}

0 commit comments

Comments
 (0)