mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-09-16 22:50:47 +00:00
sparkles: Web UI schema validations (#934)
* Allow validator to receive a config-schema It will try to process a URL first, otherwise a file and if the file is not present it will try to read the source as a cloud-config Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> * Validate cloud-config on WebUI Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com> --------- Signed-off-by: Mauro Morales <mauro.morales@spectrocloud.com>
This commit is contained in:
@@ -23,11 +23,11 @@ func JSONSchema(version string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate ensures that a given schema is Valid according to the Root Schema from the agent.
|
// Validate ensures that a given schema is Valid according to the Root Schema from the agent.
|
||||||
func Validate(file string) error {
|
func Validate(source string) error {
|
||||||
var yaml string
|
var yaml string
|
||||||
|
|
||||||
if strings.HasPrefix(file, "http") {
|
if strings.HasPrefix(source, "http") {
|
||||||
resp, err := http.Get(file)
|
resp, err := http.Get(source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -38,11 +38,16 @@ func Validate(file string) error {
|
|||||||
//Convert the body to type string
|
//Convert the body to type string
|
||||||
yaml = string(body)
|
yaml = string(body)
|
||||||
} else {
|
} else {
|
||||||
dat, err := os.ReadFile(file)
|
dat, err := os.ReadFile(source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
if strings.Contains(err.Error(), "no such file or directory") {
|
||||||
|
yaml = source
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
yaml = string(dat)
|
||||||
}
|
}
|
||||||
yaml = string(dat)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
config, err := schema.NewConfigFromYAML(yaml, schema.RootSchema{})
|
config, err := schema.NewConfigFromYAML(yaml, schema.RootSchema{})
|
||||||
|
@@ -69,7 +69,22 @@
|
|||||||
try {
|
try {
|
||||||
// Parse the YAML
|
// Parse the YAML
|
||||||
const yaml = YAML.parse(this.content);
|
const yaml = YAML.parse(this.content);
|
||||||
this.valid=true
|
const formData = new FormData()
|
||||||
|
formData.append('cloud-config', this.content)
|
||||||
|
const settings = {
|
||||||
|
method: 'POST',
|
||||||
|
body: formData,
|
||||||
|
};
|
||||||
|
fetch(`/validate`, settings)
|
||||||
|
.then((response) => response.text())
|
||||||
|
.then((text) => {
|
||||||
|
if (text === '') {
|
||||||
|
this.valid = true
|
||||||
|
} else {
|
||||||
|
this.error = text
|
||||||
|
this.valid = false
|
||||||
|
}
|
||||||
|
})
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
this.error = error
|
this.error = error
|
||||||
this.valid = false
|
this.valid = false
|
||||||
|
@@ -161,6 +161,21 @@ func Start(ctx context.Context) error {
|
|||||||
|
|
||||||
ec.GET("/*", echo.WrapHandler(http.StripPrefix("/", assetHandler)))
|
ec.GET("/*", echo.WrapHandler(http.StripPrefix("/", assetHandler)))
|
||||||
|
|
||||||
|
ec.POST("/validate", func(c echo.Context) error {
|
||||||
|
formData := new(FormData)
|
||||||
|
if err := c.Bind(formData); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cloudConfig := formData.CloudConfig
|
||||||
|
|
||||||
|
err := agent.Validate(cloudConfig)
|
||||||
|
if err != nil {
|
||||||
|
return c.String(http.StatusOK, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.String(http.StatusOK, "")
|
||||||
|
})
|
||||||
|
|
||||||
ec.POST("/install", func(c echo.Context) error {
|
ec.POST("/install", func(c echo.Context) error {
|
||||||
|
|
||||||
s.Lock()
|
s.Lock()
|
||||||
|
Reference in New Issue
Block a user