Skip to content

Commit

Permalink
Merge pull request #26 from dzapata/master
Browse files Browse the repository at this point in the history
call the new validate endpoint which returns an [] of validation issues
  • Loading branch information
coryb committed May 25, 2023
2 parents f64530e + 56e8b85 commit ac6030d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
21 changes: 14 additions & 7 deletions deliveryconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,22 +864,21 @@ func (p *DeliveryConfigProcessor) Delete(cli *Client) error {

// ValidationErrorDetail is the structure of the document from /managed/delivery-configs/validate API
type ValidationErrorDetail struct {
Error string `json:"error"`
Status int `json:"status"`
Status int `json:"severity"`
Message string `json:"message"`
}

// Validate posts the delivery config to the validation api and returns nil on success,
// or a ValidationErrorDetail
func (p *DeliveryConfigProcessor) Validate(cli *Client) (*ValidationErrorDetail, error) {
func (p *DeliveryConfigProcessor) Validate(cli *Client) ([]*ValidationErrorDetail, error) {
if len(p.content) == 0 {
err := p.Load()
if err != nil {
return nil, xerrors.Errorf("Failed to load delivery config: %w", err)
}
}

_, err := commonRequest(cli, "POST", "/managed/delivery-configs/validate", requestBody{
response, err := commonRequest(cli, "POST", "/managed/delivery-configs/validate?validate-all=true", requestBody{
Content: bytes.NewReader(p.content),
ContentType: "application/x-yaml",
})
Expand All @@ -889,16 +888,24 @@ func (p *DeliveryConfigProcessor) Validate(cli *Client) (*ValidationErrorDetail,
if errResp.StatusCode == http.StatusBadRequest {
validation := ValidationErrorDetail{}
errResp.Parse(&validation)
return &validation, xerrors.Errorf(
return []*ValidationErrorDetail{&validation}, xerrors.Errorf(
"Failed to parse response from /managed/delivery-configs/validate: %w",
errResp,
)
}
}
return nil, xerrors.Errorf("Failed to validate delivery config to spinnaker: %w", err)
}

return nil, nil
// convert content into an ValidationErrorDetail[]
data := []*ValidationErrorDetail{}
err = json.Unmarshal(response, &data)
if err != nil {
return nil, xerrors.Errorf(
"failed to parse response from validation api: %w",
ErrorInvalidContent{Content: p.content, ParseError: err},
)
}
return data, nil
}

// Plan sends the delivery config to Spinnaker to get the actuation plan
Expand Down
18 changes: 15 additions & 3 deletions mdcli/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@ func Validate(opts *CommandOptions) (int, error) {

valErr, err := mdProcessor.Validate(cli)
if err != nil {
if valErr != nil {
opts.Logger.Errorf("%s\nReason: %s", valErr.Error, valErr.Message)
opts.Logger.Errorf("Could not validate the configuration: %s\n", err)
opts.Logger.Errorf("Exiting without failing\n")
return 0, nil
}
if len(valErr) > 0 {
exitWithFailure := false
opts.Logger.Errorf("Found the following validation issues:\n")
for i := 0; i < len(valErr); i++ {
if valErr[i].Status == 1 { // only fail if there is a sev 1 issue
exitWithFailure = true
}
opts.Logger.Errorf("%s\nReason: %s", valErr[i].Message)
}
if exitWithFailure {
opts.Logger.Noticef("FAILED")
return 1, nil
}
return 1, err
}

opts.Logger.Noticef("PASSED")
Expand Down

0 comments on commit ac6030d

Please sign in to comment.