feat(volumes): add minimal volumes api
+ 74
- 2
src/docker/api/api_client.cr
@@ -9,6 +9,7 @@ require "../errors"
 require "./containers"
 require "./daemon"
 require "./images"
+require "./volumes"
 
 # Low-level wrapper for the Docker Engine API.
 #

...
@@ -32,7 +33,7 @@ class Docker::Api::ApiClient
 
     case uri.scheme
     when "unix"
-      puts "unix socket connection to: #{@socket_path}\n"
+      # puts "unix socket connection to: #{@socket_path}\n"
       @socket_path = @socket_path.gsub(/unix:\/\//, "")
       @socket = UNIXSocket.new(@socket_path)
     else

...
@@ -113,7 +114,11 @@ class Docker::Api::ApiClient
         .gsub("1d\r\n", "")   # ?
         .gsub("4f\r\n", "")   # ?
 
-      # # Parse status line, headers and body
+      # pp path
+      # pp raw
+      # puts "\n"
+
+      # Parse status line, headers and body
       head, body = raw.split("\r\n\r\n", 2)
       body = body.chomp("\r\n").chomp("\r\n")
       status_line, *header_lines = head.split("\r\n")

...
@@ -203,4 +208,5 @@ class Docker::Api::ApiClient
   include Containers
   include Daemon
   include Images
+  include ::Volumes
 end

new file
src/docker/api/models/volume.cr
@@ -0,0 +1,20 @@
+require "./response"
+
+module Docker::Api
+  struct Volume < Response
+    @[JSON::Field(key: "CreatedAt")]
+    getter created_at : String
+    @[JSON::Field(key: "Driver")]
+    getter driver : String
+    @[JSON::Field(key: "Labels")]
+    getter labels : Hash(String, String)?
+    @[JSON::Field(key: "Mountpoint")]
+    getter mountpoint : String
+    @[JSON::Field(key: "Name")]
+    getter name : String
+    @[JSON::Field(key: "Options")]
+    getter options : Hash(String, String)? # Docker::Api::Volume::Options
+    @[JSON::Field(key: "Scope")]
+    getter scope : String
+  end
+end

new file
src/docker/api/volumes.cr
@@ -0,0 +1,20 @@
+require "../../tools"
+require "./models/volume"
+
+# API queries for image interaction.
+module Docker::Api::Volumes
+  def list(filters : Hash(String, Array(String)))
+    response = @client.get("/volumes", query: filters)
+
+    Docker::Api::Models::Volume
+      .from_json_array(response.body)
+  end
+
+  def create(name : String)
+    body = { "Name" => name }.to_json
+    response = @client.post("/volumes/create", body: body)
+
+    Docker::Api::Models::Volume
+      .from_json(response.body)
+  end
+end

@@ -1,6 +1,7 @@
 require "./api_client_wrapper"
 require "./containers"
 require "./images"
+require "./volumes"
 
 # A client for communicating with a Docker server.
 #

...
@@ -21,4 +22,8 @@ class Docker::Client
       @{{component.id}} ||= {{component.id.capitalize}}.new client
     end
   {% end %}
+
+  def volumes
+    Volumes.new @client
+  end
 end

new file
src/docker/volumes.cr
@@ -0,0 +1,21 @@
+require "./api_client_wrapper"
+require "./api/models/volume"
+
+# Interact with a single container that exists on a docker client.
+class Docker::Volumes
+  include Docker::ApiClientWrapper
+
+  def initialize(@client : Docker::Api::ApiClient)
+  end
+
+  # Method to get an archive from container
+  def list(filters : Hash(String, Array(String)))
+    # filters: { "name" => [vid] }
+    @client.list_volume filters
+  end
+
+  # Method to send an archive to container
+  def create(name : String)
+    @client.create_volume name
+  end
+end