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

feat(server): json output for create subcommand #521

Closed
wants to merge 1 commit into from
Closed
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
71 changes: 53 additions & 18 deletions internal/cmd/server/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/cmpl"
"github.com/hetznercloud/cli/internal/cmd/output"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
Expand Down Expand Up @@ -77,6 +79,9 @@ var CreateCmd = base.Cmd{

cmd.Flags().Bool("without-ipv4", false, "Creates the server without an IPv4 (default: false)")
cmd.Flags().Bool("without-ipv6", false, "Creates the server without an IPv6 (default: false)")

output.AddFlag(cmd, output.OptionJSON())

return cmd
},

Expand All @@ -102,31 +107,61 @@ var CreateCmd = base.Cmd{
if err != nil {
return err
}
fmt.Printf("Server %d created\n", result.Server.ID)
if !server.PublicNet.IPv4.IsUnspecified() {
fmt.Printf("IPv4: %s\n", server.PublicNet.IPv4.IP.String())
}
if !server.PublicNet.IPv6.IsUnspecified() {
fmt.Printf("IPv6: %s1\n", server.PublicNet.IPv6.Network.IP.String())
fmt.Printf("IPv6 Network: %s\n", server.PublicNet.IPv6.Network.String())
}
if len(server.PrivateNet) > 0 {
var networks []string
for _, network := range server.PrivateNet {
networks = append(networks, fmt.Sprintf("- %s (%s)", network.IP.String(), client.Network().Name(network.Network.ID)))

outputFlags := output.FlagsForCommand(cmd)
switch {
case outputFlags.IsSet("json"):
err := createPrintJSON(result, server)
if err != nil {
return err
}
fmt.Printf("Private Networks:\n\t%s\n", strings.Join(networks, "\n"))
}
// Only print the root password if it's not empty,
// which is only the case if it wasn't created with an SSH key.
if result.RootPassword != "" {
fmt.Printf("Root password: %s\n", result.RootPassword)

default:
createPrintText(client, result, server)
}

return nil
},
}

func createPrintText(client hcapi2.Client, result hcloud.ServerCreateResult, server *hcloud.Server) {
fmt.Printf("Server %d created\n", server.ID)
if !server.PublicNet.IPv4.IsUnspecified() {
fmt.Printf("IPv4: %s\n", server.PublicNet.IPv4.IP.String())
}
if !server.PublicNet.IPv6.IsUnspecified() {
fmt.Printf("IPv6: %s1\n", server.PublicNet.IPv6.Network.IP.String())
fmt.Printf("IPv6 Network: %s\n", server.PublicNet.IPv6.Network.String())
}
if len(server.PrivateNet) > 0 {
var networks []string
for _, network := range server.PrivateNet {
networks = append(networks, fmt.Sprintf("- %s (%s)", network.IP.String(), client.Network().Name(network.Network.ID)))
}
fmt.Printf("Private Networks:\n\t%s\n", strings.Join(networks, "\n"))
}
// Only print the root password if it's not empty,
// which is only the case if it wasn't created with an SSH key.
if result.RootPassword != "" {
fmt.Printf("Root password: %s\n", result.RootPassword)
}
}

func createPrintJSON(result hcloud.ServerCreateResult, server *hcloud.Server) error {
serverSchema := serverToSchema(server)

json := map[string]any{
"server": serverSchema,
"root_password": nil,
}

if result.RootPassword != "" {
json["root_password"] = result.RootPassword
}

return util.DescribeJSON(json)
}

var userDataContentTypes = map[string]string{
"#!": "text/x-shellscript",
"#include": "text/x-include-url",
Expand Down
137 changes: 71 additions & 66 deletions internal/cmd/server/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,74 +131,79 @@ var ListCmd = base.ListCmd{
var serversSchema []schema.Server
for _, resource := range resources {
server := resource.(*hcloud.Server)

serverSchema := schema.Server{
ID: server.ID,
Name: server.Name,
Status: string(server.Status),
Created: server.Created,
Datacenter: util.DatacenterToSchema(*server.Datacenter),
ServerType: util.ServerTypeToSchema(*server.ServerType),
PublicNet: schema.ServerPublicNet{
IPv4: schema.ServerPublicNetIPv4{
IP: server.PublicNet.IPv4.IP.String(),
Blocked: server.PublicNet.IPv4.Blocked,
DNSPtr: server.PublicNet.IPv4.DNSPtr,
},
IPv6: schema.ServerPublicNetIPv6{
IP: server.PublicNet.IPv6.IP.String(),
Blocked: server.PublicNet.IPv6.Blocked,
},
},
RescueEnabled: server.RescueEnabled,
BackupWindow: hcloud.String(server.BackupWindow),
OutgoingTraffic: &server.OutgoingTraffic,
IngoingTraffic: &server.IngoingTraffic,
IncludedTraffic: server.IncludedTraffic,
Protection: schema.ServerProtection{
Delete: server.Protection.Delete,
Rebuild: server.Protection.Rebuild,
},
Labels: server.Labels,
PrimaryDiskSize: server.PrimaryDiskSize,
}
if server.PlacementGroup != nil {
serverPlacementGroup := util.PlacementGroupToSchema(*server.PlacementGroup)
serverSchema.PlacementGroup = &serverPlacementGroup
}
if server.Image != nil {
serverImage := util.ImageToSchema(*server.Image)
serverSchema.Image = &serverImage
}
if server.ISO != nil {
serverISO := util.ISOToSchema(*server.ISO)
serverSchema.ISO = &serverISO
}
for ip, dnsPTR := range server.PublicNet.IPv6.DNSPtr {
serverSchema.PublicNet.IPv6.DNSPtr = append(serverSchema.PublicNet.IPv6.DNSPtr, schema.ServerPublicNetIPv6DNSPtr{
IP: ip,
DNSPtr: dnsPTR,
})
}
for _, floatingIP := range server.PublicNet.FloatingIPs {
serverSchema.PublicNet.FloatingIPs = append(serverSchema.PublicNet.FloatingIPs, floatingIP.ID)
}
for _, volume := range server.Volumes {
serverSchema.Volumes = append(serverSchema.Volumes, volume.ID)
}
for _, privateNet := range server.PrivateNet {
privateNetSchema := schema.ServerPrivateNet{
Network: privateNet.Network.ID,
IP: privateNet.IP.String(),
MACAddress: privateNet.MACAddress,
}
for _, aliasIP := range privateNet.Aliases {
privateNetSchema.AliasIPs = append(privateNetSchema.AliasIPs, aliasIP.String())
}
serverSchema.PrivateNet = append(serverSchema.PrivateNet, privateNetSchema)
}
serverSchema := serverToSchema(server)
serversSchema = append(serversSchema, serverSchema)
}
return serversSchema
},
}

func serverToSchema(server *hcloud.Server) schema.Server {
serverSchema := schema.Server{
ID: server.ID,
Name: server.Name,
Status: string(server.Status),
Created: server.Created,
Datacenter: util.DatacenterToSchema(*server.Datacenter),
ServerType: util.ServerTypeToSchema(*server.ServerType),
PublicNet: schema.ServerPublicNet{
IPv4: schema.ServerPublicNetIPv4{
IP: server.PublicNet.IPv4.IP.String(),
Blocked: server.PublicNet.IPv4.Blocked,
DNSPtr: server.PublicNet.IPv4.DNSPtr,
},
IPv6: schema.ServerPublicNetIPv6{
IP: server.PublicNet.IPv6.IP.String(),
Blocked: server.PublicNet.IPv6.Blocked,
},
},
RescueEnabled: server.RescueEnabled,
BackupWindow: hcloud.Ptr(server.BackupWindow),
OutgoingTraffic: &server.OutgoingTraffic,
IngoingTraffic: &server.IngoingTraffic,
IncludedTraffic: server.IncludedTraffic,
Protection: schema.ServerProtection{
Delete: server.Protection.Delete,
Rebuild: server.Protection.Rebuild,
},
Labels: server.Labels,
PrimaryDiskSize: server.PrimaryDiskSize,
}
if server.PlacementGroup != nil {
serverPlacementGroup := util.PlacementGroupToSchema(*server.PlacementGroup)
serverSchema.PlacementGroup = &serverPlacementGroup
}
if server.Image != nil {
serverImage := util.ImageToSchema(*server.Image)
serverSchema.Image = &serverImage
}
if server.ISO != nil {
serverISO := util.ISOToSchema(*server.ISO)
serverSchema.ISO = &serverISO
}
for ip, dnsPTR := range server.PublicNet.IPv6.DNSPtr {
serverSchema.PublicNet.IPv6.DNSPtr = append(serverSchema.PublicNet.IPv6.DNSPtr, schema.ServerPublicNetIPv6DNSPtr{
IP: ip,
DNSPtr: dnsPTR,
})
}
for _, floatingIP := range server.PublicNet.FloatingIPs {
serverSchema.PublicNet.FloatingIPs = append(serverSchema.PublicNet.FloatingIPs, floatingIP.ID)
}
for _, volume := range server.Volumes {
serverSchema.Volumes = append(serverSchema.Volumes, volume.ID)
}
for _, privateNet := range server.PrivateNet {
privateNetSchema := schema.ServerPrivateNet{
Network: privateNet.Network.ID,
IP: privateNet.IP.String(),
MACAddress: privateNet.MACAddress,
}
for _, aliasIP := range privateNet.Aliases {
privateNetSchema.AliasIPs = append(privateNetSchema.AliasIPs, aliasIP.String())
}
serverSchema.PrivateNet = append(serverSchema.PrivateNet, privateNetSchema)
}

return serverSchema
}