/
machine_annotations_update.go
47 lines (37 loc) · 1.49 KB
/
machine_annotations_update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
package server
import (
"context"
"errors"
"fmt"
iri "github.com/ironcore-dev/ironcore/iri/apis/machine/v1alpha1"
"github.com/ironcore-dev/libvirt-provider/api"
"github.com/ironcore-dev/libvirt-provider/internal/store"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *Server) updateAnnotations(ctx context.Context, machine *api.Machine, annotations map[string]string) error {
if err := api.SetAnnotationsAnnotation(machine, annotations); err != nil {
return fmt.Errorf("failed to set machine annotations: %w", err)
}
if _, err := s.machineStore.Update(ctx, machine); err != nil {
return fmt.Errorf("failed to update machine: %w", err)
}
return nil
}
func (s *Server) UpdateMachineAnnotations(ctx context.Context, req *iri.UpdateMachineAnnotationsRequest) (*iri.UpdateMachineAnnotationsResponse, error) {
log := s.loggerFrom(ctx)
log.V(1).Info("Getting machine")
machine, err := s.machineStore.Get(ctx, req.MachineId)
if err != nil {
if !errors.Is(err, store.ErrNotFound) {
return nil, fmt.Errorf("error getting machine: %w", err)
}
return nil, status.Errorf(codes.NotFound, "machine %s not found", req.MachineId)
}
if err := s.updateAnnotations(ctx, machine, req.Annotations); err != nil {
return nil, fmt.Errorf("failed to update machine annotations: %w", err)
}
return &iri.UpdateMachineAnnotationsResponse{}, nil
}