Skip to content

Commit

Permalink
Wiring additional resources for their UPDATE/DELETE paths (project-ke…
Browse files Browse the repository at this point in the history
  • Loading branch information
josejulio authored and clyang82 committed Sep 30, 2024
1 parent 4250ccc commit d4df424
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 23 deletions.
52 changes: 49 additions & 3 deletions internal/service/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package hosts

import (
"context"

pb "github.com/project-kessel/inventory-api/api/kessel/inventory/v1beta1/resources"
authnapi "github.com/project-kessel/inventory-api/internal/authn/api"
biz "github.com/project-kessel/inventory-api/internal/biz/hosts"
Expand Down Expand Up @@ -43,11 +42,34 @@ func (c *HostsService) CreateRhelHost(ctx context.Context, r *pb.CreateRhelHostR
}

func (c *HostsService) UpdateRhelHost(ctx context.Context, r *pb.UpdateRhelHostRequest) (*pb.UpdateRhelHostResponse, error) {
return nil, nil
identity, err := middleware.GetIdentity(ctx)
if err != nil {
return nil, err
}

if h, err := hostFromUpdateRequest(r, identity); err == nil {
h.Metadata.ResourceType = biz.ResourceType
// Todo: Update to use the right ID
if resp, err := c.Ctl.Update(ctx, h, ""); err == nil {
return updateResponseFromHost(resp), nil
} else {
return nil, err
}
} else {
return nil, err
}
}

func (c *HostsService) DeleteRhelHost(ctx context.Context, r *pb.DeleteRhelHostRequest) (*pb.DeleteRhelHostResponse, error) {
return nil, nil
if input, err := fromDeleteRequest(r); err == nil {
if err := c.Ctl.Delete(ctx, input); err == nil {
return toDeleteResponse(), nil
} else {
return nil, err
}
} else {
return nil, err
}
}

func hostFromCreateRequest(r *pb.CreateRhelHostRequest, identity *authnapi.Identity) (*biz.Host, error) {
Expand All @@ -64,3 +86,27 @@ func hostFromCreateRequest(r *pb.CreateRhelHostRequest, identity *authnapi.Ident
func createResponseFromHost(*biz.Host) *pb.CreateRhelHostResponse {
return &pb.CreateRhelHostResponse{}
}

func hostFromUpdateRequest(r *pb.UpdateRhelHostRequest, identity *authnapi.Identity) (*biz.Host, error) {
var metadata = &pb.Metadata{}
if r.RhelHost.Metadata != nil {
metadata = r.RhelHost.Metadata
}

return &biz.Host{
Metadata: *conv.MetadataFromPb(metadata, r.RhelHost.ReporterData, identity),
}, nil
}

func updateResponseFromHost(*biz.Host) *pb.UpdateRhelHostResponse {
return &pb.UpdateRhelHostResponse{}
}

func fromDeleteRequest(r *pb.DeleteRhelHostRequest) (string, error) {
// Todo: Find out what IDs are we going to be using - is it inventory ids? or resources from reporters?
return r.ReporterData.LocalResourceId, nil
}

func toDeleteResponse() *pb.DeleteRhelHostResponse {
return &pb.DeleteRhelHostResponse{}
}
61 changes: 45 additions & 16 deletions internal/service/k8sclusters/k8scluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ package k8sclusters

import (
"context"
"fmt"
"strings"

"github.com/go-kratos/kratos/v2/errors"

"github.com/project-kessel/inventory-api/api/kessel/inventory/v1beta1/resources"
pb "github.com/project-kessel/inventory-api/api/kessel/inventory/v1beta1/resources"
authnapi "github.com/project-kessel/inventory-api/internal/authn/api"
Expand All @@ -30,10 +25,6 @@ func New(c *biz.K8sClusterUsecase) *K8sClustersService {
}

func (c *K8sClustersService) CreateK8SCluster(ctx context.Context, r *resources.CreateK8SClusterRequest) (*resources.CreateK8SClusterResponse, error) {
if !strings.EqualFold(r.K8SCluster.Metadata.ResourceType, biz.ResourceType) {
return nil, errors.BadRequest("BADREQUEST", fmt.Sprintf("incorrect resource type: expected %s", biz.ResourceType))
}

identity, err := middleware.GetIdentity(ctx)
if err != nil {
return nil, err
Expand All @@ -52,19 +43,37 @@ func (c *K8sClustersService) CreateK8SCluster(ctx context.Context, r *resources.
}

func (c *K8sClustersService) UpdateK8SCluster(ctx context.Context, r *resources.UpdateK8SClusterRequest) (*resources.UpdateK8SClusterResponse, error) {
return nil, nil
identity, err := middleware.GetIdentity(ctx)
if err != nil {
return nil, err
}

if k, err := k8sClusterFromUpdateRequest(r, identity); err == nil {
k.Metadata.ResourceType = biz.ResourceType
// Todo: Update to use the right ID
if resp, err := c.Ctl.Update(ctx, k, ""); err == nil {
return updateResponseFromK8sCluster(resp), nil
} else {
return nil, err
}
} else {
return nil, err
}
}

func (c *K8sClustersService) DeleteK8SCluster(ctx context.Context, r *resources.DeleteK8SClusterRequest) (*resources.DeleteK8SClusterResponse, error) {
return nil, nil
if input, err := fromDeleteRequest(r); err == nil {
if err := c.Ctl.Delete(ctx, input); err == nil {
return toDeleteResponse(), nil
} else {
return nil, err
}
} else {
return nil, err
}
}

func k8sClusterFromCreateRequest(r *pb.CreateK8SClusterRequest, identity *authnapi.Identity) (*biz.K8SCluster, error) {
if identity.Principal != r.K8SCluster.ReporterData.ReporterInstanceId {
msg := fmt.Sprintf("Reporter identity must match the provided reporter instance identity: %s != %s", identity.Principal, r.K8SCluster.ReporterData.ReporterInstanceId)
return nil, errors.Forbidden("FORBIDDEN", msg)
}

return &biz.K8SCluster{
Metadata: *conv.MetadataFromPb(r.K8SCluster.Metadata, r.K8SCluster.ReporterData, identity),
ResourceData: resourceDataFromPb(r.K8SCluster.ResourceData),
Expand All @@ -75,6 +84,26 @@ func createResponseFromK8sCluster(c *biz.K8SCluster) *pb.CreateK8SClusterRespons
return &pb.CreateK8SClusterResponse{}
}

func k8sClusterFromUpdateRequest(r *pb.UpdateK8SClusterRequest, identity *authnapi.Identity) (*biz.K8SCluster, error) {
return &biz.K8SCluster{
Metadata: *conv.MetadataFromPb(r.K8SCluster.Metadata, r.K8SCluster.ReporterData, identity),
ResourceData: resourceDataFromPb(r.K8SCluster.ResourceData),
}, nil
}

func updateResponseFromK8sCluster(c *biz.K8SCluster) *pb.UpdateK8SClusterResponse {
return &pb.UpdateK8SClusterResponse{}
}

func fromDeleteRequest(r *pb.DeleteK8SClusterRequest) (string, error) {
// Todo: Find out what IDs are we going to be using - is it inventory ids? or resources from reporters?
return r.ReporterData.LocalResourceId, nil
}

func toDeleteResponse() *pb.DeleteK8SClusterResponse {
return &pb.DeleteK8SClusterResponse{}
}

func resourceDataFromPb(r *pb.K8SClusterDetail) *biz.K8SClusterDetail {
var nodes []biz.Node
for _, n := range r.Nodes {
Expand Down
55 changes: 53 additions & 2 deletions internal/service/k8spolicies/k8spolicies.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,34 @@ func (c *K8sPolicyService) CreateK8SPolicy(ctx context.Context, r *resources.Cre
}

func (c *K8sPolicyService) UpdateK8SPolicy(ctx context.Context, r *resources.UpdateK8SPolicyRequest) (*resources.UpdateK8SPolicyResponse, error) {
return nil, nil
identity, err := middleware.GetIdentity(ctx)
if err != nil {
return nil, err
}

if h, err := k8sPolicyFromUpdateRequest(r, identity); err == nil {
// Todo: Update to use the right ID
if resp, err := c.Ctl.Update(ctx, h, ""); err == nil {
return updateResponseFromK8sPolicy(resp), nil

} else {
return nil, err
}
} else {
return nil, err
}
}

func (c *K8sPolicyService) DeleteK8SPolicy(ctx context.Context, r *resources.DeleteK8SPolicyRequest) (*resources.DeleteK8SPolicyResponse, error) {
return nil, nil
if input, err := fromDeleteRequest(r); err == nil {
if err := c.Ctl.Delete(ctx, input); err == nil {
return toDeleteResponse(), nil
} else {
return nil, err
}
} else {
return nil, err
}
}

func k8sPolicyFromCreateRequest(r *pb.CreateK8SPolicyRequest, identity *authnapi.Identity) (*biz.K8sPolicy, error) {
Expand All @@ -69,3 +92,31 @@ func k8sPolicyFromCreateRequest(r *pb.CreateK8SPolicyRequest, identity *authnapi
func createResponseFromK8sPolicy(p *biz.K8sPolicy) *pb.CreateK8SPolicyResponse {
return &pb.CreateK8SPolicyResponse{}
}

func k8sPolicyFromUpdateRequest(r *pb.UpdateK8SPolicyRequest, identity *authnapi.Identity) (*biz.K8sPolicy, error) {
var metadata = &pb.Metadata{}
if r.K8SPolicy.Metadata != nil {
metadata = r.K8SPolicy.Metadata
}

return &biz.K8sPolicy{
Metadata: *conv.MetadataFromPb(metadata, r.K8SPolicy.ReporterData, identity),
ResourceData: &biz.K8sPolicyDetail{
Disabled: r.K8SPolicy.ResourceData.Disabled,
Severity: r.K8SPolicy.ResourceData.Severity.String(),
},
}, nil
}

func updateResponseFromK8sPolicy(p *biz.K8sPolicy) *pb.UpdateK8SPolicyResponse {
return &pb.UpdateK8SPolicyResponse{}
}

func fromDeleteRequest(r *pb.DeleteK8SPolicyRequest) (string, error) {
// Todo: Find out what IDs are we going to be using - is it inventory ids? or resources from reporters?
return r.ReporterData.LocalResourceId, nil
}

func toDeleteResponse() *pb.DeleteK8SPolicyResponse {
return &pb.DeleteK8SPolicyResponse{}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,34 @@ func (c *NotificationsIntegrationsService) CreateNotificationsIntegration(ctx co
}

func (c *NotificationsIntegrationsService) UpdateNotificationsIntegration(ctx context.Context, r *pb.UpdateNotificationsIntegrationRequest) (*pb.UpdateNotificationsIntegrationResponse, error) {
return nil, nil
identity, err := middleware.GetIdentity(ctx)
if err != nil {
return nil, err
}

if h, err := notificationsIntegrationFromUpdateRequest(r, identity); err == nil {
// Todo: Update to use the right ID
if resp, err := c.Ctl.Update(ctx, h, ""); err == nil {
return updateResponseFromNotificationsIntegration(resp), nil

} else {
return nil, err
}
} else {
return nil, err
}
}

func (c *NotificationsIntegrationsService) DeleteNotificationsIntegration(ctx context.Context, r *pb.DeleteNotificationsIntegrationRequest) (*pb.DeleteNotificationsIntegrationResponse, error) {
return nil, nil
if input, err := fromDeleteRequest(r); err == nil {
if err := c.Ctl.Delete(ctx, input); err == nil {
return toDeleteResponse(), nil
} else {
return nil, err
}
} else {
return nil, err
}
}

func notificationsIntegrationFromCreateRequest(r *pb.CreateNotificationsIntegrationRequest, identity *authnapi.Identity) (*biz.NotificationsIntegration, error) {
Expand All @@ -64,3 +87,27 @@ func notificationsIntegrationFromCreateRequest(r *pb.CreateNotificationsIntegrat
func createResponseFromNotificationsIntegration(h *biz.NotificationsIntegration) *pb.CreateNotificationsIntegrationResponse {
return &pb.CreateNotificationsIntegrationResponse{}
}

func notificationsIntegrationFromUpdateRequest(r *pb.UpdateNotificationsIntegrationRequest, identity *authnapi.Identity) (*biz.NotificationsIntegration, error) {
var metadata = &pb.Metadata{}
if r.Integration.Metadata != nil {
metadata = r.Integration.Metadata
}

return &biz.NotificationsIntegration{
Metadata: *conv.MetadataFromPb(metadata, r.Integration.ReporterData, identity),
}, nil
}

func updateResponseFromNotificationsIntegration(h *biz.NotificationsIntegration) *pb.UpdateNotificationsIntegrationResponse {
return &pb.UpdateNotificationsIntegrationResponse{}
}

func fromDeleteRequest(r *pb.DeleteNotificationsIntegrationRequest) (string, error) {
// Todo: Find out what IDs are we going to be using - is it inventory ids? or resources from reporters?
return r.ReporterData.LocalResourceId, nil
}

func toDeleteResponse() *pb.DeleteNotificationsIntegrationResponse {
return &pb.DeleteNotificationsIntegrationResponse{}
}

0 comments on commit d4df424

Please sign in to comment.