Merge pull request #12919 from soapy1/box-metadata-errors

Return errors when box metadata is unavailable or invalid
This commit is contained in:
Sophia Castellarin 2022-09-28 16:58:38 -04:00 committed by GitHub
commit 7a7c8a4677
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 2 deletions

2
go.mod
View File

@ -25,7 +25,7 @@ require (
github.com/hashicorp/go-version v1.3.0
github.com/hashicorp/hcl/v2 v2.11.1
github.com/hashicorp/nomad/api v0.0.0-20200814140818-42de70466a9d
github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220928204300-c0e4b14e08c5
github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220928204555-798e860a8593
github.com/imdario/mergo v0.3.11
github.com/improbable-eng/grpc-web v0.13.0
github.com/kr/text v0.2.0

2
go.sum
View File

@ -369,6 +369,8 @@ github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220919180735-d47bfe003e94 h1:CG
github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220919180735-d47bfe003e94/go.mod h1:zA5vDskG3gH306C+obL+yURiUiLMAlx52yqO8MC2r9w=
github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220928204300-c0e4b14e08c5 h1:xxwRPE6ISOz4CFFJlk3DmDD+4ZBt7iO9YiGwk4W/CYY=
github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220928204300-c0e4b14e08c5/go.mod h1:zA5vDskG3gH306C+obL+yURiUiLMAlx52yqO8MC2r9w=
github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220928204555-798e860a8593 h1:A75xYKrvyA/fNB6nSLBosbcrEmGTZSyMvuFHH7agscY=
github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220928204555-798e860a8593/go.mod h1:zA5vDskG3gH306C+obL+yURiUiLMAlx52yqO8MC2r9w=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce h1:7UnVY3T/ZnHUrfviiAgIUjg2PXxsQfs5bphsG8F7Keo=
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=

View File

@ -10,6 +10,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
@ -18,6 +19,7 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/vagrant-plugin-sdk/core"
"github.com/hashicorp/vagrant-plugin-sdk/helper/path"
"github.com/hashicorp/vagrant-plugin-sdk/localizer"
"github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk"
"github.com/hashicorp/vagrant/internal/server/proto/vagrant_server"
"github.com/mitchellh/mapstructure"
@ -28,6 +30,8 @@ import (
// Number of seconds to wait between checks for box updates
const BoxUpdateCheckInterval = 3600
var RequiredMetadataFields = []string{"provider"}
type Box struct {
basis *Basis
box *vagrant_server.Box
@ -58,7 +62,10 @@ func NewBox(opts ...BoxOption) (b *Box, err error) {
metadataFile := filepath.Join(b.box.Directory, "metadata.json")
if _, err := os.Stat(metadataFile); err != nil {
return nil, err
return nil, localizer.LocalizeErr(
"box_does_not_have_metadata_json_file",
map[string]string{"BoxName": b.box.Name},
)
}
data, err := os.ReadFile(metadataFile)
@ -69,6 +76,19 @@ func NewBox(opts ...BoxOption) (b *Box, err error) {
if err := json.Unmarshal(data, &metadata); err != nil {
return nil, err
}
for _, field := range RequiredMetadataFields {
// If the metadata does not have a required field
if _, ok := metadata[field]; !ok {
return nil, localizer.LocalizeErr(
"box_metadata_missing_required_fields",
map[string]string{
"BoxName": b.box.Name,
"RequiredField": field,
"RequiredFields": strings.Join(RequiredMetadataFields, " ,"),
},
)
}
}
b.box.Metadata, err = structpb.NewStruct(metadata)
if err != nil {
return nil, err