-
Notifications
You must be signed in to change notification settings - Fork 0
feature(core): Add wait handler structure #19
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
base: main
Are you sure you want to change the base?
Conversation
core/src/main/java/cloud/stackit/sdk/core/oapierror/GenericOpenAPIError.java
Outdated
Show resolved
Hide resolved
services/resourcemanager/src/main/java/cloud/stackit/sdk/resourcemanager/wait/Wait.java
Outdated
Show resolved
Hide resolved
services/resourcemanager/src/test/java/cloud/stackit/sdk/resourcemanager/WaitTest.java
Outdated
Show resolved
Hide resolved
services/resourcemanager/src/main/java/cloud/stackit/sdk/resourcemanager/wait/Wait.java
Outdated
Show resolved
Hide resolved
060607e
to
b4e7330
Compare
core/src/test/java/cloud/stackit/sdk/core/wait/AsyncWaitHandlerTest.java
Outdated
Show resolved
Hide resolved
12a9371
to
49fb05c
Compare
Signed-off-by: Alexander Dahmen <[email protected]>
// the response Body | ||
public static int ApiErrorMaxCharacterLimit = 500; | ||
|
||
private int statusCode; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private int statusCode; | |
private final int statusCode; |
could be final
|
||
private int statusCode; | ||
private byte[] body; | ||
private String errorMessage; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private String errorMessage; | |
private final String errorMessage; |
same here
|
||
public class ResourcemanagerWait { | ||
/** | ||
* createProjectWaitHandler will wait for project creation. Uses the deault values for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* createProjectWaitHandler will wait for project creation. Uses the deault values for | |
* createProjectWaitHandler will wait for project creation. Uses the default values for |
() -> { | ||
try { | ||
GetProjectResponse p = apiClient.getProject(containerId, false); | ||
if (p.getContainerId() == null || p.getLifecycleState() == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition will always be false I guess. Both getContainerId()
and getLifecycleState()
are annotated with Nonnull
.
Lines 107 to 110 in 44d23b5
@javax.annotation.Nonnull | |
public String getContainerId() { | |
return containerId; | |
} |
Lines 175 to 178 in 44d23b5
@javax.annotation.Nonnull | |
public LifecycleState getLifecycleState() { | |
return lifecycleState; | |
} |
() -> { | ||
try { | ||
GetProjectResponse p = apiClient.getProject(containerId, false); | ||
if (p.getContainerId() == null || p.getLifecycleState() == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, always false.
handler.setThrottle(10, TimeUnit.MILLISECONDS); | ||
handler.setTimeout(500, TimeUnit.MILLISECONDS); | ||
|
||
assertThrows(Exception.class, () -> handler.waitWithContext(), handler.TimoutErrorMessage); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertThrows(Exception.class, () -> handler.waitWithContext(), handler.TimoutErrorMessage); | |
assertThrows(Exception.class, handler::waitWithContext, handler.TimoutErrorMessage); |
Appears multiple times within this class, please check all places
void testCreateProjectNonOpenAPIError() throws Exception { | ||
// Return containerId == null and LifecycleState == CREATING | ||
GetProjectResponse creatingResponse = new GetProjectResponse(); | ||
creatingResponse.setContainerId(null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parameter is annotated with notnull.
Lines 112 to 114 in 44d23b5
public void setContainerId(@javax.annotation.Nonnull String containerId) { | |
this.containerId = containerId; | |
} |
// Return containerId == null and LifecycleState == CREATING | ||
GetProjectResponse creatingResponse = new GetProjectResponse(); | ||
creatingResponse.setContainerId(null); | ||
creatingResponse.setLifecycleState(null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
activeResponse.setLifecycleState(LifecycleState.ACTIVE); | ||
|
||
GetProjectResponse deletingResponse = new GetProjectResponse(); | ||
deletingResponse.setContainerId(null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, param is annotated with notnull.
Lines 112 to 114 in 44d23b5
public void setContainerId(@javax.annotation.Nonnull String containerId) { | |
this.containerId = containerId; | |
} |
// Wait some seconds for the API to process the request | ||
if (sleepBeforeWaitMillis > 0) { | ||
try { | ||
Thread.sleep(sleepBeforeWaitMillis); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's that nice to set a whole thread asleep.
On the other side we're limited to Java 8 features 😄 Did you do some research if there are better alternatives?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will throw in e.g. CompletableFutures
here, it was introduced with Java 8.
* @return | ||
* @throws Exception | ||
*/ | ||
public T waitWithContext() throws Exception { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public T waitWithContext() throws Exception { | |
public T waitWithContext() throws InterruptedException, IllegalArgumentException, TimeoutException { |
Throwing Exception
is a little bit... to much here. Users of the SDK won't have the chance to handle this properly.
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
|
||
public class GenericOpenAPIError extends ApiException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public class GenericOpenAPIError extends ApiException { | |
public class GenericOpenAPIException extends ApiException { |
Would keep the "Exception" in the name here instead of "Error". "Error" is a golang thing, Java has Exceptions.
throw new Exception(TimoutErrorMessage); | ||
} | ||
|
||
private ErrorResult handleError(int retryTempErrorCounter, Exception err) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private ErrorResult handleError(int retryTempErrorCounter, Exception err) { | |
private ErrorResult handleError(int retryTempErrorCounter, Exception exception) { |
throw new Exception(TimoutErrorMessage); | ||
} | ||
|
||
private ErrorResult handleError(int retryTempErrorCounter, Exception err) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private ErrorResult handleError(int retryTempErrorCounter, Exception err) { | |
private ErrorResult handleException(int retryTempErrorCounter, Exception err) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall this feels a little bit to me like taking the Golang waiter implementation and strictly rewriting it in Java. Java and Go have a completely different approach of error raising and handling.
49fb05c
to
59254e9
Compare
Signed-off-by: Alexander Dahmen <[email protected]>
59254e9
to
3a0eea7
Compare
+ p.getLifecycleState().getValue() | ||
+ "'")); | ||
} catch (ApiException e) { | ||
return new AsyncActionResult<>(false, null, e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I'm wrong here but my understanding is that in Java you don't return exceptions, you throw
and catch
.
return new AsyncActionResult<>(false, null, null); | ||
|
||
} catch (ApiException e) { | ||
GenericOpenAPIException oapiErr = new GenericOpenAPIException(e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This catch block seems generic to me and could apply to most of our deletion wait handlers. Maybe we can have some general approach for different wait handler types (Interfaces, abstract classes)?
Signed-off-by: Alexander Dahmen <[email protected]>
Description
relates to STACKITSDK-232
Checklist
make fmt
examples/
directory)make test
(will be checked by CI)make lint
(will be checked by CI)