Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update file creation to support directory #7

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ import Mix.Config

config :lexdee, :environment, :test
config :lexdee, :task, Lexdee.TaskMock

config :logger, level: :warn
3 changes: 2 additions & 1 deletion lib/lexdee/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 16 additions & 4 deletions lib/lexdee/instances/files.ex
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions test/lexdee/instances/files_test.exs
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions test/support/fixtures/responses/instances/files/create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type":"sync",
"status":"Success",
"status_code":200,
"operation":"",
"error_code":0,
"error":"",
"metadata":{}
}
Loading