Skip to content

Commit 852f2ab

Browse files
committed
update example to v1api
1 parent a2c9450 commit 852f2ab

2 files changed

Lines changed: 249 additions & 21 deletions

File tree

examples/automation/src/main/java/cloud/stackit/sdk/automation/examples/AutomationExample.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package cloud.stackit.sdk.automation.examples;
22

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;
3+
import cloud.stackit.sdk.automation.v1api.api.AutomationApi;
4+
import cloud.stackit.sdk.automation.v1api.model.*;
5+
import cloud.stackit.sdk.automation.v1api.model.VolumeExecutionResponse.StatusEnum;
76
import cloud.stackit.sdk.core.exception.ApiException;
87
import java.io.IOException;
98
import java.util.*;
@@ -16,6 +15,7 @@ private AutomationExample() {}
1615
@SuppressWarnings({
1716
"PMD.CyclomaticComplexity",
1817
"PMD.CognitiveComplexity",
18+
"PMD.DoubleBraceInitialization",
1919
"PMD.NPathComplexity",
2020
"PMD.NcssCount",
2121
"PMD.SystemPrintln",
@@ -38,6 +38,7 @@ public static void main(String[] args) throws IOException {
3838
// the region which should be used to interact with the automation service
3939
String region = "eu01";
4040

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

0 commit comments

Comments
 (0)