Add Subnet related objects

parent 4af8ca30
package api
import errors.{APIConfigError, APIError}
import errors.APIConfigError
import requests.RequestAuth
import utils.config.{APIConfig, UserConfig}
......@@ -135,4 +135,15 @@ object Client {
val endpoint = f"$baseURL/$appName/$sectionEndpoint/$id/$subnetEndpoint"
sendRequest(endpoint, method="GET")
}
/**
* Send a request to the api to retrieve a specific subnet with unqieu `id`.
*
* @param id The unique ID of the subnet
* @return A tuple containing the status code and the returned json
*/
def getSubnet(id: String): (Int, String) = {
val endpoint = f"$baseURL/$appName/$subnetEndpoint/$id"
sendRequest(endpoint, method="GET")
}
}
package api.objects
import api.Client
import play.api.libs.json.{Json, Reads}
/**
* A single PHP Ipam subnet.
*
* @param id The unique ID of the subnet
* @param subnet The IP address of the subnet
* @param mask The netmask of the subnet
* @param sectionId The parent section ID
* @param description The description of the subnet
* @param masterSubnetId The parent subnet ID
* @param vlanId The vlan ID of the subnet
*/
case class Subnet(
id: String,
subnet: String,
mask: String,
sectionId: String,
description: Option[String],
masterSubnetId: String,
vlanId: Option[String],
)
/**
* The companion object exposing methods to operate with auth tokens and the
* API.
*/
object Subnet extends APIObject[Subnet] {
override implicit val decoder: Reads[Subnet] = Json.reads[Subnet]
/**
* Retrieve the subnet with unique `id`.
*
* @param id The unique ID of the subnet
* @return A `Subnet` object
*/
def getSubnet(id: Int): Subnet = getSubnet(id.toString)
def getSubnet(id: String): Subnet = {
val (_, json) = Client.getSubnet(id)
extractFromJson(json)
}
}
package api.objects.collections
import api.Client
import api.objects.{APIObject, Section}
import api.objects.Section
import play.api.libs.json.{Json, Reads}
/**
......@@ -17,7 +17,7 @@ object Sections extends APIObjectCollection[Section] {
* @return A list of `Section`.
*/
def getSections: List[Section] = {
val json: String = Client.getSections._2
val (_, json) = Client.getSections
extractFromJson(json)
}
}
package api.objects.collections
import api.Client
import api.objects.Subnet
import play.api.libs.json.{Json, Reads}
/**
* An object to retrieve a list of subnet.
*/
object Subnets extends APIObjectCollection[Subnet] {
protected implicit val decoder: Reads[Subnet] = Json.reads[Subnet]
/**
* Get all subnets of a section from the API.
*
* @return A list of `Section`.
*/
def getSubnetsFromSection(id: Int): List[Subnet] = getSubnetsFromSection(id.toString)
def getSubnetsFromSection(id: String): List[Subnet] = {
val (_, json) = Client.getSubnetsFromSection(id)
extractFromJson(json)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment