From f97f54f47db701305c0972fd6a967b540e3cfc1a Mon Sep 17 00:00:00 2001 From: pauhull Date: Thu, 30 Nov 2023 17:00:06 +0100 Subject: [PATCH] refactor: use new conversion methods in hcloud-go --- internal/cmd/certificate/list.go | 23 +--- internal/cmd/datacenter/list.go | 3 +- internal/cmd/firewall/list.go | 42 +------ internal/cmd/floatingip/list.go | 23 +--- internal/cmd/image/list.go | 2 +- internal/cmd/iso/list.go | 3 +- internal/cmd/loadbalancer/list.go | 82 +------------ internal/cmd/loadbalancertype/list.go | 3 +- internal/cmd/location/list.go | 3 +- internal/cmd/network/list.go | 29 +---- internal/cmd/placementgroup/list.go | 11 +- internal/cmd/primaryip/list.go | 25 +--- internal/cmd/server/list.go | 68 +---------- internal/cmd/servertype/list.go | 2 +- internal/cmd/sshkey/list.go | 10 +- internal/cmd/util/util.go | 162 -------------------------- internal/cmd/volume/list.go | 15 +-- 17 files changed, 17 insertions(+), 489 deletions(-) diff --git a/internal/cmd/certificate/list.go b/internal/cmd/certificate/list.go index b1ec9cc1..0660e9ed 100644 --- a/internal/cmd/certificate/list.go +++ b/internal/cmd/certificate/list.go @@ -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 diff --git a/internal/cmd/datacenter/list.go b/internal/cmd/datacenter/list.go index 23c9bde3..654efb22 100644 --- a/internal/cmd/datacenter/list.go +++ b/internal/cmd/datacenter/list.go @@ -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" @@ -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 diff --git a/internal/cmd/firewall/list.go b/internal/cmd/firewall/list.go index 9bb32f99..b1ea73c7 100644 --- a/internal/cmd/firewall/list.go +++ b/internal/cmd/firewall/list.go @@ -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 }, diff --git a/internal/cmd/floatingip/list.go b/internal/cmd/floatingip/list.go index fe3b7d31..cff80f37 100644 --- a/internal/cmd/floatingip/list.go +++ b/internal/cmd/floatingip/list.go @@ -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 }, diff --git a/internal/cmd/image/list.go b/internal/cmd/image/list.go index 73ef3603..0424e4ce 100644 --- a/internal/cmd/image/list.go +++ b/internal/cmd/image/list.go @@ -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 }, diff --git a/internal/cmd/iso/list.go b/internal/cmd/iso/list.go index 95cf41b2..2e960bb3 100644 --- a/internal/cmd/iso/list.go +++ b/internal/cmd/iso/list.go @@ -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" @@ -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 }, diff --git a/internal/cmd/loadbalancer/list.go b/internal/cmd/loadbalancer/list.go index b607c8a7..bdf2e9ba 100644 --- a/internal/cmd/loadbalancer/list.go +++ b/internal/cmd/loadbalancer/list.go @@ -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 }, diff --git a/internal/cmd/loadbalancertype/list.go b/internal/cmd/loadbalancertype/list.go index 8102241d..405c8ebb 100644 --- a/internal/cmd/loadbalancertype/list.go +++ b/internal/cmd/loadbalancertype/list.go @@ -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" @@ -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 }, diff --git a/internal/cmd/location/list.go b/internal/cmd/location/list.go index cdd306ce..6f1fd3e2 100644 --- a/internal/cmd/location/list.go +++ b/internal/cmd/location/list.go @@ -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" @@ -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 }, diff --git a/internal/cmd/network/list.go b/internal/cmd/network/list.go index bc508b98..dc6d41e2 100644 --- a/internal/cmd/network/list.go +++ b/internal/cmd/network/list.go @@ -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 }, diff --git a/internal/cmd/placementgroup/list.go b/internal/cmd/placementgroup/list.go index f3c5fdb9..b364dde6 100644 --- a/internal/cmd/placementgroup/list.go +++ b/internal/cmd/placementgroup/list.go @@ -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 }, diff --git a/internal/cmd/primaryip/list.go b/internal/cmd/primaryip/list.go index 826ec74c..70159b44 100644 --- a/internal/cmd/primaryip/list.go +++ b/internal/cmd/primaryip/list.go @@ -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 }, diff --git a/internal/cmd/server/list.go b/internal/cmd/server/list.go index e8804cdb..f2eb6805 100644 --- a/internal/cmd/server/list.go +++ b/internal/cmd/server/list.go @@ -163,73 +163,7 @@ var ListCmd = base.ListCmd{ serversSchema := make([]schema.Server, 0, len(resources)) 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) - } - serversSchema = append(serversSchema, serverSchema) + serversSchema = append(serversSchema, hcloud.SchemaFromServer(server)) } return serversSchema }, diff --git a/internal/cmd/servertype/list.go b/internal/cmd/servertype/list.go index a9f8f613..91103517 100644 --- a/internal/cmd/servertype/list.go +++ b/internal/cmd/servertype/list.go @@ -56,7 +56,7 @@ var ListCmd = base.ListCmd{ serverTypeSchemas := make([]schema.ServerType, 0, len(resources)) for _, resource := range resources { serverType := resource.(*hcloud.ServerType) - serverTypeSchemas = append(serverTypeSchemas, util.ServerTypeToSchema(*serverType)) + serverTypeSchemas = append(serverTypeSchemas, hcloud.SchemaFromServerType(serverType)) } return serverTypeSchemas }, diff --git a/internal/cmd/sshkey/list.go b/internal/cmd/sshkey/list.go index f8e5fd86..443b2c6f 100644 --- a/internal/cmd/sshkey/list.go +++ b/internal/cmd/sshkey/list.go @@ -54,15 +54,7 @@ var ListCmd = base.ListCmd{ sshKeySchemas := make([]schema.SSHKey, 0, len(resources)) for _, resource := range resources { sshKey := resource.(*hcloud.SSHKey) - sshKeySchema := schema.SSHKey{ - ID: sshKey.ID, - Name: sshKey.Name, - Fingerprint: sshKey.Fingerprint, - PublicKey: sshKey.PublicKey, - Labels: sshKey.Labels, - Created: sshKey.Created, - } - sshKeySchemas = append(sshKeySchemas, sshKeySchema) + sshKeySchemas = append(sshKeySchemas, hcloud.SchemaFromSSHKey(sshKey)) } return sshKeySchemas }, diff --git a/internal/cmd/util/util.go b/internal/cmd/util/util.go index 5fc05949..8662b5dd 100644 --- a/internal/cmd/util/util.go +++ b/internal/cmd/util/util.go @@ -12,9 +12,6 @@ import ( "github.com/goccy/go-yaml" "github.com/spf13/cobra" "github.com/spf13/pflag" - - "github.com/hetznercloud/hcloud-go/v2/hcloud" - "github.com/hetznercloud/hcloud-go/v2/hcloud/schema" ) func YesNo(b bool) string { @@ -168,165 +165,6 @@ func DescribeYAML(object interface{}) error { return enc.Encode(object) } -func LocationToSchema(location hcloud.Location) schema.Location { - return schema.Location{ - ID: location.ID, - Name: location.Name, - Description: location.Description, - Country: location.Country, - City: location.City, - Latitude: location.Latitude, - Longitude: location.Longitude, - NetworkZone: string(location.NetworkZone), - } -} - -func DatacenterToSchema(datacenter hcloud.Datacenter) schema.Datacenter { - datacenterSchema := schema.Datacenter{ - ID: datacenter.ID, - Name: datacenter.Name, - Description: datacenter.Description, - Location: LocationToSchema(*datacenter.Location), - } - for _, st := range datacenter.ServerTypes.Supported { - datacenterSchema.ServerTypes.Supported = append(datacenterSchema.ServerTypes.Supported, st.ID) - } - for _, st := range datacenter.ServerTypes.Available { - datacenterSchema.ServerTypes.Available = append(datacenterSchema.ServerTypes.Available, st.ID) - } - return datacenterSchema -} - -func ServerTypeToSchema(serverType hcloud.ServerType) schema.ServerType { - serverTypeSchema := schema.ServerType{ - ID: serverType.ID, - Name: serverType.Name, - Description: serverType.Description, - Cores: serverType.Cores, - Memory: serverType.Memory, - Disk: serverType.Disk, - StorageType: string(serverType.StorageType), - CPUType: string(serverType.CPUType), - Architecture: string(serverType.Architecture), - IncludedTraffic: serverType.IncludedTraffic, - DeprecatableResource: DeprecatableResourceToSchema(serverType.DeprecatableResource), - } - for _, pricing := range serverType.Pricings { - serverTypeSchema.Prices = append(serverTypeSchema.Prices, schema.PricingServerTypePrice{ - Location: pricing.Location.Name, - PriceHourly: schema.Price{ - Net: pricing.Hourly.Net, - Gross: pricing.Hourly.Gross, - }, - PriceMonthly: schema.Price{ - Net: pricing.Monthly.Net, - Gross: pricing.Monthly.Gross, - }, - }) - } - return serverTypeSchema -} - -func ImageToSchema(image hcloud.Image) schema.Image { - imageSchema := schema.Image{ - ID: image.ID, - Name: hcloud.String(image.Name), - Description: image.Description, - Status: string(image.Status), - Type: string(image.Type), - ImageSize: &image.ImageSize, - DiskSize: image.DiskSize, - Created: &image.Created, - OSFlavor: image.OSFlavor, - OSVersion: hcloud.String(image.OSVersion), - Architecture: string(image.Architecture), - RapidDeploy: image.RapidDeploy, - Protection: schema.ImageProtection{ - Delete: image.Protection.Delete, - }, - Deprecated: &image.Deprecated, - Labels: image.Labels, - } - if image.CreatedFrom != nil { - imageSchema.CreatedFrom = &schema.ImageCreatedFrom{ - ID: image.CreatedFrom.ID, - Name: image.CreatedFrom.Name, - } - } - if image.BoundTo != nil { - imageSchema.BoundTo = hcloud.Ptr(image.BoundTo.ID) - } - return imageSchema -} - -func ISOToSchema(iso hcloud.ISO) schema.ISO { - isoSchema := schema.ISO{ - ID: iso.ID, - Name: iso.Name, - Description: iso.Description, - Type: string(iso.Type), - Deprecated: &iso.Deprecated, - } - - if iso.Architecture != nil { - isoSchema.Architecture = hcloud.Ptr(string(*iso.Architecture)) - } - - return isoSchema -} - -func LoadBalancerTypeToSchema(loadBalancerType hcloud.LoadBalancerType) schema.LoadBalancerType { - loadBalancerTypeSchema := schema.LoadBalancerType{ - ID: loadBalancerType.ID, - Name: loadBalancerType.Name, - Description: loadBalancerType.Description, - MaxConnections: loadBalancerType.MaxConnections, - MaxServices: loadBalancerType.MaxServices, - MaxTargets: loadBalancerType.MaxTargets, - MaxAssignedCertificates: loadBalancerType.MaxAssignedCertificates, - } - for _, pricing := range loadBalancerType.Pricings { - loadBalancerTypeSchema.Prices = append(loadBalancerTypeSchema.Prices, schema.PricingLoadBalancerTypePrice{ - Location: pricing.Location.Name, - PriceHourly: schema.Price{ - Net: pricing.Hourly.Net, - Gross: pricing.Hourly.Gross, - }, - PriceMonthly: schema.Price{ - Net: pricing.Monthly.Net, - Gross: pricing.Monthly.Gross, - }, - }) - } - return loadBalancerTypeSchema -} - -func PlacementGroupToSchema(placementGroup hcloud.PlacementGroup) schema.PlacementGroup { - return schema.PlacementGroup{ - ID: placementGroup.ID, - Name: placementGroup.Name, - Labels: placementGroup.Labels, - Created: placementGroup.Created, - Type: string(placementGroup.Type), - Servers: placementGroup.Servers, - } -} - -func DeprecatableResourceToSchema(deprecatableResource hcloud.DeprecatableResource) schema.DeprecatableResource { - var deprecation *schema.DeprecationInfo - - if deprecatableResource.IsDeprecated() { - deprecation = &schema.DeprecationInfo{ - Announced: deprecatableResource.Deprecation.Announced, - UnavailableAfter: deprecatableResource.Deprecation.UnavailableAfter, - } - } - - return schema.DeprecatableResource{ - Deprecation: deprecation, - } -} - // ValidateRequiredFlags ensures that flags has values for all flags with // the passed names. // diff --git a/internal/cmd/volume/list.go b/internal/cmd/volume/list.go index 4a2d91f0..d3f1b05c 100644 --- a/internal/cmd/volume/list.go +++ b/internal/cmd/volume/list.go @@ -80,20 +80,7 @@ var ListCmd = base.ListCmd{ volumesSchema := make([]schema.Volume, 0, len(resources)) for _, resource := range resources { volume := resource.(*hcloud.Volume) - volumeSchema := schema.Volume{ - ID: volume.ID, - Name: volume.Name, - Location: util.LocationToSchema(*volume.Location), - Size: volume.Size, - LinuxDevice: volume.LinuxDevice, - Labels: volume.Labels, - Created: volume.Created, - Protection: schema.VolumeProtection{Delete: volume.Protection.Delete}, - } - if volume.Server != nil { - volumeSchema.Server = hcloud.Ptr(volume.Server.ID) - } - volumesSchema = append(volumesSchema, volumeSchema) + volumesSchema = append(volumesSchema, hcloud.SchemaFromVolume(volume)) } return volumesSchema },