Skip to content

Commit 46f1057

Browse files
committed
add example
1 parent a566e70 commit 46f1057

2 files changed

Lines changed: 233 additions & 0 deletions

File tree

examples/automation/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:automation')
3+
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
4+
}
5+
6+
ext.mainClassName = 'cloud.stackit.sdk.automation.examples.AutomationExample'
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
package cloud.stackit.sdk.automation.examples;
2+
3+
import cloud.stackit.sdk.automation.v1betaapi.api.AutomationApi;
4+
import cloud.stackit.sdk.automation.v1betaapi.model.*;
5+
import cloud.stackit.sdk.automation.v1betaapi.model.SnapshotRetentionPolicyCount.KindEnum;
6+
import cloud.stackit.sdk.automation.v1betaapi.model.VolumeExecutionResponse.StatusEnum;
7+
import cloud.stackit.sdk.core.exception.ApiException;
8+
import java.io.IOException;
9+
import java.util.*;
10+
import java.util.concurrent.TimeUnit;
11+
12+
final class AutomationExample {
13+
14+
private AutomationExample() {}
15+
16+
public static void main(String[] args) throws IOException {
17+
// Credentials are read from the credentialsFile in
18+
// `~/.stackit/credentials.json` or the env
19+
// STACKIT_SERVICE_ACCOUNT_KEY_PATH / STACKIT_SERVICE_ACCOUNT_KEY
20+
AutomationApi automationApi = new AutomationApi();
21+
22+
// the id of your STACKIT project, read from env var for this example
23+
String projectId = System.getenv("STACKIT_PROJECT_ID");
24+
if (projectId == null || projectId.isEmpty()) {
25+
System.err.println("Environment variable 'STACKIT_PROJECT_ID' not found.");
26+
return;
27+
}
28+
29+
@SuppressWarnings("PMD.PrematureDeclaration")
30+
// the region which should be used to interact with the automation service
31+
String region = "eu01";
32+
33+
try {
34+
/*
35+
* ///////////////////////////////////////////////////////
36+
* // V O L U M E T E M P L A T E S //
37+
* ///////////////////////////////////////////////////////
38+
*/
39+
/* list all available volume templates */
40+
ListTemplatesResponse listTemplates =
41+
automationApi.listVolumeTemplates(projectId, region, null, null);
42+
System.out.println("Listing volume templates:");
43+
Objects.requireNonNull(listTemplates.getItems());
44+
for (Template template : listTemplates.getItems()) {
45+
System.out.println("********************");
46+
System.out.println("* Template ID: " + template.getId());
47+
System.out.println("* Template name: " + template.getName());
48+
}
49+
/* if there is a nextPageToken, fetch next page of results */
50+
while (listTemplates.getNextPageToken() != null
51+
&& listTemplates.getNextPageToken() != "") {
52+
listTemplates =
53+
automationApi.listVolumeTemplates(
54+
projectId, region, null, listTemplates.getNextPageToken());
55+
System.out.println("Listing next page of volume templates:");
56+
Objects.requireNonNull(listTemplates.getItems());
57+
for (Template template : listTemplates.getItems()) {
58+
System.out.println("********************");
59+
System.out.println("* Template ID: " + template.getId());
60+
System.out.println("* Template name: " + template.getName());
61+
}
62+
}
63+
64+
/* get one specific volume template */
65+
GetVolumeTemplateResponse template =
66+
automationApi.getVolumeTemplate(
67+
projectId, region, listTemplates.getItems().get(0).getId());
68+
System.out.println("\n\nFetched volume template:");
69+
System.out.println("* Template ID: " + template.getId());
70+
System.out.println("* Template name: " + template.getName());
71+
System.out.println("* Template description: " + template.getDescription());
72+
73+
/*
74+
* ///////////////////////////////////////////////////////
75+
* // V O L U M E A U T O M A T I O N //
76+
* ///////////////////////////////////////////////////////
77+
*/
78+
/* create a volume automation with a schedule trigger (daily at 02:00) */
79+
VolumeAutomation volumeAutomation =
80+
automationApi.createVolumeAutomation(
81+
projectId,
82+
region,
83+
new CreateVolumeAutomationPayload()
84+
.description(
85+
"Creates daily recovery points for all volumes with specified label")
86+
.name("My Daily Volume Recovery Point Creation Automation")
87+
.templateId(UUID.fromString(template.getId()))
88+
.input(
89+
new VolumeAutomationInput(
90+
new VolumeRecoveryPointManagementInput()
91+
.inheritVolumeLabels(true)
92+
.kind("VolumeRecoveryPointManagement")
93+
.recoveryPointLabels(
94+
Collections.singletonMap(
95+
"exampleLabelKey1",
96+
"exampleLabelValue1"))
97+
.snapshotRetentionPolicy(
98+
new SnapshotRetentionPolicy(
99+
new SnapshotRetentionPolicyCount()
100+
.kind(
101+
KindEnum
102+
.COUNT)
103+
.value(2)))
104+
.volumeLabelSelector(
105+
"myLabelkey1=myLabelValue,myLabelKey2=myOtherLabelValue")))
106+
.triggers(
107+
new AutomationTriggers()
108+
.schedule(
109+
new AutomationScheduleTrigger()
110+
.rrule(
111+
"DTSTART;TZID=Europe/Sofia:20200803T023000\nRRULE:FREQ=DAILY;INTERVAL=1"))));
112+
System.out.println("\n\nCreated volume automation:");
113+
System.out.println("* Automation ID: " + volumeAutomation.getId());
114+
System.out.println("* Automation name: " + volumeAutomation.getName());
115+
System.out.println("* Automation description: " + volumeAutomation.getDescription());
116+
117+
/* list all volume automations */
118+
ListAutomationsResponse listAutomations =
119+
automationApi.listVolumeAutomations(projectId, region, null, null);
120+
System.out.println("\n\nListing volume automations:");
121+
Objects.requireNonNull(listAutomations.getItems());
122+
for (ListAutomationsItem automation : listAutomations.getItems()) {
123+
System.out.println("********************");
124+
System.out.println("* Automation ID: " + automation.getId());
125+
System.out.println("* Automation name: " + automation.getName());
126+
System.out.println("* Automation description: " + automation.getDescription());
127+
}
128+
/* if there is a nextPageToken, fetch next page of results */
129+
while (listAutomations.getNextPageToken() != null
130+
&& listAutomations.getNextPageToken() != "") {
131+
listAutomations =
132+
automationApi.listVolumeAutomations(
133+
projectId, region, null, listAutomations.getNextPageToken());
134+
System.out.println("\nListing next page of volume automations:");
135+
Objects.requireNonNull(listAutomations.getItems());
136+
for (ListAutomationsItem automation : listAutomations.getItems()) {
137+
System.out.println("********************");
138+
System.out.println("* Automation ID: " + automation.getId());
139+
System.out.println("* Automation name: " + automation.getName());
140+
System.out.println("* Automation description: " + automation.getDescription());
141+
}
142+
}
143+
144+
/* update an automation */
145+
VolumeAutomation updatedAutomation =
146+
automationApi.partialUpdateVolumeAutomation(
147+
projectId,
148+
region,
149+
volumeAutomation.getId().toString(),
150+
null,
151+
new PartialUpdateVolumeAutomationPayload()
152+
.description("Updated daily recovery points automation")
153+
.name(
154+
"Updated Daily Volume Recovery Point Creation Automation"));
155+
System.out.println("\n\nUpdated volume automation:");
156+
System.out.println("* Automation ID: " + updatedAutomation.getId());
157+
System.out.println("* Automation name: " + updatedAutomation.getName());
158+
System.out.println("* Automation description: " + updatedAutomation.getDescription());
159+
160+
/* get one specific volume automation */
161+
VolumeAutomation fetchedAutomation =
162+
automationApi.getVolumeAutomation(
163+
projectId, region, volumeAutomation.getId().toString());
164+
System.out.println("\n\nFetched updated volume template:");
165+
System.out.println("* Automation ID: " + fetchedAutomation.getId());
166+
System.out.println("* Automation name: " + fetchedAutomation.getName());
167+
System.out.println("* Automation description: " + fetchedAutomation.getDescription());
168+
169+
/*
170+
* ///////////////////////////////////////////////////////
171+
* // E X E C U T I O N S //
172+
* ///////////////////////////////////////////////////////
173+
*/
174+
175+
/* trigger an automation execution */
176+
VolumeExecutionResponse createdExecution =
177+
automationApi.createVolumeExecution(
178+
projectId, region, volumeAutomation.getId().toString());
179+
System.out.println("\n\nTriggered volume automation execution:");
180+
System.out.println("* Execution ID: " + createdExecution.getId());
181+
System.out.println("* Execution status: " + createdExecution.getStatus());
182+
183+
/* wait for the automation executing to complete */
184+
while (createdExecution.getStatus().equals(StatusEnum.PENDING)
185+
|| createdExecution.getStatus().equals(StatusEnum.RUNNING)) {
186+
System.out.println("* Waiting for automation execution to complete ...");
187+
TimeUnit.SECONDS.sleep(2);
188+
createdExecution =
189+
automationApi.getVolumeExecution(
190+
projectId,
191+
region,
192+
volumeAutomation.getId().toString(),
193+
createdExecution.getId().toString());
194+
}
195+
System.out.println("* Automation execution completed");
196+
197+
/* list all executions of a specifc volume automation */
198+
ListExecutionsResponse listExecutions =
199+
automationApi.listVolumeExecutions(
200+
projectId, region, volumeAutomation.getId().toString(), null, null);
201+
System.out.println("\n\nListing executions of the volume automation:");
202+
Objects.requireNonNull(listExecutions.getItems());
203+
for (ListExecutionsItem execution : listExecutions.getItems()) {
204+
System.out.println("********************");
205+
System.out.println("* Execution ID: " + execution.getId());
206+
System.out.println("* Execution status: " + execution.getStatus());
207+
System.out.println("* Execution create time: " + execution.getCreateTime());
208+
System.out.println("* Execution end time: " + execution.getEndTime());
209+
}
210+
211+
/*
212+
* ///////////////////////////////////////////////////////
213+
* // D E L E T I O N //
214+
* ///////////////////////////////////////////////////////
215+
*/
216+
/* trigger deletion of the created application load balancer instance */
217+
System.out.println("\n\nDeleting created volume automation");
218+
automationApi.deleteVolumeAutomation(
219+
projectId, region, volumeAutomation.getId().toString());
220+
System.out.printf(
221+
"* Successfully deleted automation with ID \"%s\"",
222+
volumeAutomation.getId().toString());
223+
} catch (ApiException | InterruptedException e) {
224+
throw new RuntimeException(e);
225+
}
226+
}
227+
}

0 commit comments

Comments
 (0)