1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
# This class represents a Docker Network.
class Docker::Network
include Docker::Base
def connect(container, opts = {}, body_opts = {})
body = MultiJson.dump({ container: container }.merge(body_opts))
Docker::Util.parse_json(
connection.post(path_for('connect'), opts, body: body)
)
reload
end
def disconnect(container, opts = {})
body = MultiJson.dump(container: container)
Docker::Util.parse_json(
connection.post(path_for('disconnect'), opts, body: body)
)
reload
end
def remove(opts = {})
connection.delete(path_for, opts)
nil
end
alias_method :delete, :remove
def json(opts = {})
Docker::Util.parse_json(connection.get(path_for, opts))
end
def to_s
"Docker::Network { :id => #{id}, :info => #{info.inspect}, "\
":connection => #{connection} }"
end
def reload
network_json = @connection.get("/networks/#{@id}")
hash = Docker::Util.parse_json(network_json) || {}
@info = hash
end
class << self
def create(name, opts = {}, conn = Docker.connection)
default_opts = MultiJson.dump({
'Name' => name,
'CheckDuplicate' => true
}.merge(opts))
resp = conn.post('/networks/create', {}, body: default_opts)
response_hash = Docker::Util.parse_json(resp) || {}
get(response_hash['Id'], {}, conn) || {}
end
def get(id, opts = {}, conn = Docker.connection)
network_json = conn.get("/networks/#{id}", opts)
hash = Docker::Util.parse_json(network_json) || {}
new(conn, hash)
end
def all(opts = {}, conn = Docker.connection)
hashes = Docker::Util.parse_json(conn.get('/networks', opts)) || []
hashes.map { |hash| new(conn, hash) }
end
def remove(id, opts = {}, conn = Docker.connection)
conn.delete("/networks/#{id}", opts)
nil
end
alias_method :delete, :remove
def prune(conn = Docker.connection)
conn.post("/networks/prune", {})
nil
end
end
# Convenience method to return the path for a particular resource.
def path_for(resource = nil)
["/networks/#{id}", resource].compact.join('/')
end
private :path_for
end
|