Skip to content
This repository has been archived by the owner on Jun 3, 2019. It is now read-only.

Commit

Permalink
Merge pull request #17 from travisbrown/feature/address
Browse files Browse the repository at this point in the history
Add (type-safe) address to LocalService definition
  • Loading branch information
mrfyda authored Aug 24, 2016
2 parents 6ea6ac0 + a2c47e7 commit 2ef7892
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import consul.Consul

instanciate a consul supplying an ip and a port indicating a working consul agent:
```scala
val myConsul = new consul.Consul(CONSUL_IP, CONSUL_PORT)
val myConsul = new consul.Consul(CONSUL_IP, CONSUL_PORT, Option(CONSUL_ACL_TOKEN))
import myConsul.v1._
```

Expand All @@ -38,9 +38,10 @@ catalog.nodes().map{ case nodes =>

Example - register a service with an http-check on the local node:
```scala
val myAddress = "127.0.0.1"
val myServicePort = 5000
val myServiceCheck = agent.service.httpCheck(s"http://localhost:$myServicePort/health","15s")
val myService = agent.service.LocalService(ServiceId("myServiceId"),ServiceType("myTypeOfService"),Set(ServiceTag("MyTag")),Some(myServicePort),Some(myServiceCheck))
val myService = agent.service.LocalService(ServiceId("myServiceId"),ServiceType("myTypeOfService"),Set(ServiceTag("MyTag")),Some(myServicePort),Some(myServiceCheck),Some(Address(myAddress)))
agent.service.register(myService)
```
the check ID of the registered service-check is available via:
Expand Down
8 changes: 5 additions & 3 deletions src/main/scala/consul/v1/agent/service/LocalService.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package consul.v1.agent.service

import consul.v1.common.Types.{CheckId, ServiceId, ServiceTag, ServiceType}
import consul.v1.common.Types.{Address, CheckId, ServiceId, ServiceTag, ServiceType}
import play.api.libs.functional.syntax._
import play.api.libs.json._

case class LocalService(ID: ServiceId, Name: ServiceType, Tags: Set[ServiceTag] = Set.empty, Port: Option[Int], Check: Option[Check]){
case class LocalService(ID: ServiceId, Name: ServiceType, Tags: Set[ServiceTag] = Set.empty, Port: Option[Int], Check: Option[Check],
Address: Option[Address] = None){
lazy val checkId:CheckId = CheckId(s"service:$ID")
}

Expand All @@ -14,7 +15,8 @@ object LocalService {
(__ \ "Name" ).write[ServiceType] and
(__ \ "Tags" ).write[Set[ServiceTag]] and
(__ \ "Port" ).write[Option[Int]] and
(__ \ "Check").write[Option[Check]]
(__ \ "Check").write[Option[Check]] and
(__ \ "Address").write[Option[Address]]
)( unlift(LocalService.unapply) )

//no id is provided -> id becomes name
Expand Down
4 changes: 4 additions & 0 deletions src/main/scala/consul/v1/common/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ trait Types {

type DatacenterId = WrappedType[String,DatacenterIds]
def DatacenterId: String => DatacenterId = WrappedType.apply

type Address = WrappedType[String, Addresses]
def Address: String => Address = WrappedType.apply
}

object Types extends Types{
Expand All @@ -33,6 +36,7 @@ object Types extends Types{
sealed trait ServiceTypes
sealed trait ServiceTags
sealed trait DatacenterIds
sealed trait Addresses

case class ConsulResponseParseException(error:JsError) extends Throwable
}
Expand Down

0 comments on commit 2ef7892

Please sign in to comment.