Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pipelinetemplate): Basic template & config validation (#1221)
- Loading branch information
1 parent
ee7a401
commit 4c0082d
Showing
16 changed files
with
578 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
.../netflix/spinnaker/orca/pipelinetemplate/exceptions/InvalidPipelineTemplateException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2017 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.spinnaker.orca.pipelinetemplate.exceptions; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class InvalidPipelineTemplateException extends RuntimeException { | ||
|
||
private List<Map<String, Object>> errors; | ||
|
||
public InvalidPipelineTemplateException(List<Map<String, Object>> errors) { | ||
this.errors = errors; | ||
} | ||
|
||
public InvalidPipelineTemplateException(String message, List<Map<String, Object>> errors) { | ||
super(message); | ||
this.errors = errors; | ||
} | ||
|
||
public List<Map<String, Object>> getErrors() { | ||
return errors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
.../netflix/spinnaker/orca/pipelinetemplate/v1schema/validator/V1SchemaValidationHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2017 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.spinnaker.orca.pipelinetemplate.v1schema.validator; | ||
|
||
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.StageDefinition; | ||
import com.netflix.spinnaker.orca.pipelinetemplate.validator.Errors; | ||
import com.netflix.spinnaker.orca.pipelinetemplate.validator.Errors.Error; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
public class V1SchemaValidationHelper { | ||
|
||
static void validateStageDefinitions(List<StageDefinition> stageDefinitions, Errors errors, Function<String, String> locationFormatter) { | ||
stageDefinitions.forEach(stageDefinition -> { | ||
if (stageDefinition.getId() == null) { | ||
errors.addError(Error.builder() | ||
.withMessage("Stage ID is unset") | ||
.withLocation(locationFormatter.apply("stages")) | ||
); | ||
} | ||
|
||
if (stageDefinition.getType() == null) { | ||
errors.addError(Error.builder() | ||
.withMessage("Stage is missing type") | ||
.withLocation(locationFormatter.apply("stages." + stageDefinition.getId())) | ||
); | ||
} | ||
|
||
if (stageDefinition.getConfig() == null) { | ||
errors.addError(Error.builder() | ||
.withMessage("Stage configuration is unset") | ||
.withLocation(locationFormatter.apply("stages." + stageDefinition.getId())) | ||
); | ||
} | ||
}); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...aker/orca/pipelinetemplate/v1schema/validator/V1TemplateConfigurationSchemaValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2017 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.spinnaker.orca.pipelinetemplate.v1schema.validator; | ||
|
||
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.TemplateConfiguration; | ||
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.TemplateConfiguration.PipelineDefinition; | ||
import com.netflix.spinnaker.orca.pipelinetemplate.validator.Errors; | ||
import com.netflix.spinnaker.orca.pipelinetemplate.validator.Errors.Error; | ||
import com.netflix.spinnaker.orca.pipelinetemplate.validator.SchemaValidator; | ||
import com.netflix.spinnaker.orca.pipelinetemplate.validator.VersionedSchema; | ||
|
||
public class V1TemplateConfigurationSchemaValidator implements SchemaValidator { | ||
|
||
private static final String SUPPORTED_VERSION = "1"; | ||
|
||
@Override | ||
public void validate(VersionedSchema configuration, Errors errors) { | ||
if (!(configuration instanceof TemplateConfiguration)) { | ||
throw new IllegalArgumentException("Expected TemplateConfiguration"); | ||
} | ||
TemplateConfiguration config = (TemplateConfiguration) configuration; | ||
|
||
if (!SUPPORTED_VERSION.equals(config.getSchemaVersion())) { | ||
errors.addError(Error.builder() | ||
.withMessage("config schema version is unsupported: expected '" + SUPPORTED_VERSION + "', got '" + config.getSchemaVersion() + "'")); | ||
} | ||
|
||
PipelineDefinition pipelineDefinition = config.getPipeline(); | ||
if (pipelineDefinition == null) { | ||
errors.addError(Error.builder() | ||
.withMessage("Missing pipeline configuration") | ||
.withLocation(location("pipeline")) | ||
); | ||
} else { | ||
if (pipelineDefinition.getApplication() == null) { | ||
errors.addError(Error.builder() | ||
.withMessage("Missing 'application' pipeline configuration") | ||
.withLocation(location("pipeline.application")) | ||
); | ||
} | ||
} | ||
|
||
V1SchemaValidationHelper.validateStageDefinitions(config.getStages(), errors, V1TemplateConfigurationSchemaValidator::location); | ||
|
||
// TODO rz - validate required variables are set and of the correct type | ||
} | ||
|
||
private static String location(String location) { | ||
return "configuration:" + location; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...netflix/spinnaker/orca/pipelinetemplate/v1schema/validator/V1TemplateSchemaValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2017 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.netflix.spinnaker.orca.pipelinetemplate.v1schema.validator; | ||
|
||
import com.netflix.spinnaker.orca.pipelinetemplate.v1schema.model.PipelineTemplate; | ||
import com.netflix.spinnaker.orca.pipelinetemplate.validator.Errors; | ||
import com.netflix.spinnaker.orca.pipelinetemplate.validator.Errors.Error; | ||
import com.netflix.spinnaker.orca.pipelinetemplate.validator.SchemaValidator; | ||
import com.netflix.spinnaker.orca.pipelinetemplate.validator.VersionedSchema; | ||
|
||
public class V1TemplateSchemaValidator implements SchemaValidator { | ||
|
||
private static final String SUPPORTED_VERSION = "1"; | ||
|
||
@Override | ||
public void validate(VersionedSchema pipelineTemplate, Errors errors) { | ||
if (!(pipelineTemplate instanceof PipelineTemplate)) { | ||
throw new IllegalArgumentException("Expected PipelineTemplate"); | ||
} | ||
PipelineTemplate template = (PipelineTemplate) pipelineTemplate; | ||
|
||
if (!SUPPORTED_VERSION.equals(template.getSchemaVersion())) { | ||
errors.addError(Error.builder() | ||
.withMessage("template schema version is unsupported: expected '" + SUPPORTED_VERSION + "', got '" + template.getSchemaVersion() + "'")); | ||
} | ||
|
||
V1SchemaValidationHelper.validateStageDefinitions(template.getStages(), errors, V1TemplateSchemaValidator::location); | ||
|
||
// TODO rz - validate variable type & defaultValue combinations | ||
} | ||
|
||
private static String location(String location) { | ||
return "template:" + location; | ||
} | ||
} |
Oops, something went wrong.