Skip to content

Commit 5627da9

Browse files
committed
add example
1 parent 512a054 commit 5627da9

File tree

2 files changed

+248
-0
lines changed

2 files changed

+248
-0
lines changed

examples/serverupdate/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dependencies {
2+
implementation project (':services:serverupdate')
3+
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
4+
}
5+
6+
ext.mainClassName = 'cloud.stackit.sdk.serverupdate.examples.ServerUpdateExample'
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
package cloud.stackit.sdk.serverupdate.examples;
2+
3+
import cloud.stackit.sdk.core.KeyFlowAuthenticator;
4+
import cloud.stackit.sdk.core.config.CoreConfiguration;
5+
import cloud.stackit.sdk.core.exception.ApiException;
6+
import cloud.stackit.sdk.serverupdate.api.ServerUpdateApi;
7+
import cloud.stackit.sdk.serverupdate.model.*;
8+
import java.io.IOException;
9+
import java.net.HttpURLConnection;
10+
import java.time.Duration;
11+
import java.util.Objects;
12+
import java.util.UUID;
13+
import java.util.concurrent.TimeUnit;
14+
import okhttp3.OkHttpClient;
15+
16+
final class ServerUpdateExample {
17+
18+
private ServerUpdateExample() {}
19+
20+
@SuppressWarnings({
21+
"PMD.CyclomaticComplexity",
22+
"PMD.CognitiveComplexity",
23+
"PMD.NPathComplexity",
24+
"PMD.NcssCount",
25+
"PMD.SystemPrintln",
26+
"PMD.AvoidThrowingRawExceptionTypes",
27+
"PMD.AvoidDuplicateLiterals"
28+
})
29+
public static void main(String[] args) throws IOException {
30+
// Credentials are read from the credentialsFile in `~/.stackit/credentials.json` or the env
31+
// STACKIT_SERVICE_ACCOUNT_KEY_PATH / STACKIT_SERVICE_ACCOUNT_KEY
32+
CoreConfiguration configuration = new CoreConfiguration();
33+
34+
OkHttpClient httpClient = new OkHttpClient();
35+
KeyFlowAuthenticator authenticator = new KeyFlowAuthenticator(httpClient, configuration);
36+
httpClient =
37+
httpClient
38+
.newBuilder()
39+
.authenticator(authenticator)
40+
// Some create / update requests may take a few seconds.
41+
// To prevent a timeout, we increase the read timeout to 30 seconds
42+
.readTimeout(Duration.ofSeconds(30))
43+
.build();
44+
45+
ServerUpdateApi serverUpdateApi = new ServerUpdateApi(httpClient, configuration);
46+
47+
// the id of your STACKIT project, read from env var for this example
48+
String projectId = System.getenv("STACKIT_PROJECT_ID");
49+
if (projectId == null || projectId.isEmpty()) {
50+
System.err.println("Environment variable 'STACKIT_PROJECT_ID' not found.");
51+
return;
52+
}
53+
54+
// the region which should be used to interact with serverupdate
55+
String region = "eu01"; // NOPMD
56+
57+
// the id of your STACKIT server, read from env var for this example
58+
String serverId = System.getenv("STACKIT_SERVER_ID");
59+
if (serverId == null || serverId.isEmpty()) {
60+
System.err.println("Environment variable 'STACKIT_SERVER_ID' not found.");
61+
return;
62+
}
63+
64+
try {
65+
/*
66+
* ///////////////////////////////////////////////////////
67+
* // U P D A T E P O L I C I E S //
68+
* ///////////////////////////////////////////////////////
69+
*/
70+
/* fetching all available update policies */
71+
System.out.println("List all available update policies:");
72+
GetUpdatePoliciesResponse listUpdatePolicies =
73+
serverUpdateApi.listUpdatePolicies(projectId);
74+
assert listUpdatePolicies.getItems() != null;
75+
for (UpdatePolicy policy : listUpdatePolicies.getItems()) {
76+
System.out.println("*************************");
77+
System.out.println("* Policy name: " + policy.getName());
78+
System.out.println("* Description: " + policy.getDescription());
79+
System.out.println("* Policy ID: " + policy.getId());
80+
System.out.println("* RRULE: " + policy.getRrule());
81+
System.out.println("* Enabled: " + policy.getEnabled());
82+
System.out.println("* Default: " + policy.getDefault());
83+
}
84+
85+
/*
86+
* ///////////////////////////////////////////////////////
87+
* // U P D A T E E N A B L E M E N T //
88+
* ///////////////////////////////////////////////////////
89+
*/
90+
/* checking if update service is enabled for the server */
91+
System.out.println("\nChecking update service status for the server:");
92+
try {
93+
GetUpdateServiceResponse updateServiceStatus =
94+
serverUpdateApi.getServiceResource(projectId, serverId, region);
95+
assert updateServiceStatus.getEnabled() != null;
96+
System.out.println("* Update service enabled: " + updateServiceStatus.getEnabled());
97+
} catch (ApiException e) {
98+
// If response status is not found, update service is not enabled for the server
99+
if (e.getCode() == HttpURLConnection.HTTP_NOT_FOUND) {
100+
System.out.println("* Update service is not enabled for the server");
101+
System.out.println("* Enabling update service...");
102+
String policyIdString = listUpdatePolicies.getItems().get(0).getId();
103+
assert policyIdString != null;
104+
UUID policyId = UUID.fromString(policyIdString);
105+
serverUpdateApi.enableServiceResource(
106+
projectId,
107+
serverId,
108+
region,
109+
new EnableServiceResourcePayload().updatePolicyId(policyId));
110+
System.out.println("* Update service successful enabled for the server");
111+
}
112+
}
113+
114+
/*
115+
* ///////////////////////////////////////////////////////
116+
* // U P D A T E S C H E D U L E //
117+
* ///////////////////////////////////////////////////////
118+
*/
119+
/* creating a new nightly update schedule for the server*/
120+
System.out.println("\nCreating a new update schedule:");
121+
UpdateSchedule newSchedule =
122+
serverUpdateApi.createUpdateSchedule(
123+
projectId,
124+
serverId,
125+
region,
126+
new CreateUpdateSchedulePayload()
127+
.name("Nightly 3 AM")
128+
.enabled(true)
129+
.maintenanceWindow(5)
130+
.rrule(
131+
"DTSTART;TZID=Europe/Berlin:20251210T030000 RRULE:FREQ=DAILY;INTERVAL=1"));
132+
System.out.println("* Schedule ID: " + newSchedule.getId());
133+
System.out.println("* Name: " + newSchedule.getName());
134+
System.out.println("* Enabled: " + newSchedule.getEnabled());
135+
System.out.println("* RRULE: " + newSchedule.getRrule());
136+
System.out.println("* Maintenance Window: " + newSchedule.getMaintenanceWindow());
137+
138+
/* updating the created update schedule */
139+
System.out.println("\nUpdating the update schedule:");
140+
UpdateSchedule updatedSchedule =
141+
serverUpdateApi.updateUpdateSchedule(
142+
projectId,
143+
serverId,
144+
newSchedule.getId().toString(),
145+
region,
146+
new UpdateUpdateSchedulePayload()
147+
.name("Nightly 10 PM")
148+
.enabled(false)
149+
.maintenanceWindow(2)
150+
.rrule(
151+
"DTSTART;TZID=Europe/Berlin:20251210T220000 RRULE:FREQ=DAILY;INTERVAL=1"));
152+
System.out.println("* Schedule ID: " + updatedSchedule.getId());
153+
System.out.println("* Name: " + updatedSchedule.getName());
154+
System.out.println("* Enabled: " + updatedSchedule.getEnabled());
155+
System.out.println("* RRULE: " + updatedSchedule.getRrule());
156+
System.out.println("* Maintenance Window: " + updatedSchedule.getMaintenanceWindow());
157+
158+
/* list all available update schedules of the server */
159+
System.out.println("\nList all update schedules");
160+
GetUpdateSchedulesResponse listUpdateSchedules =
161+
serverUpdateApi.listUpdateSchedules(projectId, serverId, region);
162+
assert listUpdateSchedules.getItems() != null;
163+
for (UpdateSchedule schedule : listUpdateSchedules.getItems()) {
164+
System.out.println("*************************");
165+
System.out.println("* Schedule ID: " + schedule.getId());
166+
System.out.println("* Name: " + schedule.getName());
167+
System.out.println("* Enabled: " + schedule.getEnabled());
168+
System.out.println("* RRULE: " + schedule.getRrule());
169+
System.out.println("* Maintenance Window: " + schedule.getMaintenanceWindow());
170+
}
171+
172+
/* deleting the update schedule we created */
173+
System.out.println("\nDeleting update schedule:");
174+
serverUpdateApi.deleteUpdateSchedule(
175+
projectId, serverId, newSchedule.getId().toString(), region);
176+
System.out.println("* Deleted update schedule successful");
177+
178+
/*
179+
* ///////////////////////////////////////////////////////
180+
* // U P D A T E S //
181+
* ///////////////////////////////////////////////////////
182+
*/
183+
/* trigger manually an update of the server */
184+
System.out.println("\nTrigger a server update:");
185+
Update newUpdate =
186+
serverUpdateApi.createUpdate(
187+
projectId,
188+
serverId,
189+
region,
190+
new CreateUpdatePayload()
191+
.backupBeforeUpdate(false)
192+
.maintenanceWindow(11));
193+
System.out.println("* Update status: " + newUpdate.getStatus());
194+
195+
/* wait for the update to complete */
196+
while (Objects.equals(
197+
serverUpdateApi
198+
.getUpdate(projectId, serverId, newUpdate.getId().toString(), region)
199+
.getStatus(),
200+
"running")) {
201+
System.out.println("Waiting for update to complete ...");
202+
TimeUnit.SECONDS.sleep(5);
203+
}
204+
System.out.println("* Update finished");
205+
206+
/* fetch details of the update */
207+
System.out.println("\nUpdate details:");
208+
Update getUpdate =
209+
serverUpdateApi.getUpdate(
210+
projectId, serverId, newUpdate.getId().toString(), region);
211+
System.out.println("* ID: " + getUpdate.getId());
212+
System.out.println("* Status: " + getUpdate.getStatus());
213+
System.out.println("* Installed updates: " + getUpdate.getInstalledUpdates());
214+
System.out.println("* Failed updates: " + getUpdate.getFailedUpdates());
215+
if (getUpdate.getFailReason() != null && !getUpdate.getFailReason().isEmpty()) {
216+
System.out.println("* Fail reason: " + getUpdate.getFailReason());
217+
}
218+
System.out.println("* Start date: " + getUpdate.getStartDate());
219+
System.out.println("* End date: " + getUpdate.getEndDate());
220+
221+
/* list all executed updated */
222+
System.out.println("\nList updates:");
223+
GetUpdatesListResponse listUpdates =
224+
serverUpdateApi.listUpdates(projectId, serverId, region);
225+
assert listUpdates.getItems() != null;
226+
for (Update update : listUpdates.getItems()) {
227+
System.out.println("*************************");
228+
System.out.println("* ID: " + update.getId());
229+
System.out.println("* Status: " + update.getStatus());
230+
System.out.println("* Installed updates: " + update.getInstalledUpdates());
231+
System.out.println("* Failed updates: " + update.getFailedUpdates());
232+
if (update.getFailReason() != null && !update.getFailReason().isEmpty()) {
233+
System.out.println("* Fail reason: " + update.getFailReason());
234+
}
235+
System.out.println("* Start date: " + update.getStartDate());
236+
System.out.println("* End date: " + update.getEndDate());
237+
}
238+
} catch (ApiException | InterruptedException e) {
239+
throw new RuntimeException(e);
240+
}
241+
}
242+
}

0 commit comments

Comments
 (0)