package com.hubspot.baragon.service.resources;

import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.hubspot.baragon.data.BaragonStateDatastore;
import com.hubspot.baragon.models.Service;
import com.hubspot.baragon.models.ServiceState;
import com.hubspot.baragon.models.UpstreamInfo;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Collection;

@Path("/state")
@Produces(MediaType.APPLICATION_JSON)
public class BaragonStateResource {
  private final BaragonStateDatastore datastore;

  @Inject
  public BaragonStateResource(BaragonStateDatastore datastore) {
    this.datastore = datastore;
  }

  @GET
  public Collection<String> getServices() {
    return datastore.getServices();
  }

  @GET
  @Path("/{serviceId}")
  public Optional<ServiceState> getService(@PathParam("serviceId") String serviceId) {
    final Optional<Service> maybeServiceInfo = datastore.getService(serviceId);

    if (!maybeServiceInfo.isPresent()) {
      return Optional.absent();
    }

    return Optional.of(new ServiceState(maybeServiceInfo.get(), datastore.getUpstreams(serviceId)));
  }

  @GET
  @Path("/{serviceId}/{upstream}")
  public Optional<UpstreamInfo> getUpstream(@PathParam("serviceId") String serviceId, @PathParam("upstream") String upstream) {
    return datastore.getUpstream(serviceId, upstream);
  }
}

@@ -1117,32 +1117,464 @@
 viceId)));%0A  %7D%0A%0A
+  @DELETE%0A  @Path(%22/%7BserviceId%7D%22)%0A  public Optional%3CServiceState%3E deleteService(@PathParam(%22serviceId%22) String serviceId) %7B%0A    final Optional%3CService%3E maybeServiceInfo = datastore.getService(serviceId);%0A%0A    if (!maybeServiceInfo.isPresent()) %7B%0A      return Optional.absent();%0A    %7D%0A%0A    datastore.removeService(serviceId);%0A%0A    return Optional.of(new ServiceState(maybeServiceInfo.get(), datastore.getUpstreams(serviceId)));%0A  %7D%0A%0A
   @GET%0A  @Path(%22

allow deletion of services (do with caution)