Skip to content

Commit

Permalink
fix: make JSON/YAML outputs consistent (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Dec 13, 2023
1 parent 2ab9ce3 commit 43944b8
Show file tree
Hide file tree
Showing 18 changed files with 19 additions and 491 deletions.
4 changes: 2 additions & 2 deletions internal/cmd/all/testdata/all_list_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type": "system",
"name": "debian-12",
"description": "Debian 12",
"image_size": 0,
"image_size": null,
"disk_size": 5,
"created": "2023-06-13T06:00:00Z",
"created_from": null,
Expand All @@ -19,7 +19,7 @@
"protection": {
"delete": false
},
"deprecated": "0001-01-01T00:00:00Z",
"deprecated": null,
"deleted": null,
"labels": null
}
Expand Down
23 changes: 1 addition & 22 deletions internal/cmd/certificate/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,7 @@ var ListCmd = base.ListCmd{
certSchemas := make([]schema.Certificate, 0, len(resources))
for _, resource := range resources {
cert := resource.(*hcloud.Certificate)
certSchema := schema.Certificate{
ID: cert.ID,
Certificate: cert.Certificate,
Created: cert.Created,
DomainNames: cert.DomainNames,
Fingerprint: cert.Fingerprint,
Labels: cert.Labels,
Name: cert.Name,
Type: string(cert.Type),
NotValidAfter: cert.NotValidAfter,
NotValidBefore: cert.NotValidBefore,
}
if len(cert.UsedBy) > 0 {
certSchema.UsedBy = make([]schema.CertificateUsedByRef, len(cert.UsedBy))
for i, ub := range cert.UsedBy {
certSchema.UsedBy[i] = schema.CertificateUsedByRef{
ID: ub.ID,
Type: string(ub.Type),
}
}
}
certSchemas = append(certSchemas, certSchema)
certSchemas = append(certSchemas, hcloud.SchemaFromCertificate(cert))
}

return certSchemas
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/datacenter/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/output"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
Expand Down Expand Up @@ -44,7 +43,7 @@ var ListCmd = base.ListCmd{
dcSchemas := make([]schema.Datacenter, 0, len(resources))
for _, resource := range resources {
dc := resource.(*hcloud.Datacenter)
dcSchemas = append(dcSchemas, util.DatacenterToSchema(*dc))
dcSchemas = append(dcSchemas, hcloud.SchemaFromDatacenter(dc))
}

return dcSchemas
Expand Down
42 changes: 2 additions & 40 deletions internal/cmd/firewall/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,46 +69,8 @@ var ListCmd = base.ListCmd{
Schema: func(resources []interface{}) interface{} {
firewallSchemas := make([]schema.Firewall, 0, len(resources))
for _, resource := range resources {
firewall := resource.(*hcloud.Firewall)
firewallSchema := schema.Firewall{
ID: firewall.ID,
Name: firewall.Name,
Labels: firewall.Labels,
Created: firewall.Created,
}
for _, rule := range firewall.Rules {
var sourceNets []string
for _, sourceIP := range rule.SourceIPs {
sourceNets = append(sourceNets, sourceIP.Network())
}
var destinationNets []string
for _, destinationIP := range rule.DestinationIPs {
destinationNets = append(destinationNets, destinationIP.Network())
}
firewallSchema.Rules = append(firewallSchema.Rules, schema.FirewallRule{
Direction: string(rule.Direction),
SourceIPs: sourceNets,
DestinationIPs: destinationNets,
Protocol: string(rule.Protocol),
Port: rule.Port,
Description: rule.Description,
})
}
for _, AppliedTo := range firewall.AppliedTo {
s := schema.FirewallResource{
Type: string(AppliedTo.Type),
}
switch AppliedTo.Type {
case hcloud.FirewallResourceTypeServer:
s.Server = &schema.FirewallResourceServer{ID: AppliedTo.Server.ID}
case hcloud.FirewallResourceTypeLabelSelector:
s.LabelSelector = &schema.FirewallResourceLabelSelector{Selector: AppliedTo.LabelSelector.Selector}
}

firewallSchema.AppliedTo = append(firewallSchema.AppliedTo, s)
}

firewallSchemas = append(firewallSchemas, firewallSchema)
fw := resource.(*hcloud.Firewall)
firewallSchemas = append(firewallSchemas, hcloud.SchemaFromFirewall(fw))
}
return firewallSchemas
},
Expand Down
23 changes: 1 addition & 22 deletions internal/cmd/floatingip/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,7 @@ var ListCmd = base.ListCmd{
floatingIPSchemas := make([]schema.FloatingIP, 0, len(resources))
for _, resource := range resources {
floatingIP := resource.(*hcloud.FloatingIP)
floatingIPSchema := schema.FloatingIP{
ID: floatingIP.ID,
Name: floatingIP.Name,
Description: hcloud.String(floatingIP.Description),
IP: floatingIP.IP.String(),
Created: floatingIP.Created,
Type: string(floatingIP.Type),
HomeLocation: util.LocationToSchema(*floatingIP.HomeLocation),
Blocked: floatingIP.Blocked,
Protection: schema.FloatingIPProtection{Delete: floatingIP.Protection.Delete},
Labels: floatingIP.Labels,
}
for ip, dnsPtr := range floatingIP.DNSPtr {
floatingIPSchema.DNSPtr = append(floatingIPSchema.DNSPtr, schema.FloatingIPDNSPtr{
IP: ip,
DNSPtr: dnsPtr,
})
}
if floatingIP.Server != nil {
floatingIPSchema.Server = hcloud.Ptr(floatingIP.Server.ID)
}
floatingIPSchemas = append(floatingIPSchemas, floatingIPSchema)
floatingIPSchemas = append(floatingIPSchemas, hcloud.SchemaFromFloatingIP(floatingIP))
}
return floatingIPSchemas
},
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/image/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ var ListCmd = base.ListCmd{
imageSchemas := make([]schema.Image, 0, len(resources))
for _, resource := range resources {
image := resource.(*hcloud.Image)
imageSchemas = append(imageSchemas, util.ImageToSchema(*image))
imageSchemas = append(imageSchemas, hcloud.SchemaFromImage(image))
}
return imageSchemas
},
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/iso/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ 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/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
Expand Down Expand Up @@ -94,7 +93,7 @@ var ListCmd = base.ListCmd{
isoSchemas := make([]schema.ISO, 0, len(resources))
for _, resource := range resources {
iso := resource.(*hcloud.ISO)
isoSchemas = append(isoSchemas, util.ISOToSchema(*iso))
isoSchemas = append(isoSchemas, hcloud.SchemaFromISO(iso))
}
return isoSchemas
},
Expand Down
82 changes: 1 addition & 81 deletions internal/cmd/loadbalancer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,87 +86,7 @@ var ListCmd = base.ListCmd{
loadBalancerSchemas := make([]schema.LoadBalancer, 0, len(resources))
for _, resource := range resources {
loadBalancer := resource.(*hcloud.LoadBalancer)
loadBalancerSchema := schema.LoadBalancer{
ID: loadBalancer.ID,
Name: loadBalancer.Name,
PublicNet: schema.LoadBalancerPublicNet{
Enabled: loadBalancer.PublicNet.Enabled,
IPv4: schema.LoadBalancerPublicNetIPv4{
IP: loadBalancer.PublicNet.IPv4.IP.String(),
},
IPv6: schema.LoadBalancerPublicNetIPv6{
IP: loadBalancer.PublicNet.IPv6.IP.String(),
},
},
Created: loadBalancer.Created,
Labels: loadBalancer.Labels,
LoadBalancerType: util.LoadBalancerTypeToSchema(*loadBalancer.LoadBalancerType),
Location: util.LocationToSchema(*loadBalancer.Location),
IncludedTraffic: loadBalancer.IncludedTraffic,
OutgoingTraffic: &loadBalancer.OutgoingTraffic,
IngoingTraffic: &loadBalancer.IngoingTraffic,
Protection: schema.LoadBalancerProtection{
Delete: loadBalancer.Protection.Delete,
},
Algorithm: schema.LoadBalancerAlgorithm{Type: string(loadBalancer.Algorithm.Type)},
}
for _, service := range loadBalancer.Services {
serviceSchema := schema.LoadBalancerService{
Protocol: string(service.Protocol),
ListenPort: service.ListenPort,
DestinationPort: service.DestinationPort,
Proxyprotocol: service.Proxyprotocol,
HealthCheck: &schema.LoadBalancerServiceHealthCheck{
Protocol: string(service.HealthCheck.Protocol),
Port: service.HealthCheck.Port,
Interval: int(service.HealthCheck.Interval.Seconds()),
Timeout: int(service.HealthCheck.Timeout.Seconds()),
Retries: service.HealthCheck.Retries,
},
}
if service.Protocol != hcloud.LoadBalancerServiceProtocolTCP {
serviceSchema.HTTP = &schema.LoadBalancerServiceHTTP{
StickySessions: service.HTTP.StickySessions,
CookieName: service.HTTP.CookieName,
CookieLifetime: int(service.HTTP.CookieLifetime.Seconds()),
RedirectHTTP: service.HTTP.RedirectHTTP,
}
}
if service.HealthCheck.HTTP != nil {
serviceSchema.HealthCheck.HTTP = &schema.LoadBalancerServiceHealthCheckHTTP{
Domain: service.HealthCheck.HTTP.Domain,
Path: service.HealthCheck.HTTP.Path,
StatusCodes: service.HealthCheck.HTTP.StatusCodes,
TLS: service.HealthCheck.HTTP.TLS,
Response: service.HealthCheck.HTTP.Response,
}
}
loadBalancerSchema.Services = append(loadBalancerSchema.Services, serviceSchema)
}
for _, target := range loadBalancer.Targets {
targetSchema := schema.LoadBalancerTarget{
Type: string(target.Type),
UsePrivateIP: target.UsePrivateIP,
}
if target.Type == hcloud.LoadBalancerTargetTypeServer {
targetSchema.Server = &schema.LoadBalancerTargetServer{ID: target.Server.Server.ID}
}
if target.Type == hcloud.LoadBalancerTargetTypeLabelSelector {
targetSchema.LabelSelector = &schema.LoadBalancerTargetLabelSelector{Selector: target.LabelSelector.Selector}
}
if target.Type == hcloud.LoadBalancerTargetTypeIP {
targetSchema.IP = &schema.LoadBalancerTargetIP{IP: target.IP.IP}
}
for _, healthStatus := range target.HealthStatus {
targetSchema.HealthStatus = append(targetSchema.HealthStatus, schema.LoadBalancerTargetHealthStatus{
ListenPort: healthStatus.ListenPort,
Status: string(healthStatus.Status),
})
}
loadBalancerSchema.Targets = append(loadBalancerSchema.Targets, targetSchema)
}

loadBalancerSchemas = append(loadBalancerSchemas, loadBalancerSchema)
loadBalancerSchemas = append(loadBalancerSchemas, hcloud.SchemaFromLoadBalancer(loadBalancer))
}
return loadBalancerSchemas
},
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/loadbalancertype/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/output"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
Expand Down Expand Up @@ -42,7 +41,7 @@ var ListCmd = base.ListCmd{
loadBalancerTypeSchemas := make([]schema.LoadBalancerType, 0, len(resources))
for _, resource := range resources {
loadBalancerType := resource.(*hcloud.LoadBalancerType)
loadBalancerTypeSchemas = append(loadBalancerTypeSchemas, util.LoadBalancerTypeToSchema(*loadBalancerType))
loadBalancerTypeSchemas = append(loadBalancerTypeSchemas, hcloud.SchemaFromLoadBalancerType(loadBalancerType))
}
return loadBalancerTypeSchemas
},
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/location/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/cmd/output"
"github.com/hetznercloud/cli/internal/cmd/util"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
Expand Down Expand Up @@ -41,7 +40,7 @@ var ListCmd = base.ListCmd{
locationSchemas := make([]schema.Location, 0, len(resources))
for _, resource := range resources {
location := resource.(*hcloud.Location)
locationSchemas = append(locationSchemas, util.LocationToSchema(*location))
locationSchemas = append(locationSchemas, hcloud.SchemaFromLocation(location))
}
return locationSchemas
},
Expand Down
29 changes: 1 addition & 28 deletions internal/cmd/network/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,7 @@ var ListCmd = base.ListCmd{
networkSchemas := make([]schema.Network, 0, len(resources))
for _, resource := range resources {
network := resource.(*hcloud.Network)

networkSchema := schema.Network{
ID: network.ID,
Name: network.Name,
IPRange: network.IPRange.String(),
Protection: schema.NetworkProtection{Delete: network.Protection.Delete},
Created: network.Created,
Labels: network.Labels,
ExposeRoutesToVSwitch: network.ExposeRoutesToVSwitch,
}
for _, subnet := range network.Subnets {
networkSchema.Subnets = append(networkSchema.Subnets, schema.NetworkSubnet{
Type: string(subnet.Type),
IPRange: subnet.IPRange.String(),
NetworkZone: string(subnet.NetworkZone),
Gateway: subnet.Gateway.String(),
})
}
for _, route := range network.Routes {
networkSchema.Routes = append(networkSchema.Routes, schema.NetworkRoute{
Destination: route.Destination.String(),
Gateway: route.Gateway.String(),
})
}
for _, server := range network.Servers {
networkSchema.Servers = append(networkSchema.Servers, server.ID)
}
networkSchemas = append(networkSchemas, networkSchema)
networkSchemas = append(networkSchemas, hcloud.SchemaFromNetwork(network))
}
return networkSchemas
},
Expand Down
11 changes: 1 addition & 10 deletions internal/cmd/placementgroup/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,7 @@ var ListCmd = base.ListCmd{
placementGroupSchemas := make([]schema.PlacementGroup, 0, len(resources))
for _, resource := range resources {
placementGroup := resource.(*hcloud.PlacementGroup)
placementGroupSchema := schema.PlacementGroup{
ID: placementGroup.ID,
Name: placementGroup.Name,
Labels: placementGroup.Labels,
Created: placementGroup.Created,
Servers: placementGroup.Servers,
Type: string(placementGroup.Type),
}

placementGroupSchemas = append(placementGroupSchemas, placementGroupSchema)
placementGroupSchemas = append(placementGroupSchemas, hcloud.SchemaFromPlacementGroup(placementGroup))
}
return placementGroupSchemas
},
Expand Down
25 changes: 1 addition & 24 deletions internal/cmd/primaryip/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,7 @@ var ListCmd = base.ListCmd{
primaryIPsSchema := make([]schema.PrimaryIP, 0, len(resources))
for _, resource := range resources {
primaryIP := resource.(*hcloud.PrimaryIP)
var dnsPtrs []hcloud.PrimaryIPDNSPTR
for i, d := range primaryIP.DNSPtr {
dnsPtrs = append(dnsPtrs, hcloud.PrimaryIPDNSPTR{
DNSPtr: d,
IP: i,
})
}
var primaryIPSchema = schema.PrimaryIP{
ID: primaryIP.ID,
Name: primaryIP.Name,
IP: primaryIP.IP.String(),
Type: string(primaryIP.Type),
AssigneeID: primaryIP.AssigneeID,
AssigneeType: primaryIP.AssigneeType,
AutoDelete: primaryIP.AutoDelete,
Created: primaryIP.Created,
Datacenter: util.DatacenterToSchema(*primaryIP.Datacenter),

Protection: schema.PrimaryIPProtection{
Delete: primaryIP.Protection.Delete,
},
Labels: primaryIP.Labels,
}
primaryIPsSchema = append(primaryIPsSchema, primaryIPSchema)
primaryIPsSchema = append(primaryIPsSchema, hcloud.SchemaFromPrimaryIP(primaryIP))
}
return primaryIPsSchema
},
Expand Down

0 comments on commit 43944b8

Please sign in to comment.