Skip to content

Commit

Permalink
Merge branch 'release/1.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Jan 24, 2022
2 parents 77efeb7 + 3b64fcd commit 23ec401
Show file tree
Hide file tree
Showing 13 changed files with 388 additions and 6 deletions.
14 changes: 12 additions & 2 deletions lib/lexdee.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Lexdee do

alias Lexdee.Profiles

defdelegate list_profiles(client), to: Profiles, as: :index
defdelegate list_profiles(client, opts \\ []), to: Profiles, as: :index
defdelegate get_profile(client, id), to: Profiles, as: :show
defdelegate update_profile(client, id, params), to: Profiles, as: :update
defdelegate create_profile(client, params), to: Profiles, as: :create
Expand Down Expand Up @@ -50,7 +50,7 @@ defmodule Lexdee do
to: Instances.Exec,
as: :perform

defdelegate list_instances(client), to: Instances, as: :index
defdelegate list_instances(client, options \\ []), to: Instances, as: :index
defdelegate get_instance(client, id), to: Instances, as: :show

defdelegate delete_instance(client, id), to: Instances, as: :remove
Expand Down Expand Up @@ -88,4 +88,14 @@ defmodule Lexdee do
defdelegate wait_for_operation(client, id, options \\ []),
to: Operations,
as: :wait

alias Lexdee.Networks

defdelegate list_networks(client, options \\ []),
to: Networks,
as: :index

defdelegate list_network_leases(client, id, options \\ []),
to: Networks.Leases,
as: :index
end
5 changes: 3 additions & 2 deletions lib/lexdee/instances.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ defmodule Lexdee.Instances do
@spec base_path :: binary()
def base_path, do: @path

@spec index(Tesla.Client.t()) :: {:error, any} | {:ok, Tesla.Env.t()}
def index(client), do: Tesla.get(client, @path)
@spec index(Tesla.Client.t(), Keyword.t()) ::
{:error, any} | {:ok, Tesla.Env.t()}
def index(client, options \\ []), do: Tesla.get(client, @path, options)

@spec show(Tesla.Client.t(), binary()) :: {:error, any} | {:ok, Tesla.Env.t()}
def show(client, id), do: Tesla.get(client, Path.join(@path, id))
Expand Down
9 changes: 9 additions & 0 deletions lib/lexdee/networks.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Lexdee.Networks do
use Tesla

@path "/1.0/networks"

def base_path, do: @path

def index(client, options \\ []), do: get(client, @path, options)
end
16 changes: 16 additions & 0 deletions lib/lexdee/networks/leases.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Lexdee.Networks.Leases do
use Tesla
alias Lexdee.Networks

@path "/leases"

@spec index(Tesla.Client.t(), binary(), Keyword.t()) ::
{:error, any} | {:ok, Tesla.Env.t()}
def index(client, id, opts \\ []) do
path =
[Networks.base_path(), id, @path]
|> Path.join()

get(client, path, opts)
end
end
2 changes: 1 addition & 1 deletion lib/lexdee/profiles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Lexdee.Profiles do

@path "/1.0/profiles"

def index(client), do: Tesla.get(client, @path)
def index(client, options \\ []), do: Tesla.get(client, @path, options)

def show(client, id),
do: get(client, Path.join(@path, id))
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Lexdee.MixProject do
def project do
[
app: :lexdee,
version: "1.0.0",
version: "1.0.4",
elixir: "~> 1.9",
start_permanent: Mix.env() == :prod,
package: package(),
Expand Down
35 changes: 35 additions & 0 deletions test/lexdee/networks/leases_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule Lexdee.Networks.LeasesTest do
use ExUnit.Case

describe "list network leases" do
setup do
response =
File.read!(
"test/support/fixtures/responses/networks/leases/success.json"
)

bypass = Bypass.open()

client = Lexdee.create_client("http://localhost:#{bypass.port}")

{:ok, client: client, bypass: bypass, response: response}
end

test "return list of leases", %{
bypass: bypass,
client: client,
response: response
} do
Bypass.expect(bypass, "GET", "/1.0/networks/lxdfan0/leases", fn conn ->
conn
|> Plug.Conn.put_resp_header("content-type", "application/json")
|> Plug.Conn.resp(200, response)
end)

assert {:ok, %{body: body}} =
Lexdee.list_network_leases(client, "lxdfan0")

assert Enum.count(body) == 2
end
end
end
40 changes: 40 additions & 0 deletions test/lexdee/networks_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule Lexdee.NetworksTest do
use ExUnit.Case

describe "list networks with recursive option" do
setup do
response =
File.read!(
"test/support/fixtures/responses/networks/index_recursive_one.json"
)

bypass = Bypass.open()

client = Lexdee.create_client("http://localhost:#{bypass.port}")

{:ok, bypass: bypass, response: response, client: client}
end

test "return list of networks", %{
bypass: bypass,
client: client,
response: response
} do
Bypass.expect(bypass, "GET", "/1.0/networks", fn conn ->
assert %{"recursive" => "1"} = conn.query_params

conn
|> Plug.Conn.put_resp_header("content-type", "application/json")
|> Plug.Conn.resp(200, response)
end)

assert {:ok, %{body: body}} =
Lexdee.list_networks(client, query: [recursive: 1])

assert %{"name" => "lxdfan0"} =
Enum.find(body, fn network ->
network["managed"]
end)
end
end
end
23 changes: 23 additions & 0 deletions test/lexdee/profiles_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ defmodule Lexdee.ProfilesTest do
assert {:ok, %{body: profiles}} = Lexdee.list_profiles(client)
assert Enum.count(profiles) == 2
end

test "return success for list of profiles with recursive", %{
bypass: bypass,
client: client
} do
response =
File.read!(
"test/support/fixtures/responses/profiles/index_recursive_one.json"
)

Bypass.expect(bypass, "GET", "/1.0/profiles", fn conn ->
assert %{"recursive" => "1"} = conn.query_params

conn
|> Plug.Conn.put_resp_header("content-type", "application/json")
|> Plug.Conn.resp(200, response)
end)

assert {:ok, %{body: profiles}} =
Lexdee.list_profiles(client, query: [recursive: 1])

assert Enum.count(profiles) == 3
end
end

describe "profile show" do
Expand Down
80 changes: 80 additions & 0 deletions test/support/fixtures/responses/networks/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"type": "sync",
"status": "Success",
"status_code": 200,
"operation": "",
"error_code": 0,
"error": "",
"metadata": [
{
"config": {
"bridge.mode": "fan",
"fan.underlay_subnet": "10.130.0.0/16",
"ipv4.nat": "true"
},
"description": "",
"name": "lxdfan0",
"type": "bridge",
"used_by": [
"/1.0/profiles/default",
"/1.0/instances/artellectual-web-01",
"/1.0/instances/helipad-web-test-01"
],
"managed": true,
"status": "Created",
"locations": [
"ubuntu-s-1vcpu-1gb-sgp1-01"
]
},
{
"config": {},
"description": "",
"name": "lo",
"type": "loopback",
"used_by": [],
"managed": false,
"status": "",
"locations": null
},
{
"config": {},
"description": "",
"name": "eth0",
"type": "physical",
"used_by": null,
"managed": false,
"status": "",
"locations": null
},
{
"config": {},
"description": "",
"name": "eth1",
"type": "physical",
"used_by": null,
"managed": false,
"status": "",
"locations": null
},
{
"config": {},
"description": "",
"name": "lxdfan0-mtu",
"type": "unknown",
"used_by": null,
"managed": false,
"status": "",
"locations": null
},
{
"config": {},
"description": "",
"name": "lxdfan0-fan",
"type": "unknown",
"used_by": null,
"managed": false,
"status": "",
"locations": null
}
]
}
80 changes: 80 additions & 0 deletions test/support/fixtures/responses/networks/index_recursive_one.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"type": "sync",
"status": "Success",
"status_code": 200,
"operation": "",
"error_code": 0,
"error": "",
"metadata": [
{
"config": {
"bridge.mode": "fan",
"fan.underlay_subnet": "10.130.0.0/16",
"ipv4.nat": "true"
},
"description": "",
"name": "lxdfan0",
"type": "bridge",
"used_by": [
"/1.0/profiles/default",
"/1.0/instances/artellectual-web-01",
"/1.0/instances/helipad-web-test-01"
],
"managed": true,
"status": "Created",
"locations": [
"ubuntu-s-1vcpu-1gb-sgp1-01"
]
},
{
"config": {},
"description": "",
"name": "lo",
"type": "loopback",
"used_by": [],
"managed": false,
"status": "",
"locations": null
},
{
"config": {},
"description": "",
"name": "eth0",
"type": "physical",
"used_by": null,
"managed": false,
"status": "",
"locations": null
},
{
"config": {},
"description": "",
"name": "eth1",
"type": "physical",
"used_by": null,
"managed": false,
"status": "",
"locations": null
},
{
"config": {},
"description": "",
"name": "lxdfan0-mtu",
"type": "unknown",
"used_by": null,
"managed": false,
"status": "",
"locations": null
},
{
"config": {},
"description": "",
"name": "lxdfan0-fan",
"type": "unknown",
"used_by": null,
"managed": false,
"status": "",
"locations": null
}
]
}
24 changes: 24 additions & 0 deletions test/support/fixtures/responses/networks/leases/success.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"type": "sync",
"status": "Success",
"status_code": 200,
"operation": "",
"error_code": 0,
"error": "",
"metadata": [
{
"hostname": "helipad-web-test-01",
"hwaddr": "00:16:3e:8c:10:7b",
"address": "240.0.3.227",
"type": "dynamic",
"location": "ubuntu-s-1vcpu-1gb-sgp1-01"
},
{
"hostname": "artellectual-web-01",
"hwaddr": "00:16:3e:ec:13:2f",
"address": "240.0.3.194",
"type": "dynamic",
"location": "ubuntu-s-1vcpu-1gb-sgp1-01"
}
]
}
Loading

0 comments on commit 23ec401

Please sign in to comment.