All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.jeffshaw.digitalocean.SshKey.scala Maven / Gradle / Ivy

package me.jeffshaw.digitalocean

import org.json4s.JsonDSL._
import scala.concurrent.{ExecutionContext, Future}

case class SshKey(
  id: BigInt,
  name: String,
  fingerprint: String,
  publicKey: String
) {
  def setName(name: String)(implicit client: DigitalOceanClient, ec: ExecutionContext): Future[SshKey] = {
    SshKey.setNameById(id, name)
  }

  def delete()(implicit client: DigitalOceanClient, ec: ExecutionContext): Future[Unit] = {
    SshKey.deleteById(id)
  }

  override def equals(obj: scala.Any): Boolean = {
    obj match {
      case that: SshKey =>
        eq(that) || this.id == that.id
      case _ =>
        false
    }
  }

  override def hashCode(): Int = {
    id.hashCode()
  }
}

object SshKey
  extends Path
  with Listable[SshKey, responses.SshKeys] {
  override val path: Seq[String] = Seq("account", "keys")

  def setNameByFingerprint(fingerprint: String, name: String)
      (implicit client: DigitalOceanClient, ec: ExecutionContext): Future[SshKey] = {
    val path = this.path ++ Seq(fingerprint)
    val value = "name" -> name

    for {
      response <- client.put[responses.SshKey](path, value)
    } yield {
      response.sshKey
    }
  }

  def setNameById(id: BigInt, name: String)
      (implicit client: DigitalOceanClient, ec: ExecutionContext): Future[SshKey] = {
    val path = this.path ++ Seq(id.toString)
    val value = "name" -> name

    for {
      response <- client.put[responses.SshKey](path, value)
    } yield {
      response.sshKey
    }
  }

  def create(name: String, publicKey: String)
      (implicit client: DigitalOceanClient, ec: ExecutionContext): Future[SshKey] = {
    val value = ("name" -> name) ~
      ("public_key" -> publicKey)

    for {
      response <- client.post[responses.SshKey](path, value)
    } yield {
      response.sshKey
    }
  }

  def deleteById(id: BigInt)(implicit client: DigitalOceanClient, ec: ExecutionContext): Future[Unit] = {
    val path = this.path ++ Seq(id.toString)
    client.delete(path)
  }

  def deleteByFingerprint(fingerprint: String)
      (implicit client: DigitalOceanClient, ec: ExecutionContext): Future[Unit] = {
    val path = this.path ++ Seq(fingerprint)
    client.delete(path)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy