Skip to content

Commit

Permalink
Follow redirect on instance checking
Browse files Browse the repository at this point in the history
  • Loading branch information
vknaisl committed Nov 14, 2019
1 parent bb55a73 commit e887dbd
Showing 1 changed file with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import nl.dtls.adminpanel.entity.InstanceStatus;
import nl.dtls.adminpanel.entity.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -97,10 +98,18 @@ public Optional<InstanceDTO> updateInstance(String uuid, InstanceChangeDTO reqDt
return of(instanceMapper.toDTO(updatedInstance, computeInstanceStatus(updatedInstance)));
}

public boolean deleteInstance(String uuid) {
Optional<Instance> oInstance = instanceRepository.findByUuid(uuid);
if (oInstance.isEmpty()) {
return false;
}
instanceRepository.delete(oInstance.get());
return true;
}

private InstanceStatus computeInstanceStatus(Instance instance) {
try {
ResponseEntity<String> response = restTemplate
.getForEntity(instance.getUrl(), String.class);
ResponseEntity<String> response = doHttpCall(instance.getUrl());
return
response.getStatusCode() == HttpStatus.OK
? InstanceStatus.RUNNING
Expand All @@ -110,12 +119,14 @@ private InstanceStatus computeInstanceStatus(Instance instance) {
}
}

public boolean deleteInstance(String uuid) {
Optional<Instance> oInstance = instanceRepository.findByUuid(uuid);
if (oInstance.isEmpty()) {
return false;
private ResponseEntity<String> doHttpCall(String url) {
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
if (response.getStatusCode() == HttpStatus.FOUND || response.getStatusCode() == HttpStatus.MOVED_PERMANENTLY) {
List<String> locations = response.getHeaders().get(HttpHeaders.LOCATION);
if (locations != null && locations.size() == 1) {
return doHttpCall(locations.get(0));
}
}
instanceRepository.delete(oInstance.get());
return true;
return response;
}
}

0 comments on commit e887dbd

Please sign in to comment.