Skip to content

Commit

Permalink
Add openapi discriminator on Datastore types.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitingjr committed Oct 23, 2024
1 parent 96d0c44 commit 595b87a
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 20 deletions.
19 changes: 19 additions & 0 deletions docs/site/content/en/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2895,13 +2895,17 @@ components:
description: Type of backend datastore
required:
- builtIn
- type
- apiKey
- url
type: object
properties:
builtIn:
description: Built In
type: boolean
type:
description: type information
type: string
apiKey:
description: Collector API KEY
type: string
Expand Down Expand Up @@ -3223,6 +3227,13 @@ components:
oneOf:
- $ref: "#/components/schemas/ElasticsearchDatastoreConfig"
- $ref: "#/components/schemas/PostgresDatastoreConfig"
- $ref: "#/components/schemas/CollectorApiDatastoreConfig"
discriminator:
propertyName: type
mapping:
ELASTICSEARCH: "#/components/schemas/ElasticsearchDatastoreConfig"
POSTGRES: "#/components/schemas/PostgresDatastoreConfig"
COLLECTORAPI: "#/components/schemas/CollectorApiDatastoreConfig"
type:
description: Type of backend datastore
enum:
Expand Down Expand Up @@ -3263,12 +3274,16 @@ components:
description: Type of backend datastore
required:
- builtIn
- type
- url
type: object
properties:
builtIn:
description: Built In
type: boolean
type:
description: type information
type: string
apiKey:
description: Elasticsearch API KEY
type: string
Expand Down Expand Up @@ -3884,11 +3899,15 @@ components:
description: Built in backend datastore
required:
- builtIn
- type
type: object
properties:
builtIn:
description: Built In
type: boolean
type:
description: type information
type: string
ProtectedTimeType:
required:
- access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ public abstract class BaseDatastoreConfig {
@Schema(type = SchemaType.BOOLEAN, required = true, description = "Built In")
public Boolean builtIn = true;

@Schema(type = SchemaType.STRING, required = true, description = "type information")
public String type = "";

public BaseDatastoreConfig() {
}

public BaseDatastoreConfig(Boolean builtIn) {
public BaseDatastoreConfig(Boolean builtIn, String type) {
this.builtIn = builtIn;
this.type = type;
}

public abstract String validateConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class CollectorApiDatastoreConfig extends BaseDatastoreConfig {

public CollectorApiDatastoreConfig() {
super(false);
super(false, "COLLECTORAPI");
}

@Schema(type = SchemaType.STRING, required = true, description = "Collector API KEY")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.validation.constraints.NotNull;

import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.DiscriminatorMapping;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -30,7 +31,12 @@ public class Datastore extends ProtectedType {
@JsonProperty(required = true)
@Schema(type = SchemaType.OBJECT, oneOf = {
ElasticsearchDatastoreConfig.class,
PostgresDatastoreConfig.class
PostgresDatastoreConfig.class,
CollectorApiDatastoreConfig.class
}, discriminatorProperty = "type", discriminatorMapping = {
@DiscriminatorMapping(value = "ELASTICSEARCH", schema = ElasticsearchDatastoreConfig.class),
@DiscriminatorMapping(value = "POSTGRES", schema = PostgresDatastoreConfig.class),
@DiscriminatorMapping(value = "COLLECTORAPI", schema = CollectorApiDatastoreConfig.class)
})
public ObjectNode config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@

@Schema(type = SchemaType.STRING, required = true, description = "Type of backend datastore")
public enum DatastoreType {
@com.fasterxml.jackson.annotation.JsonProperty("POSTGRES")
POSTGRES("POSTGRES", new TypeReference<PostgresDatastoreConfig>() {
}),
@com.fasterxml.jackson.annotation.JsonProperty("ELASTICSEARCH")
ELASTICSEARCH("ELASTICSEARCH", new TypeReference<ElasticsearchDatastoreConfig>() {
}),
@com.fasterxml.jackson.annotation.JsonProperty("COLLECTORAPI")
COLLECTORAPI("COLLECTORAPI", new TypeReference<CollectorApiDatastoreConfig>() {
});

Expand All @@ -29,6 +32,11 @@ public <T extends BaseDatastoreConfig> TypeReference<T> getTypeReference() {
return (TypeReference<T>) typeReference;
}

@com.fasterxml.jackson.annotation.JsonValue
public String toValue() {
return name;
}

@JsonCreator
public static DatastoreType fromString(String str) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class ElasticsearchDatastoreConfig extends BaseDatastoreConfig {

public ElasticsearchDatastoreConfig() {
super(false);
super(false, "ELASTICSEARCH");
}

@Schema(type = SchemaType.STRING, description = "Elasticsearch API KEY")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
@Schema(type = SchemaType.OBJECT, required = true, description = "Built in backend datastore")
public class PostgresDatastoreConfig extends BaseDatastoreConfig {

public PostgresDatastoreConfig() {
super(false, "POSTGRES");
}

@Override
public String validateConfig() {
return null;
Expand Down
11 changes: 4 additions & 7 deletions horreum-web/src/domain/admin/Datastores.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ import VerifyBackendModal from "./datastore/VerifyBackendModal";
import {
Access,
apiCall,
CollectorApiDatastoreConfig,
configApi,
Datastore,
Datastore, DatastoreConfig,
DatastoreTypeEnum,
ElasticsearchDatastoreConfig
} from "../../api";
import {AppContext} from "../../context/appContext";
import {AppContextType} from "../../context/@types/appContextTypes";
Expand Down Expand Up @@ -66,10 +64,9 @@ const DatastoresTable = ( props: dataStoreTableProps) => {
},

];
const newBackendConfig: ElasticsearchDatastoreConfig | CollectorApiDatastoreConfig = {
url: "",
apiKey: "",
builtIn: false
const newBackendConfig: DatastoreConfig = {
builtIn: false,
type: DatastoreTypeEnum.Postgres
}

const newDataStore: Datastore = {
Expand Down
27 changes: 18 additions & 9 deletions horreum-web/src/domain/admin/datastore/ModifyDatastoreModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Modal, TextInput
} from "@patternfly/react-core"
import {
Datastore,
Datastore, DatastoreConfig,
DatastoreTypeEnum, ElasticsearchDatastoreConfig,
} from "../../../api";
import {AppContext} from "../../../context/appContext";
Expand Down Expand Up @@ -134,8 +134,10 @@ export default function ModifyDatastoreModal({isOpen, onClose, persistDatastore,
<TextInput
value={"url" in dataStore.config ? dataStore.config.url : ""}
onChange={(_, value) => {
const config :ElasticsearchDatastoreConfig = dataStore.config as ElasticsearchDatastoreConfig;
config.url = value
const config :DatastoreConfig = {
type: DatastoreTypeEnum.Elasticsearch,
url: value
} as DatastoreConfig;
updateDatastore({...dataStore, config: config})
}}
isDisabled={enabledURL}
Expand All @@ -157,8 +159,11 @@ export default function ModifyDatastoreModal({isOpen, onClose, persistDatastore,
<TextInput
value={"apiKey" in dataStore.config ? dataStore.config.apiKey : ""}
onChange={(_, value) => {
const config :ElasticsearchDatastoreConfig = dataStore.config as ElasticsearchDatastoreConfig;
config.apiKey = value
const config :DatastoreConfig = {
type: DatastoreTypeEnum.Elasticsearch,
apiKey: value
} as DatastoreConfig;

updateDatastore({...dataStore, config: config})
}}isDisabled={enabledToken}
type="text"
Expand All @@ -180,8 +185,10 @@ export default function ModifyDatastoreModal({isOpen, onClose, persistDatastore,
<TextInput
value={"username" in dataStore.config ? dataStore.config.username : ""}
onChange={(_, value) => {
const config :ElasticsearchDatastoreConfig = dataStore.config as ElasticsearchDatastoreConfig;
config.username = value
const config :DatastoreConfig = {
type: DatastoreTypeEnum.Elasticsearch,
username: value
} as DatastoreConfig;
updateDatastore({...dataStore, config: config})
}}isDisabled={enabledToken}
type="text"
Expand All @@ -202,8 +209,10 @@ export default function ModifyDatastoreModal({isOpen, onClose, persistDatastore,
<TextInput
value={"password" in dataStore.config ? dataStore.config.password : ""}
onChange={(_, value) => {
const config :ElasticsearchDatastoreConfig = dataStore.config as ElasticsearchDatastoreConfig;
config.password = value
const config :DatastoreConfig = {
type: DatastoreTypeEnum.Elasticsearch,
password: value
} as DatastoreConfig;
updateDatastore({...dataStore, config: config})
}}isDisabled={enabledToken}
type="text"
Expand Down

0 comments on commit 595b87a

Please sign in to comment.