msp: make monitoring spec optional (#59033)

The top-level spec works if zero value and does not need to be explicitly provided, but right now this causes a JSON schema error if you don't provide monitoring. This change makes it optional for the purposes of JSON schema validation.
This commit is contained in:
Robert Lin 2023-12-15 11:15:33 -08:00 committed by GitHub
parent 52f1b57985
commit 00a1751dc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 4 deletions

View File

@ -16,6 +16,9 @@ type MonitoringSpec struct {
}
func (s *MonitoringSpec) Validate() []error {
if s == nil {
return nil
}
var errs []error
errs = append(errs, s.Alerts.Validate()...)
return errs

View File

@ -25,7 +25,7 @@ type Spec struct {
Service ServiceSpec `json:"service"`
Build BuildSpec `json:"build"`
Environments []EnvironmentSpec `json:"environments"`
Monitoring MonitoringSpec `json:"monitoring"`
Monitoring *MonitoringSpec `json:"monitoring,omitempty"`
}
// Open a specification file, validate it, unmarshal the data as a MSP spec,
@ -59,6 +59,10 @@ func parse(data []byte) (*Spec, error) {
if err := yaml.Unmarshal(data, &s); err != nil {
return nil, err
}
// Assign zero value for top-level monitoring spec for covenience
s.Monitoring = &MonitoringSpec{}
if validationErrs := s.Validate(); len(validationErrs) > 0 {
return nil, errors.Append(nil, validationErrs...)
}

View File

@ -274,7 +274,7 @@ Supports completions on services and environments.`,
return errors.Newf("environment %q not found in service spec", targetEnv)
}
if err := syncEnvironmentWorkspaces(c, tfcClient, service.Service, service.Build, *env, service.Monitoring); err != nil {
if err := syncEnvironmentWorkspaces(c, tfcClient, service.Service, service.Build, *env, *service.Monitoring); err != nil {
return errors.Wrapf(err, "sync env %q", env.ID)
}
} else {
@ -282,7 +282,7 @@ Supports completions on services and environments.`,
return errors.New("second argument environment ID is required without the '-all' flag")
}
for _, env := range service.Environments {
if err := syncEnvironmentWorkspaces(c, tfcClient, service.Service, service.Build, env, service.Monitoring); err != nil {
if err := syncEnvironmentWorkspaces(c, tfcClient, service.Service, service.Build, env, *service.Monitoring); err != nil {
return errors.Wrapf(err, "sync env %q", env.ID)
}
}
@ -452,7 +452,7 @@ func generateTerraform(serviceID string, opts generateTerraformOptions) error {
}
// Render environment
cdktf, err := renderer.RenderEnvironment(service.Service, service.Build, env, service.Monitoring)
cdktf, err := renderer.RenderEnvironment(service.Service, service.Build, env, *service.Monitoring)
if err != nil {
return err
}