Chris Roberts e958c6183a Adds initial HCP config support
Adds initial basic support for HCP based configuration in vagrant-go.
The initalization process has been updated to remove Vagrantfile parsing
from the client, moving it to the runner using init jobs for the basis
and the project (if there is one). Detection is done on the file based
on extension for Ruby based parsing or HCP based parsing.

Current HCP parsing is extremely simple and currently just a base to
build off. Config components will be able to implement an `Init`
function to handle receiving configuration data from a non-native source
file. This will be extended to include a default approach for injecting
defined data in the future.

Some cleanup was done in the state around validations. Some logging
adjustments were applied on the Ruby side for better behavior
consistency.

VirtualBox provider now caches locale detection to prevent multiple
checks every time the driver is initialized.
2023-09-07 17:26:10 -07:00

110 lines
2.2 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package state
import (
"fmt"
"reflect"
"github.com/go-ozzo/ozzo-validation/v4"
"github.com/hashicorp/go-version"
"gorm.io/gorm"
)
type ValidationCode string
const (
VALIDATION_UNIQUE ValidationCode = "unique"
VALIDATION_MODIFIED = "modified"
VALIDATION_PROJECT = "project"
VALIDATION_TYPE = "type"
VALIDATION_VERSION = "version"
)
func checkUnique(tx *gorm.DB) validation.RuleFunc {
return func(value interface{}) error {
var count int64
result := tx.Count(&count)
if result.Error != nil {
return validation.NewInternalError(result.Error)
}
if count > 0 {
return validation.NewError(
string(VALIDATION_UNIQUE),
"must be unique",
)
}
return nil
}
}
func checkNotModified(changed bool) validation.RuleFunc {
return func(_ any) error {
if changed {
return validation.NewError(
string(VALIDATION_MODIFIED),
"cannot be modified",
)
}
return nil
}
}
func checkNotModified2(original interface{}) validation.RuleFunc {
return func(value interface{}) error {
if !reflect.DeepEqual(original, value) {
return validation.NewError(
string(VALIDATION_MODIFIED),
"cannot be modified",
)
}
return nil
}
}
func checkSameProject(projectID uint) validation.RuleFunc {
return func(value interface{}) error {
vPid, ok := value.(uint)
if !ok {
project, ok := value.(*Project)
if !ok {
return validation.NewError(
string(VALIDATION_TYPE),
fmt.Sprintf("*Project or uint required for validation (%T)", value),
)
}
vPid = project.ID
}
if vPid != projectID {
return validation.NewError(
string(VALIDATION_PROJECT),
"project must match parent project",
)
}
return nil
}
}
func checkValidVersion(value interface{}) error {
v, ok := value.(string)
if !ok {
return validation.NewError(
string(VALIDATION_TYPE),
fmt.Sprintf("version string required for validation (%T)", value),
)
}
_, err := version.NewVersion(v)
if err != nil {
return validation.NewError(
string(VALIDATION_VERSION),
"invalid version string",
)
}
return nil
}