Skip to content

Commit cec64a8

Browse files
committed
work
1 parent 512fe85 commit cec64a8

File tree

12 files changed

+72
-66
lines changed

12 files changed

+72
-66
lines changed

astra-db-java/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<dependency>
7272
<groupId>org.junit.platform</groupId>
7373
<artifactId>junit-platform-launcher</artifactId>
74+
<version>${junit.platform.version}</version>
7475
<scope>test</scope>
7576
</dependency>
7677
<dependency>

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/AstraOpsClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public TokensClient tokens() {
177177
* @return
178178
* pcu groups client
179179
*/
180-
public PcuGroupsClient pcuGroups() {
180+
public PcuGroupsClient pcuGroups() { // TODO `pcu()` or `pcuGroups()`?
181181
return new PcuGroupsClient(token, environment);
182182
}
183183
}
Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.dtsx.astra.sdk.pcu;
22

3-
import com.dtsx.astra.sdk.AbstractApiClient;
4-
import com.dtsx.astra.sdk.db.domain.Datacenter;
5-
import com.dtsx.astra.sdk.pcu.domain.PcuGroup;
6-
import com.dtsx.astra.sdk.pcu.domain.PcuGroupDbAssociation;
3+
import com.dtsx.astra.sdk.pcu.domain.PcuGroupDatacenterAssociation;
74
import com.dtsx.astra.sdk.pcu.exception.PcuGroupDbAssociationNotFound;
85
import com.dtsx.astra.sdk.pcu.exception.PcuGroupNotFoundException;
6+
import com.dtsx.astra.sdk.AbstractApiClient;
97
import com.dtsx.astra.sdk.utils.*;
108
import com.fasterxml.jackson.core.type.TypeReference;
119
import lombok.Getter;
@@ -18,7 +16,7 @@
1816

1917
@Slf4j
2018
public class PcuGroupDatacenterAssociationsClient extends AbstractApiClient {
21-
private static final TypeReference<List<PcuGroupDbAssociation>> PCU_GROUP_DB_ASSOCIATIONS =
19+
private static final TypeReference<List<PcuGroupDatacenterAssociation>> PCU_GROUP_DB_ASSOCIATIONS =
2220
new TypeReference<>() {};
2321

2422
@Getter
@@ -49,11 +47,7 @@ public boolean exist(@NonNull String datacenterId) {
4947
.anyMatch((assoc) -> assoc.getDatacenterUUID().equals(datacenterId));
5048
}
5149

52-
public boolean exist(@NonNull Datacenter datacenter) {
53-
return exist(datacenter.getId());
54-
}
55-
56-
public PcuGroupDbAssociation findByDatacenterId(@NonNull String datacenterId) {
50+
public PcuGroupDatacenterAssociation findByDatacenterId(@NonNull String datacenterId) {
5751
Assert.isDatacenterID(datacenterId, "datacenter id");
5852

5953
return findAll()
@@ -62,53 +56,37 @@ public PcuGroupDbAssociation findByDatacenterId(@NonNull String datacenterId) {
6256
.orElseThrow(() -> new PcuGroupDbAssociationNotFound(pcuGroupId, datacenterId));
6357
}
6458

65-
public PcuGroupDbAssociation findByDatacenter(@NonNull Datacenter datacenter) {
66-
return findByDatacenterId(datacenter.getId());
67-
}
68-
69-
public Stream<PcuGroupDbAssociation> findAll() {
59+
public Stream<PcuGroupDatacenterAssociation> findAll() {
7060
val res = GET(getEndpointPcuAssociations() + "/" + pcuGroupId, getOperationName("findAll"));
7161

72-
return unmarshallOrThrow(res, PCU_GROUP_DB_ASSOCIATIONS, 200, "get pcu group db associations").stream();
62+
return unmarshallOrThrow(res, PCU_GROUP_DB_ASSOCIATIONS, "get pcu group db associations").stream();
7363
}
7464

75-
public PcuGroupDbAssociation associate(@NonNull String datacenterId) {
65+
public PcuGroupDatacenterAssociation associate(@NonNull String datacenterId) {
7666
Assert.isDatacenterID(datacenterId, "datacenter id");
7767

7868
val res = POST(getEndpointPcuAssociations() + "/" + pcuGroupId + "/" + datacenterId, getOperationName("associate"));
7969

80-
return unmarshallOrThrow(res, new TypeReference<>() {}, 201, "associate db to pcu group");
81-
}
82-
83-
public PcuGroupDbAssociation associate(@NonNull Datacenter datacenter) {
84-
return associate(datacenter.getId());
70+
return unmarshallOrThrow(res, new TypeReference<List<PcuGroupDatacenterAssociation>>() {}, "associate db to pcu group").get(0);
8571
}
8672

8773
private record TransferReqBody(String fromPCUGroupUUID, String toPCUGroupUUID, String datacenterUUID) {}
8874

89-
public PcuGroupDbAssociation transfer(@NonNull String toPcuGroup, @NonNull String datacenterId) {
75+
public PcuGroupDatacenterAssociation transfer(@NonNull String toPcuGroup, @NonNull String datacenterId) {
9076
Assert.isUUID(toPcuGroup, "target pcu group id");
9177
Assert.isDatacenterID(datacenterId, "datacenter id");
9278

9379
val reqBody = JsonUtils.marshall(new TransferReqBody(this.pcuGroupId, toPcuGroup, datacenterId));
9480
val res = POST(getEndpointPcuAssociations() + "/transfer/" + pcuGroupId, reqBody, getOperationName("transfer"));
9581

96-
return unmarshallOrThrow(res, new TypeReference<>() {}, 200, "transfer db to pcu group");
97-
}
98-
99-
public PcuGroupDbAssociation transfer(@NonNull PcuGroup toPcuGroup, @NonNull Datacenter datacenter) {
100-
return transfer(toPcuGroup.getUuid(), datacenter.getId());
82+
return unmarshallOrThrow(res, new TypeReference<List<PcuGroupDatacenterAssociation>>() {}, "transfer db to pcu group").get(0);
10183
}
10284

10385
public void dissociate(@NonNull String datacenterId) {
10486
Assert.isDatacenterID(datacenterId, "datacenter id");
10587
DELETE(getEndpointPcuAssociations() + "/" + pcuGroupId + "/" + datacenterId, getOperationName("dissociate"));
10688
}
10789

108-
public void dissociate(@NonNull Datacenter datacenter) {
109-
dissociate(datacenter.getId());
110-
}
111-
11290
// ---------------------------------
11391
// ---- Utilities ----
11492
// ---------------------------------
@@ -117,8 +95,9 @@ public String getEndpointPcuAssociations() {
11795
return ApiLocator.getApiDevopsEndpoint(environment) + "/pcus/association";
11896
}
11997

120-
private <T> T unmarshallOrThrow(ApiResponseHttp res, TypeReference<T> clazz, int expectedCode, String operation) {
98+
private <T> T unmarshallOrThrow(ApiResponseHttp res, TypeReference<T> clazz, String operation) {
12199
try {
100+
System.out.println(res.getBody());
122101
return JsonUtils.unmarshallType(res.getBody(), clazz);
123102
} catch (Exception e) {
124103
ApiResponseError responseError = null;
@@ -128,12 +107,12 @@ private <T> T unmarshallOrThrow(ApiResponseHttp res, TypeReference<T> clazz, int
128107
} catch (Exception ignored) {}
129108

130109
if (responseError != null && responseError.getErrors() != null && !responseError.getErrors().isEmpty()) {
131-
if (responseError.getErrors().getFirst().getId() == 2000367) {
110+
if (responseError.getErrors().get(0).getId() == 2000367) {
132111
throw PcuGroupNotFoundException.forId(pcuGroupId);
133112
}
134113
}
135114

136-
throw new IllegalStateException("Expected code " + expectedCode + " to " + operation + " but got " + res.getCode() + "body=" + res.getBody());
115+
throw new IllegalStateException("Expected code 2xx to " + operation + " but got " + res.getCode() + "body=" + res.getBody());
137116
}
138117
}
139118
}

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/pcu/PcuGroupOpsClient.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package com.dtsx.astra.sdk.pcu;
22

3-
import com.dtsx.astra.sdk.AbstractApiClient;
43
import com.dtsx.astra.sdk.pcu.domain.PcuGroup;
54
import com.dtsx.astra.sdk.pcu.domain.PcuGroupStatusType;
65
import com.dtsx.astra.sdk.pcu.domain.PcuGroupUpdateRequest;
76
import com.dtsx.astra.sdk.pcu.exception.PcuGroupNotFoundException;
7+
import com.dtsx.astra.sdk.AbstractApiClient;
88
import com.dtsx.astra.sdk.utils.ApiLocator;
99
import com.dtsx.astra.sdk.utils.AstraEnvironment;
1010
import com.dtsx.astra.sdk.utils.JsonUtils;
1111
import lombok.Getter;
1212
import lombok.extern.slf4j.Slf4j;
1313
import lombok.val;
1414

15+
import java.util.List;
1516
import java.util.Optional;
1617

1718
@Slf4j
@@ -46,7 +47,7 @@ public Optional<PcuGroup> find() {
4647
}
4748

4849
public PcuGroup get() {
49-
return new PcuGroupsClient(token, environment).findById(pcuGroupId);
50+
return new PcuGroupsClient(token, environment).findById(pcuGroupId).orElseThrow(() -> PcuGroupNotFoundException.forId(pcuGroupId));
5051
}
5152

5253
public boolean exist() {
@@ -67,7 +68,7 @@ public boolean isCreatedOrActive() {
6768

6869
public void update(PcuGroupUpdateRequest req) {
6970
val base = get();
70-
PATCH(getEndpointPcus() + "/" + pcuGroupId, JsonUtils.marshall(req.withDefaultsAndValidations(base)), getOperationName("update"));
71+
PUT(getEndpointPcus(), JsonUtils.marshall(List.of(req.withDefaultsAndValidations(base))), getOperationName("update"));
7172
}
7273

7374
// ---------------------------------
@@ -76,12 +77,18 @@ public void update(PcuGroupUpdateRequest req) {
7677

7778
public void park() {
7879
val res = POST(getEndpointPcus() + "/park/" + pcuGroupId, getOperationName("park"));
79-
assertHttpCodeAccepted(res, "park", pcuGroupId);
80+
81+
if (res.getCode() >= 300) {
82+
throw new IllegalStateException("Expected code 200 to park pcu group but got " + res.getCode() + "body=" + res.getBody());
83+
}
8084
}
8185

8286
public void unpark() {
8387
val res = POST(getEndpointPcus() + "/unpark/" + pcuGroupId, getOperationName("unpark"));
84-
assertHttpCodeAccepted(res, "unpark", pcuGroupId);
88+
89+
if (res.getCode() >= 300) {
90+
throw new IllegalStateException("Expected code 200 to unpark pcu group but got " + res.getCode() + "body=" + res.getBody());
91+
}
8592
}
8693

8794
public void delete() {

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/pcu/PcuGroupsClient.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.dtsx.astra.sdk.pcu;
22

3-
import com.dtsx.astra.sdk.AbstractApiClient;
43
import com.dtsx.astra.sdk.pcu.domain.PcuGroup;
5-
import com.dtsx.astra.sdk.pcu.domain.PcuGroupCreateUpdateRequest;
64
import com.dtsx.astra.sdk.pcu.domain.PcuGroupCreationRequest;
75
import com.dtsx.astra.sdk.pcu.exception.PcuGroupNotFoundException;
86
import com.dtsx.astra.sdk.pcu.exception.PcuGroupsNotFoundException;
7+
import com.dtsx.astra.sdk.AbstractApiClient;
98
import com.dtsx.astra.sdk.utils.*;
109
import com.fasterxml.jackson.core.type.TypeReference;
1110
import lombok.extern.slf4j.Slf4j;
1211
import lombok.val;
1312

1413
import java.net.HttpURLConnection;
1514
import java.util.List;
15+
import java.util.Optional;
1616
import java.util.stream.Stream;
1717

1818
@Slf4j
@@ -38,32 +38,37 @@ public String getServiceName() {
3838
// ---------------------------------
3939

4040
public PcuGroup create(PcuGroupCreationRequest req) {
41-
val res = POST(getEndpointPcus(), JsonUtils.marshall(req.withDefaultsAndValidations()), getOperationName("create"));
41+
val res = POST(getEndpointPcus(), JsonUtils.marshall(List.of(req.withDefaultsAndValidations())), getOperationName("create"));
4242

4343
if (HttpURLConnection.HTTP_CREATED != res.getCode()) {
44-
throw new IllegalStateException("Expected code 201 to create db but got " + res.getCode() + "body=" + res.getBody());
44+
throw new IllegalStateException("Expected code 201 to create pcu group but got " + res.getCode() + "body=" + res.getBody());
4545
}
4646

47-
return JsonUtils.unmarshallType(res.getBody(), RESPONSE_PCU_GROUPS).getFirst();
47+
return JsonUtils.unmarshallType(res.getBody(), RESPONSE_PCU_GROUPS).get(0);
48+
}
49+
50+
public Optional<PcuGroup> findById(String id) {
51+
try {
52+
return findAllImpl(List.of(id), "id", (_e) -> PcuGroupNotFoundException.forId(id)).findFirst();
53+
} catch (PcuGroupNotFoundException e) {
54+
return Optional.empty();
55+
}
4856
}
4957

50-
public PcuGroup findById(String id) {
51-
return findAllImpl(List.of(id), "id", (_e) -> PcuGroupNotFoundException.forId(id)).findFirst().orElseThrow(); // it can never throw unless the API itself is broken
58+
public Stream<PcuGroup> findByTitle(String title) {
59+
return findAll().filter(pg -> title.equals(pg.getTitle())); // order is important here since pg.title is nullable
5260
}
5361

54-
public PcuGroup findByTitle(String title) {
55-
return findAll()
56-
.filter(pg -> pg.getTitle().equals(title))
57-
.findFirst()
58-
.orElseThrow(() -> PcuGroupNotFoundException.forTitle(title));
62+
public Optional<PcuGroup> findFirstByTitle(String title) {
63+
return findByTitle(title).findFirst();
5964
}
6065

6166
public Stream<PcuGroup> findAll() {
6267
return findAll(null);
6368
}
6469

6570
public Stream<PcuGroup> findAll(List<String> ids) {
66-
return findAllImpl(ids, "ids[%d]", (e) -> new PcuGroupsNotFoundException(e.getErrors().getFirst().getMessage()));
71+
return findAllImpl(ids, "ids[%d]", (e) -> new PcuGroupsNotFoundException(e.getErrors().get(0).getMessage()));
6772
}
6873

6974
protected interface FindAll404Handler {
@@ -101,7 +106,7 @@ protected Stream<PcuGroup> findAllImpl(List<String> ids, String validationErrorF
101106
}
102107

103108
if (responseError != null && responseError.getErrors() != null && !responseError.getErrors().isEmpty()) {
104-
if (responseError.getErrors().getFirst().getId() == 340018) { // TODO is this the right error code? also why does find all get special treatment for auth errors?
109+
if (responseError.getErrors().get(0).getId() == 340018) { // TODO is this the right error code? also why does find all get special treatment for auth errors?
105110
throw new IllegalArgumentException("You have provided an invalid token, please check", e);
106111
}
107112
}

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/pcu/domain/PcuGroup.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
import com.dtsx.astra.sdk.db.domain.CloudProviderType;
44
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
56
import lombok.Data;
67
import lombok.NoArgsConstructor;
78

89
@Data
910
@NoArgsConstructor
1011
@JsonIgnoreProperties(ignoreUnknown = true)
1112
public class PcuGroup {
12-
private String uuid; // TODO should we call it 'id' for consistency? w/ @JsonProperty("uuid") + should this be a UUID?
13+
@JsonProperty("uuid")
14+
private String id;
1315
private String orgId;
1416

1517
private String title;
16-
private String description; // TODO should we use Optional<...>s?
18+
private String description;
1719

1820
private CloudProviderType cloudProvider;
1921
private String region;
@@ -25,7 +27,7 @@ public class PcuGroup {
2527
private int max;
2628
private int reserved;
2729

28-
private String createdAt; // TODO should these be DateTimes/Instants instead?
30+
private String createdAt;
2931
private String updatedAt;
3032
private String createdBy;
3133
private String updatedBy;

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/pcu/domain/PcuGroupCreateUpdateRequest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
import com.dtsx.astra.sdk.db.domain.CloudProviderType;
44
import lombok.Getter;
55
import lombok.Setter;
6-
import lombok.ToString;
76
import lombok.experimental.SuperBuilder;
87

98
@Getter
109
@Setter
11-
@ToString
1210
@SuperBuilder
1311
public sealed abstract class PcuGroupCreateUpdateRequest permits PcuGroupCreationRequest, PcuGroupUpdateRequest {
1412
protected String title;

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/pcu/domain/PcuGroupCreationRequest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.dtsx.astra.sdk.pcu.domain;
22

3+
import lombok.Getter;
4+
import lombok.Setter;
35
import lombok.experimental.SuperBuilder;
46

7+
@Getter
8+
@Setter
59
@SuperBuilder
610
public final class PcuGroupCreationRequest extends PcuGroupCreateUpdateRequest {
711
private String instanceType;

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/pcu/domain/PcuGroupDbAssociation.java renamed to astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/pcu/domain/PcuGroupDatacenterAssociation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// TODO add the rest of the fields once the PCU team is clear about what is going on
77
@Data
88
@JsonIgnoreProperties(ignoreUnknown = true)
9-
public class PcuGroupDbAssociation {
9+
public class PcuGroupDatacenterAssociation {
1010
private String pcuGroupUUID;
1111
private String datacenterUUID;
1212
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.dtsx.astra.sdk.pcu.domain;
22

3-
// TODO should this be a sealed interface with an 'OTHER(<status>)' (or UNKNOWN) implementation to future-proof?
43
public enum PcuGroupStatusType {
54
CREATED,
65
PLACING,
76
INITIALIZING,
87
ACTIVE,
98
PARKED,
109
PARKING,
11-
UNPARKING
10+
UNPARKING,
11+
OTHER // TODO make this work with Jackson
1212
}

0 commit comments

Comments
 (0)