Skip to content

Commit 2eeaf78

Browse files
author
Tadeusz Matenko
committed
Implement custom config create, list, get, delete
1 parent 204c606 commit 2eeaf78

3 files changed

Lines changed: 387 additions & 42 deletions

File tree

apiv2/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.3
1+
2.1.4-dev

apiv2/internal/server/customconfig.go

Lines changed: 126 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,149 @@ package server
66
import (
77
"context"
88

9-
"google.golang.org/grpc/codes"
10-
11-
customconfig_v1 "github.com/open-edge-platform/infra-core/apiv2/v2/internal/pbapi/resources/customconfig/v1"
9+
customconfigv1 "github.com/open-edge-platform/infra-core/apiv2/v2/internal/pbapi/resources/customconfig/v1"
1210
restv1 "github.com/open-edge-platform/infra-core/apiv2/v2/internal/pbapi/services/v1"
11+
inv_computev1 "github.com/open-edge-platform/infra-core/inventory/v2/pkg/api/compute/v1"
12+
inventory "github.com/open-edge-platform/infra-core/inventory/v2/pkg/api/inventory/v1"
1313
"github.com/open-edge-platform/infra-core/inventory/v2/pkg/errors"
14+
"github.com/open-edge-platform/infra-core/inventory/v2/pkg/validator"
1415
)
1516

17+
func toInvCustomConfig(customConfig *customconfigv1.CustomConfigResource) (*inv_computev1.CustomConfigResource, error) {
18+
if customConfig == nil {
19+
return &inv_computev1.CustomConfigResource{}, nil
20+
}
21+
22+
invCustomConfig := &inv_computev1.CustomConfigResource{
23+
Name: customConfig.GetName(),
24+
Description: customConfig.GetDescription(),
25+
Config: customConfig.GetConfigContent(),
26+
}
27+
28+
err := validator.ValidateMessage(invCustomConfig)
29+
if err != nil {
30+
zlog.InfraErr(err).Msg("Failed to validate inventory custom config resource")
31+
return nil, err
32+
}
33+
34+
return invCustomConfig, nil
35+
}
36+
37+
func fromInvCustomConfig(invCustomConfig *inv_computev1.CustomConfigResource) *customconfigv1.CustomConfigResource {
38+
if invCustomConfig == nil {
39+
return &customconfigv1.CustomConfigResource{}
40+
}
41+
42+
return &customconfigv1.CustomConfigResource{
43+
ResourceId: invCustomConfig.GetResourceId(),
44+
Name: invCustomConfig.GetName(),
45+
Description: invCustomConfig.GetDescription(),
46+
ConfigContent: invCustomConfig.GetConfig(),
47+
Timestamps: GrpcToOpenAPITimestamps(invCustomConfig),
48+
}
49+
}
50+
1651
func (is *InventorygRPCServer) CreateCustomConfig(
17-
_ context.Context,
18-
_ *restv1.CreateCustomConfigRequest,
19-
) (*customconfig_v1.CustomConfigResource, error) {
52+
ctx context.Context,
53+
req *restv1.CreateCustomConfigRequest,
54+
) (*customconfigv1.CustomConfigResource, error) {
2055
zlog.Debug().Msg("CreateCustomConfig")
21-
return nil, errors.Errorfc(codes.Unimplemented, "CreateCustomConfig not implemented")
56+
57+
invCustomConfig, err := toInvCustomConfig(req.GetCustomConfig())
58+
if err != nil {
59+
zlog.InfraErr(err).Msg("Failed to convert to inventory custom config")
60+
return nil, errors.Wrap(err)
61+
}
62+
63+
invRes := &inventory.Resource{
64+
Resource: &inventory.Resource_CustomConfig{
65+
CustomConfig: invCustomConfig,
66+
},
67+
}
68+
69+
invResp, err := is.InvClient.Create(ctx, invRes)
70+
if err != nil {
71+
zlog.InfraErr(err).Msg("Failed to create custom config in inventory")
72+
return nil, errors.Wrap(err)
73+
}
74+
75+
customConfigCreated := fromInvCustomConfig(invResp.GetCustomConfig())
76+
zlog.Debug().Msgf("Created %s", customConfigCreated)
77+
return customConfigCreated, nil
2278
}
2379

2480
func (is *InventorygRPCServer) ListCustomConfigs(
25-
_ context.Context,
26-
_ *restv1.ListCustomConfigsRequest,
81+
ctx context.Context,
82+
req *restv1.ListCustomConfigsRequest,
2783
) (*restv1.ListCustomConfigsResponse, error) {
28-
zlog.Debug().Msg("ListCustomConfig")
29-
return nil, errors.Errorfc(codes.Unimplemented, "ListCustomConfigs not implemented")
84+
zlog.Debug().Msg("ListCustomConfigs")
85+
86+
filter := &inventory.ResourceFilter{
87+
Resource: &inventory.Resource{Resource: &inventory.Resource_CustomConfig{
88+
CustomConfig: &inv_computev1.CustomConfigResource{},
89+
}},
90+
Offset: req.GetOffset(),
91+
Limit: req.GetPageSize(),
92+
OrderBy: req.GetOrderBy(),
93+
Filter: req.GetFilter(),
94+
}
95+
96+
if err := validator.ValidateMessage(filter); err != nil {
97+
zlog.InfraSec().InfraErr(err).Msg("failed to validate query params")
98+
return nil, errors.Wrap(err)
99+
}
100+
101+
invResp, err := is.InvClient.List(ctx, filter)
102+
if err != nil {
103+
zlog.InfraErr(err).Msg("Failed to list custom configs from inventory")
104+
return nil, errors.Wrap(err)
105+
}
106+
107+
customConfigs := []*customconfigv1.CustomConfigResource{}
108+
for _, invRes := range invResp.GetResources() {
109+
cc := fromInvCustomConfig(invRes.GetResource().GetCustomConfig())
110+
customConfigs = append(customConfigs, cc)
111+
}
112+
113+
resp := &restv1.ListCustomConfigsResponse{
114+
CustomConfigs: customConfigs,
115+
TotalElements: invResp.GetTotalElements(),
116+
HasNext: invResp.GetHasNext(),
117+
}
118+
zlog.Debug().Msgf("Listed %s", resp)
119+
return resp, nil
30120
}
31121

32122
func (is *InventorygRPCServer) GetCustomConfig(
33-
_ context.Context,
34-
_ *restv1.GetCustomConfigRequest,
35-
) (*customconfig_v1.CustomConfigResource, error) {
123+
ctx context.Context,
124+
req *restv1.GetCustomConfigRequest,
125+
) (*customconfigv1.CustomConfigResource, error) {
36126
zlog.Debug().Msg("GetCustomConfig")
37-
return nil, errors.Errorfc(codes.Unimplemented, "GetCustomConfig not implemented")
127+
128+
invResp, err := is.InvClient.Get(ctx, req.GetResourceId())
129+
if err != nil {
130+
zlog.InfraErr(err).Msg("Failed to get custom config from inventory")
131+
return nil, errors.Wrap(err)
132+
}
133+
134+
invCustomConfig := invResp.GetResource().GetCustomConfig()
135+
customConfig := fromInvCustomConfig(invCustomConfig)
136+
zlog.Debug().Msgf("Got %s", customConfig)
137+
return customConfig, nil
38138
}
39139

40140
func (is *InventorygRPCServer) DeleteCustomConfig(
41-
_ context.Context,
42-
_ *restv1.DeleteCustomConfigRequest,
141+
ctx context.Context,
142+
req *restv1.DeleteCustomConfigRequest,
43143
) (*restv1.DeleteCustomConfigResponse, error) {
44144
zlog.Debug().Msg("DeleteCustomConfig")
45-
return nil, errors.Errorfc(codes.Unimplemented, "DeleteCustomConfig not implemented")
145+
146+
_, err := is.InvClient.Delete(ctx, req.GetResourceId())
147+
if err != nil {
148+
zlog.InfraErr(err).Msg("Failed to delete custom config from inventory")
149+
return nil, errors.Wrap(err)
150+
}
151+
152+
zlog.Debug().Msgf("Deleted %s", req.GetResourceId())
153+
return &restv1.DeleteCustomConfigResponse{}, nil
46154
}

0 commit comments

Comments
 (0)