From 4fe19d8f6a84fb62aa44bc597c9d7be9ea134085 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Tue, 21 Jun 2022 20:28:25 +0200 Subject: [PATCH] feat: implemented container_list --- ROADMAP.md | 4 +-- containers.v | 76 ++++++++++++++++++++++++++++++++++++++++++++++++---- docker.v | 30 ++++++++++----------- errors.v | 14 +++++----- volumes.v | 10 ++++++- 5 files changed, 104 insertions(+), 30 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 68b953f..52e22a2 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -8,7 +8,7 @@ This list was taking from the [API reference](https://docs.docker.com/engine/api/v1.41/). * Containers - - [ ] List containers + - [x] List containers - [ ] Create a container - [ ] Inspect a container - [ ] List processes running inside a container @@ -61,7 +61,7 @@ reference](https://docs.docker.com/engine/api/v1.41/). - [ ] Delete unused networks * Volumes - - [*] List volumes + - [x] List volumes - [ ] Create a volume - [ ] Inspect a volume - [ ] Remove a volume diff --git a/containers.v b/containers.v index 0abedff..9991e75 100644 --- a/containers.v +++ b/containers.v @@ -4,12 +4,78 @@ import json import time import net.http { Method } +pub struct Port { + ip string [json: IP] + private_port u16 [json: PrivatePort] + public_port u16 [json: PublicPort] + type_ string [json: Type] +} + +pub struct HostConfig { + network_mode string [json: NetworkMode] +} + +pub struct EndpointIpamConfig { + ipv4_address string [json: IPv4Address] + ipv6_address string [json: IPv6Address] + link_local_ips []string [json: LinkLocalIPs] +} + +pub struct EndpointSettings { + ipam_config EndpointIpamConfig [json: IPAMConfig] + links []string [json: Links] + aliases []string [json: Aliases] + network_id string [json: NetworkID] + endpoint_id string [json: EndpointID] + gateway string [json: Gateway] + ip_address string [json: IPAddress] + ip_prefix_len int [json: IPPrefixLen] + ipv6_gateway string [json: IPv6Gateway] + global_ipv6_address string [json: GlobalIPv6Address] + global_ipv6_prefix_len i64 [json: GlobalIPv6PrefixLen] + mac_address string [json: MacAddress] + driver_opts map[string]string [json: DriverOpts] +} + +pub struct NetworkSettings { + networks map[string]EndpointSettings [json: Networks] +} + +pub struct MountPoint { + type_ string [json: Type] + name string [json: Name] + source string [json: Source] + destination string [json: Destination] + driver string [json: Driver] + mode string [json: Mode] + rw bool [json: RW] + propagation string [json: Propagation] +} + pub struct ContainerListItem { - id string [json: Id] - names []string [json: Names] - image string [json: Image] - image_id string [json: ImageID] - command string [json: Command] + id string [json: Id] + names []string [json: Names] + image string [json: Image] + image_id string [json: ImageID] + command string [json: Command] + created i64 [json: Created] + ports []Port [json: Ports] + size_rw i64 [json: SizeRw] + size_root_fs i64 [json: sizeRootFs] + labels map[string]string [json: Labels] + state string [json: State] + status string [json: Status] + host_config HostConfig [json: HostConfig] + network_settings NetworkSettings [json: NetworkSettings] + mounts []MountPoint [json: Mounts] +} + +pub fn (mut d DockerConn) container_list() ?[]ContainerListItem { + d.send_request(Method.get, '/containers/json')? + + data := d.read_json_response<[]ContainerListItem>()? + + return data } pub struct NewContainer { diff --git a/docker.v b/docker.v index 4920203..9eaa1a9 100644 --- a/docker.v +++ b/docker.v @@ -14,8 +14,8 @@ const ( buf_len = 10 * 1024 http_separator = [u8(`\r`), `\n`, `\r`, `\n`] http_chunk_separator = [u8(`\r`), `\n`] - timestamp_attr = 'timestamp' - api_version = 'v1.41' + timestamp_attr = 'timestamp' + api_version = 'v1.41' ) pub struct DockerConn { @@ -43,7 +43,7 @@ pub fn (mut d DockerConn) close() ? { // send_request sends an HTTP request without body. fn (mut d DockerConn) send_request(method http.Method, url_str string) ? { - url := urllib.parse('/$api_version$url_str')? + url := urllib.parse('/$vdocker.api_version$url_str')? req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n' d.socket.write_string(req)? @@ -54,7 +54,7 @@ fn (mut d DockerConn) send_request(method http.Method, url_str string) ? { // send_request_with_body sends an HTTP request with the given body. fn (mut d DockerConn) send_request_with_body(method http.Method, url_str string, content_type string, body string) ? { - url := urllib.parse('/$api_version$url_str')? + url := urllib.parse('/$vdocker.api_version$url_str')? req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\nContent-Type: $content_type\nContent-Length: $body.len\n\n$body\n\n' d.socket.write_string(req)? @@ -127,21 +127,21 @@ fn (mut d DockerConn) read_response() ?(http.Response, string) { fn (mut d DockerConn) read_json_response() ?T { head, body := d.read_response()? - if head.status_code < 200 || head.status_code > 300 { - data := json.decode(DockerError, body)? + if head.status_code < 200 || head.status_code > 300 { + data := json.decode(DockerError, body)? - return docker_error(head.status_code, data.message) - } + return docker_error(head.status_code, data.message) + } - mut data := json.decode(T, body)? + mut data := json.decode(T, body)? - /* $for field in T.fields { */ - /* $if field.typ is time.Time { */ - /* data.$(field.name) = time.parse_rfc3339(data.$(field.name + '_str'))? */ - /* } */ - /* } */ + //$for field in T.fields { + //$if field.typ is time.Time { + // data.$(field.name) = time.parse_rfc3339(data.$(field.name + '_str'))? + //} + //} - return data + return data } // get_chunked_response_reader returns a ChunkedResponseReader using the socket diff --git a/errors.v b/errors.v index ff24028..e5da55a 100644 --- a/errors.v +++ b/errors.v @@ -1,21 +1,21 @@ module vdocker struct DockerError { - status int [skip] + status int [skip] message string } fn (err DockerError) code() int { - return err.status + return err.status } fn (err DockerError) msg() string { - return err.message + return err.message } fn docker_error(status int, message string) IError { - return IError(DockerError{ - status: status - message: message - }) + return IError(DockerError{ + status: status + message: message + }) } diff --git a/volumes.v b/volumes.v index b0c6a04..9642198 100644 --- a/volumes.v +++ b/volumes.v @@ -22,6 +22,14 @@ pub mut: usage_data UsageData [json: UsageData] } +[params] +pub struct VolumeListFilter { + dangling bool + driver string + labels []string + name string +} + struct VolumeListResponse { volumes []Volume [json: Volumes] warnings []string [json: Warnings] @@ -30,7 +38,7 @@ struct VolumeListResponse { pub fn (mut d DockerConn) volume_list() ?VolumeListResponse { d.send_request(Method.get, '/volumes')? - mut data := d.read_json_response()? + mut data := d.read_json_response()? for mut vol in data.volumes { vol.created_at = time.parse_rfc3339(vol.created_at_str)?