From 160209ef0904f00e33ca3943ad6f2a0b1084e067 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Thu, 21 Mar 2024 11:43:21 +0100 Subject: [PATCH 1/7] Validate machine response against swagger spec. --- .../internal/service/machine-service_test.go | 129 ++++++++++++++++++ cmd/metal-api/internal/service/v1/image.go | 2 +- cmd/metal-api/internal/service/v1/machine.go | 12 +- cmd/metal-api/internal/testdata/testdata.go | 48 +++++++ go.mod | 5 +- spec/metal-api.json | 10 +- 6 files changed, 190 insertions(+), 16 deletions(-) diff --git a/cmd/metal-api/internal/service/machine-service_test.go b/cmd/metal-api/internal/service/machine-service_test.go index 1f13b4910..55d856921 100644 --- a/cmd/metal-api/internal/service/machine-service_test.go +++ b/cmd/metal-api/internal/service/machine-service_test.go @@ -9,7 +9,12 @@ import ( "net/http/httptest" "testing" + restfulspec "github.com/emicklei/go-restful-openapi/v2" "github.com/emicklei/go-restful/v3" + "github.com/go-openapi/spec" + "github.com/go-openapi/strfmt" + "github.com/go-openapi/validate" + "github.com/google/go-cmp/cmp" goipam "github.com/metal-stack/go-ipam" "github.com/metal-stack/metal-api/cmd/metal-api/internal/datastore" "github.com/metal-stack/metal-api/cmd/metal-api/internal/ipam" @@ -17,6 +22,7 @@ import ( v1 "github.com/metal-stack/metal-api/cmd/metal-api/internal/service/v1" "github.com/metal-stack/metal-api/cmd/metal-api/internal/testdata" "github.com/metal-stack/metal-lib/bus" + "github.com/metal-stack/metal-lib/pkg/pointer" "github.com/metal-stack/security" "github.com/stretchr/testify/require" "golang.org/x/crypto/ssh" @@ -1339,3 +1345,126 @@ func Test_gatherNetworksFromSpec(t *testing.T) { }) } } + +func TestNewMachineResponse(t *testing.T) { + tests := []struct { + name string + m *metal.Machine + s *metal.Size + p *metal.Partition + i *metal.Image + ec *metal.ProvisioningEventContainer + want *v1.MachineResponse + }{ + { + name: "test firewall response", + m: &testdata.FW1, + s: &testdata.Sz1, + p: &testdata.Partition1, + i: &testdata.Img1, + ec: &metal.ProvisioningEventContainer{}, + want: &v1.MachineResponse{ + Common: v1.Common{ + Identifiable: v1.Identifiable{ + ID: testdata.FW1.ID, + }, + Describable: v1.Describable{ + Name: pointer.Pointer(""), + Description: pointer.Pointer(""), + }, + }, + MachineBase: v1.MachineBase{ + Partition: v1.NewPartitionResponse(&testdata.Partition1), + RackID: "", + Size: v1.NewSizeResponse(&testdata.Sz1), + Hardware: v1.MachineHardware{ + MachineHardwareBase: v1.MachineHardwareBase{ + Memory: testdata.FW1.Hardware.Memory, + CPUCores: testdata.FW1.Hardware.CPUCores, + Disks: []v1.MachineBlockDevice{ + { + Size: testdata.FW1.Hardware.Disks[0].Size, + }, + { + Size: testdata.FW1.Hardware.Disks[1].Size, + }, + { + Size: testdata.FW1.Hardware.Disks[2].Size, + }, + }, + }, + Nics: v1.MachineNics{}, + }, + Allocation: &v1.MachineAllocation{ + Name: testdata.FW1.Allocation.Name, + Project: testdata.FW1.Allocation.Project, + Image: v1.NewImageResponse(&testdata.Img1), + MachineNetworks: []v1.MachineNetwork{ + { + IPs: []string{}, + NetworkType: "privateprimaryunshared", + Vrf: 1, + Private: true, + }, + }, + Role: "firewall", + FirewallRules: &v1.FirewallRules{ + Egress: []v1.FirewallEgressRule{ + { + Protocol: "tcp", + Ports: []int{443}, + To: []string{"0.0.0.0/0"}, + Comment: "test", + }, + }, + Ingress: nil, + }, + }, + RecentProvisioningEvents: v1.MachineRecentProvisioningEvents{ + Events: []v1.MachineProvisioningEvent{}, + }, + Tags: testdata.FW1.Tags, + }, + Timestamps: v1.Timestamps{}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := v1.NewMachineResponse(tt.m, tt.s, tt.p, tt.i, tt.ec) + if diff := cmp.Diff(got, tt.want); diff != "" { + t.Errorf("diff (-want +got):\n%s", diff) + } + + ws, err := NewMachine(slog.Default(), nil, &emptyPublisher{}, bus.DirectEndpoints(), ipam.New(nil), nil, nil, nil, 0, nil, metal.DisabledIPMISuperUser()) + require.NoError(t, err) + + validateAgainstSwaggerSpec(t, ws, "v1.MachineResponse", got) + }) + } +} + +func validateAgainstSwaggerSpec(t *testing.T, ws *restful.WebService, definitionKey string, obj any) { + container := restful.NewContainer() + container.Add(ws) + + actual := restfulspec.BuildSwagger(restfulspec.Config{ + WebServices: container.RegisteredWebServices(), + }) + + schemaJSON, err := json.MarshalIndent(actual, "", " ") + require.NoError(t, err) + + schema := new(spec.Schema) + err = json.Unmarshal(schemaJSON, schema) + require.NoError(t, err) + + // you need to pass the definition of the response to the validator otherwise it will not find any problems + def, ok := schema.Definitions[definitionKey] + require.True(t, ok) + + def.Definitions = schema.Definitions + + err = validate.AgainstSchema(&def, obj, strfmt.Default, validate.EnableArrayMustHaveItemsCheck(true), validate.EnableObjectArrayTypeCheck(true)) + require.NoError(t, err) +} diff --git a/cmd/metal-api/internal/service/v1/image.go b/cmd/metal-api/internal/service/v1/image.go index b4cf79a57..bb54d204f 100644 --- a/cmd/metal-api/internal/service/v1/image.go +++ b/cmd/metal-api/internal/service/v1/image.go @@ -12,7 +12,7 @@ type ImageBase struct { Features []string `json:"features" description:"features of this image" optional:"true"` ExpirationDate time.Time `json:"expirationDate" description:"expirationDate of this image" optional:"false"` Classification string `json:"classification" description:"classification of this image" optional:"true"` - UsedBy []string `json:"usedby" description:"machines where this image is in use" optional:"true"` + UsedBy []string `json:"usedby,omitempty" description:"machines where this image is in use" optional:"true"` } type ImageCreateRequest struct { diff --git a/cmd/metal-api/internal/service/v1/machine.go b/cmd/metal-api/internal/service/v1/machine.go index 6f2cca5a6..8571ce84c 100644 --- a/cmd/metal-api/internal/service/v1/machine.go +++ b/cmd/metal-api/internal/service/v1/machine.go @@ -36,7 +36,7 @@ type MachineAllocation struct { FilesystemLayout *FilesystemLayoutResponse `json:"filesystemlayout" description:"filesystemlayout to create on this machine" optional:"true"` MachineNetworks []MachineNetwork `json:"networks" description:"the networks of this machine"` Hostname string `json:"hostname" description:"the hostname which will be used when creating the machine"` - SSHPubKeys []string `json:"ssh_pub_keys" description:"the public ssh keys to access the machine with"` + SSHPubKeys []string `json:"ssh_pub_keys,omitempty" description:"the public ssh keys to access the machine with"` UserData string `json:"user_data,omitempty" description:"userdata to execute post installation tasks" optional:"true"` Succeeded bool `json:"succeeded" description:"if the allocation of the machine was successful, this is set to true"` Reinstall bool `json:"reinstall" description:"indicates whether to reinstall the machine"` @@ -64,9 +64,9 @@ type BootInfo struct { type MachineNetwork struct { NetworkID string `json:"networkid" description:"the networkID of the allocated machine in this vrf"` - Prefixes []string `json:"prefixes" description:"the prefixes of this network"` - IPs []string `json:"ips" description:"the ip addresses of the allocated machine in this vrf"` - DestinationPrefixes []string `json:"destinationprefixes" modelDescription:"prefixes that are reachable within this network" description:"the destination prefixes of this network"` + Prefixes []string `json:"prefixes,omitempty" description:"the prefixes of this network"` + IPs []string `json:"ips,omitempty" description:"the ip addresses of the allocated machine in this vrf"` + DestinationPrefixes []string `json:"destinationprefixes,omitempty" modelDescription:"prefixes that are reachable within this network" description:"the destination prefixes of this network"` NetworkType string `json:"networktype" description:"the network type, types can be looked up in the network package of metal-lib"` Vrf uint `json:"vrf" description:"the vrf of the allocated machine"` // Attention, uint32 is converted to integer by swagger which is int32 which is to small to hold a asn @@ -112,7 +112,7 @@ type MachineBlockDevice struct { type MachineRecentProvisioningEvents struct { Events []MachineProvisioningEvent `json:"log" description:"the log of recent machine provisioning events"` - LastEventTime *time.Time `json:"last_event_time" description:"the time where the last event was received" optional:"true"` + LastEventTime *time.Time `json:"last_event_time,omitempty" description:"the time where the last event was received" optional:"true"` LastErrorEvent *MachineProvisioningEvent `json:"last_error_event,omitempty" description:"the last erroneous event received" optional:"true"` CrashLoop bool `json:"crash_loop" description:"indicates that machine is provisioning crash loop"` FailedMachineReclaim bool `json:"failed_machine_reclaim" description:"indicates that machine reclaim has failed"` @@ -199,7 +199,7 @@ type MachineAllocateRequest struct { SizeID string `json:"sizeid" description:"the size id to assign this machine to"` ImageID string `json:"imageid" description:"the image id to assign this machine to"` FilesystemLayoutID *string `json:"filesystemlayoutid" description:"the filesystemlayout id to assing to this machine" optional:"true"` - SSHPubKeys []string `json:"ssh_pub_keys" description:"the public ssh keys to access the machine with"` + SSHPubKeys []string `json:"ssh_pub_keys,omitempty" description:"the public ssh keys to access the machine with"` UserData *string `json:"user_data" description:"cloud-init.io compatible userdata must be base64 encoded" optional:"true"` Tags []string `json:"tags" description:"tags for this machine" optional:"true"` Networks MachineAllocationNetworks `json:"networks" description:"the networks that this machine will be placed in." optional:"true"` diff --git a/cmd/metal-api/internal/testdata/testdata.go b/cmd/metal-api/internal/testdata/testdata.go index ae90776bc..64b037a41 100644 --- a/cmd/metal-api/internal/testdata/testdata.go +++ b/cmd/metal-api/internal/testdata/testdata.go @@ -26,6 +26,54 @@ import ( // (go tool cover -html=cover.out -o cover.html) // Html output var ( + // Firewalls + FW1 = metal.Machine{ + Base: metal.Base{ID: "1"}, + PartitionID: "1", + SizeID: "1", + Allocation: &metal.MachineAllocation{ + Name: "d1", + ImageID: "image-1", + Project: "p1", + Role: metal.RoleFirewall, + MachineNetworks: []*metal.MachineNetwork{ + { + Private: true, + Vrf: 1, + }, + }, + FirewallRules: &metal.FirewallRules{ + Egress: []metal.EgressRule{ + { + Protocol: metal.ProtocolTCP, + Ports: []int{443}, + To: []string{"0.0.0.0/0"}, + Comment: "test", + }, + }, + Ingress: []metal.IngressRule{}, + }, + }, + Hardware: metal.MachineHardware{ + CPUCores: 8, + Memory: 1 << 30, + Disks: []metal.BlockDevice{ + { + Size: 1000, + }, + { + Size: 1000, + }, + { + Size: 1000, + }, + }, + }, + + IPMI: IPMI1, + Tags: []string{"1"}, + } + // Machines M1 = metal.Machine{ Base: metal.Base{ID: "1"}, diff --git a/go.mod b/go.mod index ffcbf4330..6850a3283 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/emicklei/go-restful-openapi/v2 v2.9.1 github.com/emicklei/go-restful/v3 v3.11.3 github.com/go-openapi/spec v0.21.0 + github.com/go-openapi/validate v0.24.0 github.com/google/go-cmp v0.6.0 github.com/google/uuid v1.6.0 github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.0 @@ -77,11 +78,13 @@ require ( github.com/go-logr/logr v1.4.1 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.3.0 // indirect + github.com/go-openapi/analysis v0.23.0 // indirect github.com/go-openapi/errors v0.22.0 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect github.com/go-openapi/jsonreference v0.21.0 // indirect + github.com/go-openapi/loads v0.22.0 // indirect github.com/go-openapi/runtime v0.28.0 // indirect - github.com/go-openapi/strfmt v0.23.0 // indirect + github.com/go-openapi/strfmt v0.23.0 github.com/go-openapi/swag v0.23.0 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/gofrs/uuid/v5 v5.0.0 // indirect diff --git a/spec/metal-api.json b/spec/metal-api.json index c5ef503fa..5a60b5882 100644 --- a/spec/metal-api.json +++ b/spec/metal-api.json @@ -1105,8 +1105,7 @@ "imageid", "partitionid", "projectid", - "sizeid", - "ssh_pub_keys" + "sizeid" ] }, "v1.FirewallEgressRule": { @@ -2080,8 +2079,7 @@ "imageid", "partitionid", "projectid", - "sizeid", - "ssh_pub_keys" + "sizeid" ] }, "v1.MachineAllocation": { @@ -2181,7 +2179,6 @@ "project", "reinstall", "role", - "ssh_pub_keys", "succeeded" ] }, @@ -3153,12 +3150,9 @@ }, "required": [ "asn", - "destinationprefixes", - "ips", "nat", "networkid", "networktype", - "prefixes", "private", "underlay", "vrf" From 9fe3e6a5acf82027752eea83805f45620ec45e25 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Thu, 21 Mar 2024 12:11:59 +0100 Subject: [PATCH 2/7] Put replace section at the bottom. --- go.mod | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 6850a3283..bf60f5cd3 100644 --- a/go.mod +++ b/go.mod @@ -35,14 +35,6 @@ require ( gopkg.in/rethinkdb/rethinkdb-go.v6 v6.2.2 ) -replace ( - // netipx and x/exp must be replaced for tailscale < 1.48 - go4.org/netipx => go4.org/netipx v0.0.0-20230303233057-f1b76eb4bb35 - golang.org/x/exp => golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 - // tailscale 1.48 and headscale 0.22 are not compatible yet - tailscale.com => tailscale.com v1.44.0 -) - require ( connectrpc.com/connect v1.15.0 // indirect dario.cat/mergo v1.0.0 // indirect @@ -210,3 +202,11 @@ require ( nhooyr.io/websocket v1.8.10 // indirect tailscale.com v1.54.0 // indirect ) + +replace ( + // netipx and x/exp must be replaced for tailscale < 1.48 + go4.org/netipx => go4.org/netipx v0.0.0-20230303233057-f1b76eb4bb35 + golang.org/x/exp => golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 + // tailscale 1.48 and headscale 0.22 are not compatible yet + tailscale.com => tailscale.com v1.44.0 +) From 759a1e1e5ba36d1e9469f2492297c473a212020d Mon Sep 17 00:00:00 2001 From: Gerrit Date: Thu, 21 Mar 2024 12:42:58 +0100 Subject: [PATCH 3/7] One more test + spec fixes. --- .../internal/service/machine-service_test.go | 151 +++++++++++++++++- .../internal/service/v1/filesystem.go | 18 +-- cmd/metal-api/internal/service/v1/machine.go | 33 ++-- spec/metal-api.json | 29 +--- 4 files changed, 181 insertions(+), 50 deletions(-) diff --git a/cmd/metal-api/internal/service/machine-service_test.go b/cmd/metal-api/internal/service/machine-service_test.go index 55d856921..f052a0634 100644 --- a/cmd/metal-api/internal/service/machine-service_test.go +++ b/cmd/metal-api/internal/service/machine-service_test.go @@ -8,6 +8,7 @@ import ( "net/http" "net/http/httptest" "testing" + "time" restfulspec "github.com/emicklei/go-restful-openapi/v2" "github.com/emicklei/go-restful/v3" @@ -1356,6 +1357,146 @@ func TestNewMachineResponse(t *testing.T) { ec *metal.ProvisioningEventContainer want *v1.MachineResponse }{ + { + name: "validate empty values", + m: &metal.Machine{ + Base: metal.Base{ + ID: "", + Name: "", + Description: "", + Created: time.Time{}, + Changed: time.Time{}, + }, + Allocation: &metal.MachineAllocation{ + Creator: "", + Created: time.Time{}, + Name: "", + Description: "", + Project: "", + ImageID: "", + FilesystemLayout: &metal.FilesystemLayout{ + Base: metal.Base{ + ID: "", + Name: "", + Description: "", + Created: time.Time{}, + Changed: time.Time{}, + }, + Filesystems: []metal.Filesystem{}, + Disks: []metal.Disk{}, + Raid: []metal.Raid{}, + VolumeGroups: []metal.VolumeGroup{}, + LogicalVolumes: []metal.LogicalVolume{}, + Constraints: metal.FilesystemLayoutConstraints{}, + }, + MachineNetworks: []*metal.MachineNetwork{}, + Hostname: "", + SSHPubKeys: []string{}, + UserData: "", + ConsolePassword: "", + Succeeded: false, + Reinstall: false, + MachineSetup: &metal.MachineSetup{ + ImageID: "", + PrimaryDisk: "", + OSPartition: "", + Initrd: "", + Cmdline: "", + Kernel: "", + BootloaderID: "", + }, + Role: metal.RoleFirewall, + VPN: &metal.MachineVPN{ + ControlPlaneAddress: "", + AuthKey: "", + Connected: false, + }, + UUID: "", + FirewallRules: &metal.FirewallRules{ + Egress: []metal.EgressRule{}, + Ingress: []metal.IngressRule{}, + }, + }, + PartitionID: "", + SizeID: "", + RackID: "", + Waiting: false, + PreAllocated: false, + Hardware: metal.MachineHardware{ + Memory: 0, + CPUCores: 0, + Nics: []metal.Nic{}, + Disks: []metal.BlockDevice{}, + }, + State: metal.MachineState{ + Value: "", + Description: "", + Issuer: "", + MetalHammerVersion: "", + }, + LEDState: metal.ChassisIdentifyLEDState{ + Value: "", + Description: "", + }, + Tags: []string{}, + IPMI: metal.IPMI{ + Address: "", + MacAddress: "", + User: "", + Password: "", + Interface: "", + Fru: metal.Fru{ + ChassisPartNumber: "", + ChassisPartSerial: "", + BoardMfg: "", + BoardMfgSerial: "", + BoardPartNumber: "", + ProductManufacturer: "", + ProductPartNumber: "", + ProductSerial: "", + }, + BMCVersion: "", + PowerState: "", + PowerMetric: &metal.PowerMetric{ + AverageConsumedWatts: 0, + IntervalInMin: 0, + MaxConsumedWatts: 0, + MinConsumedWatts: 0, + }, + LastUpdated: time.Time{}, + }, + BIOS: metal.BIOS{ + Version: "", + Vendor: "", + Date: "", + }, + }, + want: &v1.MachineResponse{ + Common: v1.Common{ + Describable: v1.Describable{ + Name: pointer.Pointer(""), + Description: pointer.Pointer(""), + }, + }, + MachineBase: v1.MachineBase{ + Allocation: &v1.MachineAllocation{ + FilesystemLayout: &v1.FilesystemLayoutResponse{ + Common: v1.Common{ + Describable: v1.Describable{ + Name: pointer.Pointer(""), + Description: pointer.Pointer(""), + }, + }, + }, + BootInfo: &v1.BootInfo{}, + Role: "firewall", + VPN: &v1.MachineVPN{}, + FirewallRules: &v1.FirewallRules{}, + }, + Hardware: v1.MachineHardware{}, + }, + }, + }, { name: "test firewall response", m: &testdata.FW1, @@ -1393,7 +1534,6 @@ func TestNewMachineResponse(t *testing.T) { }, }, }, - Nics: v1.MachineNics{}, }, Allocation: &v1.MachineAllocation{ Name: testdata.FW1.Allocation.Name, @@ -1420,10 +1560,8 @@ func TestNewMachineResponse(t *testing.T) { Ingress: nil, }, }, - RecentProvisioningEvents: v1.MachineRecentProvisioningEvents{ - Events: []v1.MachineProvisioningEvent{}, - }, - Tags: testdata.FW1.Tags, + RecentProvisioningEvents: v1.MachineRecentProvisioningEvents{}, + Tags: testdata.FW1.Tags, }, Timestamps: v1.Timestamps{}, }, @@ -1459,10 +1597,11 @@ func validateAgainstSwaggerSpec(t *testing.T, ws *restful.WebService, definition err = json.Unmarshal(schemaJSON, schema) require.NoError(t, err) - // you need to pass the definition of the response to the validator otherwise it will not find any problems + // you need to pass the definition of the object to the validator otherwise it will not find any problems def, ok := schema.Definitions[definitionKey] require.True(t, ok) + // we now put the entire defintions in the specific definition such that references can be resolved def.Definitions = schema.Definitions err = validate.AgainstSchema(&def, obj, strfmt.Default, validate.EnableArrayMustHaveItemsCheck(true), validate.EnableObjectArrayTypeCheck(true)) diff --git a/cmd/metal-api/internal/service/v1/filesystem.go b/cmd/metal-api/internal/service/v1/filesystem.go index dd469f4dd..197f7d105 100644 --- a/cmd/metal-api/internal/service/v1/filesystem.go +++ b/cmd/metal-api/internal/service/v1/filesystem.go @@ -4,11 +4,11 @@ import "github.com/metal-stack/metal-api/cmd/metal-api/internal/metal" type ( FilesystemLayoutBase struct { - Filesystems []Filesystem `json:"filesystems" description:"list of filesystems to create" optional:"true"` - Disks []Disk `json:"disks" description:"list of disks that belong to this layout" optional:"true"` - Raid []Raid `json:"raid" description:"list of raid arrays to create" optional:"true"` - VolumeGroups []VolumeGroup `json:"volumegroups" description:"list of volumegroups to create" optional:"true"` - LogicalVolumes []LogicalVolume `json:"logicalvolumes" description:"list of logicalvolumes to create" optional:"true"` + Filesystems []Filesystem `json:"filesystems,omitempty" description:"list of filesystems to create" optional:"true"` + Disks []Disk `json:"disks,omitempty" description:"list of disks that belong to this layout" optional:"true"` + Raid []Raid `json:"raid,omitempty" description:"list of raid arrays to create" optional:"true"` + VolumeGroups []VolumeGroup `json:"volumegroups,omitempty" description:"list of volumegroups to create" optional:"true"` + LogicalVolumes []LogicalVolume `json:"logicalvolumes,omitempty" description:"list of logicalvolumes to create" optional:"true"` Constraints FilesystemLayoutConstraints `json:"constraints" description:"constraints which must match that this layout is taken, if sizes and images are empty these are develop layouts"` } FilesystemLayoutResponse struct { @@ -37,16 +37,16 @@ type ( } FilesystemLayoutConstraints struct { - Sizes []string `json:"sizes" description:"list of sizes this layout applies to" optional:"true"` - Images map[string]string `json:"images" description:"list of images this layout applies to"` + Sizes []string `json:"sizes,omitempty" description:"list of sizes this layout applies to" optional:"true"` + Images map[string]string `json:"images,omitempty" description:"list of images this layout applies to"` } Filesystem struct { Path *string `json:"path" description:"the mountpoint where this filesystem should be mounted on" optional:"true"` Device string `json:"device" description:"the underlaying device where this filesystem should be created"` Format string `json:"format" description:"the filesystem format"` Label *string `json:"label" description:"optional label for this this filesystem" optional:"true"` - MountOptions []string `json:"mountoptions" description:"the options to use to mount this filesystem" optional:"true"` - CreateOptions []string `json:"createoptions" description:"the options to use to create (mkfs) this filesystem" optional:"true"` + MountOptions []string `json:"mountoptions,omitempty" description:"the options to use to mount this filesystem" optional:"true"` + CreateOptions []string `json:"createoptions,omitempty" description:"the options to use to create (mkfs) this filesystem" optional:"true"` } Disk struct { Device string `json:"device" description:"the device to create the partitions"` diff --git a/cmd/metal-api/internal/service/v1/machine.go b/cmd/metal-api/internal/service/v1/machine.go index 8571ce84c..1d3fbf639 100644 --- a/cmd/metal-api/internal/service/v1/machine.go +++ b/cmd/metal-api/internal/service/v1/machine.go @@ -23,7 +23,7 @@ type MachineBase struct { LEDState ChassisIdentifyLEDState `json:"ledstate" rethinkdb:"ledstate" description:"the state of this chassis identify LED"` Liveliness string `json:"liveliness" description:"the liveliness of this machine"` RecentProvisioningEvents MachineRecentProvisioningEvents `json:"events" description:"recent events of this machine during provisioning"` - Tags []string `json:"tags" description:"tags for this machine"` + Tags []string `json:"tags,omitempty" description:"tags for this machine"` } type MachineAllocation struct { @@ -34,7 +34,7 @@ type MachineAllocation struct { Project string `json:"project" description:"the project id that this machine is assigned to" ` Image *ImageResponse `json:"image" description:"the image assigned to this machine" readOnly:"true" optional:"true"` FilesystemLayout *FilesystemLayoutResponse `json:"filesystemlayout" description:"filesystemlayout to create on this machine" optional:"true"` - MachineNetworks []MachineNetwork `json:"networks" description:"the networks of this machine"` + MachineNetworks []MachineNetwork `json:"networks,omitempty" description:"the networks of this machine"` Hostname string `json:"hostname" description:"the hostname which will be used when creating the machine"` SSHPubKeys []string `json:"ssh_pub_keys,omitempty" description:"the public ssh keys to access the machine with"` UserData string `json:"user_data,omitempty" description:"userdata to execute post installation tasks" optional:"true"` @@ -85,12 +85,12 @@ type MachineNetwork struct { type MachineHardwareBase struct { Memory uint64 `json:"memory" description:"the total memory of the machine"` CPUCores int `json:"cpu_cores" description:"the number of cpu cores"` - Disks []MachineBlockDevice `json:"disks" description:"the list of block devices of this machine"` + Disks []MachineBlockDevice `json:"disks,omitempty" description:"the list of block devices of this machine"` } type MachineHardware struct { MachineHardwareBase - Nics MachineNics `json:"nics" description:"the list of network interfaces of this machine"` + Nics MachineNics `json:"nics,omitempty" description:"the list of network interfaces of this machine"` } type MachineState struct { @@ -111,7 +111,7 @@ type MachineBlockDevice struct { } type MachineRecentProvisioningEvents struct { - Events []MachineProvisioningEvent `json:"log" description:"the log of recent machine provisioning events"` + Events []MachineProvisioningEvent `json:"log,omitempty" description:"the log of recent machine provisioning events"` LastEventTime *time.Time `json:"last_event_time,omitempty" description:"the time where the last event was received" optional:"true"` LastErrorEvent *MachineProvisioningEvent `json:"last_error_event,omitempty" description:"the last erroneous event received" optional:"true"` CrashLoop bool `json:"crash_loop" description:"indicates that machine is provisioning crash loop"` @@ -137,7 +137,7 @@ type MachineNic struct { MacAddress string `json:"mac" description:"the mac address of this network interface"` Name string `json:"name" description:"the name of this network interface"` Identifier string `json:"identifier" description:"the unique identifier of this network interface"` - Neighbors MachineNics `json:"neighbors" description:"the neighbors visible to this network interface"` + Neighbors MachineNics `json:"neighbors,omitempty" description:"the neighbors visible to this network interface"` } type MachineBIOS struct { @@ -308,7 +308,7 @@ type MachineIssue struct { } func NewMetalMachineHardware(r *MachineHardware) metal.MachineHardware { - nics := metal.Nics{} + var nics metal.Nics for i := range r.Nics { var neighbors metal.Nics for i2 := range r.Nics[i].Neighbors { @@ -453,17 +453,17 @@ func NewMachineIPMIResponse(m *metal.Machine, s *metal.Size, p *metal.Partition, func NewMachineResponse(m *metal.Machine, s *metal.Size, p *metal.Partition, i *metal.Image, ec *metal.ProvisioningEventContainer) *MachineResponse { var hardware MachineHardware - nics := MachineNics{} + var nics MachineNics for i := range m.Hardware.Nics { n := m.Hardware.Nics[i] - neighs := MachineNics{} + var neighs MachineNics for j := range n.Neighbors { neigh := n.Neighbors[j] neighs = append(neighs, MachineNic{ MacAddress: string(neigh.MacAddress), Name: neigh.Name, Identifier: neigh.Identifier, - Neighbors: MachineNics{}, + Neighbors: nil, }) } nic := MachineNic{ @@ -475,7 +475,7 @@ func NewMachineResponse(m *metal.Machine, s *metal.Size, p *metal.Partition, i * nics = append(nics, nic) } - disks := []MachineBlockDevice{} + var disks []MachineBlockDevice for i := range m.Hardware.Disks { disk := MachineBlockDevice{ Name: m.Hardware.Disks[i].Name, @@ -552,6 +552,11 @@ func NewMachineResponse(m *metal.Machine, s *metal.Size, p *metal.Partition, i * } } + var sshPubKeys []string + if len(m.Allocation.SSHPubKeys) > 0 { + sshPubKeys = m.Allocation.SSHPubKeys + } + allocation = &MachineAllocation{ Creator: m.Allocation.Creator, Created: m.Allocation.Created, @@ -560,7 +565,7 @@ func NewMachineResponse(m *metal.Machine, s *metal.Size, p *metal.Partition, i * Image: NewImageResponse(i), Project: m.Allocation.Project, Hostname: m.Allocation.Hostname, - SSHPubKeys: m.Allocation.SSHPubKeys, + SSHPubKeys: sshPubKeys, UserData: m.Allocation.UserData, MachineNetworks: networks, Succeeded: m.Allocation.Succeeded, @@ -585,7 +590,7 @@ func NewMachineResponse(m *metal.Machine, s *metal.Size, p *metal.Partition, i * } } - tags := []string{} + var tags []string if len(m.Tags) > 0 { tags = m.Tags } @@ -637,7 +642,7 @@ func NewMachineResponse(m *metal.Machine, s *metal.Size, p *metal.Partition, i * } func NewMachineRecentProvisioningEvents(ec *metal.ProvisioningEventContainer) *MachineRecentProvisioningEvents { - es := []MachineProvisioningEvent{} + var es []MachineProvisioningEvent if ec == nil { return &MachineRecentProvisioningEvents{ Events: es, diff --git a/spec/metal-api.json b/spec/metal-api.json index 5a60b5882..2a72c2a37 100644 --- a/spec/metal-api.json +++ b/spec/metal-api.json @@ -797,10 +797,7 @@ }, "type": "array" } - }, - "required": [ - "images" - ] + } }, "v1.FilesystemLayoutCreateRequest": { "properties": { @@ -1451,8 +1448,7 @@ "id", "ledstate", "liveliness", - "state", - "tags" + "state" ] }, "v1.FirewallRules": { @@ -2175,7 +2171,6 @@ "creator", "hostname", "name", - "networks", "project", "reinstall", "role", @@ -2280,8 +2275,7 @@ "hardware", "ledstate", "liveliness", - "state", - "tags" + "state" ] }, "v1.MachineBlockDevice": { @@ -2589,9 +2583,7 @@ }, "required": [ "cpu_cores", - "disks", - "memory", - "nics" + "memory" ] }, "v1.MachineHardwareBase": { @@ -2616,7 +2608,6 @@ }, "required": [ "cpu_cores", - "disks", "memory" ] }, @@ -2757,8 +2748,7 @@ "ipmi", "ledstate", "liveliness", - "state", - "tags" + "state" ] }, "v1.MachineIpmiReport": { @@ -3183,8 +3173,7 @@ "required": [ "identifier", "mac", - "name", - "neighbors" + "name" ] }, "v1.MachineProvisioningEvent": { @@ -3237,8 +3226,7 @@ }, "required": [ "crash_loop", - "failed_machine_reclaim", - "log" + "failed_machine_reclaim" ] }, "v1.MachineReinstallRequest": { @@ -3349,8 +3337,7 @@ "id", "ledstate", "liveliness", - "state", - "tags" + "state" ] }, "v1.MachineState": { From eb2d8605a1fdc27f28652ea587a59e6fd4da8378 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Thu, 21 Mar 2024 12:53:43 +0100 Subject: [PATCH 4/7] One more test. --- .../internal/service/machine-service_test.go | 157 +++++++++++++++++- .../internal/service/v1/filesystem.go | 14 +- cmd/metal-api/internal/service/v1/firewall.go | 8 +- cmd/metal-api/internal/service/v1/machine.go | 2 +- spec/metal-api.json | 12 +- 5 files changed, 169 insertions(+), 24 deletions(-) diff --git a/cmd/metal-api/internal/service/machine-service_test.go b/cmd/metal-api/internal/service/machine-service_test.go index f052a0634..39bb72c07 100644 --- a/cmd/metal-api/internal/service/machine-service_test.go +++ b/cmd/metal-api/internal/service/machine-service_test.go @@ -1358,7 +1358,7 @@ func TestNewMachineResponse(t *testing.T) { want *v1.MachineResponse }{ { - name: "validate empty values", + name: "test zero values", m: &metal.Machine{ Base: metal.Base{ ID: "", @@ -1497,6 +1497,160 @@ func TestNewMachineResponse(t *testing.T) { }, }, }, + { + name: "test zero values, slices expanded", + m: &metal.Machine{ + Base: metal.Base{ + ID: "", + Name: "", + Description: "", + Created: time.Time{}, + Changed: time.Time{}, + }, + Allocation: &metal.MachineAllocation{ + Creator: "", + Created: time.Time{}, + Name: "", + Description: "", + Project: "", + ImageID: "", + FilesystemLayout: &metal.FilesystemLayout{ + Base: metal.Base{ + ID: "", + Name: "", + Description: "", + Created: time.Time{}, + Changed: time.Time{}, + }, + Filesystems: []metal.Filesystem{{}}, + Disks: []metal.Disk{{}}, + Raid: []metal.Raid{{}}, + VolumeGroups: []metal.VolumeGroup{{}}, + LogicalVolumes: []metal.LogicalVolume{{}}, + Constraints: metal.FilesystemLayoutConstraints{}, + }, + MachineNetworks: []*metal.MachineNetwork{{}}, + Hostname: "", + SSHPubKeys: []string{}, + UserData: "", + ConsolePassword: "", + Succeeded: false, + Reinstall: false, + MachineSetup: &metal.MachineSetup{ + ImageID: "", + PrimaryDisk: "", + OSPartition: "", + Initrd: "", + Cmdline: "", + Kernel: "", + BootloaderID: "", + }, + Role: metal.RoleFirewall, + VPN: &metal.MachineVPN{ + ControlPlaneAddress: "", + AuthKey: "", + Connected: false, + }, + UUID: "", + FirewallRules: &metal.FirewallRules{ + Egress: []metal.EgressRule{{}}, + Ingress: []metal.IngressRule{{}}, + }, + }, + PartitionID: "", + SizeID: "", + RackID: "", + Waiting: false, + PreAllocated: false, + Hardware: metal.MachineHardware{ + Memory: 0, + CPUCores: 0, + Nics: []metal.Nic{{}}, + Disks: []metal.BlockDevice{{}}, + }, + State: metal.MachineState{ + Value: "", + Description: "", + Issuer: "", + MetalHammerVersion: "", + }, + LEDState: metal.ChassisIdentifyLEDState{ + Value: "", + Description: "", + }, + Tags: []string{}, + IPMI: metal.IPMI{ + Address: "", + MacAddress: "", + User: "", + Password: "", + Interface: "", + Fru: metal.Fru{ + ChassisPartNumber: "", + ChassisPartSerial: "", + BoardMfg: "", + BoardMfgSerial: "", + BoardPartNumber: "", + ProductManufacturer: "", + ProductPartNumber: "", + ProductSerial: "", + }, + BMCVersion: "", + PowerState: "", + PowerMetric: &metal.PowerMetric{ + AverageConsumedWatts: 0, + IntervalInMin: 0, + MaxConsumedWatts: 0, + MinConsumedWatts: 0, + }, + LastUpdated: time.Time{}, + }, + BIOS: metal.BIOS{ + Version: "", + Vendor: "", + Date: "", + }, + }, + want: &v1.MachineResponse{ + Common: v1.Common{ + Describable: v1.Describable{ + Name: pointer.Pointer(""), + Description: pointer.Pointer(""), + }, + }, + MachineBase: v1.MachineBase{ + Allocation: &v1.MachineAllocation{ + FilesystemLayout: &v1.FilesystemLayoutResponse{ + Common: v1.Common{Describable: v1.Describable{Name: pointer.Pointer(""), Description: pointer.Pointer("")}}, + FilesystemLayoutBase: v1.FilesystemLayoutBase{ + Filesystems: []v1.Filesystem{{}}, + Disks: []v1.Disk{{}}, + Raid: []v1.Raid{{}}, + VolumeGroups: []v1.VolumeGroup{{}}, + LogicalVolumes: []v1.LogicalVolume{{}}, + Constraints: v1.FilesystemLayoutConstraints{}, + }, + }, + BootInfo: &v1.BootInfo{}, + Role: "firewall", + VPN: &v1.MachineVPN{}, + FirewallRules: &v1.FirewallRules{ + Egress: []v1.FirewallEgressRule{{}}, + Ingress: []v1.FirewallIngressRule{{}}, + }, + MachineNetworks: []v1.MachineNetwork{{ + NetworkType: "external", + }}, + }, + Hardware: v1.MachineHardware{ + MachineHardwareBase: v1.MachineHardwareBase{ + Disks: []v1.MachineBlockDevice{{}}, + }, + Nics: v1.MachineNics{{}}, + }, + }, + }, + }, { name: "test firewall response", m: &testdata.FW1, @@ -1541,7 +1695,6 @@ func TestNewMachineResponse(t *testing.T) { Image: v1.NewImageResponse(&testdata.Img1), MachineNetworks: []v1.MachineNetwork{ { - IPs: []string{}, NetworkType: "privateprimaryunshared", Vrf: 1, Private: true, diff --git a/cmd/metal-api/internal/service/v1/filesystem.go b/cmd/metal-api/internal/service/v1/filesystem.go index 197f7d105..23bbee0d0 100644 --- a/cmd/metal-api/internal/service/v1/filesystem.go +++ b/cmd/metal-api/internal/service/v1/filesystem.go @@ -41,23 +41,23 @@ type ( Images map[string]string `json:"images,omitempty" description:"list of images this layout applies to"` } Filesystem struct { - Path *string `json:"path" description:"the mountpoint where this filesystem should be mounted on" optional:"true"` + Path *string `json:"path,omitempty" description:"the mountpoint where this filesystem should be mounted on" optional:"true"` Device string `json:"device" description:"the underlaying device where this filesystem should be created"` Format string `json:"format" description:"the filesystem format"` - Label *string `json:"label" description:"optional label for this this filesystem" optional:"true"` + Label *string `json:"label,omitempty" description:"optional label for this this filesystem" optional:"true"` MountOptions []string `json:"mountoptions,omitempty" description:"the options to use to mount this filesystem" optional:"true"` CreateOptions []string `json:"createoptions,omitempty" description:"the options to use to create (mkfs) this filesystem" optional:"true"` } Disk struct { Device string `json:"device" description:"the device to create the partitions"` - Partitions []DiskPartition `json:"partitions" description:"list of partitions to create on this disk" optional:"true"` + Partitions []DiskPartition `json:"partitions,omitempty" description:"list of partitions to create on this disk" optional:"true"` WipeOnReinstall bool `json:"wipeonreinstall" description:"if set to true, this disk will be wiped before reinstallation"` } Raid struct { ArrayName string `json:"arrayname" description:"the name of the resulting array device"` - Devices []string `json:"devices" description:"list of devices to form the raid array from" optional:"true"` + Devices []string `json:"devices,omitempty" description:"list of devices to form the raid array from" optional:"true"` Level string `json:"level" description:"raid level to create, should be 0 or 1"` - CreateOptions []string `json:"createoptions" description:"the options to use to create the raid array" optional:"true"` + CreateOptions []string `json:"createoptions,omitempty" description:"the options to use to create the raid array" optional:"true"` Spares int `json:"spares" description:"number of spares for the raid array"` } DiskPartition struct { @@ -68,8 +68,8 @@ type ( } VolumeGroup struct { Name string `json:"name" description:"the name of the resulting volume group"` - Devices []string `json:"devices" description:"list of devices to form the volume group from" optional:"true"` - Tags []string `json:"tags" description:"list of tags to add to the volume group" optional:"true"` + Devices []string `json:"devices,omitempty" description:"list of devices to form the volume group from" optional:"true"` + Tags []string `json:"tags,omitempty" description:"list of tags to add to the volume group" optional:"true"` } LogicalVolume struct { diff --git a/cmd/metal-api/internal/service/v1/firewall.go b/cmd/metal-api/internal/service/v1/firewall.go index 727d6e1dc..0d12cd145 100644 --- a/cmd/metal-api/internal/service/v1/firewall.go +++ b/cmd/metal-api/internal/service/v1/firewall.go @@ -11,16 +11,16 @@ type FirewallAllocateRequest struct { type FirewallEgressRule struct { Protocol string `json:"protocol,omitempty" description:"the protocol for the rule, defaults to tcp" enum:"tcp|udp" optional:"true"` - Ports []int `json:"ports" description:"the ports affected by this rule"` - To []string `json:"to" description:"the cidrs affected by this rule"` + Ports []int `json:"ports,omitempty" description:"the ports affected by this rule"` + To []string `json:"to,omitempty" description:"the cidrs affected by this rule"` Comment string `json:"comment,omitempty" description:"an optional comment describing what this rule is used for" optional:"true"` } type FirewallIngressRule struct { Protocol string `json:"protocol,omitempty" description:"the protocol for the rule, defaults to tcp" enum:"tcp|udp" optional:"true"` - Ports []int `json:"ports" description:"the ports affected by this rule"` + Ports []int `json:"ports,omitempty" description:"the ports affected by this rule"` To []string `json:"to,omitempty" description:"the cidrs affected by this rule" optional:"true"` - From []string `json:"from" description:"the cidrs affected by this rule"` + From []string `json:"from,omitempty" description:"the cidrs affected by this rule"` Comment string `json:"comment,omitempty" description:"an optional comment describing what this rule is used for" optional:"true"` } diff --git a/cmd/metal-api/internal/service/v1/machine.go b/cmd/metal-api/internal/service/v1/machine.go index 1d3fbf639..37842e38e 100644 --- a/cmd/metal-api/internal/service/v1/machine.go +++ b/cmd/metal-api/internal/service/v1/machine.go @@ -497,7 +497,7 @@ func NewMachineResponse(m *metal.Machine, s *metal.Size, p *metal.Partition, i * if m.Allocation != nil { var networks []MachineNetwork for _, nw := range m.Allocation.MachineNetworks { - ips := append([]string{}, nw.IPs...) + ips := nw.IPs nt, err := nw.NetworkType() if err != nil { continue diff --git a/spec/metal-api.json b/spec/metal-api.json index 2a72c2a37..7609d99be 100644 --- a/spec/metal-api.json +++ b/spec/metal-api.json @@ -1134,11 +1134,7 @@ }, "type": "array" } - }, - "required": [ - "ports", - "to" - ] + } }, "v1.FirewallFindRequest": { "properties": { @@ -1358,11 +1354,7 @@ }, "type": "array" } - }, - "required": [ - "from", - "ports" - ] + } }, "v1.FirewallResponse": { "properties": { From ff9e6f7223522a9d9df44515747a3f1a842da2b2 Mon Sep 17 00:00:00 2001 From: Gerrit91 Date: Thu, 21 Mar 2024 12:58:18 +0100 Subject: [PATCH 5/7] Review comment. --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index bf60f5cd3..88434495c 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/emicklei/go-restful-openapi/v2 v2.9.1 github.com/emicklei/go-restful/v3 v3.11.3 github.com/go-openapi/spec v0.21.0 + github.com/go-openapi/strfmt v0.23.0 github.com/go-openapi/validate v0.24.0 github.com/google/go-cmp v0.6.0 github.com/google/uuid v1.6.0 @@ -76,7 +77,6 @@ require ( github.com/go-openapi/jsonreference v0.21.0 // indirect github.com/go-openapi/loads v0.22.0 // indirect github.com/go-openapi/runtime v0.28.0 // indirect - github.com/go-openapi/strfmt v0.23.0 github.com/go-openapi/swag v0.23.0 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/gofrs/uuid/v5 v5.0.0 // indirect From ff619d8f9232d93eb81f87ae4ee69d55a0e438c5 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Thu, 21 Mar 2024 13:20:54 +0100 Subject: [PATCH 6/7] Make slices omitempty. --- cmd/metal-api/internal/service/ip-service_test.go | 1 - cmd/metal-api/internal/service/v1/image.go | 2 +- cmd/metal-api/internal/service/v1/ip.go | 2 +- cmd/metal-api/internal/service/v1/partition.go | 4 ++-- cmd/metal-api/internal/service/v1/size.go | 2 +- cmd/metal-api/internal/service/v1/switch.go | 4 ++-- spec/metal-api.json | 8 +------- 7 files changed, 8 insertions(+), 15 deletions(-) diff --git a/cmd/metal-api/internal/service/ip-service_test.go b/cmd/metal-api/internal/service/ip-service_test.go index 9b0b9c7c4..c165f5163 100644 --- a/cmd/metal-api/internal/service/ip-service_test.go +++ b/cmd/metal-api/internal/service/ip-service_test.go @@ -330,7 +330,6 @@ func TestUpdateIP(t *testing.T) { wantedIPBase: &v1.IPBase{ ProjectID: testdata.IP1.ProjectID, Type: "static", - Tags: []string{}, }, }, { diff --git a/cmd/metal-api/internal/service/v1/image.go b/cmd/metal-api/internal/service/v1/image.go index bb54d204f..05755c372 100644 --- a/cmd/metal-api/internal/service/v1/image.go +++ b/cmd/metal-api/internal/service/v1/image.go @@ -9,7 +9,7 @@ import ( type ImageBase struct { URL *string `json:"url" modelDescription:"an image that can be attached to a machine" description:"the url of this image" optional:"true"` - Features []string `json:"features" description:"features of this image" optional:"true"` + Features []string `json:"features,omitempty" description:"features of this image" optional:"true"` ExpirationDate time.Time `json:"expirationDate" description:"expirationDate of this image" optional:"false"` Classification string `json:"classification" description:"classification of this image" optional:"true"` UsedBy []string `json:"usedby,omitempty" description:"machines where this image is in use" optional:"true"` diff --git a/cmd/metal-api/internal/service/v1/ip.go b/cmd/metal-api/internal/service/v1/ip.go index a09292670..c3d293658 100644 --- a/cmd/metal-api/internal/service/v1/ip.go +++ b/cmd/metal-api/internal/service/v1/ip.go @@ -9,7 +9,7 @@ type IPBase struct { ProjectID string `json:"projectid" description:"the project this ip address belongs to"` NetworkID string `json:"networkid" description:"the network this ip allocate request address belongs to"` Type metal.IPType `json:"type" enum:"static|ephemeral" description:"the ip type, ephemeral leads to automatic cleanup of the ip address, static will enable re-use of the ip at a later point in time"` - Tags []string `json:"tags" description:"free tags that you associate with this ip." optional:"true"` + Tags []string `json:"tags,omitempty" description:"free tags that you associate with this ip." optional:"true"` } type IPIdentifiable struct { diff --git a/cmd/metal-api/internal/service/v1/partition.go b/cmd/metal-api/internal/service/v1/partition.go index beed5d2fc..b1ae03a78 100644 --- a/cmd/metal-api/internal/service/v1/partition.go +++ b/cmd/metal-api/internal/service/v1/partition.go @@ -57,9 +57,9 @@ type ServerCapacity struct { Reservations int `json:"reservations" description:"the amount of reservations for this size"` UsedReservations int `json:"usedreservations" description:"the amount of used reservations for this size"` Faulty int `json:"faulty" description:"servers with issues with this size"` - FaultyMachines []string `json:"faultymachines" description:"servers with issues with this size"` + FaultyMachines []string `json:"faultymachines,omitempty" description:"servers with issues with this size"` Other int `json:"other" description:"servers neither free, allocated or faulty with this size"` - OtherMachines []string `json:"othermachines" description:"servers neither free, allocated or faulty with this size"` + OtherMachines []string `json:"othermachines,omitempty" description:"servers neither free, allocated or faulty with this size"` } func NewPartitionResponse(p *metal.Partition) *PartitionResponse { diff --git a/cmd/metal-api/internal/service/v1/size.go b/cmd/metal-api/internal/service/v1/size.go index 8fe747e1f..679a4451a 100644 --- a/cmd/metal-api/internal/service/v1/size.go +++ b/cmd/metal-api/internal/service/v1/size.go @@ -14,7 +14,7 @@ type SizeReservation struct { Amount int `json:"amount" description:"the amount of reserved machine allocations for this size"` Description string `json:"description,omitempty" description:"a description for this reservation"` ProjectID string `json:"projectid" description:"the project for which this size reservation is considered"` - PartitionIDs []string `json:"partitionids" description:"the partitions in which this size reservation is considered, the amount is valid for every partition"` + PartitionIDs []string `json:"partitionids,omitempty" description:"the partitions in which this size reservation is considered, the amount is valid for every partition"` } type SizeCreateRequest struct { diff --git a/cmd/metal-api/internal/service/v1/switch.go b/cmd/metal-api/internal/service/v1/switch.go index 7fad81462..4f24a86b6 100644 --- a/cmd/metal-api/internal/service/v1/switch.go +++ b/cmd/metal-api/internal/service/v1/switch.go @@ -34,8 +34,8 @@ type SwitchNic struct { } type BGPFilter struct { - CIDRs []string `json:"cidrs" description:"the cidr addresses that are allowed to be announced at this switch port"` - VNIs []string `json:"vnis" description:"the virtual networks that are exposed at this switch port" optional:"true"` + CIDRs []string `json:"cidrs,omitempty" description:"the cidr addresses that are allowed to be announced at this switch port"` + VNIs []string `json:"vnis,omitempty" description:"the virtual networks that are exposed at this switch port" optional:"true"` } func NewBGPFilter(vnis, cidrs []string) BGPFilter { diff --git a/spec/metal-api.json b/spec/metal-api.json index 7609d99be..26a9b4e0f 100644 --- a/spec/metal-api.json +++ b/spec/metal-api.json @@ -537,10 +537,7 @@ }, "type": "array" } - }, - "required": [ - "cidrs" - ] + } }, "v1.BoardRevisions": { "properties": { @@ -4357,10 +4354,8 @@ "required": [ "allocated", "faulty", - "faultymachines", "free", "other", - "othermachines", "reservations", "size", "total", @@ -4606,7 +4601,6 @@ }, "required": [ "amount", - "partitionids", "projectid" ] }, From 25c41cda877ec0218c9f359e5f89285e8bf5c42e Mon Sep 17 00:00:00 2001 From: Gerrit Date: Tue, 26 Mar 2024 16:25:09 +0100 Subject: [PATCH 7/7] Revert from changes in switch entity. --- cmd/metal-api/internal/service/v1/switch.go | 4 ++-- spec/metal-api.json | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/cmd/metal-api/internal/service/v1/switch.go b/cmd/metal-api/internal/service/v1/switch.go index 4f24a86b6..7fad81462 100644 --- a/cmd/metal-api/internal/service/v1/switch.go +++ b/cmd/metal-api/internal/service/v1/switch.go @@ -34,8 +34,8 @@ type SwitchNic struct { } type BGPFilter struct { - CIDRs []string `json:"cidrs,omitempty" description:"the cidr addresses that are allowed to be announced at this switch port"` - VNIs []string `json:"vnis,omitempty" description:"the virtual networks that are exposed at this switch port" optional:"true"` + CIDRs []string `json:"cidrs" description:"the cidr addresses that are allowed to be announced at this switch port"` + VNIs []string `json:"vnis" description:"the virtual networks that are exposed at this switch port" optional:"true"` } func NewBGPFilter(vnis, cidrs []string) BGPFilter { diff --git a/spec/metal-api.json b/spec/metal-api.json index 26a9b4e0f..19469fc24 100644 --- a/spec/metal-api.json +++ b/spec/metal-api.json @@ -537,7 +537,10 @@ }, "type": "array" } - } + }, + "required": [ + "cidrs" + ] }, "v1.BoardRevisions": { "properties": {