Search in sources :

Example 1 with JSONSchemaProps

use of io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps in project kubernetes-client by fabric8io.

the class CRGeneratorRunner method generate.

public List<WritableCRCompilationUnit> generate(CustomResourceDefinition crd, String basePackageName) {
    CustomResourceDefinitionSpec crSpec = crd.getSpec();
    String crName = crSpec.getNames().getKind();
    String group = crSpec.getGroup();
    List<WritableCRCompilationUnit> writableCUs = new ArrayList<>(crSpec.getVersions().size());
    for (CustomResourceDefinitionVersion crdv : crSpec.getVersions()) {
        String version = crdv.getName();
        String pkg = Optional.ofNullable(basePackageName).map(p -> p + "." + version).orElse(version);
        AbstractJSONSchema2Pojo specGenerator = null;
        String prefix = crName;
        if (config.getPrefixStrategy() == Config.Prefix.NEVER) {
            prefix = "";
        }
        JSONSchemaProps spec = crdv.getSchema().getOpenAPIV3Schema().getProperties().get("spec");
        if (spec != null) {
            String suffix = (config.getSuffixStrategy() != Config.Suffix.NEVER) ? "Spec" : "";
            specGenerator = AbstractJSONSchema2Pojo.fromJsonSchema("spec", spec, pkg, prefix, suffix, config);
        }
        AbstractJSONSchema2Pojo statusGenerator = null;
        JSONSchemaProps status = crdv.getSchema().getOpenAPIV3Schema().getProperties().get("status");
        if (status != null) {
            String suffix = (config.getSuffixStrategy() != Config.Suffix.NEVER) ? "Status" : "";
            statusGenerator = AbstractJSONSchema2Pojo.fromJsonSchema("status", status, pkg, prefix, suffix, config);
        }
        AbstractJSONSchema2Pojo crGenerator = new JCRObject(pkg, crName, group, version, prefix + "Spec", prefix + "Status", specGenerator != null, statusGenerator != null, crdv.getStorage(), crdv.getServed(), config);
        List<GeneratorResult.ClassResult> classResults = validateAndAggregate(crGenerator, specGenerator, statusGenerator);
        writableCUs.add(new WritableCRCompilationUnit(classResults));
    }
    return writableCUs;
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) AbstractJSONSchema2Pojo(io.fabric8.java.generator.nodes.AbstractJSONSchema2Pojo) Files(java.nio.file.Files) CustomResourceDefinitionSpec(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionSpec) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) GeneratorResult(io.fabric8.java.generator.nodes.GeneratorResult) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Stream(java.util.stream.Stream) CustomResourceDefinitionVersion(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion) Serialization(io.fabric8.kubernetes.client.utils.Serialization) FileVisitOption(java.nio.file.FileVisitOption) JavaGeneratorException(io.fabric8.java.generator.exceptions.JavaGeneratorException) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) JCRObject(io.fabric8.java.generator.nodes.JCRObject) JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps) Path(java.nio.file.Path) JCRObject(io.fabric8.java.generator.nodes.JCRObject) AbstractJSONSchema2Pojo(io.fabric8.java.generator.nodes.AbstractJSONSchema2Pojo) JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps) CustomResourceDefinitionVersion(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion) CustomResourceDefinitionSpec(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionSpec)

Example 2 with JSONSchemaProps

use of io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps in project kubernetes-client by fabric8io.

the class CustomResourceDefinitionTest method readSchema.

JSONSchemaProps readSchema() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    final URL resource = getClass().getResource("/test-crd-validation-schema.json");
    final JSONSchemaProps jsonSchemaProps = mapper.readValue(resource, JSONSchemaProps.class);
    return jsonSchemaProps;
}
Also used : JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaProps) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) URL(java.net.URL)

Example 3 with JSONSchemaProps

use of io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps in project kubernetes-client by fabric8io.

the class GeneratorTest method testObjectOfObjectsWithAlwaysPrefix.

@Test
void testObjectOfObjectsWithAlwaysPrefix() {
    // Arrange
    Config config = new Config(null, Config.Prefix.ALWAYS, null, null, null, null);
    Map<String, JSONSchemaProps> props = new HashMap<>();
    JSONSchemaProps newObj = new JSONSchemaProps();
    newObj.setType("object");
    props.put("o1", newObj);
    JObject obj = new JObject(null, "t", props, null, false, "My", "", config, null);
    // Act
    GeneratorResult res = obj.generateJava();
    // Assert
    assertEquals(2, res.getTopLevelClasses().size());
    assertEquals("MyO1", res.getTopLevelClasses().get(0).getName());
    assertEquals("MyT", res.getTopLevelClasses().get(1).getName());
}
Also used : JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps) Test(org.junit.jupiter.api.Test)

Example 4 with JSONSchemaProps

use of io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps in project kubernetes-client by fabric8io.

the class GeneratorTest method testObjectOfPrimitives.

@Test
void testObjectOfPrimitives() {
    // Arrange
    Map<String, JSONSchemaProps> props = new HashMap<>();
    JSONSchemaProps newBool = new JSONSchemaProps();
    newBool.setType("boolean");
    props.put("o1", newBool);
    JObject obj = new JObject("v1alpha1", "t", props, null, false, "", "", defaultConfig, null);
    // Act
    GeneratorResult res = obj.generateJava();
    // Assert
    assertEquals("v1alpha1.T", obj.getType());
    assertEquals(1, res.getTopLevelClasses().size());
    assertEquals("T", res.getTopLevelClasses().get(0).getName());
    Optional<ClassOrInterfaceDeclaration> clz = res.getTopLevelClasses().get(0).getCompilationUnit().getClassByName("T");
    assertTrue(clz.isPresent());
    assertEquals(1, clz.get().getFields().size());
    assertTrue(clz.get().getFieldByName("o1").isPresent());
}
Also used : ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps) Test(org.junit.jupiter.api.Test)

Example 5 with JSONSchemaProps

use of io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps in project kubernetes-client by fabric8io.

the class GeneratorTest method testDefaultEnum.

@Test
void testDefaultEnum() {
    // Arrange
    Map<String, JSONSchemaProps> props = new HashMap<>();
    JSONSchemaProps newEnum = new JSONSchemaProps();
    newEnum.setType("string");
    List<JsonNode> enumValues = new ArrayList<>();
    enumValues.add(new TextNode("foo"));
    enumValues.add(new TextNode("bar"));
    enumValues.add(new TextNode("baz"));
    props.put("e1", newEnum);
    JEnum enu = new JEnum("t", enumValues, defaultConfig, null);
    // Act
    GeneratorResult res = enu.generateJava();
    // Assert
    assertEquals("T", enu.getType());
    assertEquals(1, res.getInnerClasses().size());
    assertEquals("T", res.getInnerClasses().get(0).getName());
    Optional<EnumDeclaration> en = res.getInnerClasses().get(0).getCompilationUnit().getEnumByName("T");
    assertTrue(en.isPresent());
    assertEquals(3, en.get().getEntries().size());
    assertEquals("FOO", en.get().getEntries().get(0).getName().asString());
    assertEquals("BAR", en.get().getEntries().get(1).getName().asString());
    assertEquals("BAZ", en.get().getEntries().get(2).getName().asString());
}
Also used : JSONSchemaProps(io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)22 JSONSchemaProps (io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps)19 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition)5 JSONSchemaProps (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaProps)5 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)4 TypeDef (io.sundr.model.TypeDef)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 TextNode (com.fasterxml.jackson.databind.node.TextNode)3 CompilationUnit (com.github.javaparser.ast.CompilationUnit)2 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)2 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition)2 JSONSchemaPropsBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsBuilder)2 JSONSchemaPropsOrBoolBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaPropsOrBoolBuilder)2 Serialization (io.fabric8.kubernetes.client.utils.Serialization)2 InputStream (java.io.InputStream)2 IntNode (com.fasterxml.jackson.databind.node.IntNode)1 JavaGeneratorException (io.fabric8.java.generator.exceptions.JavaGeneratorException)1 AbstractJSONSchema2Pojo (io.fabric8.java.generator.nodes.AbstractJSONSchema2Pojo)1 GeneratorResult (io.fabric8.java.generator.nodes.GeneratorResult)1