From f0816dabcf998ed5209cb09b2dc82aae11a6aa55 Mon Sep 17 00:00:00 2001 From: iljarotar Date: Mon, 11 Nov 2024 09:21:34 +0100 Subject: [PATCH 1/3] Delete Switch status along with switch --- cmd/metal-api/internal/datastore/switch.go | 5 +++++ cmd/metal-api/internal/service/switch-service.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/cmd/metal-api/internal/datastore/switch.go b/cmd/metal-api/internal/datastore/switch.go index 1f8b4e34..d5a48b15 100644 --- a/cmd/metal-api/internal/datastore/switch.go +++ b/cmd/metal-api/internal/datastore/switch.go @@ -223,3 +223,8 @@ func (rs *RethinkStore) GetSwitchStatus(id string) (*metal.SwitchStatus, error) func (rs *RethinkStore) SetSwitchStatus(state *metal.SwitchStatus) error { return rs.upsertEntity(rs.switchStatusTable(), state) } + +// DeleteSwitchStatus delete SwitchStatus +func (rs *RethinkStore) DeleteSwitchStatus(status *metal.SwitchStatus) error { + return rs.deleteEntity(rs.switchStatusTable(), status) +} diff --git a/cmd/metal-api/internal/service/switch-service.go b/cmd/metal-api/internal/service/switch-service.go index 2ef392ce..ac48d5d2 100644 --- a/cmd/metal-api/internal/service/switch-service.go +++ b/cmd/metal-api/internal/service/switch-service.go @@ -232,6 +232,18 @@ func (r *switchResource) deleteSwitch(request *restful.Request, response *restfu return } + status, err := r.ds.GetSwitchStatus(id) + if err != nil { + r.sendError(request, response, defaultError(err)) + return + } + + err = r.ds.DeleteSwitchStatus(status) + if err != nil { + r.sendError(request, response, defaultError(err)) + return + } + resp, err := r.makeSwitchResponse(s) if err != nil { r.sendError(request, response, defaultError(err)) From d8130ccf9027872a8abe73b2c7c7783414132dd1 Mon Sep 17 00:00:00 2001 From: iljarotar Date: Mon, 11 Nov 2024 09:39:55 +0100 Subject: [PATCH 2/3] check if error is not found error --- cmd/metal-api/internal/service/switch-service.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/metal-api/internal/service/switch-service.go b/cmd/metal-api/internal/service/switch-service.go index ac48d5d2..88a9d433 100644 --- a/cmd/metal-api/internal/service/switch-service.go +++ b/cmd/metal-api/internal/service/switch-service.go @@ -233,13 +233,13 @@ func (r *switchResource) deleteSwitch(request *restful.Request, response *restfu } status, err := r.ds.GetSwitchStatus(id) - if err != nil { - r.sendError(request, response, defaultError(err)) - return - } - - err = r.ds.DeleteSwitchStatus(status) - if err != nil { + if err == nil { + err = r.ds.DeleteSwitchStatus(status) + if err != nil { + r.sendError(request, response, defaultError(err)) + return + } + } else if !metal.IsNotFound(err) { r.sendError(request, response, defaultError(err)) return } From 5a43158e225d781f2ba41c087467d781ddd949b0 Mon Sep 17 00:00:00 2001 From: iljarotar Date: Mon, 11 Nov 2024 09:48:18 +0100 Subject: [PATCH 3/3] linter --- cmd/metal-api/internal/eventbus/nsq.go | 8 ++++---- cmd/metal-api/internal/grpc/grpc-server.go | 2 +- cmd/metal-api/internal/metal/filesystem.go | 2 +- cmd/metal-api/internal/metal/size_reservation.go | 4 ++-- cmd/metal-api/internal/service/audit-service.go | 2 +- cmd/metal-api/internal/service/firewall-service.go | 2 +- cmd/metal-api/internal/service/image-service.go | 2 +- cmd/metal-api/internal/service/image-service_test.go | 3 +-- cmd/metal-api/internal/service/machine-service.go | 4 ++-- 9 files changed, 14 insertions(+), 15 deletions(-) diff --git a/cmd/metal-api/internal/eventbus/nsq.go b/cmd/metal-api/internal/eventbus/nsq.go index 90e97255..5eb3502c 100644 --- a/cmd/metal-api/internal/eventbus/nsq.go +++ b/cmd/metal-api/internal/eventbus/nsq.go @@ -59,7 +59,7 @@ func (n *NSQClient) CreateEndpoints(lookupds ...string) error { } // WaitForTopicsCreated blocks until the topices are created within the given partitions. -func (n NSQClient) WaitForTopicsCreated(partitions metal.Partitions, topics []metal.NSQTopic) { +func (n *NSQClient) WaitForTopicsCreated(partitions metal.Partitions, topics []metal.NSQTopic) { for { if err := n.createTopics(partitions, topics); err != nil { n.logger.Error("cannot create topics", "error", err) @@ -71,7 +71,7 @@ func (n NSQClient) WaitForTopicsCreated(partitions metal.Partitions, topics []me } // CreateTopic creates a topic with given name. -func (n NSQClient) CreateTopic(name string) error { +func (n *NSQClient) CreateTopic(name string) error { if err := n.Publisher.CreateTopic(name); err != nil { n.logger.Error("cannot create topic", "topic", name) return err @@ -80,7 +80,7 @@ func (n NSQClient) CreateTopic(name string) error { return nil } -func (n NSQClient) createTopics(partitions metal.Partitions, topics []metal.NSQTopic) error { +func (n *NSQClient) createTopics(partitions metal.Partitions, topics []metal.NSQTopic) error { for _, topic := range topics { if topic.PartitionAgnostic { continue @@ -106,6 +106,6 @@ func (n NSQClient) createTopics(partitions metal.Partitions, topics []metal.NSQT return nil } -func (n NSQClient) delay() { +func (n *NSQClient) delay() { time.Sleep(nsqdRetryDelay) } diff --git a/cmd/metal-api/internal/grpc/grpc-server.go b/cmd/metal-api/internal/grpc/grpc-server.go index bb865ed3..c7213a10 100644 --- a/cmd/metal-api/internal/grpc/grpc-server.go +++ b/cmd/metal-api/internal/grpc/grpc-server.go @@ -106,7 +106,7 @@ func Run(cfg *ServerConfig) error { }) grpcPanicRecoveryHandler := func(p any) (err error) { panicsTotal.Inc() - log.Error("msg", "recovered from panic", "panic", p, "stack", debug.Stack()) + log.Error("recovered from panic", "panic", p, "stack", debug.Stack()) return status.Errorf(codes.Internal, "%s", p) } diff --git a/cmd/metal-api/internal/metal/filesystem.go b/cmd/metal-api/internal/metal/filesystem.go index 0934c5d7..1fee69c8 100644 --- a/cmd/metal-api/internal/metal/filesystem.go +++ b/cmd/metal-api/internal/metal/filesystem.go @@ -169,7 +169,7 @@ type ( ) // Validate a existing FilesystemLayout -func (f FilesystemLayout) Validate() error { +func (f *FilesystemLayout) Validate() error { // check device existence from disk.partition -> raid.device -> filesystem // collect all provided devices providedDevices := make(map[string]bool) diff --git a/cmd/metal-api/internal/metal/size_reservation.go b/cmd/metal-api/internal/metal/size_reservation.go index 8af03b35..df130b6a 100644 --- a/cmd/metal-api/internal/metal/size_reservation.go +++ b/cmd/metal-api/internal/metal/size_reservation.go @@ -19,9 +19,9 @@ type SizeReservation struct { type SizeReservations []SizeReservation -func (rs SizeReservations) BySize() map[string]SizeReservations { +func (rs *SizeReservations) BySize() map[string]SizeReservations { res := map[string]SizeReservations{} - for _, rv := range rs { + for _, rv := range *rs { res[rv.SizeID] = append(res[rv.SizeID], rv) } return res diff --git a/cmd/metal-api/internal/service/audit-service.go b/cmd/metal-api/internal/service/audit-service.go index 2532b6ab..87bc0212 100644 --- a/cmd/metal-api/internal/service/audit-service.go +++ b/cmd/metal-api/internal/service/audit-service.go @@ -28,7 +28,7 @@ func NewAudit(log *slog.Logger, a auditing.Auditing) *restful.WebService { return ir.webService() } -func (r auditResource) webService() *restful.WebService { +func (r *auditResource) webService() *restful.WebService { ws := new(restful.WebService) ws. Path(BasePath + "v1/audit"). diff --git a/cmd/metal-api/internal/service/firewall-service.go b/cmd/metal-api/internal/service/firewall-service.go index 50b8d06c..4d9620ad 100644 --- a/cmd/metal-api/internal/service/firewall-service.go +++ b/cmd/metal-api/internal/service/firewall-service.go @@ -231,7 +231,7 @@ func (r *firewallResource) allocateFirewall(request *restful.Request, response * r.send(request, response, http.StatusOK, resp) } -func (r firewallResource) setVPNConfigInSpec(ctx context.Context, allocationSpec *machineAllocationSpec) error { +func (r *firewallResource) setVPNConfigInSpec(ctx context.Context, allocationSpec *machineAllocationSpec) error { if r.headscaleClient == nil { return nil } diff --git a/cmd/metal-api/internal/service/image-service.go b/cmd/metal-api/internal/service/image-service.go index 0d6b06a0..a647d569 100644 --- a/cmd/metal-api/internal/service/image-service.go +++ b/cmd/metal-api/internal/service/image-service.go @@ -35,7 +35,7 @@ func NewImage(log *slog.Logger, ds *datastore.RethinkStore) *restful.WebService return ir.webService() } -func (ir imageResource) webService() *restful.WebService { +func (ir *imageResource) webService() *restful.WebService { ws := new(restful.WebService) ws. Path(BasePath + "v1/image"). diff --git a/cmd/metal-api/internal/service/image-service_test.go b/cmd/metal-api/internal/service/image-service_test.go index 032c1bfd..54a2d7a1 100644 --- a/cmd/metal-api/internal/service/image-service_test.go +++ b/cmd/metal-api/internal/service/image-service_test.go @@ -6,7 +6,6 @@ import ( "log/slog" "net/http" "net/http/httptest" - "strings" "testing" "github.com/metal-stack/metal-api/cmd/metal-api/internal/datastore" @@ -183,7 +182,7 @@ func TestCreateImageWithBrokenURL(t *testing.T) { var result httperrors.HTTPErrorResponse err = json.NewDecoder(resp.Body).Decode(&result) require.NoError(t, err) - require.True(t, strings.Contains(result.Message, "is not accessible under")) + require.Contains(t, result.Message, "is not accessible under") createRequest.URL = "http://images.metal-stack.io/this-file-does-not-exist" diff --git a/cmd/metal-api/internal/service/machine-service.go b/cmd/metal-api/internal/service/machine-service.go index 338b2a7c..ed39f4f2 100644 --- a/cmd/metal-api/internal/service/machine-service.go +++ b/cmd/metal-api/internal/service/machine-service.go @@ -754,7 +754,7 @@ func (r *machineResource) setMachineState(request *restful.Request, response *re r.send(request, response, http.StatusOK, resp) } -func (r machineResource) findIPMIMachine(request *restful.Request, response *restful.Response) { +func (r *machineResource) findIPMIMachine(request *restful.Request, response *restful.Response) { id := request.PathParameter("id") m, err := r.ds.FindMachineByID(id) @@ -1782,7 +1782,7 @@ func uniqueTags(tags []string) []string { return uniqueTags } -func (r machineResource) freeMachine(request *restful.Request, response *restful.Response) { +func (r *machineResource) freeMachine(request *restful.Request, response *restful.Response) { id := request.PathParameter("id") m, err := r.ds.FindMachineByID(id) if err != nil {