Sha256: 86b1615d86e364303f76dbaaa1c47b42e0479ac7cb760093452efa36a458cf95

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

module Hcloud
  class ServerResource < AbstractResource
    include Enumerable

    def create(name:,
               server_type:,
               datacenter: nil,
               location: nil,
               start_after_create: nil,
               image:,
               ssh_keys: [],
               user_data: nil)
      query = {}
      method(:create).parameters.inject(query) do |r,x| 
        (var = eval(x.last.to_s)).nil? ? r : r.merge!(x.last => var)
      end
      j = Oj.load(request("servers", j: query, code: 200).run.body)
      [
        Action.new(j["action"], self, client),
        Server.new(j["server"], self, client),
        j["root_password"]
      ]
    end

    def all
      Oj.load(request("servers").run.body)["servers"].map do |x|
        Server.new(x, self, client)
      end
    end

    def find(id)
      Server.new(
        Oj.load(request("servers/#{id.to_i}").run.body)["server"], self, client
      )
    end

    def [](arg)
      case arg
      when Integer
       begin
         find(arg)
       rescue Error::NotFound
       end
      when String
        find_by(name: arg)
      end
    end
    
    def find_by(name:)
      x = Oj.load(request("servers", q: {name: name}).run.body)["servers"]
      return nil if x.none?
      x.each do |s|
        return Server.new(s, self, client)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hcloud-0.1.0.pre.alpha4 lib/hcloud/server_resource.rb
hcloud-0.1.0.pre.alpha3 lib/hcloud/server_resource.rb