concourse: Allow redefinition of built in resource types

See: https://github.com/spring-projects/sts4/issues/196
This commit is contained in:
Kris De Volder
2019-02-04 15:30:13 -08:00
parent 0eb4645380
commit a516d2d9ee
4 changed files with 48 additions and 10 deletions

View File

@@ -372,25 +372,27 @@ public class ConcourseModel {
});
}
public Multiset<String> getResourceTypeNames(DynamicSchemaContext dc) {
Collection<YValueHint> hints = getResourceTypeNameHints(dc);
public Multiset<String> getResourceTypeNames(DynamicSchemaContext dc, boolean includeBuiltin) {
Collection<YValueHint> hints = getResourceTypeNameHints(dc, includeBuiltin);
if (hints!=null) {
return ImmutableMultiset.copyOf(YTypeFactory.values(hints));
}
return null;
}
public Collection<YValueHint> getResourceTypeNameHints(DynamicSchemaContext dc) {
public Collection<YValueHint> getResourceTypeNameHints(DynamicSchemaContext dc, boolean includeBuiltin) {
IDocument doc = dc.getDocument();
Multiset<String> userDefined = getStringsFromAst(doc, RESOURCE_TYPE_NAMES_PATH);
if (userDefined!=null) {
Builder<YValueHint> builder = ImmutableMultiset.builder();
builder.addAll(YTypeFactory.hints(userDefined));
builder.addAll(
Arrays.stream(PipelineYmlSchema.BUILT_IN_RESOURCE_TYPES)
.map(h -> addExtraInsertion(h, dc))
.collect(Collectors.toList())
);
if (includeBuiltin) {
builder.addAll(
Arrays.stream(PipelineYmlSchema.BUILT_IN_RESOURCE_TYPES)
.map(h -> addExtraInsertion(h, dc))
.collect(Collectors.toList())
);
}
return builder.build();
}
return null;

View File

@@ -49,7 +49,7 @@ public class ConcourseValueParsers {
}
public static SchemaContextAware<ValueParser> resourceTypeNameDef(ConcourseModel models) {
return acceptOnlyUniqueNames(models::getResourceTypeNames, "resource-type name");
return acceptOnlyUniqueNames(dc -> models.getResourceTypeNames(dc, false), "resource-type name");
}
public static SchemaContextAware<ValueParser> acceptOnlyUniqueNames(

View File

@@ -183,7 +183,7 @@ public class PipelineYmlSchema implements YamlSchema {
return "The '"+parseString+"' Resource Type does not exist. Existing types: "+validValues;
},
(DynamicSchemaContext dc) -> {
return PartialCollection.compute(() -> models.getResourceTypeNameHints(dc));
return PartialCollection.compute(() -> models.getResourceTypeNameHints(dc, true));
}
);

View File

@@ -732,6 +732,42 @@ public class ConcourseEditorTest {
);
}
@Test
public void reconcileDuplicateResourceTypeNames_exempt_Builtins() throws Exception {
//See https://github.com/spring-projects/sts4/issues/196
Editor editor;
//Redefintion of built-in resource-type: no errors!
editor = harness.newEditor(
"resource_types:\n" +
"- name: docker-image\n" +
" privileged: true\n" +
" type: docker-image\n" +
" source:\n" +
" repository: concourse/docker-image-resource"
);
editor.assertProblems(/*None*/);
//... unless they are redefined twice...
editor = harness.newEditor(
"resource_types:\n" +
"- name: docker-image\n" +
" privileged: true\n" +
" type: docker-image\n" +
" source:\n" +
" repository: concourse/docker-image-resource\n" +
"- name: docker-image\n" +
" privileged: true\n" +
" type: docker-image\n" +
" source:\n" +
" repository: concourse/docker-image-resource"
);
editor.assertProblems(
"docker-image|Duplicate",
"docker-image|Duplicate"
);
}
@Test
public void violatedPropertyConstraintsAreWarnings() throws Exception {
Editor editor;