Skip to content

Commit

Permalink
fix: [CDE-192]: correctly set resources for infraprovider config. (ha…
Browse files Browse the repository at this point in the history
…rness#2527)

* fix: [CDE-192]: db show ids in error
* fix: [CDE-192]: db show ids in error
* fix: [CDE-192]: db show ids in error
* fix: [CDE-192]: correctly set resources for infraprovider config.
  • Loading branch information
n00bitax authored and Harness committed Aug 19, 2024
1 parent 6d0db95 commit 54113d4
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 79 deletions.
7 changes: 4 additions & 3 deletions app/api/controller/gitspace/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package gitspace

import (
"context"
"errors"
"fmt"
"strconv"
"strings"
Expand All @@ -24,6 +25,7 @@ import (
apiauth "github.com/harness/gitness/app/api/auth"
"github.com/harness/gitness/app/auth"
events "github.com/harness/gitness/app/events/gitspace"
"github.com/harness/gitness/store"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/check"
"github.com/harness/gitness/types/enum"
Expand Down Expand Up @@ -104,9 +106,8 @@ func (c *Controller) startGitspaceAction(
config *types.GitspaceConfig,
) error {
savedGitspaceInstance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, config.ID, config.SpaceID)
const resourceNotFoundErr = "Failed to find gitspace: resource not found"
if err != nil && err.Error() != resourceNotFoundErr {
return fmt.Errorf("failed to find gitspace instance for config ID : %s %w", config.Identifier, err)
if err != nil && !errors.Is(err, store.ErrResourceNotFound) {
return err
}
config.GitspaceInstance = savedGitspaceInstance
err = c.gitspaceBusyOperation(ctx, config)
Expand Down
5 changes: 3 additions & 2 deletions app/api/controller/gitspace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
apiauth "github.com/harness/gitness/app/api/auth"
"github.com/harness/gitness/app/api/usererror"
"github.com/harness/gitness/app/auth"
"github.com/harness/gitness/errors"
"github.com/harness/gitness/store"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/check"
"github.com/harness/gitness/types/enum"
Expand All @@ -33,7 +35,6 @@ import (

const allowedUIDAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
const defaultResourceIdentifier = "default"
const infraProviderResourceMissingErr = "Failed to find infraProviderResource: resource not found"

var (
// errSecretRequiresParent if the user tries to create a secret without a parent space.
Expand Down Expand Up @@ -174,7 +175,7 @@ func (c *Controller) createOrFindInfraProviderResource(
parentSpace.ID,
resourceIdentifier)
if err != nil &&
err.Error() == infraProviderResourceMissingErr &&
errors.Is(err, store.ErrResourceNotFound) &&
resourceIdentifier == defaultResourceIdentifier {
err = c.autoCreateDefaultResource(ctx, parentSpace, now)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion app/api/controller/infraprovider/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (c *Controller) Create(
Created: now,
Updated: now,
}
infraProviderConfig.Resources = mapToResourceEntity(in.Resources, *parentSpace, infraProviderConfig.ID)
infraProviderConfig.Resources = mapToResourceEntity(in.Resources, *parentSpace, now)
err = c.infraproviderSvc.CreateInfraProvider(ctx, infraProviderConfig)
if err != nil {
return nil, fmt.Errorf("unable to create the infraprovider: %q %w", infraProviderConfig.Identifier, err)
Expand Down
8 changes: 4 additions & 4 deletions app/services/gitspace/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ package gitspace

import (
"context"
"errors"
"fmt"

"github.com/harness/gitness/store"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
)

const resourceNotFoundErr = "Failed to find gitspace: resource not found"

func (c *Service) Find(
ctx context.Context,
spaceID int64,
Expand All @@ -44,8 +44,8 @@ func (c *Service) Find(
gitspaceConfig.SpacePath = spacePath
gitspaceConfig.InfraProviderResourceIdentifier = infraProviderResource.Identifier
instance, err := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, gitspaceConfig.ID, gitspaceConfig.SpaceID)
if err != nil && err.Error() != resourceNotFoundErr { // TODO fix this
return fmt.Errorf("failed to find gitspace instance for config ID : %s %w", gitspaceConfig.Identifier, err)
if err != nil && !errors.Is(err, store.ErrResourceNotFound) {
return err
}
if instance != nil {
gitspaceConfig.GitspaceInstance = instance
Expand Down
53 changes: 50 additions & 3 deletions app/services/infraprovider/infraprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/harness/gitness/infraprovider"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"

"github.com/rs/zerolog/log"
)

func NewService(
Expand Down Expand Up @@ -123,7 +125,52 @@ func (c *Service) CreateResources(ctx context.Context, resources []types.InfraPr
return c.createResources(ctx, resources, configID)
})
if err != nil {
return fmt.Errorf("failed to complete txn for the infraprovider resource %w", err)
return fmt.Errorf("failed to complete create txn for the infraprovider resource %w", err)
}
return nil
}

func (c *Service) UpdateResource(ctx context.Context, resource types.InfraProviderResource) error {
err := c.tx.WithTx(ctx, func(ctx context.Context) error {
space, err := c.spaceStore.FindByRef(ctx, resource.SpacePath)
if err != nil {
return err
}
infraProviderResource, err := c.FindResourceByIdentifier(ctx, space.ID, resource.Identifier)
if err != nil {
return err
}
resource.ID = infraProviderResource.ID
if err = c.infraProviderResourceStore.Update(ctx, &resource); err != nil {
return err
}
return nil
})
if err != nil {
return fmt.Errorf("failed to complete update txn for the infraprovider resource %w", err)
}
return nil
}

func (c *Service) UpdateTemplate(ctx context.Context, template types.InfraProviderTemplate) error {
err := c.tx.WithTx(ctx, func(ctx context.Context) error {
space, err := c.spaceStore.FindByRef(ctx, template.SpacePath)
if err != nil {
return err
}
templateInDB, err := c.infraProviderTemplateStore.FindByIdentifier(ctx, space.ID, template.Identifier)
if err != nil {
return err
}
template.ID = templateInDB.ID
template.SpaceID = space.ID
if err = c.infraProviderTemplateStore.Update(ctx, &template); err != nil {
return err
}
return nil
})
if err != nil {
return fmt.Errorf("failed to complete update txn for the infraprovider template %w", err)
}
return nil
}
Expand Down Expand Up @@ -163,8 +210,8 @@ func (c *Service) validateTemplates(
_, err := c.infraProviderTemplateStore.FindByIdentifier(
ctx, res.SpaceID, templateIdentifier)
if err != nil {
return fmt.Errorf("unable to get template params for ID : %s %w",
res.Metadata[key], err)
log.Warn().Msgf("unable to get template params for ID : %s",
res.Metadata[key])
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions app/store/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,9 @@ type (

// Create creates a new infra provider config in the datastore.
Create(ctx context.Context, infraProviderConfig *types.InfraProviderConfig) error

// Update tries to update the infra provider config in the datastore.
Update(ctx context.Context, infraProviderConfig *types.InfraProviderConfig) error
}

InfraProviderResourceStore interface {
Expand All @@ -647,6 +650,9 @@ type (
// Create creates a new infra provider resource in the datastore.
Create(ctx context.Context, infraProviderResource *types.InfraProviderResource) error

// Update tries to update the infra provider resource in the datastore.
Update(ctx context.Context, infraProviderResource *types.InfraProviderResource) error

// List lists the infra provider resource present for the gitspace config in a parent space ID in the datastore.
List(ctx context.Context,
infraProviderConfigID int64,
Expand Down Expand Up @@ -1076,6 +1082,7 @@ type (
InfraProviderTemplateStore interface {
FindByIdentifier(ctx context.Context, spaceID int64, identifier string) (*types.InfraProviderTemplate, error)
Find(ctx context.Context, id int64) (*types.InfraProviderTemplate, error)
Update(ctx context.Context, infraProviderTemplate *types.InfraProviderTemplate) error
Create(ctx context.Context, infraProviderTemplate *types.InfraProviderTemplate) error
Delete(ctx context.Context, id int64) error
}
Expand Down
14 changes: 8 additions & 6 deletions app/store/database/gitspace_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (s gitspaceConfigStore) Find(ctx context.Context, id int64) (*types.Gitspac
}
db := dbtx.GetAccessor(ctx, s.db)
if err := db.GetContext(ctx, dst, sql, args...); err != nil {
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find gitspace config")
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find gitspace config for %d", id)
}
return s.mapToGitspaceConfig(ctx, dst)
}
Expand All @@ -148,7 +148,7 @@ func (s gitspaceConfigStore) FindByIdentifier(
dst := new(gitspaceConfig)
db := dbtx.GetAccessor(ctx, s.db)
if err := db.GetContext(ctx, dst, sql, args...); err != nil {
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find gitspace config")
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find gitspace config for %s", identifier)
}
return s.mapToGitspaceConfig(ctx, dst)
}
Expand Down Expand Up @@ -184,7 +184,8 @@ func (s gitspaceConfigStore) Create(ctx context.Context, gitspaceConfig *types.G
}
db := dbtx.GetAccessor(ctx, s.db)
if err = db.QueryRowContext(ctx, sql, args...).Scan(&gitspaceConfig.ID); err != nil {
return database.ProcessSQLErrorf(ctx, err, "gitspace config query failed")
return database.ProcessSQLErrorf(
ctx, err, "gitspace config create query failed for %s", gitspaceConfig.Identifier)
}
return nil
}
Expand All @@ -206,7 +207,8 @@ func (s gitspaceConfigStore) Update(ctx context.Context,
}
db := dbtx.GetAccessor(ctx, s.db)
if _, err := db.ExecContext(ctx, sql, args...); err != nil {
return database.ProcessSQLErrorf(ctx, err, "Failed to update gitspace config")
return database.ProcessSQLErrorf(
ctx, err, "Failed to update gitspace config for %s", gitspaceConfig.Identifier)
}
return nil
}
Expand Down Expand Up @@ -254,7 +256,7 @@ func (s gitspaceConfigStore) List(ctx context.Context, filter *types.GitspaceFil
db := dbtx.GetAccessor(ctx, s.db)
var dst []*gitspaceConfig
if err = db.SelectContext(ctx, &dst, sql, args...); err != nil {
return nil, database.ProcessSQLErrorf(ctx, err, "Failed executing custom list query")
return nil, database.ProcessSQLErrorf(ctx, err, "Failed executing gitspace config list query")
}
return s.mapToGitspaceConfigs(ctx, dst)
}
Expand All @@ -276,7 +278,7 @@ func (s gitspaceConfigStore) ListAll(
db := dbtx.GetAccessor(ctx, s.db)
var dst []*gitspaceConfig
if err = db.SelectContext(ctx, &dst, sql, args...); err != nil {
return nil, database.ProcessSQLErrorf(ctx, err, "Failed executing custom list query")
return nil, database.ProcessSQLErrorf(ctx, err, "Failed executing gitspace config list query")
}
return s.mapToGitspaceConfigs(ctx, dst)
}
Expand Down
7 changes: 4 additions & 3 deletions app/store/database/gitspace_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (g gitspaceEventStore) FindLatestByTypeAndGitspaceConfigID(
db := dbtx.GetAccessor(ctx, g.db)
gitspaceEventEntity := new(gitspaceEvent)
if err = db.GetContext(ctx, gitspaceEventEntity, sql, args...); err != nil {
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find gitspace event")
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find gitspace event for %d", gitspaceConfigID)
}
return g.mapGitspaceEvent(gitspaceEventEntity), nil
}
Expand All @@ -107,7 +107,8 @@ func (g gitspaceEventStore) Create(ctx context.Context, gitspaceEvent *types.Git
return fmt.Errorf("failed to convert squirrel builder to sql: %w", err)
}
if err = db.QueryRowContext(ctx, sql, args...).Scan(&gitspaceEvent.ID); err != nil {
return database.ProcessSQLErrorf(ctx, err, "%s query failed", gitspaceEventsTable)
return database.ProcessSQLErrorf(
ctx, err, "failed to create gitspace event for %s", gitspaceEvent.QueryKey)
}
return nil
}
Expand All @@ -134,7 +135,7 @@ func (g gitspaceEventStore) List(

var gitspaceEventEntities []*gitspaceEvent
if err = db.SelectContext(ctx, &gitspaceEventEntities, sql, args...); err != nil {
return nil, 0, database.ProcessSQLErrorf(ctx, err, "Failed to find gitspace event")
return nil, 0, database.ProcessSQLErrorf(ctx, err, "Failed to list gitspace event")
}

countStmt := database.Builder.
Expand Down
16 changes: 10 additions & 6 deletions app/store/database/gitspace_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (g gitspaceInstanceStore) Find(ctx context.Context, id int64) (*types.Gitsp
gitspace := new(gitspaceInstance)
db := dbtx.GetAccessor(ctx, g.db)
if err := db.GetContext(ctx, gitspace, sql, args...); err != nil {
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find gitspace")
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find gitspace %d", id)
}
return g.mapToGitspaceInstance(ctx, gitspace)
}
Expand Down Expand Up @@ -130,7 +130,8 @@ func (g gitspaceInstanceStore) Create(ctx context.Context, gitspaceInstance *typ
}
db := dbtx.GetAccessor(ctx, g.db)
if err = db.QueryRowContext(ctx, sql, args...).Scan(&gitspaceInstance.ID); err != nil {
return database.ProcessSQLErrorf(ctx, err, "gitspace query failed")
return database.ProcessSQLErrorf(
ctx, err, "gitspace instance query failed for %s", gitspaceInstance.Identifier)
}
return nil
}
Expand All @@ -152,7 +153,8 @@ func (g gitspaceInstanceStore) Update(
}
db := dbtx.GetAccessor(ctx, g.db)
if _, err := db.ExecContext(ctx, sql, args...); err != nil {
return database.ProcessSQLErrorf(ctx, err, "Failed to update gitspace")
return database.ProcessSQLErrorf(
ctx, err, "Failed to update gitspace instance for %s", gitspaceInstance.Identifier)
}
return nil
}
Expand All @@ -176,7 +178,8 @@ func (g gitspaceInstanceStore) FindLatestByGitspaceConfigID(
gitspace := new(gitspaceInstance)
db := dbtx.GetAccessor(ctx, g.db)
if err := db.GetContext(ctx, gitspace, sql, args...); err != nil {
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find gitspace")
return nil, database.ProcessSQLErrorf(
ctx, err, "Failed to find latest gitspace instance for %d", gitspaceConfigID)
}
return g.mapToGitspaceInstance(ctx, gitspace)
}
Expand All @@ -198,7 +201,7 @@ func (g gitspaceInstanceStore) List(
db := dbtx.GetAccessor(ctx, g.db)
var dst []*gitspaceInstance
if err := db.SelectContext(ctx, &dst, sql, args...); err != nil {
return nil, database.ProcessSQLErrorf(ctx, err, "Failed executing custom list query")
return nil, database.ProcessSQLErrorf(ctx, err, "Failed executing gitspace instance list query")
}
return g.mapToGitspaceInstances(ctx, dst)
}
Expand Down Expand Up @@ -231,7 +234,8 @@ func (g gitspaceInstanceStore) FindAllLatestByGitspaceConfigID(
db := dbtx.GetAccessor(ctx, g.db)
var dst []*gitspaceInstance
if err := db.SelectContext(ctx, &dst, sql, args...); err != nil {
return nil, database.ProcessSQLErrorf(ctx, err, "Failed executing custom list query")
return nil, database.ProcessSQLErrorf(
ctx, err, "Failed executing all latest gitspace instance list query")
}
return g.mapToGitspaceInstances(ctx, dst)
}
Expand Down
Loading

0 comments on commit 54113d4

Please sign in to comment.