-
Notifications
You must be signed in to change notification settings - Fork 2
fix(examples): improve logging and setup logic in examples #316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2ad70a3
fix(examples): improve logging and setup logic in examples
cshamrick c1c62f1
fix(examples): wrap sdk initialization in try-with-resources
cshamrick c73e9ce
fix(examples): remove needless throws exception
cshamrick bb3e626
fix(examples): enhance error handling and logging
cshamrick 5b7c299
fix(examples): update namespace name in examples
cshamrick a86c1f7
Merge branch 'main' into refactor-examples
cshamrick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 60 additions & 17 deletions
77
examples/src/main/java/io/opentdf/platform/CreateAttribute.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,80 @@ | ||
| package io.opentdf.platform; | ||
|
|
||
| import com.connectrpc.ResponseMessageKt; | ||
| import io.opentdf.platform.policy.Attribute; | ||
| import io.opentdf.platform.policy.AttributeRuleTypeEnum; | ||
| import io.opentdf.platform.policy.Namespace; | ||
| import io.opentdf.platform.policy.attributes.CreateAttributeRequest; | ||
| import io.opentdf.platform.policy.attributes.CreateAttributeResponse; | ||
| import io.opentdf.platform.policy.namespaces.GetNamespaceRequest; | ||
| import io.opentdf.platform.sdk.*; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Objects; | ||
|
|
||
| public class CreateAttribute { | ||
| public static void main(String[] args) { | ||
|
|
||
| String clientId = "opentdf"; | ||
| String clientSecret = "secret"; | ||
| String platformEndpoint = "localhost:8080"; | ||
| private static final Logger logger = LogManager.getLogger(CreateAttribute.class); | ||
|
|
||
| public static void main(String[] args) { | ||
|
|
||
| String clientId = "opentdf"; | ||
| String clientSecret = "secret"; | ||
| String platformEndpoint = "localhost:8080"; | ||
| String namespaceName = "mynamespace.com"; | ||
|
|
||
| SDKBuilder builder = new SDKBuilder(); | ||
|
|
||
| SDKBuilder builder = new SDKBuilder(); | ||
| SDK sdk = builder.platformEndpoint(platformEndpoint) | ||
| .clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true) | ||
| .build(); | ||
| try (SDK sdk = | ||
| builder | ||
| .platformEndpoint(platformEndpoint) | ||
| .clientSecret(clientId, clientSecret) | ||
| .useInsecurePlaintextConnection(true) | ||
| .build()) { | ||
|
|
||
| CreateAttributeRequest request = CreateAttributeRequest.newBuilder() | ||
| .setNamespaceId("877990d1-609b-42ab-a273-4253b8b321eb") | ||
| .setName("test") | ||
| .setRule(AttributeRuleTypeEnum.forNumber(AttributeRuleTypeEnum.ATTRIBUTE_RULE_TYPE_ENUM_ALL_OF_VALUE)) | ||
| .addAllValues(Arrays.asList("test1", "test2")).build(); | ||
| Namespace namespace = | ||
| ResponseMessageKt.getOrThrow( | ||
| sdk.getServices() | ||
| .namespaces() | ||
| .getNamespaceBlocking( | ||
| GetNamespaceRequest.newBuilder() | ||
| .setFqn("https://" + namespaceName) | ||
| .build(), | ||
| Collections.emptyMap()) | ||
| .execute()) | ||
| .getNamespace(); | ||
|
|
||
| CreateAttributeResponse resp = ResponseMessageKt.getOrThrow(sdk.getServices().attributes().createAttributeBlocking(request, Collections.emptyMap()).execute()); | ||
| CreateAttributeRequest createAttributeRequest = | ||
| CreateAttributeRequest.newBuilder() | ||
| .setNamespaceId(namespace.getId()) | ||
| .setName("test") | ||
| .setRule( | ||
| AttributeRuleTypeEnum.forNumber( | ||
| AttributeRuleTypeEnum.ATTRIBUTE_RULE_TYPE_ENUM_ALL_OF_VALUE)) | ||
| .addAllValues(Arrays.asList("test1", "test2")) | ||
| .build(); | ||
|
|
||
| Attribute attribute = resp.getAttribute(); | ||
| CreateAttributeResponse createAttributeResponse = | ||
| ResponseMessageKt.getOrThrow( | ||
| sdk.getServices() | ||
| .attributes() | ||
| .createAttributeBlocking(createAttributeRequest, Collections.emptyMap()) | ||
| .execute()); | ||
|
|
||
| logger.info( | ||
| "Successfully created attribute with ID: {}", | ||
| createAttributeResponse.getAttribute().getId()); | ||
| } catch (Exception e) { | ||
| if (Objects.equals(e.getMessage(), "resource not found")) { | ||
| logger.error("Namespace '{}' not found", namespaceName, e); | ||
| } else if (Objects.equals(e.getMessage(), "resource unique field violation")) { | ||
| logger.error("Attribute already exists", e); | ||
| } else { | ||
| logger.error("Failed to create attribute", e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
52 changes: 39 additions & 13 deletions
52
examples/src/main/java/io/opentdf/platform/CreateNamespace.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,55 @@ | ||
| package io.opentdf.platform; | ||
|
|
||
| import com.connectrpc.ResponseMessageKt; | ||
| import io.opentdf.platform.policy.namespaces.CreateNamespaceRequest; | ||
| import io.opentdf.platform.policy.namespaces.CreateNamespaceResponse; | ||
| import io.opentdf.platform.sdk.*; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.Objects; | ||
|
|
||
| public class CreateNamespace { | ||
| public static void main(String[] args) { | ||
|
|
||
| String clientId = "opentdf"; | ||
| String clientSecret = "secret"; | ||
| String platformEndpoint = "localhost:8080"; | ||
| private static final Logger logger = LogManager.getLogger(CreateNamespace.class); | ||
|
|
||
| public static void main(String[] args) { | ||
|
|
||
| String clientId = "opentdf"; | ||
| String clientSecret = "secret"; | ||
| String platformEndpoint = "localhost:8080"; | ||
| String namespaceName = "mynamespace.com"; | ||
|
|
||
| SDKBuilder builder = new SDKBuilder(); | ||
|
|
||
| try (SDK sdk = | ||
| builder | ||
| .platformEndpoint(platformEndpoint) | ||
| .clientSecret(clientId, clientSecret) | ||
| .useInsecurePlaintextConnection(true) | ||
| .build()) { | ||
|
|
||
| SDKBuilder builder = new SDKBuilder(); | ||
| SDK sdk = builder.platformEndpoint(platformEndpoint) | ||
| .clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true) | ||
| .build(); | ||
| CreateNamespaceRequest createNamespaceRequest = | ||
| CreateNamespaceRequest.newBuilder().setName(namespaceName).build(); | ||
|
|
||
| CreateNamespaceRequest request = CreateNamespaceRequest.newBuilder().setName("mynamespace.com").build(); | ||
| CreateNamespaceResponse createNamespaceResponse = | ||
| ResponseMessageKt.getOrThrow( | ||
| sdk.getServices() | ||
| .namespaces() | ||
| .createNamespaceBlocking(createNamespaceRequest, Collections.emptyMap()) | ||
| .execute()); | ||
|
|
||
| CreateNamespaceResponse resp = ResponseMessageKt.getOrThrow(sdk.getServices().namespaces().createNamespaceBlocking(request, Collections.emptyMap()).execute()); | ||
| logger.info( | ||
| "Successfully created namespace with ID: {}", | ||
| createNamespaceResponse.getNamespace().getId()); | ||
|
|
||
| System.out.println(resp.getNamespace().getName()); | ||
|
|
||
| } catch (Exception e) { | ||
| if (Objects.equals(e.getMessage(), "resource unique field violation")) { | ||
| logger.error("Namespace '{}' already exists", namespaceName, e); | ||
| } else { | ||
| logger.error("Failed to create namespace", e); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.