Class: Docker::Network

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/docker/network.rb

Overview

This class represents a Docker Network.

Instance Attribute Summary

Attributes included from Base

#connection, #id, #info

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#initialize, #normalize_hash

Class Method Details

.all(opts = {}, conn = Docker.connection) ⇒ Object



60
61
62
63
# File 'lib/docker/network.rb', line 60

def all(opts = {}, conn = Docker.connection)
  hashes = Docker::Util.parse_json(conn.get('/networks', opts)) || []
  hashes.map { |hash| new(conn, hash) }
end

.create(name, opts = {}, conn = Docker.connection) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/docker/network.rb', line 43

def create(name, opts = {}, conn = Docker.connection)
  default_opts = {
    'Name' => name,
    'CheckDuplicate' => true
  }
  resp = conn.post('/networks/create', {},
                   body: default_opts.merge(opts).to_json)
  response_hash = Docker::Util.parse_json(resp) || {}
  get(response_hash['Id'], {}, conn) || {}
end

.get(id, opts = {}, conn = Docker.connection) ⇒ Object



54
55
56
57
58
# File 'lib/docker/network.rb', line 54

def get(id, opts = {}, conn = Docker.connection)
  network_json = conn.get("/networks/#{URI.encode(id)}", opts)
  hash = Docker::Util.parse_json(network_json) || {}
  new(conn, hash)
end

.remove(id, opts = {}, conn = Docker.connection) ⇒ Object Also known as: delete



65
66
67
68
# File 'lib/docker/network.rb', line 65

def remove(id, opts = {}, conn = Docker.connection)
  conn.delete("/networks/#{URI.encode(id)}", opts)
  nil
end

Instance Method Details

#connect(container, opts = {}, body_opts = {}) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/docker/network.rb', line 5

def connect(container, opts = {}, body_opts = {})
  Docker::Util.parse_json(
    connection.post(path_for('connect'), opts,
                    body: { container: container }.merge(body_opts).to_json)
  )
  reload
end

#deleteObject



25
26
27
28
# File 'lib/docker/network.rb', line 25

def remove(opts = {})
  connection.delete(path_for, opts)
  nil
end

#disconnect(container, opts = {}) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/docker/network.rb', line 13

def disconnect(container, opts = {})
  Docker::Util.parse_json(
    connection.post(path_for('disconnect'), opts,
                    body: { container: container }.to_json)
  )
  reload
end

#json(opts = {}) ⇒ Object



27
28
29
# File 'lib/docker/network.rb', line 27

def json(opts = {})
  Docker::Util.parse_json(connection.get(path_for, opts))
end

#reloadObject



36
37
38
39
40
# File 'lib/docker/network.rb', line 36

def reload
  network_json = @connection.get("/networks/#{URI.encode(@id)}")
  hash = Docker::Util.parse_json(network_json) || {}
  @info = hash
end

#remove(opts = {}) ⇒ Object



21
22
23
24
# File 'lib/docker/network.rb', line 21

def remove(opts = {})
  connection.delete(path_for, opts)
  nil
end

#to_sObject



31
32
33
34
# File 'lib/docker/network.rb', line 31

def to_s
  "Docker::Network { :id => #{id}, :info => #{info.inspect}, "\
    ":connection => #{connection} }"
end