Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 126 additions & 18 deletions apiv2/internal/server/customconfig.go
Comment thread
tmatenko marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,149 @@ package server
import (
"context"

"google.golang.org/grpc/codes"

customconfig_v1 "github.com/open-edge-platform/infra-core/apiv2/v2/internal/pbapi/resources/customconfig/v1"
customconfigv1 "github.com/open-edge-platform/infra-core/apiv2/v2/internal/pbapi/resources/customconfig/v1"
restv1 "github.com/open-edge-platform/infra-core/apiv2/v2/internal/pbapi/services/v1"
inv_computev1 "github.com/open-edge-platform/infra-core/inventory/v2/pkg/api/compute/v1"
inventory "github.com/open-edge-platform/infra-core/inventory/v2/pkg/api/inventory/v1"
"github.com/open-edge-platform/infra-core/inventory/v2/pkg/errors"
"github.com/open-edge-platform/infra-core/inventory/v2/pkg/validator"
)

func toInvCustomConfig(customConfig *customconfigv1.CustomConfigResource) (*inv_computev1.CustomConfigResource, error) {
if customConfig == nil {
return &inv_computev1.CustomConfigResource{}, nil
Comment thread
cjnolan marked this conversation as resolved.
}

invCustomConfig := &inv_computev1.CustomConfigResource{
Name: customConfig.GetName(),
Description: customConfig.GetDescription(),
Config: customConfig.GetConfig(),
}

err := validator.ValidateMessage(invCustomConfig)
if err != nil {
zlog.InfraErr(err).Msg("Failed to validate inventory custom config resource")
return nil, err
}

return invCustomConfig, nil
}

func fromInvCustomConfig(invCustomConfig *inv_computev1.CustomConfigResource) *customconfigv1.CustomConfigResource {
Comment thread
cjnolan marked this conversation as resolved.
if invCustomConfig == nil {
return &customconfigv1.CustomConfigResource{}
Comment thread
cjnolan marked this conversation as resolved.
}

return &customconfigv1.CustomConfigResource{
ResourceId: invCustomConfig.GetResourceId(),
Name: invCustomConfig.GetName(),
Description: invCustomConfig.GetDescription(),
Config: invCustomConfig.GetConfig(),
Timestamps: GrpcToOpenAPITimestamps(invCustomConfig),
}
}

func (is *InventorygRPCServer) CreateCustomConfig(
_ context.Context,
_ *restv1.CreateCustomConfigRequest,
) (*customconfig_v1.CustomConfigResource, error) {
ctx context.Context,
req *restv1.CreateCustomConfigRequest,
) (*customconfigv1.CustomConfigResource, error) {
zlog.Debug().Msg("CreateCustomConfig")
return nil, errors.Errorfc(codes.Unimplemented, "CreateCustomConfig not implemented")

invCustomConfig, err := toInvCustomConfig(req.GetCustomConfig())
if err != nil {
zlog.InfraErr(err).Msg("Failed to convert to inventory custom config")
return nil, errors.Wrap(err)
}

invRes := &inventory.Resource{
Resource: &inventory.Resource_CustomConfig{
CustomConfig: invCustomConfig,
},
}

invResp, err := is.InvClient.Create(ctx, invRes)
if err != nil {
zlog.InfraErr(err).Msg("Failed to create custom config in inventory")
return nil, errors.Wrap(err)
}

customConfigCreated := fromInvCustomConfig(invResp.GetCustomConfig())
zlog.Debug().Msgf("Created %s", customConfigCreated)
return customConfigCreated, nil
}

func (is *InventorygRPCServer) ListCustomConfigs(
_ context.Context,
_ *restv1.ListCustomConfigsRequest,
ctx context.Context,
req *restv1.ListCustomConfigsRequest,
) (*restv1.ListCustomConfigsResponse, error) {
zlog.Debug().Msg("ListCustomConfig")
return nil, errors.Errorfc(codes.Unimplemented, "ListCustomConfigs not implemented")
zlog.Debug().Msg("ListCustomConfigs")

filter := &inventory.ResourceFilter{
Resource: &inventory.Resource{Resource: &inventory.Resource_CustomConfig{
CustomConfig: &inv_computev1.CustomConfigResource{},
}},
Offset: req.GetOffset(),
Limit: req.GetPageSize(),
OrderBy: req.GetOrderBy(),
Filter: req.GetFilter(),
}

if err := validator.ValidateMessage(filter); err != nil {
zlog.InfraSec().InfraErr(err).Msg("failed to validate query params")
return nil, errors.Wrap(err)
}

invResp, err := is.InvClient.List(ctx, filter)
if err != nil {
zlog.InfraErr(err).Msg("Failed to list custom configs from inventory")
return nil, errors.Wrap(err)
}

customConfigs := []*customconfigv1.CustomConfigResource{}
for _, invRes := range invResp.GetResources() {
cc := fromInvCustomConfig(invRes.GetResource().GetCustomConfig())
customConfigs = append(customConfigs, cc)
}

resp := &restv1.ListCustomConfigsResponse{
CustomConfigs: customConfigs,
TotalElements: invResp.GetTotalElements(),
HasNext: invResp.GetHasNext(),
}
zlog.Debug().Msgf("Listed %s", resp)
return resp, nil
}

func (is *InventorygRPCServer) GetCustomConfig(
_ context.Context,
_ *restv1.GetCustomConfigRequest,
) (*customconfig_v1.CustomConfigResource, error) {
ctx context.Context,
req *restv1.GetCustomConfigRequest,
) (*customconfigv1.CustomConfigResource, error) {
zlog.Debug().Msg("GetCustomConfig")
return nil, errors.Errorfc(codes.Unimplemented, "GetCustomConfig not implemented")

invResp, err := is.InvClient.Get(ctx, req.GetResourceId())
if err != nil {
zlog.InfraErr(err).Msg("Failed to get custom config from inventory")
return nil, errors.Wrap(err)
}

invCustomConfig := invResp.GetResource().GetCustomConfig()
customConfig := fromInvCustomConfig(invCustomConfig)
zlog.Debug().Msgf("Got %s", customConfig)
return customConfig, nil
}

func (is *InventorygRPCServer) DeleteCustomConfig(
_ context.Context,
_ *restv1.DeleteCustomConfigRequest,
ctx context.Context,
req *restv1.DeleteCustomConfigRequest,
) (*restv1.DeleteCustomConfigResponse, error) {
zlog.Debug().Msg("DeleteCustomConfig")
return nil, errors.Errorfc(codes.Unimplemented, "DeleteCustomConfig not implemented")

_, err := is.InvClient.Delete(ctx, req.GetResourceId())
if err != nil {
zlog.InfraErr(err).Msg("Failed to delete custom config from inventory")
return nil, errors.Wrap(err)
}

zlog.Debug().Msgf("Deleted %s", req.GetResourceId())
return &restv1.DeleteCustomConfigResponse{}, nil
}
Loading