-
Notifications
You must be signed in to change notification settings - Fork 1
/
host_group.go
143 lines (131 loc) · 4 KB
/
host_group.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package handler
import (
"context"
"fmt"
"github.com/ScoreTrak/ScoreTrak/pkg/hostgroup"
"github.com/ScoreTrak/ScoreTrak/pkg/hostgroup/hostgroupservice"
"github.com/gofrs/uuid"
"github.com/golang/protobuf/ptypes/wrappers"
host_groupv1 "go.buf.build/grpc/go/scoretrak/scoretrakapis/scoretrak/host_group/v1"
protov1 "go.buf.build/grpc/go/scoretrak/scoretrakapis/scoretrak/proto/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type HostGroupController struct {
svc hostgroupservice.Serv
host_groupv1.UnimplementedHostGroupServiceServer
}
func (p HostGroupController) GetByID(ctx context.Context, request *host_groupv1.GetByIDRequest) (*host_groupv1.GetByIDResponse, error) {
uid, err := extractUUID(request)
if err != nil {
return nil, err
}
hst, err := p.svc.GetByID(ctx, uid)
if err != nil {
return nil, getErrorParser(err)
}
return &host_groupv1.GetByIDResponse{HostGroup: ConvertHostGroupToHostGroupPb(hst)}, nil
}
func (p HostGroupController) GetAll(ctx context.Context, request *host_groupv1.GetAllRequest) (*host_groupv1.GetAllResponse, error) {
hostgrps, err := p.svc.GetAll(ctx)
if err != nil {
return nil, getErrorParser(err)
}
hostgrpspb := make([]*host_groupv1.HostGroup, 0, len(hostgrps))
for i := range hostgrps {
hostgrpspb = append(hostgrpspb, ConvertHostGroupToHostGroupPb(hostgrps[i]))
}
return &host_groupv1.GetAllResponse{HostGroups: hostgrpspb}, nil
}
func (p HostGroupController) Delete(ctx context.Context, request *host_groupv1.DeleteRequest) (*host_groupv1.DeleteResponse, error) {
uid, err := extractUUID(request)
if err != nil {
return nil, err
}
err = p.svc.Delete(ctx, uid)
if err != nil {
return nil, deleteErrorParser(err)
}
return &host_groupv1.DeleteResponse{}, nil
}
func (p HostGroupController) Store(ctx context.Context, request *host_groupv1.StoreRequest) (*host_groupv1.StoreResponse, error) {
servcspb := request.GetHostGroups()
props := make([]*hostgroup.HostGroup, 0, len(servcspb))
for i := range servcspb {
hstgrp, err := ConvertHostGroupPBtoHostGroup(false, servcspb[i])
if err != nil {
return nil, err
}
props = append(props, hstgrp)
}
if err := p.svc.Store(ctx, props); err != nil {
return nil, status.Errorf(
codes.Internal,
fmt.Sprintf("Unknown internal error: %v", err),
)
}
ids := make([]*protov1.UUID, 0, len(props))
for i := range props {
ids = append(ids, &protov1.UUID{Value: props[i].ID.String()})
}
return &host_groupv1.StoreResponse{Ids: ids}, nil
}
func (p HostGroupController) Update(ctx context.Context, request *host_groupv1.UpdateRequest) (*host_groupv1.UpdateResponse, error) {
hstgrp, err := ConvertHostGroupPBtoHostGroup(true, request.GetHostGroup())
if err != nil {
return nil, err
}
err = p.svc.Update(ctx, hstgrp)
if err != nil {
return nil, status.Errorf(
codes.Internal,
fmt.Sprintf("Unknown internal error: %v", err),
)
}
return &host_groupv1.UpdateResponse{}, nil
}
func NewHostGroupController(svc hostgroupservice.Serv) *HostGroupController {
return &HostGroupController{svc: svc}
}
func ConvertHostGroupPBtoHostGroup(requireID bool, pb *host_groupv1.HostGroup) (*hostgroup.HostGroup, error) {
var id uuid.UUID
var err error
if pb.GetId() != nil {
id, err = uuid.FromString(pb.GetId().GetValue())
if err != nil {
return nil, status.Errorf(
codes.InvalidArgument,
unableToParseID+": %v", err,
)
}
} else if requireID {
return nil, status.Errorf(
codes.InvalidArgument,
idNotSpecified,
)
}
var pause *bool
if pb.GetPause() != nil {
pause = &pb.GetPause().Value
}
var hide *bool
if pb.GetHide() != nil {
hide = &pb.GetHide().Value
}
return &hostgroup.HostGroup{
ID: id,
Name: pb.GetName(),
Pause: pause,
Hide: hide,
Hosts: nil,
}, nil
}
func ConvertHostGroupToHostGroupPb(obj *hostgroup.HostGroup) *host_groupv1.HostGroup {
return &host_groupv1.HostGroup{
Id: &protov1.UUID{Value: obj.ID.String()},
Name: obj.Name,
Pause: &wrappers.BoolValue{Value: *obj.Pause},
Hide: &wrappers.BoolValue{Value: *obj.Hide},
Hosts: nil,
}
}