1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
package com.yahoo.vespa.hosted.controller.persistence;
import com.yahoo.config.provision.CloudAccount;
import com.yahoo.slime.Cursor;
import com.yahoo.slime.Slime;
import com.yahoo.slime.SlimeUtils;
import com.yahoo.vespa.hosted.controller.api.identifiers.ClusterId;
import com.yahoo.vespa.hosted.controller.api.integration.dns.RecordData;
import com.yahoo.vespa.hosted.controller.api.integration.dns.RecordName;
import com.yahoo.vespa.hosted.controller.api.integration.dns.VpcEndpointService.DnsChallenge;
import com.yahoo.vespa.hosted.controller.api.integration.dns.VpcEndpointService.ChallengeState;
import java.time.Instant;
import static com.yahoo.yolean.Exceptions.uncheck;
/**
* @author jonmv
*/
class DnsChallengeSerializer {
private static final String nameField = "name";
private static final String dataField = "data";
private static final String serviceIdField = "serviceId";
private static final String accountField = "account";
private static final String createdAtField = "createdAt";
private static final String stateField = "state";
DnsChallenge fromJson(byte[] json, ClusterId clusterId) {
Cursor object = SlimeUtils.jsonToSlime(json).get();
return new DnsChallenge(RecordName.from(object.field(nameField).asString()),
RecordData.from(object.field(dataField).asString()),
clusterId,
object.field(serviceIdField).asString(),
SlimeUtils.optionalString(object.field(accountField)).map(CloudAccount::from),
Instant.ofEpochMilli(object.field(createdAtField).asLong()),
toState(object.field(stateField).asString()));
}
byte[] toJson(DnsChallenge challenge) {
Slime slime = new Slime();
Cursor object = slime.setObject();
object.setString(nameField, challenge.name().name());
object.setString(dataField, challenge.data().data());
object.setString(serviceIdField, challenge.serviceId());
challenge.account().ifPresent(account -> object.setString(accountField, account.value()));
object.setLong(createdAtField, challenge.createdAt().toEpochMilli());
object.setString(stateField, toString(challenge.state()));
return uncheck(() -> SlimeUtils.toJsonBytes(slime));
}
private static ChallengeState toState(String value) {
return switch (value) {
case "pending" -> ChallengeState.pending;
case "ready" -> ChallengeState.ready;
case "running" -> ChallengeState.running;
case "done" -> ChallengeState.done;
default -> throw new IllegalArgumentException("invalid serialized state: " + value);
};
}
private static String toString(ChallengeState state) {
return switch (state) {
case pending -> "pending";
case ready -> "ready";
case running -> "running";
case done -> "done";
};
}
}
|