diff --git a/app/api/controller/gitspace/action.go b/app/api/controller/gitspace/action.go index dd88b60cf8..f9a5e0d4c6 100644 --- a/app/api/controller/gitspace/action.go +++ b/app/api/controller/gitspace/action.go @@ -16,6 +16,7 @@ package gitspace import ( "context" + "errors" "fmt" "strconv" "strings" @@ -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" @@ -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) diff --git a/app/api/controller/gitspace/create.go b/app/api/controller/gitspace/create.go index 8216b793f7..ba66a114dc 100644 --- a/app/api/controller/gitspace/create.go +++ b/app/api/controller/gitspace/create.go @@ -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" @@ -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. @@ -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 { diff --git a/app/api/controller/infraprovider/create.go b/app/api/controller/infraprovider/create.go index 432c2c682b..acfa040946 100644 --- a/app/api/controller/infraprovider/create.go +++ b/app/api/controller/infraprovider/create.go @@ -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) diff --git a/app/services/gitspace/find.go b/app/services/gitspace/find.go index 7c5ae0035b..caf144d6b2 100644 --- a/app/services/gitspace/find.go +++ b/app/services/gitspace/find.go @@ -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, @@ -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 diff --git a/app/services/infraprovider/infraprovider.go b/app/services/infraprovider/infraprovider.go index ea43b5b7c8..d1c116034a 100644 --- a/app/services/infraprovider/infraprovider.go +++ b/app/services/infraprovider/infraprovider.go @@ -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( @@ -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 } @@ -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]) } } } diff --git a/app/store/database.go b/app/store/database.go index eca9bc661c..82c05b7e0e 100644 --- a/app/store/database.go +++ b/app/store/database.go @@ -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 { @@ -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, @@ -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 } diff --git a/app/store/database/gitspace_config.go b/app/store/database/gitspace_config.go index a74a231e8d..bc325a494c 100644 --- a/app/store/database/gitspace_config.go +++ b/app/store/database/gitspace_config.go @@ -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) } @@ -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) } @@ -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 } @@ -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 } @@ -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) } @@ -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) } diff --git a/app/store/database/gitspace_event.go b/app/store/database/gitspace_event.go index 9cd19901c5..1deafb66d1 100644 --- a/app/store/database/gitspace_event.go +++ b/app/store/database/gitspace_event.go @@ -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 } @@ -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 } @@ -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. diff --git a/app/store/database/gitspace_instance.go b/app/store/database/gitspace_instance.go index fe8b2065f7..a092fc3d52 100644 --- a/app/store/database/gitspace_instance.go +++ b/app/store/database/gitspace_instance.go @@ -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) } @@ -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 } @@ -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 } @@ -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) } @@ -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) } @@ -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) } diff --git a/app/store/database/infra_provider_config.go b/app/store/database/infra_provider_config.go index 874bcb3407..d6fa68484b 100644 --- a/app/store/database/infra_provider_config.go +++ b/app/store/database/infra_provider_config.go @@ -64,6 +64,25 @@ type infraProviderConfigStore struct { db *sqlx.DB } +func (i infraProviderConfigStore) Update(ctx context.Context, infraProviderConfig *types.InfraProviderConfig) error { + dbinfraProviderConfig := i.mapToInternalInfraProviderConfig(ctx, infraProviderConfig) + stmt := database.Builder. + Update(infraProviderConfigTable). + Set("ipconf_display_name", dbinfraProviderConfig.Name). + Set("ipconf_updated", dbinfraProviderConfig.Updated). + Where("ipconf_id = ?", infraProviderConfig.ID) + sql, args, err := stmt.ToSql() + if err != nil { + return errors.Wrap(err, "Failed to convert squirrel builder to sql") + } + db := dbtx.GetAccessor(ctx, i.db) + if _, err := db.ExecContext(ctx, sql, args...); err != nil { + return database.ProcessSQLErrorf( + ctx, err, "Failed to update infra provider config %s", infraProviderConfig.Identifier) + } + return nil +} + func (i infraProviderConfigStore) Find(ctx context.Context, id int64) (*types.InfraProviderConfig, error) { stmt := database.Builder. Select(infraProviderConfigSelectColumns). @@ -76,9 +95,9 @@ func (i infraProviderConfigStore) Find(ctx context.Context, id int64) (*types.In dst := new(infraProviderConfig) db := dbtx.GetAccessor(ctx, i.db) if err := db.GetContext(ctx, dst, sql, args...); err != nil { - return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraProviderConfig") + return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraprovider config %d", id) } - return i.mapToInfraProviderConfig(ctx, dst) + return i.mapToInfraProviderConfig(ctx, dst), nil } func (i infraProviderConfigStore) FindByIdentifier( @@ -98,9 +117,9 @@ func (i infraProviderConfigStore) FindByIdentifier( dst := new(infraProviderConfig) db := dbtx.GetAccessor(ctx, i.db) if err := db.GetContext(ctx, dst, sql, args...); err != nil { - return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraProviderConfig") + return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraprovider config %s", identifier) } - return i.mapToInfraProviderConfig(ctx, dst) + return i.mapToInfraProviderConfig(ctx, dst), nil } func (i infraProviderConfigStore) Create(ctx context.Context, infraProviderConfig *types.InfraProviderConfig) error { @@ -122,14 +141,15 @@ func (i infraProviderConfigStore) Create(ctx context.Context, infraProviderConfi } db := dbtx.GetAccessor(ctx, i.db) if err = db.QueryRowContext(ctx, sql, args...).Scan(&infraProviderConfig.ID); err != nil { - return database.ProcessSQLErrorf(ctx, err, "infra config query failed") + return database.ProcessSQLErrorf( + ctx, err, "infraprovider config create query failed for %s", infraProviderConfig.Identifier) } return nil } func (i infraProviderConfigStore) mapToInfraProviderConfig( _ context.Context, - in *infraProviderConfig) (*types.InfraProviderConfig, error) { + in *infraProviderConfig) *types.InfraProviderConfig { infraProviderConfigEntity := &types.InfraProviderConfig{ ID: in.ID, Identifier: in.Identifier, @@ -139,5 +159,19 @@ func (i infraProviderConfigStore) mapToInfraProviderConfig( Created: in.Created, Updated: in.Updated, } - return infraProviderConfigEntity, nil + return infraProviderConfigEntity +} + +func (i infraProviderConfigStore) mapToInternalInfraProviderConfig( + _ context.Context, + in *types.InfraProviderConfig) *infraProviderConfig { + infraProviderConfigEntity := &infraProviderConfig{ + Identifier: in.Identifier, + Name: in.Name, + Type: in.Type, + SpaceID: in.SpaceID, + Created: in.Created, + Updated: in.Updated, + } + return infraProviderConfigEntity } diff --git a/app/store/database/infra_provider_resource.go b/app/store/database/infra_provider_resource.go index c9203be4e3..69cda7c846 100644 --- a/app/store/database/infra_provider_resource.go +++ b/app/store/database/infra_provider_resource.go @@ -17,6 +17,7 @@ package database import ( "context" "encoding/json" + "fmt" "github.com/harness/gitness/app/store" "github.com/harness/gitness/store/database" @@ -100,39 +101,11 @@ func (s infraProviderResourceStore) List(ctx context.Context, infraProviderConfi db := dbtx.GetAccessor(ctx, s.db) dst := new([]infraProviderResource) 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 to list infraprovider resources") } return s.mapToInfraProviderResources(ctx, *dst) } -func (s infraProviderResourceStore) mapToInfraProviderResource(_ context.Context, - in *infraProviderResource) (*types.InfraProviderResource, error) { - openTofuParamsMap := make(map[string]string) - marshalErr := json.Unmarshal(in.OpenTofuParams, &openTofuParamsMap) - if marshalErr != nil { - return nil, marshalErr - } - return &types.InfraProviderResource{ - Identifier: in.Identifier, - InfraProviderConfigID: in.InfraProviderConfigID, - ID: in.ID, - InfraProviderType: in.InfraProviderType, - Name: in.Name, - SpaceID: in.SpaceID, - CPU: in.CPU.Ptr(), - Memory: in.Memory.Ptr(), - Disk: in.Disk.Ptr(), - Network: in.Network.Ptr(), - Region: in.Region, - Metadata: openTofuParamsMap, - GatewayHost: in.GatewayHost.Ptr(), - GatewayPort: in.GatewayPort.Ptr(), - TemplateID: in.TemplateID.Ptr(), - Created: in.Created, - Updated: in.Updated, - }, nil -} - func (s infraProviderResourceStore) Find(ctx context.Context, id int64) (*types.InfraProviderResource, error) { stmt := database.Builder. Select(infraProviderResourceSelectColumns). @@ -146,7 +119,7 @@ func (s infraProviderResourceStore) Find(ctx context.Context, id int64) (*types. dst := new(infraProviderResource) 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 infraProviderResource") + return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraprovider resource %d", id) } return s.mapToInfraProviderResource(ctx, dst) } @@ -168,7 +141,7 @@ func (s infraProviderResourceStore) FindByIdentifier( dst := new(infraProviderResource) 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 infraProviderResource") + return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraprovider resource %s", identifier) } return s.mapToInfraProviderResource(ctx, dst) } @@ -209,7 +182,39 @@ func (s infraProviderResourceStore) Create( } db := dbtx.GetAccessor(ctx, s.db) if err = db.QueryRowContext(ctx, sql, args...).Scan(&infraProviderResource.ID); err != nil { - return database.ProcessSQLErrorf(ctx, err, "infra provider resource query failed") + return database.ProcessSQLErrorf( + ctx, err, "infra provider resource create failed %s", infraProviderResource.Identifier) + } + return nil +} + +func (s infraProviderResourceStore) Update( + ctx context.Context, + infraProviderResource *types.InfraProviderResource, +) error { + dbinfraProviderResource, err := s.mapToInternalInfraProviderResource(ctx, infraProviderResource) + if err != nil { + return fmt.Errorf( + "failed to map to DB Obj for infraprovider resource %s", infraProviderResource.Identifier) + } + stmt := database.Builder. + Update(infraProviderResourceTable). + Set("ipreso_display_name", dbinfraProviderResource.Name). + Set("ipreso_updated", dbinfraProviderResource.Updated). + Set("ipreso_memory", dbinfraProviderResource.Memory). + Set("ipreso_disk", dbinfraProviderResource.Disk). + Set("ipreso_network", dbinfraProviderResource.Network). + Set("ipreso_region", dbinfraProviderResource.Region). + Set("ipreso_opentofu_params", dbinfraProviderResource.OpenTofuParams). + Where("ipreso_id = ?", infraProviderResource.ID) + sql, args, err := stmt.ToSql() + if err != nil { + return errors.Wrap(err, "Failed to convert squirrel builder to sql") + } + db := dbtx.GetAccessor(ctx, s.db) + if _, err := db.ExecContext(ctx, sql, args...); err != nil { + return database.ProcessSQLErrorf( + ctx, err, "Failed to update infraprovider resource %s", infraProviderResource.Identifier) } return nil } @@ -225,11 +230,66 @@ func (s infraProviderResourceStore) DeleteByIdentifier(ctx context.Context, spac } db := dbtx.GetAccessor(ctx, s.db) if _, err := db.ExecContext(ctx, sql, args...); err != nil { - return database.ProcessSQLErrorf(ctx, err, "Failed to delete infra provider resource") + return database.ProcessSQLErrorf( + ctx, err, "Failed to delete infra provider resource %s", identifier) } return nil } +func (s infraProviderResourceStore) mapToInfraProviderResource(_ context.Context, + in *infraProviderResource) (*types.InfraProviderResource, error) { + openTofuParamsMap := make(map[string]string) + marshalErr := json.Unmarshal(in.OpenTofuParams, &openTofuParamsMap) + if marshalErr != nil { + return nil, marshalErr + } + return &types.InfraProviderResource{ + Identifier: in.Identifier, + InfraProviderConfigID: in.InfraProviderConfigID, + ID: in.ID, + InfraProviderType: in.InfraProviderType, + Name: in.Name, + SpaceID: in.SpaceID, + CPU: in.CPU.Ptr(), + Memory: in.Memory.Ptr(), + Disk: in.Disk.Ptr(), + Network: in.Network.Ptr(), + Region: in.Region, + Metadata: openTofuParamsMap, + GatewayHost: in.GatewayHost.Ptr(), + GatewayPort: in.GatewayPort.Ptr(), + TemplateID: in.TemplateID.Ptr(), + Created: in.Created, + Updated: in.Updated, + }, nil +} + +func (s infraProviderResourceStore) mapToInternalInfraProviderResource(_ context.Context, + in *types.InfraProviderResource) (*infraProviderResource, error) { + jsonBytes, marshalErr := json.Marshal(in.Metadata) + if marshalErr != nil { + return nil, marshalErr + } + return &infraProviderResource{ + Identifier: in.Identifier, + InfraProviderConfigID: in.InfraProviderConfigID, + InfraProviderType: in.InfraProviderType, + Name: in.Name, + SpaceID: in.SpaceID, + CPU: null.StringFromPtr(in.CPU), + Memory: null.StringFromPtr(in.Memory), + Disk: null.StringFromPtr(in.Disk), + Network: null.StringFromPtr(in.Network), + Region: in.Region, + OpenTofuParams: jsonBytes, + GatewayHost: null.StringFromPtr(in.GatewayHost), + GatewayPort: null.StringFromPtr(in.GatewayPort), + TemplateID: null.IntFromPtr(in.TemplateID), + Created: in.Created, + Updated: in.Updated, + }, nil +} + func (s infraProviderResourceStore) mapToInfraProviderResources(ctx context.Context, resources []infraProviderResource) ([]*types.InfraProviderResource, error) { var err error diff --git a/app/store/database/infra_provider_template.go b/app/store/database/infra_provider_template.go index 2418bbc5f5..7ee5d13d68 100644 --- a/app/store/database/infra_provider_template.go +++ b/app/store/database/infra_provider_template.go @@ -85,7 +85,7 @@ func (i infraProviderTemplateStore) FindByIdentifier( infraProviderTemplateEntity := new(infraProviderTemplate) db := dbtx.GetAccessor(ctx, i.db) if err := db.GetContext(ctx, infraProviderTemplateEntity, sql, args...); err != nil { - return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraProviderTemplate") + return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraprovider template %s", identifier) } return infraProviderTemplateEntity.mapToDTO(), nil } @@ -107,7 +107,7 @@ func (i infraProviderTemplateStore) Find( infraProviderTemplateEntity := new(infraProviderTemplate) db := dbtx.GetAccessor(ctx, i.db) if err := db.GetContext(ctx, infraProviderTemplateEntity, sql, args...); err != nil { - return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraProviderTemplate") + return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraprovider template %d", id) } return infraProviderTemplateEntity.mapToDTO(), nil } @@ -135,7 +135,32 @@ func (i infraProviderTemplateStore) Create( } db := dbtx.GetAccessor(ctx, i.db) if err = db.QueryRowContext(ctx, sql, args...).Scan(&infraProviderTemplate.ID); err != nil { - return database.ProcessSQLErrorf(ctx, err, "infraProviderTemplate query failed") + return database.ProcessSQLErrorf( + ctx, err, "infraprovider template create failed %s", infraProviderTemplate.Identifier) + } + return nil +} + +func (i infraProviderTemplateStore) Update( + ctx context.Context, + infraProviderTemplate *types.InfraProviderTemplate, +) error { + dbinfraProviderTemplate := i.mapToInternalInfraProviderTemplate(infraProviderTemplate) + stmt := database.Builder. + Update(infraProviderResourceTable). + Set("iptemp_description", dbinfraProviderTemplate.Description). + Set("ipreso_updated", dbinfraProviderTemplate.Updated). + Set("iptemp_data", dbinfraProviderTemplate.Data). + Set("iptemp_version", dbinfraProviderTemplate.Version+1). + Where("iptemp_id = ?", infraProviderTemplate.ID) + sql, args, err := stmt.ToSql() + if err != nil { + return errors.Wrap(err, "Failed to convert squirrel builder to sql") + } + db := dbtx.GetAccessor(ctx, i.db) + if _, err := db.ExecContext(ctx, sql, args...); err != nil { + return database.ProcessSQLErrorf( + ctx, err, "Failed to update infraprovider template %s", infraProviderTemplate.Identifier) } return nil } @@ -151,11 +176,25 @@ func (i infraProviderTemplateStore) Delete(ctx context.Context, id int64) error } db := dbtx.GetAccessor(ctx, i.db) if _, err := db.ExecContext(ctx, sql, args...); err != nil { - return database.ProcessSQLErrorf(ctx, err, "Failed to delete infra provider template") + return database.ProcessSQLErrorf(ctx, err, "Failed to delete infraprovider template") } return nil } +func (i infraProviderTemplateStore) mapToInternalInfraProviderTemplate( + template *types.InfraProviderTemplate) infraProviderTemplate { + return infraProviderTemplate{ + Identifier: template.Identifier, + InfraProviderConfigID: template.InfraProviderConfigID, + Description: template.Description, + Data: template.Data, + Version: template.Version, + SpaceID: template.SpaceID, + Created: template.Created, + Updated: template.Updated, + } +} + func (entity infraProviderTemplate) mapToDTO() *types.InfraProviderTemplate { return &types.InfraProviderTemplate{ ID: entity.ID, diff --git a/app/store/database/infra_provisioned.go b/app/store/database/infra_provisioned.go index 94720754c7..530d785f8e 100644 --- a/app/store/database/infra_provisioned.go +++ b/app/store/database/infra_provisioned.go @@ -101,7 +101,7 @@ func (i infraProvisionedStore) Find(ctx context.Context, id int64) (*types.Infra db := dbtx.GetAccessor(ctx, i.db) err = db.GetContext(ctx, entity, sql, args...) if err != nil { - return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraProvisioned") + return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraprovisioned for %d", id) } return entity.toDTO(), nil @@ -129,7 +129,8 @@ func (i infraProvisionedStore) FindAllLatestByGateway( db := dbtx.GetAccessor(ctx, i.db) err = db.SelectContext(ctx, &entities, sql, args...) if err != nil { - return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraProvisioned") + return nil, database.ProcessSQLErrorf( + ctx, err, "Failed to find infraprovisioned for host %s", gatewayHost) } var result = make([]*types.InfraProvisionedGatewayView, len(entities)) @@ -165,7 +166,8 @@ func (i infraProvisionedStore) FindLatestByGitspaceInstanceID( entity := new(infraProvisioned) db := dbtx.GetAccessor(ctx, i.db) if err := db.GetContext(ctx, entity, sql, args...); err != nil { - return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraProvisioned") + return nil, database.ProcessSQLErrorf( + ctx, err, "Failed to find latestinfraprovisioned for instance %d", gitspaceInstanceID) } return entity.toDTO(), nil @@ -192,7 +194,8 @@ func (i infraProvisionedStore) FindLatestByGitspaceInstanceIdentifier( entity := new(infraProvisioned) db := dbtx.GetAccessor(ctx, i.db) if err := db.GetContext(ctx, entity, sql, args...); err != nil { - return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraProvisioned") + return nil, database.ProcessSQLErrorf( + ctx, err, "Failed to find infraprovisioned for instance %s", gitspaceInstanceIdentifier) } return entity.toDTO(), nil @@ -226,7 +229,9 @@ func (i infraProvisionedStore) Create(ctx context.Context, infraProvisioned *typ db := dbtx.GetAccessor(ctx, i.db) if err = db.QueryRowContext(ctx, sql, args...).Scan(&infraProvisioned.ID); err != nil { - return database.ProcessSQLErrorf(ctx, err, "infraProvisioned query failed") + return database.ProcessSQLErrorf( + ctx, err, "infraprovisioned create query failed for instance : %d", + infraProvisioned.GitspaceInstanceID) } return nil @@ -244,7 +249,7 @@ func (i infraProvisionedStore) Delete(ctx context.Context, id int64) error { db := dbtx.GetAccessor(ctx, i.db) if _, err := db.ExecContext(ctx, sql, args...); err != nil { - return database.ProcessSQLErrorf(ctx, err, "Failed to delete infra provisioned") + return database.ProcessSQLErrorf(ctx, err, "Failed to delete infraprovisioned for %d", id) } return nil @@ -270,7 +275,8 @@ func (i infraProvisionedStore) Update(ctx context.Context, infraProvisioned *typ db := dbtx.GetAccessor(ctx, i.db) if _, err := db.ExecContext(ctx, sql, args...); err != nil { - return database.ProcessSQLErrorf(ctx, err, "Failed to update infra provisioned") + return database.ProcessSQLErrorf( + ctx, err, "Failed to update infra provisioned for instance %d", infraProvisioned.GitspaceInstanceID) } return nil