Skip to content

Commit de5668e

Browse files
committed
add example
1 parent a566e70 commit de5668e

2 files changed

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

0 commit comments

Comments
 (0)