mirror of
https://github.com/harness/drone.git
synced 2025-04-05 00:11:22 +00:00
feat: [CDE-699]: modify infra provider create resource api (#3600)
* feat: [CDE-697]: make cpu, memory and network as empty if nil * feat: [CDE-699]: add comment * feat: [CDE-699]: modify infra provider create resource api
This commit is contained in:
parent
93c3f324ce
commit
d79d292d87
@ -39,7 +39,7 @@ type ResourceInput struct {
|
|||||||
Memory *string `json:"memory" yaml:"memory"`
|
Memory *string `json:"memory" yaml:"memory"`
|
||||||
Disk *string `json:"disk" yaml:"disk"`
|
Disk *string `json:"disk" yaml:"disk"`
|
||||||
Network *string `json:"network" yaml:"network"`
|
Network *string `json:"network" yaml:"network"`
|
||||||
Region []string `json:"region" yaml:"region"`
|
Region string `json:"region" yaml:"region"`
|
||||||
Metadata map[string]string `json:"metadata" yaml:"metadata"`
|
Metadata map[string]string `json:"metadata" yaml:"metadata"`
|
||||||
GatewayHost *string `json:"gateway_host" yaml:"gateway_host"`
|
GatewayHost *string `json:"gateway_host" yaml:"gateway_host"`
|
||||||
GatewayPort *string `json:"gateway_port" yaml:"gateway_port"`
|
GatewayPort *string `json:"gateway_port" yaml:"gateway_port"`
|
||||||
|
@ -17,7 +17,6 @@ package infraprovider
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
apiauth "github.com/harness/gitness/app/api/auth"
|
apiauth "github.com/harness/gitness/app/api/auth"
|
||||||
@ -124,7 +123,7 @@ func (c *Controller) MapToResourceEntity(
|
|||||||
Memory: res.Memory,
|
Memory: res.Memory,
|
||||||
Disk: res.Disk,
|
Disk: res.Disk,
|
||||||
Network: res.Network,
|
Network: res.Network,
|
||||||
Region: strings.Join(res.Region, " "), // TODO fix
|
Region: res.Region,
|
||||||
Metadata: res.Metadata,
|
Metadata: res.Metadata,
|
||||||
Created: now,
|
Created: now,
|
||||||
Updated: now,
|
Updated: now,
|
||||||
|
@ -19,7 +19,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/harness/gitness/infraprovider"
|
|
||||||
"github.com/harness/gitness/store"
|
"github.com/harness/gitness/store"
|
||||||
"github.com/harness/gitness/types"
|
"github.com/harness/gitness/types"
|
||||||
|
|
||||||
@ -47,10 +46,27 @@ func (c *Service) createMissingResources(
|
|||||||
configID int64,
|
configID int64,
|
||||||
spaceID int64,
|
spaceID int64,
|
||||||
) error {
|
) error {
|
||||||
|
emptyStr := ""
|
||||||
for idx := range resources {
|
for idx := range resources {
|
||||||
resource := &resources[idx]
|
resource := &resources[idx]
|
||||||
resource.InfraProviderConfigID = configID
|
resource.InfraProviderConfigID = configID
|
||||||
resource.SpaceID = spaceID
|
resource.SpaceID = spaceID
|
||||||
|
if resource.CPU == nil {
|
||||||
|
resource.CPU = &emptyStr
|
||||||
|
}
|
||||||
|
if resource.Memory == nil {
|
||||||
|
resource.Memory = &emptyStr
|
||||||
|
}
|
||||||
|
if resource.Network == nil {
|
||||||
|
resource.Network = &emptyStr
|
||||||
|
}
|
||||||
|
// updating metadata based on infra provider type
|
||||||
|
updatedMetadata, err := c.updateResourceMetadata(resource)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("creating missing infra resources: %w", err)
|
||||||
|
}
|
||||||
|
resource.Metadata = updatedMetadata
|
||||||
|
|
||||||
if err := c.validateResource(ctx, resource); err != nil {
|
if err := c.validateResource(ctx, resource); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -66,6 +82,20 @@ func (c *Service) createMissingResources(
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Service) updateResourceMetadata(resource *types.InfraProviderResource) (map[string]string, error) {
|
||||||
|
infraProvider, err := c.infraProviderFactory.GetInfraProvider(resource.InfraProviderType)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to fetch infra impl for type : %q %w", resource.InfraProviderType, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
params, err := infraProvider.UpdateParams(toResourceParams(resource.Metadata))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return toMetadata(params), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Service) validateResource(ctx context.Context, resource *types.InfraProviderResource) error {
|
func (c *Service) validateResource(ctx context.Context, resource *types.InfraProviderResource) error {
|
||||||
infraProvider, err := c.infraProviderFactory.GetInfraProvider(resource.InfraProviderType)
|
infraProvider, err := c.infraProviderFactory.GetInfraProvider(resource.InfraProviderType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -79,7 +109,7 @@ func (c *Service) validateResource(ctx context.Context, resource *types.InfraPro
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.validateResourceParams(infraProvider, *resource)
|
err = infraProvider.ValidateParams(toResourceParams(resource.Metadata))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -87,16 +117,24 @@ func (c *Service) validateResource(ctx context.Context, resource *types.InfraPro
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Service) validateResourceParams(
|
func toResourceParams(metadata map[string]string) []types.InfraProviderParameter {
|
||||||
infraProvider infraprovider.InfraProvider,
|
var infraResourceParams []types.InfraProviderParameter
|
||||||
res types.InfraProviderResource,
|
for key, value := range metadata {
|
||||||
) error {
|
|
||||||
infraResourceParams := make([]types.InfraProviderParameter, 0)
|
|
||||||
for key, value := range res.Metadata {
|
|
||||||
infraResourceParams = append(infraResourceParams, types.InfraProviderParameter{
|
infraResourceParams = append(infraResourceParams, types.InfraProviderParameter{
|
||||||
Name: key,
|
Name: key,
|
||||||
Value: value,
|
Value: value,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return infraProvider.ValidateParams(infraResourceParams)
|
|
||||||
|
return infraResourceParams
|
||||||
|
}
|
||||||
|
|
||||||
|
func toMetadata(params []types.InfraProviderParameter) map[string]string {
|
||||||
|
metadata := make(map[string]string)
|
||||||
|
|
||||||
|
for _, param := range params {
|
||||||
|
metadata[param.Name] = param.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
return metadata
|
||||||
}
|
}
|
||||||
|
@ -288,6 +288,10 @@ func (d DockerProvider) ValidateParams(_ []types.InfraProviderParameter) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d DockerProvider) UpdateParams(ip []types.InfraProviderParameter) ([]types.InfraProviderParameter, error) {
|
||||||
|
return ip, nil
|
||||||
|
}
|
||||||
|
|
||||||
// TemplateParams returns nil as no template params are used.
|
// TemplateParams returns nil as no template params are used.
|
||||||
func (d DockerProvider) TemplateParams() []types.InfraProviderParameterSchema {
|
func (d DockerProvider) TemplateParams() []types.InfraProviderParameterSchema {
|
||||||
return nil
|
return nil
|
||||||
|
@ -70,6 +70,9 @@ type InfraProvider interface {
|
|||||||
// AvailableParams provides a schema to define the infrastructure.
|
// AvailableParams provides a schema to define the infrastructure.
|
||||||
AvailableParams() []types.InfraProviderParameterSchema
|
AvailableParams() []types.InfraProviderParameterSchema
|
||||||
|
|
||||||
|
// UpdateParams updates input Parameters to add or modify given inputParameters.
|
||||||
|
UpdateParams(inputParameters []types.InfraProviderParameter) ([]types.InfraProviderParameter, error)
|
||||||
|
|
||||||
// ValidateParams validates the supplied params before defining the infrastructure resource .
|
// ValidateParams validates the supplied params before defining the infrastructure resource .
|
||||||
ValidateParams(inputParameters []types.InfraProviderParameter) error
|
ValidateParams(inputParameters []types.InfraProviderParameter) error
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user