Skip to content

Commit 7e09841

Browse files
authored
Adding metadata endpoint (#1049)
* adding metadata endpoint Signed-off-by: salaboy <Salaboy@gmail.com> * apply formatting Signed-off-by: salaboy <Salaboy@gmail.com> * updating formatting Signed-off-by: salaboy <Salaboy@gmail.com> * updating formatting Signed-off-by: salaboy <Salaboy@gmail.com> * reverting formatting Signed-off-by: salaboy <Salaboy@gmail.com> * making domain classes final Signed-off-by: salaboy <Salaboy@gmail.com> * making domain model immutable Signed-off-by: salaboy <Salaboy@gmail.com> * equals/hashcode Signed-off-by: salaboy <Salaboy@gmail.com> * updating tests and clients Signed-off-by: salaboy <Salaboy@gmail.com> * rebasing after http client removed Signed-off-by: salaboy <Salaboy@gmail.com> * reverting delete Signed-off-by: salaboy <Salaboy@gmail.com> * adding space Signed-off-by: salaboy <Salaboy@gmail.com> * update copy and spaces Signed-off-by: salaboy <Salaboy@gmail.com> * eof line Signed-off-by: salaboy <Salaboy@gmail.com> * another new line Signed-off-by: salaboy <Salaboy@gmail.com> * adding coverage tests Signed-off-by: salaboy <Salaboy@gmail.com> * removing equals and hashcode Signed-off-by: salaboy <Salaboy@gmail.com> --------- Signed-off-by: salaboy <Salaboy@gmail.com>
1 parent 502f7c0 commit 7e09841

File tree

7 files changed

+332
-5
lines changed

7 files changed

+332
-5
lines changed

sdk/src/main/java/io/dapr/client/DaprClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package io.dapr.client;
1515

1616
import io.dapr.client.domain.ConfigurationItem;
17+
import io.dapr.client.domain.DaprMetadata;
1718
import io.dapr.client.domain.DeleteStateRequest;
1819
import io.dapr.client.domain.ExecuteStateTransactionRequest;
1920
import io.dapr.client.domain.GetBulkSecretRequest;
@@ -671,6 +672,13 @@ Flux<SubscribeConfigurationResponse> subscribeConfiguration(String storeName, Li
671672
*/
672673
<T extends AbstractStub<T>> T newGrpcStub(String appId, Function<Channel, T> stubBuilder);
673674

675+
/**
676+
* Fetches Dapr Metadata from the metadata endpoint.
677+
*
678+
* @return DaprMetadata containing Dapr Metadata from the metadata endpoint.
679+
*/
680+
Mono<DaprMetadata> getMetadata();
681+
674682
/**
675683
* Gracefully shutdown the dapr runtime.
676684
*

sdk/src/main/java/io/dapr/client/DaprClientImpl.java

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import io.dapr.client.domain.BulkPublishRequest;
2121
import io.dapr.client.domain.BulkPublishResponse;
2222
import io.dapr.client.domain.BulkPublishResponseFailedEntry;
23+
import io.dapr.client.domain.ComponentMetadata;
2324
import io.dapr.client.domain.ConfigurationItem;
25+
import io.dapr.client.domain.DaprMetadata;
2426
import io.dapr.client.domain.DeleteStateRequest;
2527
import io.dapr.client.domain.ExecuteStateTransactionRequest;
2628
import io.dapr.client.domain.GetBulkSecretRequest;
@@ -36,11 +38,13 @@
3638
import io.dapr.client.domain.QueryStateItem;
3739
import io.dapr.client.domain.QueryStateRequest;
3840
import io.dapr.client.domain.QueryStateResponse;
41+
import io.dapr.client.domain.RuleMetadata;
3942
import io.dapr.client.domain.SaveStateRequest;
4043
import io.dapr.client.domain.State;
4144
import io.dapr.client.domain.StateOptions;
4245
import io.dapr.client.domain.SubscribeConfigurationRequest;
4346
import io.dapr.client.domain.SubscribeConfigurationResponse;
47+
import io.dapr.client.domain.SubscriptionMetadata;
4448
import io.dapr.client.domain.TransactionalStateOperation;
4549
import io.dapr.client.domain.UnlockRequest;
4650
import io.dapr.client.domain.UnlockResponseStatus;
@@ -58,6 +62,10 @@
5862
import io.dapr.v1.CommonProtos;
5963
import io.dapr.v1.DaprGrpc;
6064
import io.dapr.v1.DaprProtos;
65+
import io.dapr.v1.DaprProtos.PubsubSubscription;
66+
import io.dapr.v1.DaprProtos.PubsubSubscriptionRule;
67+
import io.dapr.v1.DaprProtos.RegisteredComponents;
68+
import io.grpc.CallOptions;
6169
import io.grpc.Channel;
6270
import io.grpc.stub.AbstractStub;
6371
import io.grpc.stub.StreamObserver;
@@ -118,7 +126,8 @@ public class DaprClientImpl extends AbstractDaprClient {
118126
private final DaprHttp httpClient;
119127

120128
/**
121-
* Default access level constructor, in order to create an instance of this class use io.dapr.client.DaprClientBuilder
129+
* Default access level constructor, in order to create an instance of this
130+
* class use io.dapr.client.DaprClientBuilder
122131
*
123132
* @param channel Facade for the managed GRPC channel
124133
* @param asyncStub async gRPC stub
@@ -1169,8 +1178,7 @@ private ConfigurationItem buildConfigurationItem(
11691178
key,
11701179
configurationItem.getValue(),
11711180
configurationItem.getVersion(),
1172-
configurationItem.getMetadataMap()
1173-
);
1181+
configurationItem.getMetadataMap());
11741182
}
11751183

11761184
/**
@@ -1231,4 +1239,43 @@ public void onCompleted() {
12311239
}
12321240
};
12331241
}
1234-
}
1242+
1243+
@Override
1244+
public Mono<DaprMetadata> getMetadata() {
1245+
DaprProtos.GetMetadataRequest metadataRequest = DaprProtos.GetMetadataRequest.newBuilder().build();
1246+
return Mono.deferContextual(
1247+
context -> this.<DaprProtos.GetMetadataResponse>createMono(
1248+
it -> intercept(context, asyncStub).getMetadata(metadataRequest, it)))
1249+
.map(
1250+
it -> {
1251+
try {
1252+
return buildDaprMetadata(it);
1253+
} catch (IOException ex) {
1254+
throw DaprException.propagate(ex);
1255+
}
1256+
});
1257+
}
1258+
1259+
private DaprMetadata buildDaprMetadata(
1260+
DaprProtos.GetMetadataResponse response) throws IOException {
1261+
List<RegisteredComponents> registeredComponentsList = response.getRegisteredComponentsList();
1262+
1263+
List<ComponentMetadata> components = new ArrayList<>();
1264+
for (RegisteredComponents rc : registeredComponentsList) {
1265+
components.add(new ComponentMetadata(rc.getName(), rc.getType(), rc.getVersion()));
1266+
}
1267+
1268+
List<PubsubSubscription> subscriptionsList = response.getSubscriptionsList();
1269+
List<SubscriptionMetadata> subscriptions = new ArrayList<>();
1270+
for (PubsubSubscription s : subscriptionsList) {
1271+
List<PubsubSubscriptionRule> rulesList = s.getRules().getRulesList();
1272+
List<RuleMetadata> rules = new ArrayList<>();
1273+
for (PubsubSubscriptionRule r : rulesList) {
1274+
rules.add(new RuleMetadata(r.getPath()));
1275+
}
1276+
subscriptions.add(new SubscriptionMetadata(s.getTopic(), s.getPubsubName(), s.getDeadLetterTopic(), rules));
1277+
}
1278+
1279+
return new DaprMetadata(response.getId(), response.getRuntimeVersion(), components, subscriptions);
1280+
}
1281+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2024 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.client.domain;
15+
16+
import java.util.Objects;
17+
18+
/**
19+
* ComponentMetadata describes a Dapr Component.
20+
*/
21+
public final class ComponentMetadata {
22+
23+
private String name;
24+
private String type;
25+
private String version;
26+
27+
/**
28+
* Constructor for a ComponentMetadata.
29+
*
30+
* @param name of the component
31+
* @param type component type
32+
* @param version version of the component
33+
*/
34+
public ComponentMetadata(String name, String type, String version) {
35+
this.name = name;
36+
this.type = type;
37+
this.version = version;
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
44+
public String getType() {
45+
return type;
46+
}
47+
48+
public String getVersion() {
49+
return version;
50+
}
51+
52+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2024 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.client.domain;
15+
16+
import java.util.Collections;
17+
import java.util.List;
18+
import java.util.Objects;
19+
20+
/**
21+
* DaprMetadata describes the Dapr Metadata.
22+
*/
23+
public final class DaprMetadata {
24+
25+
private String id;
26+
private String runtimeVersion;
27+
private List<ComponentMetadata> components;
28+
private List<SubscriptionMetadata> subscriptions;
29+
30+
/**
31+
* Constructor for a DaprMetadata.
32+
*
33+
* @param id of the application
34+
* @param runtimeVersion Dapr version
35+
* @param components list of registered componnets
36+
* @param subscriptions list of registered subscription
37+
*/
38+
public DaprMetadata(String id, String runtimeVersion, List<ComponentMetadata> components,
39+
List<SubscriptionMetadata> subscriptions) {
40+
this.id = id;
41+
this.runtimeVersion = runtimeVersion;
42+
this.components = components == null ? Collections.emptyList() : Collections.unmodifiableList(components);
43+
this.subscriptions = subscriptions == null ? Collections.emptyList() : Collections.unmodifiableList(subscriptions);
44+
}
45+
46+
public String getId() {
47+
return id;
48+
}
49+
50+
public String getRuntimeVersion() {
51+
return runtimeVersion;
52+
}
53+
54+
public List<ComponentMetadata> getComponents() {
55+
return components;
56+
}
57+
58+
public List<SubscriptionMetadata> getSubscriptions() {
59+
return subscriptions;
60+
}
61+
62+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2024 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.client.domain;
15+
16+
/**
17+
* RuleMetadata describes the Subscription Rule's Metadata.
18+
*/
19+
public final class RuleMetadata {
20+
private String path;
21+
22+
public RuleMetadata(String path) {
23+
this.path = path;
24+
}
25+
26+
public String getPath() {
27+
return path;
28+
}
29+
30+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2024 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.client.domain;
15+
16+
import java.util.Collections;
17+
import java.util.List;
18+
import java.util.Objects;
19+
20+
/**
21+
* SubscriptionMetadata describes the Subscription Metadata.
22+
*/
23+
public final class SubscriptionMetadata {
24+
private String topic;
25+
private String pubsubname;
26+
private String deadLetterTopic;
27+
private List<RuleMetadata> rules;
28+
29+
/**
30+
* Constructor for a SubscriptionMetadata.
31+
*
32+
* @param topic of the pubsub component
33+
* @param pubsubname component name
34+
* @param deadLetterTopic dead letter topic
35+
* @param rules subscription path rules
36+
*/
37+
public SubscriptionMetadata(String topic, String pubsubname, String deadLetterTopic, List<RuleMetadata> rules) {
38+
this.topic = topic;
39+
this.pubsubname = pubsubname;
40+
this.deadLetterTopic = deadLetterTopic;
41+
this.rules = rules == null ? Collections.emptyList() : Collections.unmodifiableList(rules);
42+
}
43+
44+
public String getTopic() {
45+
return topic;
46+
}
47+
48+
public String getPubsubname() {
49+
return pubsubname;
50+
}
51+
52+
53+
public String getDeadLetterTopic() {
54+
return deadLetterTopic;
55+
}
56+
57+
58+
public List<RuleMetadata> getRules() {
59+
return rules;
60+
}
61+
62+
}

0 commit comments

Comments
 (0)