diff --git a/.tool-versions b/.tool-versions index 51b0306..d7eacf1 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,2 +1,2 @@ -erlang 24.0.5 -elixir 1.12.2-otp-24 +elixir 1.14.4-otp-25 +erlang 25.3.2.7 \ No newline at end of file diff --git a/config/test.exs b/config/test.exs index 30e64c3..875db47 100644 --- a/config/test.exs +++ b/config/test.exs @@ -2,3 +2,5 @@ import Mix.Config config :lexdee, :environment, :test config :lexdee, :task, Lexdee.TaskMock + +config :logger, level: :warn diff --git a/lib/lexdee/client.ex b/lib/lexdee/client.ex index fa688d2..00b9b85 100644 --- a/lib/lexdee/client.ex +++ b/lib/lexdee/client.ex @@ -8,7 +8,8 @@ defmodule Lexdee.Client do middleware = [ Lexdee.Response, {Tesla.Middleware.BaseUrl, base_url}, - Tesla.Middleware.JSON + Tesla.Middleware.JSON, + {Tesla.Middleware.Logger, [debug: false]} ] adapter = get_adapter(cert, key) diff --git a/lib/lexdee/instances/files.ex b/lib/lexdee/instances/files.ex index a6bf220..4ff8d29 100644 --- a/lib/lexdee/instances/files.ex +++ b/lib/lexdee/instances/files.ex @@ -1,13 +1,25 @@ defmodule Lexdee.Instances.Files do + use Tesla + + alias Lexdee.Instances + @path "/files" - def create(client, id, path, content, options \\ []) do + def create(client, instance, file_path, content, options \\ []) do query = Keyword.get(options, :query) project = Keyword.get(query, :project) + type = Keyword.get(query, :type, "file") + + path = + [Instances.base_path(), instance, @path] + |> Path.join() - Tesla.post(client, Path.join(id, @path), content, - query: [path: path, project: project], - headers: [{"content-type", "application/octet-stream"}] + post(client, path, content, + query: [path: file_path, project: project], + headers: [ + {"content-type", "application/octet-stream"}, + {"X-LXD-type", type} + ] ) end end diff --git a/test/lexdee/instances/files_test.exs b/test/lexdee/instances/files_test.exs new file mode 100644 index 0000000..5e73d29 --- /dev/null +++ b/test/lexdee/instances/files_test.exs @@ -0,0 +1,33 @@ +defmodule Lexdee.Instances.FilesTest do + use ExUnit.Case + + setup do + id = "test-02" + + bypass = Bypass.open() + + client = Lexdee.create_client("http://localhost:#{bypass.port}") + + {:ok, client: client, bypass: bypass, id: id} + end + + describe "create file" do + test "can create file", %{bypass: bypass, client: client, id: id} do + response = + File.read!( + "test/support/fixtures/responses/instances/files/create.json" + ) + + Bypass.expect(bypass, "POST", "/1.0/instances/#{id}/files", fn conn -> + conn + |> Plug.Conn.put_resp_header("content-type", "application/json") + |> Plug.Conn.resp(200, response) + end) + + assert {:ok, %{body: %{}}} = + Lexdee.create_file(client, id, "/root/example.json", "test", + query: [project: "default"] + ) + end + end +end diff --git a/test/support/fixtures/responses/instances/files/create.json b/test/support/fixtures/responses/instances/files/create.json new file mode 100644 index 0000000..5a32c17 --- /dev/null +++ b/test/support/fixtures/responses/instances/files/create.json @@ -0,0 +1,9 @@ +{ + "type":"sync", + "status":"Success", + "status_code":200, + "operation":"", + "error_code":0, + "error":"", + "metadata":{} +} \ No newline at end of file