Skip to content

Commit 42adbad

Browse files
committed
delete keyspace
1 parent 5c1f085 commit 42adbad

File tree

2 files changed

+84
-28
lines changed

2 files changed

+84
-28
lines changed

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/db/DbKeyspacesClient.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import com.dtsx.astra.sdk.AbstractApiClient;
44
import com.dtsx.astra.sdk.db.domain.Database;
55
import com.dtsx.astra.sdk.db.exception.KeyspaceAlreadyExistException;
6+
import com.dtsx.astra.sdk.db.exception.KeyspaceNotFoundException;
67
import com.dtsx.astra.sdk.utils.Assert;
78

9+
import java.util.Set;
10+
811
/**
912
* Delegate Operation to work on Keyspaces
1013
*/
@@ -28,6 +31,27 @@ public DbKeyspacesClient(String token, String databaseId) {
2831
this.db = new DatabaseClient(token, databaseId).get();
2932
}
3033

34+
/**
35+
* Find all keyspace in current DB.
36+
*
37+
* @return
38+
*/
39+
public Set<String> findAll() {
40+
return db.getInfo().getKeyspaces();
41+
}
42+
43+
/**
44+
* Evaluate if a keyspace exists.
45+
*
46+
* @param keyspace
47+
* keyspace identifier
48+
* @return
49+
* if keyspace exists
50+
*/
51+
public boolean exist(String keyspace) {
52+
return findAll().contains(keyspace);
53+
}
54+
3155
/**
3256
* Create a new keyspace in a DB.
3357
*
@@ -42,6 +66,20 @@ public void create(String keyspace) {
4266
getHttpClient().POST(getEndpointKeyspace(keyspace), getToken());
4367
}
4468

69+
/**
70+
* Delete a keyspace from db.
71+
*
72+
* @param keyspace
73+
* current keyspace
74+
*/
75+
public void delete(String keyspace) {
76+
Assert.hasLength(keyspace, "keyspace");
77+
if (!db.getInfo().getKeyspaces().contains(keyspace)) {
78+
throw new KeyspaceNotFoundException(db.getInfo().getName(), keyspace);
79+
}
80+
getHttpClient().DELETE(getEndpointKeyspace(keyspace), getToken());
81+
}
82+
4583
/**
4684
* Endpoint to access keyspace. (static).
4785
*

astra-sdk-devops/src/test/java/com/datastax/astra/sdk/devops/DatabaseClientTest.java

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.dtsx.astra.sdk.db.DatabaseClient;
44
import com.dtsx.astra.sdk.db.domain.*;
55
import com.dtsx.astra.sdk.db.exception.KeyspaceAlreadyExistException;
6+
import com.dtsx.astra.sdk.db.exception.KeyspaceNotFoundException;
67
import com.dtsx.astra.sdk.db.exception.RegionAlreadyExistException;
78
import com.dtsx.astra.sdk.db.exception.RegionNotFoundException;
89
import com.dtsx.astra.sdk.utils.TestUtils;
@@ -27,7 +28,7 @@ public void shouldCreateKeyspacesTest() {
2728
Assertions.assertThrows(IllegalArgumentException.class, () -> getSdkTestDatabaseClient().keyspaces().create(""));
2829
Assertions.assertThrows(IllegalArgumentException.class, () -> getSdkTestDatabaseClient().keyspaces().create(null));
2930
// Given
30-
Assertions.assertFalse(getSdkTestDatabaseClient().find().get().getInfo().getKeyspaces().contains(SDK_TEST_KEYSPACE2));
31+
Assertions.assertFalse(getSdkTestDatabaseClient().keyspaces().exist(SDK_TEST_KEYSPACE2));
3132
// When
3233
getSdkTestDatabaseClient().keyspaces().create(SDK_TEST_KEYSPACE2);
3334
Assertions.assertEquals(DatabaseStatusType.MAINTENANCE, getSdkTestDatabaseClient().find().get().getStatus());
@@ -36,16 +37,31 @@ public void shouldCreateKeyspacesTest() {
3637
// When
3738
Assertions.assertEquals(DatabaseStatusType.ACTIVE, getSdkTestDatabaseClient().find().get().getStatus());
3839
// Then
39-
Database db = getSdkTestDatabaseClient().get();
40-
Assertions.assertTrue(db.getInfo().getKeyspaces().contains(SDK_TEST_KEYSPACE2));
40+
Assertions.assertTrue(getSdkTestDatabaseClient().keyspaces().exist(SDK_TEST_KEYSPACE2));
4141
// Cannot create keyspace that already exist
4242
Assertions.assertThrows(KeyspaceAlreadyExistException.class,
4343
() -> getSdkTestDatabaseClient().keyspaces().create(SDK_TEST_KEYSPACE2));
4444
}
4545

4646
@Test
4747
@Order(2)
48-
@DisplayName("02. Download Default Cloud SecureBundle")
48+
@DisplayName("02. Delete a new Keyspace")
49+
public void shouldDeleteKeyspacesTest() {
50+
// Givem
51+
Assertions.assertThrows(KeyspaceNotFoundException.class,
52+
() -> getSdkTestDatabaseClient().keyspaces().delete("invalid"));
53+
// Given
54+
Assertions.assertTrue(getSdkTestDatabaseClient().keyspaces().exist(SDK_TEST_KEYSPACE2));
55+
// When
56+
getSdkTestDatabaseClient().keyspaces().delete(SDK_TEST_KEYSPACE2);
57+
// Then
58+
TestUtils.waitForDbStatus(getSdkTestDatabaseClient(), DatabaseStatusType.ACTIVE, 300);
59+
Assertions.assertFalse(getSdkTestDatabaseClient().keyspaces().exist(SDK_TEST_KEYSPACE2));
60+
}
61+
62+
@Test
63+
@Order(3)
64+
@DisplayName("03. Download Default Cloud SecureBundle")
4965
public void shouldDownloadDefaultScbTest() {
5066
// Given
5167
String randomFile = "/tmp/" + UUID.randomUUID().toString().replaceAll("-", "") + ".zip";
@@ -58,8 +74,8 @@ public void shouldDownloadDefaultScbTest() {
5874
}
5975

6076
@Test
61-
@Order(3)
62-
@DisplayName("03. Download Region Cloud SecureBundle")
77+
@Order(4)
78+
@DisplayName("04. Download Region Cloud SecureBundle")
6379
public void shouldDownloadRegionScbTest() {
6480
// Given
6581
String randomFile = "/tmp/" + UUID.randomUUID().toString().replaceAll("-", "") + ".zip";
@@ -73,8 +89,8 @@ public void shouldDownloadRegionScbTest() {
7389
}
7490

7591
@Test
76-
@Order(4)
77-
@DisplayName("04. Download All Cloud Secured Bundle")
92+
@Order(5)
93+
@DisplayName("05. Download All Cloud Secured Bundle")
7894
public void shouldDownloadAllScbTest() {
7995
// When
8096
Assertions.assertThrows(IllegalArgumentException.class, () ->
@@ -91,63 +107,63 @@ public void shouldDownloadAllScbTest() {
91107
}
92108

93109
@Test
94-
@Order(5)
95-
@DisplayName("05. Should not PARK Serverless")
110+
@Order(6)
111+
@DisplayName("06. Should not PARK Serverless")
96112
public void shouldNotParkServerlessTest() {
97113
Assertions.assertThrows(IllegalArgumentException.class, () -> getSdkTestDatabaseClient().park());
98114
}
99115

100116
@Test
101-
@Order(6)
102-
@DisplayName("06. Should not UNPARK Serverless")
117+
@Order(7)
118+
@DisplayName("07. Should not UNPARK Serverless")
103119
public void shouldNotUnParkServerlessTest() {
104120
Assertions.assertThrows(IllegalArgumentException.class, () -> getSdkTestDatabaseClient().unpark());
105121
}
106122

107123
@Test
108-
@Order(7)
109-
@DisplayName("07. Should not RESIZE Serverless")
124+
@Order(8)
125+
@DisplayName("08. Should not RESIZE Serverless")
110126
public void shouldNotResizeServerlessTest() {
111127
Assertions.assertThrows(IllegalArgumentException.class,
112128
() -> getSdkTestDatabaseClient().resize(2));
113129
}
114130

115131
@Test
116-
@Order(8)
117-
@DisplayName("08. Should not RESET PASSWORD Serverless")
132+
@Order(9)
133+
@DisplayName("09. Should not RESET PASSWORD Serverless")
118134
public void shouldNotResetPasswordTest() {
119135
Assertions.assertThrows(RuntimeException.class,
120136
() -> getSdkTestDatabaseClient().resetPassword("token", "cedrick1"));
121137
}
122138

123139
@Test
124-
@Order(9)
125-
@DisplayName("09. Should List regions")
140+
@Order(10)
141+
@DisplayName("10. Should List regions")
126142
public void shouldListRegionsTest() {
127143
List<Datacenter> regions = getSdkTestDatabaseClient().datacenters().findAll().collect(Collectors.toList());
128144
Assertions.assertEquals(1, regions.size());
129145
Assertions.assertEquals(SDK_TEST_DB_REGION, regions.get(0).getRegion());
130146
}
131147

132148
@Test
133-
@Order(10)
134-
@DisplayName("10. Should find region")
149+
@Order(11)
150+
@DisplayName("11. Should find region")
135151
public void shouldFindRegionsTest() {
136152
Assertions.assertTrue(getSdkTestDatabaseClient().datacenters().findByRegionName(SDK_TEST_DB_REGION).isPresent());
137153
Assertions.assertFalse(getSdkTestDatabaseClient().datacenters().findByRegionName("eu-west-1").isPresent());
138154
}
139155

140156
@Test
141-
@Order(11)
142-
@DisplayName("11. Should not remove invalid region")
157+
@Order(12)
158+
@DisplayName("12. Should not remove invalid region")
143159
public void shouldNotRemoveRegionsTest() {
144160
Assertions.assertThrows(RegionNotFoundException.class,
145161
() -> getSdkTestDatabaseClient().datacenters().delete("eu-west-1"));
146162
}
147163

148164
@Test
149-
@Order(12)
150-
@DisplayName("12. Should not add existing region")
165+
@Order(13)
166+
@DisplayName("13. Should not add existing region")
151167
public void shouldNotAddRegionsTest() {
152168
Assertions.assertThrows(RegionAlreadyExistException.class,
153169
() -> getSdkTestDatabaseClient()
@@ -157,7 +173,8 @@ public void shouldNotAddRegionsTest() {
157173

158174

159175
@Test
160-
@DisplayName("Should add a region")
176+
@Order(14)
177+
@DisplayName("14. Should add a region")
161178
public void shouldAddRegionTest() {
162179
// create an AWS DB
163180
// if (getDatabasesClient().findByName("aws_multiple_regions").count() == 0) {
@@ -174,14 +191,15 @@ public void shouldAddRegionTest() {
174191
}
175192

176193
@Test
177-
@DisplayName("Should delete a region")
194+
@Order(15)
195+
@DisplayName("14. Should delete a region")
178196
public void shouldDeleteRegionTest() {
179197
// getDatabasesClient().databaseByName("aws_multiple_regions").datacenters().delete("eu-central-1");
180198
}
181199

182200
@Test
183-
@Order(13)
184-
@DisplayName("13. Should terminate DB")
201+
@Order(16)
202+
@DisplayName("16. Should terminate DB")
185203
public void shouldTerminateDbTest() {
186204
Assert.assertTrue(getSdkTestDatabaseClient().exist());
187205
//getSdkTestDatabaseClient().delete();

0 commit comments

Comments
 (0)