mirror of https://github.com/harness/drone.git
feat: [CDE-636]: Support hybrid gcp vm usecases (#3434)
* Fixing infra config and resources create flow. * Changing the metadata field's type in infra config create input. * feat: [CDE-636]: Adding validation before infra config create and update. Adding ipconf_metadata column.jobatzil/login/xforwardedfor
parent
8a5137a56b
commit
ff4c51b56f
|
@ -33,7 +33,7 @@ type CreateInput struct {
|
|||
SpaceRef string `json:"space_ref" yaml:"space_ref"` // Ref of the parent space
|
||||
Name string `json:"name" yaml:"name"`
|
||||
Type enum.InfraProviderType `json:"type" yaml:"type"`
|
||||
Metadata map[string]string `json:"metadata" yaml:"metadata"`
|
||||
Metadata map[string]any `json:"metadata" yaml:"metadata"`
|
||||
Resources []ResourceInput `json:"resources" yaml:"resources"`
|
||||
}
|
||||
|
||||
|
@ -101,6 +101,7 @@ func (c *Controller) MapToInfraProviderConfig(
|
|||
Type: in.Type,
|
||||
Created: now,
|
||||
Updated: now,
|
||||
Metadata: in.Metadata,
|
||||
}
|
||||
infraProviderConfig.Resources = mapToResourceEntity(in.Resources, parentSpace, now)
|
||||
return infraProviderConfig
|
||||
|
|
|
@ -101,7 +101,7 @@ func (c *Controller) CreateResources(
|
|||
return nil, fmt.Errorf("failed to find infraprovider config by ref: %q %w", infraProviderConfig.Identifier, err)
|
||||
}
|
||||
resources := mapToResourceEntity(in, parentSpace, now)
|
||||
err = c.infraproviderSvc.CreateResources(ctx, resources, infraProviderConfig.ID)
|
||||
err = c.infraproviderSvc.CreateResources(ctx, resources, infraProviderConfig.ID, infraProviderConfig.Identifier)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -17,7 +17,9 @@ package infraprovider
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/harness/gitness/app/api/usererror"
|
||||
"github.com/harness/gitness/infraprovider"
|
||||
"github.com/harness/gitness/types"
|
||||
|
||||
|
@ -36,12 +38,16 @@ func (c *Service) CreateInfraProvider(
|
|||
infraProviderConfig *types.InfraProviderConfig,
|
||||
) error {
|
||||
err := c.tx.WithTx(ctx, func(ctx context.Context) error {
|
||||
err := c.createConfig(ctx, infraProviderConfig)
|
||||
err := c.areNewConfigsAllowed(ctx, infraProviderConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
configID, err := c.createConfig(ctx, infraProviderConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not create the config: %q %w", infraProviderConfig.Identifier, err)
|
||||
}
|
||||
configID := infraProviderConfig.ID
|
||||
err = c.createResources(ctx, infraProviderConfig.Resources, configID)
|
||||
err = c.createResources(ctx, infraProviderConfig.Resources, configID, infraProviderConfig.Identifier)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not create the resources: %v %w", infraProviderConfig.Resources, err)
|
||||
}
|
||||
|
@ -53,17 +59,74 @@ func (c *Service) CreateInfraProvider(
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Service) createConfig(ctx context.Context, infraProviderConfig *types.InfraProviderConfig) error {
|
||||
err := c.infraProviderConfigStore.Create(ctx, infraProviderConfig)
|
||||
func (c *Service) validateConfigAndResources(infraProviderConfig *types.InfraProviderConfig) error {
|
||||
infraProvider, err := c.infraProviderFactory.GetInfraProvider(infraProviderConfig.Type)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create infraprovider config for : %q %w", infraProviderConfig.Identifier, err)
|
||||
return fmt.Errorf("failed to fetch infra provider for type %s: %w", infraProviderConfig.Type, err)
|
||||
}
|
||||
|
||||
err = infraProvider.ValidateConfigAndResources(infraProviderConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Service) areNewConfigsAllowed(ctx context.Context, infraProviderConfig *types.InfraProviderConfig) error {
|
||||
existingConfigs, err := c.fetchExistingConfigs(ctx, infraProviderConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(existingConfigs) > 0 {
|
||||
return usererror.NewWithPayload(http.StatusForbidden, fmt.Sprintf(
|
||||
"%d infra configs for provider %s exist for this account. Only 1 is allowed",
|
||||
len(existingConfigs), infraProviderConfig.Type))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Service) CreateResources(ctx context.Context, resources []types.InfraProviderResource, configID int64) error {
|
||||
func (c *Service) fetchExistingConfigs(
|
||||
ctx context.Context,
|
||||
infraProviderConfig *types.InfraProviderConfig,
|
||||
) ([]*types.InfraProviderConfig, error) {
|
||||
existingConfigs, err := c.infraProviderConfigStore.FindByType(ctx, infraProviderConfig.SpaceID,
|
||||
infraProviderConfig.Type)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to find existing infraprovider config for type %s & space %d: %w",
|
||||
infraProviderConfig.Type, infraProviderConfig.SpaceID, err)
|
||||
}
|
||||
return existingConfigs, nil
|
||||
}
|
||||
|
||||
func (c *Service) createConfig(ctx context.Context, infraProviderConfig *types.InfraProviderConfig) (int64, error) {
|
||||
err := c.validateConfigAndResources(infraProviderConfig)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
err = c.infraProviderConfigStore.Create(ctx, infraProviderConfig)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to create infraprovider config for %s: %w", infraProviderConfig.Identifier, err)
|
||||
}
|
||||
|
||||
newInfraProviderConfig, err := c.infraProviderConfigStore.FindByIdentifier(ctx, infraProviderConfig.SpaceID,
|
||||
infraProviderConfig.Identifier)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to find newly created infraprovider config %s in space %d: %w",
|
||||
infraProviderConfig.Identifier, infraProviderConfig.SpaceID, err)
|
||||
}
|
||||
return newInfraProviderConfig.ID, nil
|
||||
}
|
||||
|
||||
func (c *Service) CreateResources(
|
||||
ctx context.Context,
|
||||
resources []types.InfraProviderResource,
|
||||
configID int64,
|
||||
infraProviderConfigIdentifier string,
|
||||
) error {
|
||||
err := c.tx.WithTx(ctx, func(ctx context.Context) error {
|
||||
return c.createResources(ctx, resources, configID)
|
||||
return c.createResources(ctx, resources, configID, infraProviderConfigIdentifier)
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to complete create txn for the infraprovider resource %w", err)
|
||||
|
@ -71,20 +134,22 @@ func (c *Service) CreateResources(ctx context.Context, resources []types.InfraPr
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Service) createResources(ctx context.Context, resources []types.InfraProviderResource, configID int64) error {
|
||||
func (c *Service) createResources(
|
||||
ctx context.Context,
|
||||
resources []types.InfraProviderResource,
|
||||
configID int64,
|
||||
infraProviderConfigIdentifier string,
|
||||
) error {
|
||||
for idx := range resources {
|
||||
resource := &resources[idx]
|
||||
resource.InfraProviderConfigID = configID
|
||||
infraProvider, err := c.infraProviderFactory.GetInfraProvider(resource.InfraProviderType)
|
||||
resource.InfraProviderConfigIdentifier = infraProviderConfigIdentifier
|
||||
|
||||
err := c.validate(ctx, resource)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch infrastructure impl for type : %q %w", resource.InfraProviderType, err)
|
||||
}
|
||||
if len(infraProvider.TemplateParams()) > 0 {
|
||||
err = c.validateTemplates(ctx, infraProvider, *resource)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
err = c.infraProviderResourceStore.Create(ctx, resource)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create infraprovider resource for : %q %w", resource.UID, err)
|
||||
|
@ -96,14 +161,21 @@ func (c *Service) createResources(ctx context.Context, resources []types.InfraPr
|
|||
func (c *Service) validate(ctx context.Context, resource *types.InfraProviderResource) error {
|
||||
infraProvider, err := c.infraProviderFactory.GetInfraProvider(resource.InfraProviderType)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch infrastructure impl for type : %q %w", resource.InfraProviderType, err)
|
||||
return fmt.Errorf("failed to fetch infra impl for type : %q %w", resource.InfraProviderType, err)
|
||||
}
|
||||
|
||||
if len(infraProvider.TemplateParams()) > 0 {
|
||||
err = c.validateTemplates(ctx, infraProvider, *resource)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err = c.validateResourceParams(infraProvider, *resource)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -127,3 +199,17 @@ func (c *Service) validateTemplates(
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Service) validateResourceParams(
|
||||
infraProvider infraprovider.InfraProvider,
|
||||
res types.InfraProviderResource,
|
||||
) error {
|
||||
infraResourceParams := make([]types.InfraProviderParameter, 0)
|
||||
for key, value := range res.Metadata {
|
||||
infraResourceParams = append(infraResourceParams, types.InfraProviderParameter{
|
||||
Name: key,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
return infraProvider.ValidateParams(infraResourceParams)
|
||||
}
|
||||
|
|
|
@ -23,11 +23,17 @@ import (
|
|||
)
|
||||
|
||||
func (c *Service) updateConfig(ctx context.Context, infraProviderConfig *types.InfraProviderConfig) error {
|
||||
infraProviderConfig.Updated = time.Now().UnixMilli()
|
||||
err := c.infraProviderConfigStore.Update(ctx, infraProviderConfig)
|
||||
err := c.validateConfigAndResources(infraProviderConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update infraprovider config for : %q %w", infraProviderConfig.Identifier, err)
|
||||
return err
|
||||
}
|
||||
|
||||
infraProviderConfig.Updated = time.Now().UnixMilli()
|
||||
err = c.infraProviderConfigStore.Update(ctx, infraProviderConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update infraprovider config for %s: %w", infraProviderConfig.Identifier, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ func (c *Service) UpsertInfraProvider(
|
|||
return c.upsertConfig(ctx, space, infraProviderConfig)
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to complete txn for the infraprovider %w", err)
|
||||
return fmt.Errorf("failed to complete txn for the infraprovider: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -49,23 +49,23 @@ func (c *Service) upsertConfig(
|
|||
infraProviderConfig *types.InfraProviderConfig,
|
||||
) error {
|
||||
providerConfigInDB, err := c.Find(ctx, space, infraProviderConfig.Identifier)
|
||||
var infraProviderConfigID int64
|
||||
if errors.Is(err, store.ErrResourceNotFound) {
|
||||
if err = c.createConfig(ctx, infraProviderConfig); err != nil {
|
||||
if infraProviderConfigID, err = c.createConfig(ctx, infraProviderConfig); err != nil {
|
||||
return fmt.Errorf("could not create the config: %q %w", infraProviderConfig.Identifier, err)
|
||||
}
|
||||
log.Info().Msgf("created new infraconfig %s", infraProviderConfig.Identifier)
|
||||
providerConfigInDB, err = c.Find(ctx, space, infraProviderConfig.Identifier)
|
||||
} else if err != nil {
|
||||
} else if err != nil { // todo: should this not be err == nil?
|
||||
infraProviderConfig.ID = providerConfigInDB.ID
|
||||
if err = c.updateConfig(ctx, infraProviderConfig); err != nil {
|
||||
return fmt.Errorf("could not update the config: %q %w", infraProviderConfig.Identifier, err)
|
||||
return fmt.Errorf("could not update the config %s: %w", infraProviderConfig.Identifier, err)
|
||||
}
|
||||
log.Info().Msgf("updated infraconfig %s", infraProviderConfig.Identifier)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = c.UpsertResources(ctx, infraProviderConfig.Resources, providerConfigInDB.ID, space.ID); err != nil {
|
||||
if err = c.UpsertResources(ctx, infraProviderConfig.Resources, infraProviderConfigID, space.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
@ -87,13 +87,13 @@ func (c *Service) UpsertResources(
|
|||
_, err := c.infraProviderResourceStore.FindByIdentifier(ctx, resource.SpaceID, resource.UID)
|
||||
if errors.Is(err, store.ErrResourceNotFound) {
|
||||
if err = c.infraProviderResourceStore.Create(ctx, resource); err != nil {
|
||||
return fmt.Errorf("failed to create infraprovider resource for : %q %w", resource.UID, err)
|
||||
return fmt.Errorf("failed to create infraprovider resource for %s: %w", resource.UID, err)
|
||||
}
|
||||
log.Info().Msgf("created new resource %s/%s", resource.InfraProviderConfigIdentifier, resource.UID)
|
||||
} else {
|
||||
if err = c.UpdateResource(ctx, *resource); err != nil {
|
||||
log.Info().Msgf("updated resource %s/%s", resource.InfraProviderConfigIdentifier, resource.UID)
|
||||
return fmt.Errorf("could not update the resources: %v %w", resource.UID, err)
|
||||
return fmt.Errorf("could not update the resources %s: %w", resource.UID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -750,6 +750,13 @@ type (
|
|||
// FindByIdentifier returns a infra provider config with a given UID in a space
|
||||
FindByIdentifier(ctx context.Context, spaceID int64, identifier string) (*types.InfraProviderConfig, error)
|
||||
|
||||
// FindByType returns a infra provider config with a given type in a space
|
||||
FindByType(
|
||||
ctx context.Context,
|
||||
spaceID int64,
|
||||
infraProviderType enum.InfraProviderType,
|
||||
) ([]*types.InfraProviderConfig, error)
|
||||
|
||||
// Create creates a new infra provider config in the datastore.
|
||||
Create(ctx context.Context, infraProviderConfig *types.InfraProviderConfig) error
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ package database
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/harness/gitness/app/store"
|
||||
"github.com/harness/gitness/store/database"
|
||||
|
@ -35,7 +36,8 @@ const (
|
|||
ipconf_type,
|
||||
ipconf_space_id,
|
||||
ipconf_created,
|
||||
ipconf_updated
|
||||
ipconf_updated,
|
||||
ipconf_metadata
|
||||
`
|
||||
infraProviderConfigSelectColumns = "ipconf_id," + infraProviderConfigInsertColumns
|
||||
infraProviderConfigTable = `infra_provider_configs`
|
||||
|
@ -46,6 +48,7 @@ type infraProviderConfig struct {
|
|||
Identifier string `db:"ipconf_uid"`
|
||||
Name string `db:"ipconf_display_name"`
|
||||
Type enum.InfraProviderType `db:"ipconf_type"`
|
||||
Metadata []byte `db:"ipconf_metadata"`
|
||||
SpaceID int64 `db:"ipconf_space_id"`
|
||||
Created int64 `db:"ipconf_created"`
|
||||
Updated int64 `db:"ipconf_updated"`
|
||||
|
@ -53,7 +56,6 @@ type infraProviderConfig struct {
|
|||
|
||||
var _ store.InfraProviderConfigStore = (*infraProviderConfigStore)(nil)
|
||||
|
||||
// NewGitspaceConfigStore returns a new GitspaceConfigStore.
|
||||
func NewInfraProviderConfigStore(db *sqlx.DB) store.InfraProviderConfigStore {
|
||||
return &infraProviderConfigStore{
|
||||
db: db,
|
||||
|
@ -65,11 +67,15 @@ type infraProviderConfigStore struct {
|
|||
}
|
||||
|
||||
func (i infraProviderConfigStore) Update(ctx context.Context, infraProviderConfig *types.InfraProviderConfig) error {
|
||||
dbinfraProviderConfig := i.mapToInternalInfraProviderConfig(ctx, infraProviderConfig)
|
||||
dbinfraProviderConfig, err := i.mapToInternalInfraProviderConfig(infraProviderConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stmt := database.Builder.
|
||||
Update(infraProviderConfigTable).
|
||||
Set("ipconf_display_name", dbinfraProviderConfig.Name).
|
||||
Set("ipconf_updated", dbinfraProviderConfig.Updated).
|
||||
Set("ipconf_metadata", dbinfraProviderConfig.Metadata).
|
||||
Where("ipconf_id = ?", infraProviderConfig.ID)
|
||||
sql, args, err := stmt.ToSql()
|
||||
if err != nil {
|
||||
|
@ -97,7 +103,30 @@ func (i infraProviderConfigStore) Find(ctx context.Context, id int64) (*types.In
|
|||
if err := db.GetContext(ctx, dst, sql, args...); err != nil {
|
||||
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraprovider config %d", id)
|
||||
}
|
||||
return i.mapToInfraProviderConfig(ctx, dst), nil
|
||||
return i.mapToInfraProviderConfig(dst)
|
||||
}
|
||||
|
||||
func (i infraProviderConfigStore) FindByType(
|
||||
ctx context.Context,
|
||||
spaceID int64,
|
||||
infraProviderType enum.InfraProviderType,
|
||||
) ([]*types.InfraProviderConfig, error) {
|
||||
stmt := database.Builder.
|
||||
Select(infraProviderConfigSelectColumns).
|
||||
From(infraProviderConfigTable).
|
||||
Where("ipconf_type = $1", infraProviderType). //nolint:goconst
|
||||
Where("ipconf_space_id = $2", spaceID)
|
||||
sql, args, err := stmt.ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Failed to convert squirrel builder to sql")
|
||||
}
|
||||
|
||||
db := dbtx.GetAccessor(ctx, i.db)
|
||||
dst := new([]*infraProviderConfig)
|
||||
if err := db.SelectContext(ctx, dst, sql, args...); err != nil {
|
||||
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to list infraprovider resources")
|
||||
}
|
||||
return i.mapToInfraProviderConfigs(*dst)
|
||||
}
|
||||
|
||||
func (i infraProviderConfigStore) FindByIdentifier(
|
||||
|
@ -119,20 +148,25 @@ func (i infraProviderConfigStore) FindByIdentifier(
|
|||
if err := db.GetContext(ctx, dst, sql, args...); err != nil {
|
||||
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraprovider config %s", identifier)
|
||||
}
|
||||
return i.mapToInfraProviderConfig(ctx, dst), nil
|
||||
return i.mapToInfraProviderConfig(dst)
|
||||
}
|
||||
|
||||
func (i infraProviderConfigStore) Create(ctx context.Context, infraProviderConfig *types.InfraProviderConfig) error {
|
||||
dbinfraProviderConfig, err := i.mapToInternalInfraProviderConfig(infraProviderConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stmt := database.Builder.
|
||||
Insert(infraProviderConfigTable).
|
||||
Columns(infraProviderConfigInsertColumns).
|
||||
Values(
|
||||
infraProviderConfig.Identifier,
|
||||
infraProviderConfig.Name,
|
||||
infraProviderConfig.Type,
|
||||
infraProviderConfig.SpaceID,
|
||||
infraProviderConfig.Created,
|
||||
infraProviderConfig.Updated,
|
||||
dbinfraProviderConfig.Identifier,
|
||||
dbinfraProviderConfig.Name,
|
||||
dbinfraProviderConfig.Type,
|
||||
dbinfraProviderConfig.SpaceID,
|
||||
dbinfraProviderConfig.Created,
|
||||
dbinfraProviderConfig.Updated,
|
||||
dbinfraProviderConfig.Metadata,
|
||||
).
|
||||
Suffix(ReturningClause + infraProviderConfigIDColumn)
|
||||
sql, args, err := stmt.ToSql()
|
||||
|
@ -140,31 +174,55 @@ func (i infraProviderConfigStore) Create(ctx context.Context, infraProviderConfi
|
|||
return errors.Wrap(err, "Failed to convert squirrel builder to sql")
|
||||
}
|
||||
db := dbtx.GetAccessor(ctx, i.db)
|
||||
if err = db.QueryRowContext(ctx, sql, args...).Scan(&infraProviderConfig.ID); err != nil {
|
||||
if err = db.QueryRowContext(ctx, sql, args...).Scan(&dbinfraProviderConfig.ID); err != nil {
|
||||
return database.ProcessSQLErrorf(
|
||||
ctx, err, "infraprovider config create query failed for %s", infraProviderConfig.Identifier)
|
||||
ctx, err, "infraprovider config create query failed for %s", dbinfraProviderConfig.Identifier)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i infraProviderConfigStore) mapToInfraProviderConfig(
|
||||
_ context.Context,
|
||||
in *infraProviderConfig) *types.InfraProviderConfig {
|
||||
in *infraProviderConfig,
|
||||
) (*types.InfraProviderConfig, error) {
|
||||
metadataMap := make(map[string]any)
|
||||
marshalErr := json.Unmarshal(in.Metadata, &metadataMap)
|
||||
if marshalErr != nil {
|
||||
return nil, marshalErr
|
||||
}
|
||||
infraProviderConfigEntity := &types.InfraProviderConfig{
|
||||
ID: in.ID,
|
||||
Identifier: in.Identifier,
|
||||
Name: in.Name,
|
||||
Type: in.Type,
|
||||
Metadata: metadataMap,
|
||||
SpaceID: in.SpaceID,
|
||||
Created: in.Created,
|
||||
Updated: in.Updated,
|
||||
}
|
||||
return infraProviderConfigEntity
|
||||
return infraProviderConfigEntity, nil
|
||||
}
|
||||
|
||||
func (i infraProviderConfigStore) mapToInfraProviderConfigs(
|
||||
in []*infraProviderConfig,
|
||||
) ([]*types.InfraProviderConfig, error) {
|
||||
var err error
|
||||
res := make([]*types.InfraProviderConfig, len(in))
|
||||
for index := range in {
|
||||
res[index], err = i.mapToInfraProviderConfig(in[index])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (i infraProviderConfigStore) mapToInternalInfraProviderConfig(
|
||||
_ context.Context,
|
||||
in *types.InfraProviderConfig) *infraProviderConfig {
|
||||
in *types.InfraProviderConfig,
|
||||
) (*infraProviderConfig, error) {
|
||||
jsonBytes, marshalErr := json.Marshal(in.Metadata)
|
||||
if marshalErr != nil {
|
||||
return nil, marshalErr
|
||||
}
|
||||
infraProviderConfigEntity := &infraProviderConfig{
|
||||
Identifier: in.Identifier,
|
||||
Name: in.Name,
|
||||
|
@ -172,6 +230,7 @@ func (i infraProviderConfigStore) mapToInternalInfraProviderConfig(
|
|||
SpaceID: in.SpaceID,
|
||||
Created: in.Created,
|
||||
Updated: in.Updated,
|
||||
Metadata: jsonBytes,
|
||||
}
|
||||
return infraProviderConfigEntity
|
||||
return infraProviderConfigEntity, nil
|
||||
}
|
||||
|
|
|
@ -98,7 +98,7 @@ func (s infraProviderResourceStore) List(ctx context.Context, infraProviderConfi
|
|||
if err := db.SelectContext(ctx, dst, sql, args...); err != nil {
|
||||
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to list infraprovider resources")
|
||||
}
|
||||
return mapToInfraProviderResources(ctx, *dst)
|
||||
return mapToInfraProviderResources(*dst)
|
||||
}
|
||||
|
||||
func (s infraProviderResourceStore) Find(ctx context.Context, id int64) (*types.InfraProviderResource, error) {
|
||||
|
@ -116,7 +116,7 @@ func (s infraProviderResourceStore) Find(ctx context.Context, id int64) (*types.
|
|||
if err := db.GetContext(ctx, dst, sql, args...); err != nil {
|
||||
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraprovider resource %d", id)
|
||||
}
|
||||
return mapToInfraProviderResource(ctx, dst)
|
||||
return mapToInfraProviderResource(dst)
|
||||
}
|
||||
|
||||
func (s infraProviderResourceStore) FindByIdentifier(
|
||||
|
@ -138,7 +138,7 @@ func (s infraProviderResourceStore) FindByIdentifier(
|
|||
if err := db.GetContext(ctx, dst, sql, args...); err != nil {
|
||||
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraprovider resource %s", identifier)
|
||||
}
|
||||
return mapToInfraProviderResource(ctx, dst)
|
||||
return mapToInfraProviderResource(dst)
|
||||
}
|
||||
|
||||
func (s infraProviderResourceStore) Create(
|
||||
|
@ -184,7 +184,7 @@ func (s infraProviderResourceStore) Update(
|
|||
ctx context.Context,
|
||||
infraProviderResource *types.InfraProviderResource,
|
||||
) error {
|
||||
dbinfraProviderResource, err := s.mapToInternalInfraProviderResource(ctx, infraProviderResource)
|
||||
dbinfraProviderResource, err := s.mapToInternalInfraProviderResource(infraProviderResource)
|
||||
if err != nil {
|
||||
return fmt.Errorf(
|
||||
"failed to map to DB Obj for infraprovider resource %s", infraProviderResource.UID)
|
||||
|
@ -228,8 +228,7 @@ func (s infraProviderResourceStore) DeleteByIdentifier(ctx context.Context, spac
|
|||
return nil
|
||||
}
|
||||
|
||||
func mapToInfraProviderResource(_ context.Context,
|
||||
in *infraProviderResource) (*types.InfraProviderResource, error) {
|
||||
func mapToInfraProviderResource(in *infraProviderResource) (*types.InfraProviderResource, error) {
|
||||
metadataParamsMap := make(map[string]string)
|
||||
marshalErr := json.Unmarshal(in.OpenTofuParams, &metadataParamsMap)
|
||||
if marshalErr != nil {
|
||||
|
@ -253,8 +252,9 @@ func mapToInfraProviderResource(_ context.Context,
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (s infraProviderResourceStore) mapToInternalInfraProviderResource(_ context.Context,
|
||||
in *types.InfraProviderResource) (*infraProviderResource, error) {
|
||||
func (s infraProviderResourceStore) mapToInternalInfraProviderResource(
|
||||
in *types.InfraProviderResource,
|
||||
) (*infraProviderResource, error) {
|
||||
jsonBytes, marshalErr := json.Marshal(in.Metadata)
|
||||
if marshalErr != nil {
|
||||
return nil, marshalErr
|
||||
|
@ -276,12 +276,11 @@ func (s infraProviderResourceStore) mapToInternalInfraProviderResource(_ context
|
|||
}, nil
|
||||
}
|
||||
|
||||
func mapToInfraProviderResources(ctx context.Context,
|
||||
resources []infraProviderResource) ([]*types.InfraProviderResource, error) {
|
||||
func mapToInfraProviderResources(resources []infraProviderResource) ([]*types.InfraProviderResource, error) {
|
||||
var err error
|
||||
res := make([]*types.InfraProviderResource, len(resources))
|
||||
for i := range resources {
|
||||
res[i], err = mapToInfraProviderResource(ctx, &resources[i])
|
||||
res[i], err = mapToInfraProviderResource(&resources[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -320,7 +319,7 @@ func (i InfraProviderResourceView) Find(ctx context.Context, id int64) (*types.I
|
|||
if err := db.GetContext(ctx, dst, sql, args...); err != nil {
|
||||
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraprovider providerResource %d", id)
|
||||
}
|
||||
providerResource, err := mapToInfraProviderResource(ctx, dst)
|
||||
providerResource, err := mapToInfraProviderResource(dst)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -370,5 +369,5 @@ func (i InfraProviderResourceView) FindMany(ctx context.Context, ids []int64) ([
|
|||
if err := db.GetContext(ctx, dst, sql, args...); err != nil {
|
||||
return nil, database.ProcessSQLErrorf(ctx, err, "Failed to find infraprovider resources")
|
||||
}
|
||||
return mapToInfraProviderResources(ctx, *dst)
|
||||
return mapToInfraProviderResources(*dst)
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
ALTER TABLE infra_provider_configs DROP COLUMN ipconf_metadata;
|
|
@ -0,0 +1 @@
|
|||
ALTER TABLE infra_provider_configs ADD COLUMN ipconf_metadata JSONB;
|
|
@ -0,0 +1 @@
|
|||
ALTER TABLE infra_provider_configs DROP COLUMN ipconf_metadata;
|
|
@ -0,0 +1 @@
|
|||
ALTER TABLE infra_provider_configs ADD COLUMN ipconf_metadata JSONB;
|
|
@ -329,3 +329,7 @@ func volumeName(spacePath string, resourceKey string) string {
|
|||
name := "gitspace-" + strings.ReplaceAll(spacePath, "/", "-") + "-" + resourceKey
|
||||
return name
|
||||
}
|
||||
|
||||
func (d DockerProvider) ValidateConfigAndResources(_ *types.InfraProviderConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -68,4 +68,7 @@ type InfraProvider interface {
|
|||
|
||||
// ProvisioningType specifies whether the provider will provision new infra resources or it will reuse existing.
|
||||
ProvisioningType() enum.InfraProvisioningType
|
||||
|
||||
// ValidateConfigAndResources checks if the provided infra config and resources are valid as per the provider.
|
||||
ValidateConfigAndResources(infraProviderConfig *types.InfraProviderConfig) error
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ type InfraProviderConfig struct {
|
|||
Identifier string `json:"identifier"`
|
||||
Name string `json:"name"`
|
||||
Type enum.InfraProviderType `json:"type"`
|
||||
Metadata map[string]string `json:"metadata"`
|
||||
Metadata map[string]any `json:"metadata"`
|
||||
Resources []InfraProviderResource `json:"resources"`
|
||||
SpaceID int64 `json:"-"`
|
||||
SpacePath string `json:"space_path"`
|
||||
|
|
Loading…
Reference in New Issue