|
1 | 1 | package io.opentdf.platform; |
| 2 | + |
2 | 3 | import com.connectrpc.ResponseMessageKt; |
3 | | -import io.opentdf.platform.policy.Attribute; |
4 | 4 | import io.opentdf.platform.policy.AttributeRuleTypeEnum; |
| 5 | +import io.opentdf.platform.policy.Namespace; |
5 | 6 | import io.opentdf.platform.policy.attributes.CreateAttributeRequest; |
6 | 7 | import io.opentdf.platform.policy.attributes.CreateAttributeResponse; |
| 8 | +import io.opentdf.platform.policy.namespaces.GetNamespaceRequest; |
7 | 9 | import io.opentdf.platform.sdk.*; |
| 10 | +import org.apache.logging.log4j.LogManager; |
| 11 | +import org.apache.logging.log4j.Logger; |
8 | 12 |
|
9 | 13 | import java.util.Collections; |
10 | | -import java.util.concurrent.ExecutionException; |
11 | 14 |
|
12 | 15 | import java.util.Arrays; |
| 16 | +import java.util.Objects; |
13 | 17 |
|
14 | 18 | public class CreateAttribute { |
15 | | - public static void main(String[] args) { |
16 | 19 |
|
17 | | - String clientId = "opentdf"; |
18 | | - String clientSecret = "secret"; |
19 | | - String platformEndpoint = "localhost:8080"; |
| 20 | + private static final Logger logger = LogManager.getLogger(CreateAttribute.class); |
| 21 | + |
| 22 | + public static void main(String[] args) { |
| 23 | + |
| 24 | + String clientId = "opentdf"; |
| 25 | + String clientSecret = "secret"; |
| 26 | + String platformEndpoint = "localhost:8080"; |
| 27 | + String namespaceName = "opentdf.io"; |
| 28 | + |
| 29 | + SDKBuilder builder = new SDKBuilder(); |
20 | 30 |
|
21 | | - SDKBuilder builder = new SDKBuilder(); |
22 | | - SDK sdk = builder.platformEndpoint(platformEndpoint) |
23 | | - .clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true) |
24 | | - .build(); |
| 31 | + try (SDK sdk = |
| 32 | + builder |
| 33 | + .platformEndpoint(platformEndpoint) |
| 34 | + .clientSecret(clientId, clientSecret) |
| 35 | + .useInsecurePlaintextConnection(true) |
| 36 | + .build()) { |
25 | 37 |
|
26 | | - CreateAttributeRequest request = CreateAttributeRequest.newBuilder() |
27 | | - .setNamespaceId("877990d1-609b-42ab-a273-4253b8b321eb") |
28 | | - .setName("test") |
29 | | - .setRule(AttributeRuleTypeEnum.forNumber(AttributeRuleTypeEnum.ATTRIBUTE_RULE_TYPE_ENUM_ALL_OF_VALUE)) |
30 | | - .addAllValues(Arrays.asList("test1", "test2")).build(); |
| 38 | + Namespace namespace = |
| 39 | + ResponseMessageKt.getOrThrow( |
| 40 | + sdk.getServices() |
| 41 | + .namespaces() |
| 42 | + .getNamespaceBlocking( |
| 43 | + GetNamespaceRequest.newBuilder() |
| 44 | + .setFqn("https://" + namespaceName) |
| 45 | + .build(), |
| 46 | + Collections.emptyMap()) |
| 47 | + .execute()) |
| 48 | + .getNamespace(); |
31 | 49 |
|
32 | | - CreateAttributeResponse resp = ResponseMessageKt.getOrThrow(sdk.getServices().attributes().createAttributeBlocking(request, Collections.emptyMap()).execute()); |
| 50 | + CreateAttributeRequest createAttributeRequest = |
| 51 | + CreateAttributeRequest.newBuilder() |
| 52 | + .setNamespaceId(namespace.getId()) |
| 53 | + .setName("test-attribute") |
| 54 | + .setRule( |
| 55 | + AttributeRuleTypeEnum.forNumber( |
| 56 | + AttributeRuleTypeEnum.ATTRIBUTE_RULE_TYPE_ENUM_ALL_OF_VALUE)) |
| 57 | + .addAllValues(Arrays.asList("test1", "test2")) |
| 58 | + .build(); |
33 | 59 |
|
34 | | - Attribute attribute = resp.getAttribute(); |
| 60 | + CreateAttributeResponse createAttributeResponse = |
| 61 | + ResponseMessageKt.getOrThrow( |
| 62 | + sdk.getServices() |
| 63 | + .attributes() |
| 64 | + .createAttributeBlocking(createAttributeRequest, Collections.emptyMap()) |
| 65 | + .execute()); |
35 | 66 |
|
| 67 | + logger.info( |
| 68 | + "Successfully created attribute with ID: {}", |
| 69 | + createAttributeResponse.getAttribute().getId()); |
| 70 | + } catch (Exception e) { |
| 71 | + if (Objects.equals(e.getMessage(), "resource not found")) { |
| 72 | + logger.error("Namespace '{}' not found", namespaceName, e); |
| 73 | + } else if (Objects.equals(e.getMessage(), "resource unique field violation")) { |
| 74 | + logger.error("Attribute already exists", e); |
| 75 | + } else { |
| 76 | + logger.error("Failed to create attribute", e); |
| 77 | + } |
36 | 78 | } |
| 79 | + } |
37 | 80 | } |
0 commit comments