Skip to content

Commit

Permalink
Make ComponentValidator internal (#24)
Browse files Browse the repository at this point in the history
...as its no longer used outside of the `ResourceInitializer`.
  • Loading branch information
big-andy-coates authored Sep 8, 2022
1 parent bf13e34 commit 9257bc2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 20 deletions.
23 changes: 17 additions & 6 deletions metadata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ Resource descriptors define the resources a component uses in its [inputs](#inpu
There are corresponding `ComponentInput`, `ComponentInternal` and `ComponentOutput` marker interfaces, respectively.

Authors of creek services are not expected to implement the above marker interfaces. Creek extensions,
(such as [creek-kafka][1]), expose their own interfaces, which extend the marker interfaces, and which can be used to
(such as [creek-kafka][1]), expose their own interfaces, which extend these marker interfaces, and which can be used to
define a resource the component uses, e.g. a Kafka Topic.

> ### NOTE
> Extensions expose resource _interfaces_ rather than _classes_ to keep code coupling to a minimum, minimising chances of
> dependency hell, etc.
> The reason extensions expose resource _interfaces_ rather than _classes_ is to keep code coupling to a minimum,
> thereby minimising chances of dependency hell. Extensions provide example code that can be cut & paste for
> creating the required implementations.
### Resource initialization

Expand Down Expand Up @@ -92,17 +93,23 @@ When the service starts, Creek will automatically create the resource when initi
> Future plans are to support a mode where owned resources are created by an initialization tool, prior to deployment.
> See [issue-68][2].
The owned resource types provided by extensions will define helper methods to obtain an unowned descriptor from an
owned one. For example, the `OwnedKafkaTopicOutput` has a `toInput` method to obtain an unowned `KafkaTopicInput`.

##### Unowned Resources

Resources tagged with the `UnownedResource` interface are conceptually owns by another service.

For example, services generally consume Kafka the _owned_ output topics of upstream services.
Therefore, the service's descriptor will define an _unowned_ descriptor for such resource, e.g. defining the
topic name, key & value types, partition count, etc. The unowned descriptor is created by calling `toInput` on
the owned descriptor.
topic name, key & value types, partition count, etc.

When the service starts, Creek will _not_ initialize unowned resources.


Unowned resource types provided by extensions should not normally be directly created. Instead, the unowned descriptor
should be created by calling an appropriate helper method on the owned resource. For example, an unowned `KafkaTopicInput`
is obtained by calling `toInput` on the associated `OwnedKafkaTopicOutput`.

##### Shared Resources

Resources tagged with the `SharedResource` interface are conceptually not owned by any service.
Expand All @@ -113,6 +120,10 @@ that wish to use it.
Shared resources are initialised via the [Init tool](https://github.com/creek-service/creek-platform/issues/7) before
any service that requires them are deployed.

Shared resource types provided by extensions will implement the `ComponentInput`, `ComponentInternal` and/or `ComponentOutput`
interfaces, as appropriate for the resource type. This allows a shared single definition to be used directly as a component's
input, internal or output.

##### Unmanaged Resources

Any resource descriptor that does not implement one of the resource initialization marker interfaces are deemed not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public interface ResourceHandler<T extends ResourceDescriptor> {
* <p>Instructs an extension to ensure the resources described by the supplied descriptor exist
* and are initialized.
*
* <p>Implementations should consider outputting a warning or failing if the resource alreay
* <p>Implementations should consider outputting a warning or failing if the resource already
* exists, but does not match the expected configuration.
*
* @param resources the resource instances to ensure exists and are initialized. Resources must
* be {@link ResourceDescriptor#isCreatable creatable}.
* @param resources the resource instances to ensure exists and are initialized. Resources
* passed will be {@link ResourceDescriptor#isCreatable creatable}.
*/
void ensure(Collection<T> resources);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.creekservice.api.platform.metadata.ResourceDescriptor.isShared;
import static org.creekservice.api.platform.metadata.ResourceDescriptor.isUnmanaged;
import static org.creekservice.api.platform.metadata.ResourceDescriptor.isUnowned;
import static org.creekservice.api.platform.resource.ComponentValidator.componentValidator;

import java.net.URI;
import java.util.Collection;
Expand All @@ -37,6 +36,7 @@
import org.creekservice.api.platform.metadata.ComponentDescriptor;
import org.creekservice.api.platform.metadata.ResourceDescriptor;
import org.creekservice.api.platform.metadata.ResourceHandler;
import org.creekservice.internal.platform.resource.ComponentValidator;

/**
* Initializer of resources.
Expand Down Expand Up @@ -67,7 +67,7 @@ public interface ResourceHandlers {
* @return an initializer instance.
*/
public static ResourceInitializer resourceInitializer(final ResourceHandlers handlers) {
return new ResourceInitializer(handlers, componentValidator());
return new ResourceInitializer(handlers, new ComponentValidator());
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.creekservice.api.platform.resource;
package org.creekservice.internal.platform.resource;


import java.lang.reflect.Method;
Expand All @@ -36,11 +36,7 @@ public final class ComponentValidator {

private static final Pattern CTRL_CHAR = Pattern.compile("\\p{Cntrl}");

public static ComponentValidator componentValidator() {
return new ComponentValidator();
}

private ComponentValidator() {}
public ComponentValidator() {}

public void validate(final ComponentDescriptor... components) {
Arrays.stream(components).forEach(this::validateComponent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.creekservice.api.platform.metadata.ResourceHandler;
import org.creekservice.api.platform.metadata.SharedResource;
import org.creekservice.api.platform.metadata.UnownedResource;
import org.creekservice.internal.platform.resource.ComponentValidator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
* limitations under the License.
*/

package org.creekservice.api.platform.resource;
package org.creekservice.internal.platform.resource;

import static org.creekservice.api.platform.resource.ComponentValidator.componentValidator;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.matchesRegex;
Expand Down Expand Up @@ -60,7 +59,7 @@ class ComponentValidatorTest {
@Mock(name = "jane")
private AggregateDescriptor aggregate;

private final ComponentValidator validator = componentValidator();
private final ComponentValidator validator = new ComponentValidator();

@BeforeEach
void setUp() {
Expand Down

0 comments on commit 9257bc2

Please sign in to comment.