mirror of https://github.com/harness/drone.git
feat: [CDE-244]: API for infraprovider template (#2609)
* feat: [CDE-244]: API for infraprovider templatepull/3545/head
parent
6b12b45950
commit
74e00cbcdf
|
@ -29,33 +29,33 @@ import (
|
|||
const NoResourceIdentifier = ""
|
||||
|
||||
type CreateInput struct {
|
||||
Identifier string `json:"identifier"`
|
||||
SpaceRef string `json:"space_ref"` // Ref of the parent space
|
||||
Name string `json:"name"`
|
||||
Type enum.InfraProviderType `json:"type"`
|
||||
Metadata map[string]string `json:"metadata"`
|
||||
Resources []ResourceInput `json:"resources"`
|
||||
Identifier string `json:"identifier" yaml:"identifier"`
|
||||
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"`
|
||||
Resources []ResourceInput `json:"resources" yaml:"resources"`
|
||||
}
|
||||
|
||||
type ResourceInput struct {
|
||||
Identifier string `json:"identifier"`
|
||||
Name string `json:"name"`
|
||||
InfraProviderType enum.InfraProviderType `json:"infra_provider_type"`
|
||||
CPU *string `json:"cpu"`
|
||||
Memory *string `json:"memory"`
|
||||
Disk *string `json:"disk"`
|
||||
Network *string `json:"network"`
|
||||
Region []string `json:"region"`
|
||||
Metadata map[string]string `json:"metadata"`
|
||||
GatewayHost *string `json:"gateway_host"`
|
||||
GatewayPort *string `json:"gateway_port"`
|
||||
TemplateIdentifier *string `json:"template_identifier"`
|
||||
Identifier string `json:"identifier" yaml:"identifier"`
|
||||
Name string `json:"name" yaml:"name"`
|
||||
InfraProviderType enum.InfraProviderType `json:"infra_provider_type" yaml:"infra_provider_type"`
|
||||
CPU *string `json:"cpu" yaml:"cpu"`
|
||||
Memory *string `json:"memory" yaml:"memory"`
|
||||
Disk *string `json:"disk" yaml:"disk"`
|
||||
Network *string `json:"network" yaml:"network"`
|
||||
Region []string `json:"region" yaml:"region"`
|
||||
Metadata map[string]string `json:"metadata" yaml:"metadata"`
|
||||
GatewayHost *string `json:"gateway_host" yaml:"gateway_host"`
|
||||
GatewayPort *string `json:"gateway_port" yaml:"gateway_port"`
|
||||
TemplateIdentifier *string `json:"template_identifier" yaml:"template_identifier"`
|
||||
}
|
||||
|
||||
type TemplateInput struct {
|
||||
Identifier string `json:"identifier"`
|
||||
Description string `json:"description"`
|
||||
Data string `json:"data"`
|
||||
Identifier string `json:"identifier" yaml:"identifier"`
|
||||
Description string `json:"description" yaml:"description"`
|
||||
Data string `json:"data" yaml:"data"`
|
||||
}
|
||||
|
||||
// Create creates a new infra provider.
|
||||
|
@ -81,6 +81,19 @@ func (c *Controller) Create(
|
|||
return nil, err
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
infraProviderConfig := c.MapToInfraProviderConfig(in, 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)
|
||||
}
|
||||
return infraProviderConfig, nil
|
||||
}
|
||||
|
||||
func (c *Controller) MapToInfraProviderConfig(
|
||||
in CreateInput,
|
||||
parentSpace *types.Space,
|
||||
now int64,
|
||||
) *types.InfraProviderConfig {
|
||||
infraProviderConfig := &types.InfraProviderConfig{
|
||||
Identifier: in.Identifier,
|
||||
Name: in.Name,
|
||||
|
@ -90,11 +103,7 @@ func (c *Controller) Create(
|
|||
Updated: now,
|
||||
}
|
||||
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)
|
||||
}
|
||||
return infraProviderConfig, nil
|
||||
return infraProviderConfig
|
||||
}
|
||||
|
||||
func (c *Controller) sanitizeCreateInput(in CreateInput) error {
|
||||
|
|
|
@ -38,11 +38,11 @@ func (c *Service) CreateInfraProvider(
|
|||
err := c.tx.WithTx(ctx, func(ctx context.Context) error {
|
||||
err := c.createConfig(ctx, infraProviderConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not autocreate the config: %q %w", infraProviderConfig.Identifier, err)
|
||||
return fmt.Errorf("could not create the config: %q %w", infraProviderConfig.Identifier, err)
|
||||
}
|
||||
err = c.createResources(ctx, infraProviderConfig.Resources, infraProviderConfig.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not autocreate the resources: %v %w", infraProviderConfig.Resources, err)
|
||||
return fmt.Errorf("could not create the resources: %v %w", infraProviderConfig.Resources, err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
|
|
@ -17,10 +17,42 @@ package infraprovider
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/harness/gitness/types"
|
||||
)
|
||||
|
||||
func (c *Service) UpdateInfraProvider(
|
||||
ctx context.Context,
|
||||
infraProviderConfig *types.InfraProviderConfig,
|
||||
) error {
|
||||
err := c.tx.WithTx(ctx, func(ctx context.Context) error {
|
||||
err := c.updateConfig(ctx, infraProviderConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not update the config: %q %w", infraProviderConfig.Identifier, err)
|
||||
}
|
||||
for _, resource := range infraProviderConfig.Resources {
|
||||
if err = c.UpdateResource(ctx, resource); err != nil {
|
||||
return fmt.Errorf("could not update the resources: %v %w", infraProviderConfig.Resources, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to complete txn for the infraprovider %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Service) updateConfig(ctx context.Context, infraProviderConfig *types.InfraProviderConfig) error {
|
||||
infraProviderConfig.Updated = time.Now().UnixMilli()
|
||||
err := c.infraProviderConfigStore.Update(ctx, infraProviderConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update infraprovider config for : %q %w", infraProviderConfig.Identifier, 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)
|
||||
|
@ -32,6 +64,7 @@ func (c *Service) UpdateResource(ctx context.Context, resource types.InfraProvid
|
|||
return err
|
||||
}
|
||||
resource.ID = infraProviderResource.ID
|
||||
resource.Updated = time.Now().UnixMilli()
|
||||
if err = c.infraProviderResourceStore.Update(ctx, &resource); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -200,12 +200,12 @@ func (s infraProviderResourceStore) Update(
|
|||
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).
|
||||
Set("ipreso_updated", dbinfraProviderResource.Updated).
|
||||
Where("ipreso_id = ?", infraProviderResource.ID)
|
||||
sql, args, err := stmt.ToSql()
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue