Skip to content
Permalink
Browse files

feat: Add Capability annotation.

  • Loading branch information...
iocanel committed Jul 8, 2019
1 parent b550b7e commit 7ed4e8dd0fd5b5c487a6f3fc37ec2225f8db8aea
@@ -0,0 +1,41 @@
/**
* Copyright 2018 The original authors.
*
* 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 io.dekorate.component.annotation;

import io.dekorate.kubernetes.config.Configuration;
import io.sundr.builder.annotations.Adapter;
import io.sundr.builder.annotations.Buildable;
import io.sundr.builder.annotations.Pojo;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Buildable(builderPackage = "io.dekorate.deps.kubernetes.api.builder")
@Pojo(name = "CapabilityConfig", mutable = true, superClass = Configuration.class, relativePath = "../config", withStaticAdapterMethod = false,
adapter = @Adapter(suffix = "Adapter", relativePath = "../adapter", withMapAdapterMethod = true))
@Target({ElementType.CONSTRUCTOR, ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
public @interface Capability {
String category();
String kind();
String name();
String version() default "";
Parameter[] parameters() default {};
}
@@ -0,0 +1,34 @@
/**
* Copyright 2018 The original authors.
*
* 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 io.dekorate.component.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.CONSTRUCTOR, ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
public @interface Parameter {
String key();
String value();
}

@@ -0,0 +1,49 @@
/**
* Copyright 2018 The original authors.
*
* 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 io.dekorate.component.apt;

import io.dekorate.component.generator.CapabilityConfigGenerator;
import io.dekorate.doc.Description;
import io.dekorate.processor.AbstractAnnotationProcessor;

import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import java.util.Set;

@Description("Generate link custom resources.")
@SupportedAnnotationTypes("io.dekorate.component.annotation.Capability")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class CapabilityAnnotationProcessor extends AbstractAnnotationProcessor implements CapabilityConfigGenerator {

public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
session.close();
return true;
}
for (TypeElement typeElement : annotations) {
for (Element mainClass : roundEnv.getElementsAnnotatedWith(typeElement)) {
add(mainClass);
}
}
return false;
}
}
@@ -0,0 +1,54 @@
/**
* Copyright 2018 The original authors.
*
* 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 io.dekorate.component.generator;

import io.dekorate.Generator;
import io.dekorate.WithProject;
import io.dekorate.component.adapter.CapabilityConfigAdapter;
import io.dekorate.component.annotation.Capability;
import io.dekorate.component.config.CapabilityConfig;
import io.dekorate.component.handler.CapabilityHandler;
import io.dekorate.config.ConfigurationSupplier;

import javax.lang.model.element.Element;
import java.util.Map;

public interface CapabilityConfigGenerator extends Generator, WithProject {

String GENERATOR_KEY = "component";

@Override
default void add(Map map) {
add(new ConfigurationSupplier<>(
CapabilityConfigAdapter
.newBuilder(propertiesMap(map, Capability.class))));
}

@Override
default void add(Element element) {
Capability capability = element.getAnnotation(Capability.class);
add(capability != null
? new ConfigurationSupplier<>(CapabilityConfigAdapter.newBuilder(capability))
: new ConfigurationSupplier<>(CapabilityConfig.newCapabilityConfigBuilder()));
}

default void add(ConfigurationSupplier<CapabilityConfig> config) {
session.configurators().add(config);
session.handlers().add(new CapabilityHandler(session.resources()));
}
}
@@ -0,0 +1,88 @@
/**
* Copyright 2018 The original authors.
*
* 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 io.dekorate.component.handler;

import io.dekorate.Handler;
import io.dekorate.Resources;
import io.dekorate.component.config.CapabilityConfig;
import io.dekorate.component.config.EditableCapabilityConfig;
import io.dekorate.component.model.Capability;
import io.dekorate.component.model.CapabilityBuilder;
import io.dekorate.component.model.CapabilityFluent;
import io.dekorate.component.model.Parameter;
import io.dekorate.kubernetes.config.Configuration;
import io.dekorate.utils.Strings;

import java.util.stream.Collectors;
import java.util.Arrays;

public class CapabilityHandler implements Handler<CapabilityConfig> {

private final Resources resources;

CapabilityHandler() {
this(new Resources());

}
public CapabilityHandler(Resources resources) {
this.resources = resources;
}

@Override
public int order() {
return 1200;
}

@Override
public void handle(CapabilityConfig config) {
if (Strings.isNullOrEmpty(resources.getName())) {
resources.setName(config.getName());
}
resources.addCustom(ResourceGroup.NAME, createCapability(config));
}

@Override
public boolean canHandle(Class<? extends Configuration> type) {
return type.equals(CapabilityConfig.class) ||
type.equals(EditableCapabilityConfig.class);
}

/**
* Create a {@link Capability} from a {@link CapabilityConfig}.
*
* @param config The config.
* @return The link.
*/
private Capability createCapability(CapabilityConfig config) {
return new CapabilityBuilder()
.withNewMetadata()
.withName(config.getName())
.endMetadata()
.withNewSpec()
.withCategory(config.getCategory())
.withKind(config.getKind())
.withVersion(config.getVersion())
.addAllToParameters(Arrays.stream(config.getParameters())
.map(p->new Parameter(p.getKey(),p.getValue()))
.collect(Collectors.toList()))

.endSpec()
.build();
}

}
@@ -54,9 +54,6 @@ private Capability createServiceInstance(ServiceCatalogInstance instance) {
.withName(instance.getName())
.endMetadata()
.withNewSpec()
.withServiceClass(instance.getServiceClass())
.withServicePlan(instance.getServicePlan())
.withSecretName(instance.getBindingSecret())
.withParameters(convertToModelParameter(instance.getParameters()))
.endSpec().build();
}
@@ -31,10 +31,9 @@
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"class",
"plan",
"externalId",
"secretName",
"category",
"kind",
"version",
"parameters",
"parametersJson",
})
@@ -48,57 +47,45 @@
@CustomResource(group = "devexp.runtime.redhat.com", version = "v1alpha2")
public class CapabilitySpec {

@JsonProperty("class")
private String serviceClass;
@JsonProperty("plan")
private String servicePlan;
private String externalId;
private String secretName;
private String category;
private String kind;
private String version;
private Parameter[] parameters;
private String parametersJson;

public CapabilitySpec() {
}

public CapabilitySpec(String serviceClass, String servicePlan, String externalId, String secretName, Parameter[] parameters, String parametersJson) {
this.serviceClass = serviceClass;
this.servicePlan = servicePlan;
this.externalId = externalId;
this.secretName = secretName;
public CapabilitySpec(String category, String kind, Parameter[] parameters, String parametersJson) {
this.category = category;
this.kind = kind;
this.version = version;
this.parameters = parameters;
this.parametersJson = parametersJson;
}

public String getServiceClass() {
return serviceClass;
public String getCategory() {
return this.category;
}

public void setServiceClass(String serviceClass) {
this.serviceClass = serviceClass;
public void setCategory(String category) {
this.category=category;
}

public String getServicePlan() {
return servicePlan;
public String getKind() {
return this.kind;
}

public void setServicePlan(String servicePlan) {
this.servicePlan = servicePlan;
public void setKind(String kind) {
this.kind=kind;
}

public String getExternalId() {
return externalId;
public String getVersion() {
return this.version;
}

public void setExternalId(String externalId) {
this.externalId = externalId;
}

public String getSecretName() {
return secretName;
}

public void setSecretName(String secretName) {
this.secretName = secretName;
public void setVersion(String version) {
this.version=version;
}

public Parameter[] getParameters() {
@@ -1,2 +1,3 @@
io.dekorate.component.apt.ComponentAnnotationProcessor
io.dekorate.component.apt.LinkAnnotationProcessor
io.dekorate.component.apt.CapabilityAnnotationProcessor
@@ -11,6 +11,3 @@ 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.



0 comments on commit 7ed4e8d

Please sign in to comment.
You can’t perform that action at this time.