Class: SilverlineAPI::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/librato_silverline_api.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Template

Returns a new instance of Template.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/librato_silverline_api.rb', line 65

def initialize(args = {})
  @id = args[:id] if args[:id]
  @name = args[:name] if args[:name]
  @min_cpu = args[:min_cpu] if args[:min_cpu]
  @min_mem = args[:min_mem] if args[:min_mem]
  @min_network_rbw = args[:min_network_rbw] if args[:min_network_rbw]
  @min_network_wbw = args[:min_network_wbw] if args[:min_network_wbw]
  @min_storage_rbw = args[:min_storage_rbw] if args[:min_storage_rbw]
  @min_storage_wbw = args[:min_storage_wbw] if args[:min_storage_wbw]
  @containers = []
  if args[:containers]
    args[:containers].each do |container|
      container.map {|k, v| container[k.to_sym] = v}
      @containers.push(Container.new(container))
    end
  end

  if @containers.size == 0
    @containers.push(Container.new(:name => "system"))
    @containers.push(Container.new(:name => "harvest", :cpu => {:allocation => 0}))
  end
end

Instance Attribute Details

#containersObject

Returns the value of attribute containers.



62
63
64
# File 'lib/librato_silverline_api.rb', line 62

def containers
  @containers
end

#errorsObject (readonly)

Returns the value of attribute errors.



63
64
65
# File 'lib/librato_silverline_api.rb', line 63

def errors
  @errors
end

#idObject

Returns the value of attribute id.



62
63
64
# File 'lib/librato_silverline_api.rb', line 62

def id
  @id
end

#min_cpuObject

Returns the value of attribute min_cpu.



62
63
64
# File 'lib/librato_silverline_api.rb', line 62

def min_cpu
  @min_cpu
end

#min_memObject

Returns the value of attribute min_mem.



62
63
64
# File 'lib/librato_silverline_api.rb', line 62

def min_mem
  @min_mem
end

#min_network_rbwObject

Returns the value of attribute min_network_rbw.



62
63
64
# File 'lib/librato_silverline_api.rb', line 62

def min_network_rbw
  @min_network_rbw
end

#min_network_wbwObject

Returns the value of attribute min_network_wbw.



62
63
64
# File 'lib/librato_silverline_api.rb', line 62

def min_network_wbw
  @min_network_wbw
end

#min_storage_rbwObject

Returns the value of attribute min_storage_rbw.



62
63
64
# File 'lib/librato_silverline_api.rb', line 62

def min_storage_rbw
  @min_storage_rbw
end

#min_storage_wbwObject

Returns the value of attribute min_storage_wbw.



62
63
64
# File 'lib/librato_silverline_api.rb', line 62

def min_storage_wbw
  @min_storage_wbw
end

#nameObject

Returns the value of attribute name.



62
63
64
# File 'lib/librato_silverline_api.rb', line 62

def name
  @name
end

Class Method Details

.create(args = {}) ⇒ Object



123
124
125
126
127
# File 'lib/librato_silverline_api.rb', line 123

def self.create(args = {})
  template = Template.new(args)
  template.save
  template
end

.find(*args) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/librato_silverline_api.rb', line 129

def self.find(*args)
  scope   = args.slice!(0)
  options = args.slice!(0) || {}

  if scope == :all
    array = options.map {|k, v| "#{k}=#{URI.escape(v.to_s)}"}
    arguments = array.join("&")
    unless arguments.empty?
      query = "/templates.json?#{arguments}"
    else
      query = "/templates.json"
    end
    response = RestClient.get("#{SilverlineAPI.url}#{query}")
    hash = symbolize_keys(JSON.parse(response))

    templates = []
    hash[:templates].each {|template| templates.push(Template.new(template))}
    SilverlineAPI.last_response = hash[:query]
    templates
  else # scope is ID => Template.find(5)
    query = "/templates/#{scope}.json"
    response = RestClient.get("#{SilverlineAPI.url}#{query}")
    hash = symbolize_keys(JSON.parse(response))
    Template.new(hash)
  end
end

.find_by_name(template_name) ⇒ Object



157
158
159
160
161
# File 'lib/librato_silverline_api.rb', line 157

def self.find_by_name(template_name)
  response = RestClient.get("#{SilverlineAPI.url}/templates.json?q=#{template_name}")
  template = symbolize_keys(JSON.parse(response))
  Template.new(template.first)
end

Instance Method Details

#container_by_name(container_name) ⇒ Object



163
164
165
# File 'lib/librato_silverline_api.rb', line 163

def container_by_name(container_name)
  @containers.find {|c| c.name == container_name}
end

#new?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/librato_silverline_api.rb', line 88

def new?
  @id.nil?
end

#saveObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/librato_silverline_api.rb', line 92

def save
  @errors = []
  parameters = {
    :name => @name, 
    :min_cpu => @min_cpu, 
    :min_mem => @min_mem,
    :min_network_rbw => @min_network_rbw,
    :min_network_wbw => @min_network_wbw,
    :min_storage_rbw => @min_storage_rbw,
    :min_storage_wbw => @min_storage_wbw,
    :containers => @containers
  }.to_json

  begin
    if @id
      RestClient.put("#{SilverlineAPI.url}/templates/#{@id}.json", parameters, :content_type => :json)
    else
      x = RestClient.post("#{SilverlineAPI.url}/templates.json", parameters, :content_type => :json)
      @id = x.headers[:location].scan(/\d+/).last
    end
  rescue RestClient::UnprocessableEntity => e
    @errors = JSON.parse(e.http_body)["errors"]
    return false
  rescue RestClient::BadRequest => e
    @errors = JSON.parse(e.http_body)["errors"]
    return false
  end

  true
end