com.yahoo.vespa.hosted.controller.api.integration.dns.MockVpcEndpointService Maven / Gradle / Ivy
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.dns;
import ai.vespa.http.DomainName;
import com.yahoo.config.provision.CloudAccount;
import com.yahoo.vespa.hosted.controller.api.identifiers.ClusterId;
import com.yahoo.vespa.hosted.controller.api.integration.dns.Record.Type;
import java.time.Clock;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* @author jonmv
*/
public class MockVpcEndpointService implements VpcEndpointService {
public final AtomicBoolean enabled = new AtomicBoolean();
public final Map outcomes = new ConcurrentHashMap<>();
private final Clock clock;
private final NameService nameService;
public MockVpcEndpointService(Clock clock, NameService nameService) {
this.clock = clock;
this.nameService = nameService;
}
@Override
public synchronized Optional setPrivateDns(DomainName privateDnsName, ClusterId clusterId, Optional account, boolean isGenerated) {
DnsChallenge challenge = new DnsChallenge(RecordName.from("challenge--" + privateDnsName.value()),
RecordData.from(account.map(CloudAccount::value).orElse("system")),
clusterId,
"service-id",
account,
clock.instant(),
ChallengeState.pending);
return Optional.ofNullable(enabled.get() && nameService.findRecords(Type.TXT, challenge.name()).isEmpty() ? challenge : null);
}
@Override
public synchronized ChallengeState process(DnsChallenge challenge) {
if (outcomes.containsKey(challenge.name())) return outcomes.get(challenge.name());
if (nameService.findRecords(Type.TXT, challenge.name()).isEmpty()) throw new RuntimeException("No TXT record found for " + challenge.name());
return ChallengeState.done;
}
@Override
public synchronized List getConnections(ClusterId cluster, Optional account) {
return List.of(new VpcEndpoint("endpoint-1", "available", EndpointState.open));
}
}