Skip to content

Instantly share code, notes, and snippets.

@sheenobu
Created November 23, 2012 20:55
Show Gist options
  • Select an option

  • Save sheenobu/4137273 to your computer and use it in GitHub Desktop.

Select an option

Save sheenobu/4137273 to your computer and use it in GitHub Desktop.
class ProdOpenstackExternalInterfaceH(logger: ActorRef, xActorSystem: ActorSystem, _httpClient: ActorRef)
extends OpenstackExternalInterfaceH
with HttpHelper with JsonProcessor {
implicit val system = xActorSystem
implicit val httpClient = _httpClient
def parseIpAddress = { json: JValue =>
((json \ "server" \ "addresses" \ "public").filter({
case JObject(List(JField("version", JInt(x)), JField("addr", JString(_)))) => x == 4
case _ => false
})(0) \ "addr").extract[String]
}
def is_auth_header = {
x: HttpHeader => x._1.equalsIgnoreCase("X-Auth-Token")
}
def with_authentication_headers()(callback: Either[Throwable, Tuple2[URI, List[HttpHeader]]] => Unit) {
val responseF = HttpDialog(httpClient, Settings.openstackHost, Settings.openstackPort)
.send(new HttpRequest(
uri = "/v1.1",
method = HttpMethods.GET,
headers = List(
new HttpHeader("X-AUTH-USER", Settings.openstackUsername),
new HttpHeader("X-AUTH-KEY", Settings.openstackApiKey))))
.end
responseF onComplete {
case Left(error) =>
callback(Left(error))
case Right(HttpResponse(204, headers: List[HttpHeader], _, _)) =>
val uri = new URI(headers.find(_._1.equalsIgnoreCase("X-Server-Management-Url")).get._2)
callback(
Right(Tuple2(uri,
withJson ++ headers.filter(is_auth_header))))
case Right(HttpResponse(status: Int, _, _, _)) =>
callback(Left(new Exception("Got Unknown Status Code trying to authenticate with Openstack: " + status)))
}
}
def with_json(resp: HttpResponse)(callback: JValue => Unit) {
callback(parse(resp.bodyAsString))
}
override def get_server_status(m_serverId: String)(callback: (Either[Throwable, ServerInformation] => Unit)) {
with_authentication_headers() {
case Left(error) =>
callback(Left(error))
case Right(Tuple2(serverUrl: URI, headers: List[HttpHeader])) =>
val responseF = HttpDialog(httpClient, serverUrl.getHost, serverUrl.getPort)
.send(new HttpRequest(
uri = serverUrl.getPath + "/servers/%s".format(m_serverId),
method = HttpMethods.GET,
headers = headers))
.end
responseF onFailure {
case error: Exception => callback(Left(error))
}
responseF onSuccess {
case resp: HttpResponse => with_json(resp) { jv =>
(jv \ "server" \ "status") match {
case JString("ACTIVE") =>
callback(Right(
ServerInformation(
id = m_serverId,
address = IpV4Address(address = parseIpAddress(jv)),
status = ACTIVE)))
case JString("BUILD") =>
callback(Right(
ServerInformation(
id = m_serverId,
address = IpV4Address(address = ""),
status = BUILD)))
case _ =>
callback(Left(new Exception("Got Unknown Server Status")))
}
}
} //onSuccess
} //with_authentication_headers
}
override def create_server(x: OpenstackCreateServer)(callback: (Either[Throwable, ServerInformation] => Unit)) {
val body = ("server" ->
("name" -> x.serverNameSimple) ~
("imageRef" -> x.imageRef) ~
("flavorRef" -> x.flavorRef) ~
("metaData" -> (
("Server Name" -> x.serverName) ~
("PortalAccount" -> x.email))))
with_authentication_headers() {
case Left(error) =>
callback(Left(error))
case Right(Tuple2(serverUrl: URI, headers: List[HttpHeader])) =>
val json = compact(render(body)).getBytes
val responseF = HttpDialog(httpClient, serverUrl.getHost, serverUrl.getPort)
.send(new HttpRequest(
uri = serverUrl.getPath + "/servers",
method = HttpMethods.POST,
headers = headers,
body = json))
.end
responseF onFailure {
case error: Exception => callback(Left(error))
}
responseF onSuccess {
case resp: HttpResponse => with_json(resp) { jv =>
val serverId = (jv \ "server" \ "id").extract[String]
(jv \ "server" \ "status") match {
case JString("ACTIVE") =>
callback(Right(
ServerInformation(
id = serverId,
address = IpV4Address(address = ""),
status = ACTIVE)))
case JString("BUILD") =>
callback(Right(
ServerInformation(
id = serverId,
address = IpV4Address(address = ""),
status = BUILD)))
case _ =>
callback(Left(new Exception("Got Unknown Server Status")))
}
}
} //onSuccess
} //with_authentication_headers
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment