Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicated rhel_host resources #54

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions internal/biz/common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ type Label struct {
Key string
Value string
}

// ResourceId Acts as a resource id from the standpoint of a reporter
type ResourceId struct {
LocalResourceId string
ReporterType string
ReporterId string
}
19 changes: 18 additions & 1 deletion internal/biz/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package hosts

import (
"context"
"errors"
"fmt"
"github.com/project-kessel/inventory-api/internal/biz/common"
"gorm.io/gorm"

"github.com/go-kratos/kratos/v2/log"
)
Expand All @@ -15,7 +19,7 @@ type HostRepo interface {
Save(context.Context, *Host) (*Host, error)
Update(context.Context, *Host, string) (*Host, error)
Delete(context.Context, string) error
FindByID(context.Context, string) (*Host, error)
FindByID(context.Context, common.ResourceId) (*Host, error)
ListAll(context.Context) ([]*Host, error)
}

Expand All @@ -32,6 +36,19 @@ func New(repo HostRepo, logger log.Logger) *HostUsecase {

// CreateHost creates a Host in the repository and returns the new Host.
func (uc *HostUsecase) CreateHost(ctx context.Context, h *Host) (*Host, error) {
resourceId := common.ResourceId{
LocalResourceId: h.Metadata.Reporters[0].LocalResourceId,
ReporterType: h.Metadata.Reporters[0].ReporterType,
ReporterId: h.Metadata.Reporters[0].ReporterID,
}

_, err := uc.repo.FindByID(ctx, resourceId)
if err == nil {
return nil, fmt.Errorf("rhel_host with local_resource_id: `%v` already exists for current reporter", resourceId.LocalResourceId)
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, err
}
Comment on lines +45 to +50
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there also a unique index defined for resource id, reporter type, reporter id?

Otherwise this will be prone to race conditions and you could still end up with duplicate resources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there is one at the Reporter level

https://github.com/project-kessel/inventory-api/blob/main/internal/biz/common/common.go#L34-L63

Although we are missing one for the metadata_id to allow the reporter to share the same resource_id among different types of resources.

#171


if ret, err := uc.repo.Save(ctx, h); err != nil {
return nil, err
} else {
Expand Down
23 changes: 19 additions & 4 deletions internal/data/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package hosts

import (
"context"

"github.com/project-kessel/inventory-api/internal/biz/common"
"gorm.io/gorm"
"gorm.io/gorm/clause"

Expand Down Expand Up @@ -35,7 +35,7 @@ func New(g *gorm.DB, a authzapi.Authorizer, e eventingapi.Manager, l *log.Helper
func (r *hostsRepo) Save(ctx context.Context, model *biz.Host) (*biz.Host, error) {
identity, err := middleware.GetIdentity(ctx)
if err != nil {
return nil, nil
return nil, err
}

if err := r.Db.Session(&gorm.Session{FullSaveAssociations: true}).Create(model).Error; err != nil {
Expand Down Expand Up @@ -67,8 +67,23 @@ func (r *hostsRepo) Delete(context.Context, string) error {
return nil
}

func (r *hostsRepo) FindByID(context.Context, string) (*biz.Host, error) {
return nil, nil
func (r *hostsRepo) FindByID(ctx context.Context, resourceId common.ResourceId) (*biz.Host, error) {
host := biz.Host{}

reporter := common.Reporter{}
session := gorm.Session{}

err := r.Db.Session(&session).Take(&reporter, "local_resource_id = ? and reporter_type = ? and reporter_id = ?", resourceId.LocalResourceId, resourceId.ReporterType, resourceId.ReporterId).Error
if err != nil {
return nil, err
}

r.Db.Session(&session).Joins("Metadata").Take(&host, "metadata.id = ?", reporter.MetadataID)
if err != nil {
return nil, err
}

return &host, nil
}

func (r *hostsRepo) ListAll(context.Context) ([]*biz.Host, error) {
Expand Down
7 changes: 3 additions & 4 deletions internal/service/common/resource.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package common

import (
timestamppb "google.golang.org/protobuf/types/known/timestamppb"

pb "github.com/project-kessel/inventory-api/api/kessel/inventory/v1beta1"
authnapi "github.com/project-kessel/inventory-api/internal/authn/api"
biz "github.com/project-kessel/inventory-api/internal/biz/common"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)

func MetadataFromPb(in *pb.Metadata, reporter *pb.ReporterData, identity *authnapi.Identity) *biz.Metadata {
func MetadataFromPb(in *pb.Metadata, reporter *pb.ReporterData, identity *authnapi.Identity, resourceType string) *biz.Metadata {
var labels []*biz.Label
for _, t := range in.Labels {
labels = append(labels, &biz.Label{Key: t.Key, Value: t.Value})
}

return &biz.Metadata{
ID: in.Id,
ResourceType: in.ResourceType,
ResourceType: resourceType,
Workspace: in.Workspace,
CreatedAt: in.FirstReported.AsTime(),
UpdatedAt: in.LastReported.AsTime(),
Expand Down
1 change: 1 addition & 0 deletions internal/service/common/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func TestMetadataFromPb(t *testing.T) {
Href: "",
IsGuest: false,
},
"astromech",
)

expected := createBizMetadata(created, updated)
Expand Down
2 changes: 1 addition & 1 deletion internal/service/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func hostFromCreateRequest(r *pb.CreateRhelHostRequest, identity *authnapi.Ident
}

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

Expand Down
26 changes: 21 additions & 5 deletions internal/service/hosts/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hosts

import (
"context"
"github.com/project-kessel/inventory-api/internal/biz/common"
"testing"

"github.com/go-kratos/kratos/v2/log"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/project-kessel/inventory-api/internal/middleware"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"gorm.io/gorm"
)

type HostRepoMock struct {
Expand All @@ -29,14 +31,28 @@ func (m *HostRepoMock) Delete(ctx context.Context, hostId string) error {
return nil
}

func (m *HostRepoMock) FindByID(ctx context.Context, hostId string) (*hosts.Host, error) {
return nil, nil
func (m *HostRepoMock) FindByID(ctx context.Context, hostId common.ResourceId) (*hosts.Host, error) {
margs := m.Called(ctx, hostId)

err := margs.Error(1)

if err != nil {
return nil, err
}

return margs.Get(0).(*hosts.Host), err
}

func (m *HostRepoMock) ListAll(ctx context.Context) ([]*hosts.Host, error) {
return []*hosts.Host{}, nil
}

func newHostRepoMock() *HostRepoMock {
repo := new(HostRepoMock)
repo.On("FindByID", mock.Anything, mock.Anything).Return(nil, gorm.ErrRecordNotFound)
return repo
}

func mockContext() context.Context {
return context.WithValue(
context.Background(),
Expand All @@ -48,7 +64,7 @@ func mockContext() context.Context {
}

func TestCreateHostWithRequiredDataIsSuccess(t *testing.T) {
repo := new(HostRepoMock)
repo := newHostRepoMock()
hostUsecase := hosts.New(repo, log.DefaultLogger)

service := HostsService{
Expand All @@ -73,7 +89,7 @@ func TestCreateHostWithRequiredDataIsSuccess(t *testing.T) {
}

func TestCreateHostWithOptionalAttributesIsSuccess(t *testing.T) {
repo := new(HostRepoMock)
repo := newHostRepoMock()
hostUsecase := hosts.New(repo, log.DefaultLogger)

service := HostsService{
Expand Down Expand Up @@ -104,7 +120,7 @@ func TestCreateHostWithOptionalAttributesIsSuccess(t *testing.T) {
}

func TestCreateInvalidHostIsBadRequest(t *testing.T) {
repo := new(HostRepoMock)
repo := newHostRepoMock()
hostUsecase := hosts.New(repo, log.DefaultLogger)

service := HostsService{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func notificationsIntegrationFromCreateRequest(r *pb.CreateNotificationsIntegrat
}

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

Expand Down
Loading