Module: Collins::Api::Asset

Included in:
Collins::Api
Defined in:
lib/collins/api/asset.rb

Instance Method Summary collapse

Instance Method Details

#count(options = {}) ⇒ Object

Count number of assets matching the specified criteria

Parameters:

  • options (Hash) (defaults to: {})

    Query options (same as in the “find” method)

Returns:

  • integer The number of assets matching the query

Raises:



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/collins/api/asset.rb', line 133

def count options = {}
  # create a copy so that we do not modify the original options array
  options = options.dup

  if options.include? :size or options.include? :page
    raise ExpectationFailedError.new "Do not specify 'size' or 'page' options when counting assets"
  else
    options[:size] = 1
    options[:page] = 0
  end

  query = asset_hash_to_find_query options
  params = query.to_a.map do |param|
    key, val = param
    if val.is_a?(Array)
      val.map{|v| "#{key}=#{asset_escape_attribute(v)}"}.join("&")
    else
      "#{key}=#{asset_escape_attribute(val)}"
    end
  end.reject{|s| s.empty?}

  logger.debug("Counting assets using params #{params.inspect}")
  http_get("/api/assets", params) do |response|
    parse_response response, :expects => 200, :as => :data do |json|
      json["Pagination"]["TotalResults"].to_i
    end
  end
end

#create!(asset_or_tag, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/collins/api/asset.rb', line 7

def create! asset_or_tag, options = {}
  asset = get_asset_or_tag asset_or_tag
  parameters = {
    :generate_ipmi => get_option(:generate_ipmi, options, false),
    :ipmi_pool => get_option(:ipmi_pool, options, nil),
    :status => get_option(:status, options, asset.status),
    :type => get_option(:type, options, asset.type)
  }
  parameters = select_non_empty_parameters parameters
  logger.debug("Creating asset #{asset.tag} with parameters #{parameters.inspect}")
  http_put("/api/asset/#{asset.tag}", parameters) do |response|
    parse_response response, :expects => 201, :as => :asset
  end
end

#delete!(asset_or_tag, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/collins/api/asset.rb', line 22

def delete! asset_or_tag, options = {}
  asset = get_asset_or_tag asset_or_tag
  parameters = {
    :reason => get_option(:reason, options, nil),
    :nuke   => get_option(:nuke, options, false),
  }
  parameters = select_non_empty_parameters parameters
  logger.debug("Deleting asset #{asset.tag} with parameters #{parameters.inspect}")
  http_delete("/api/asset/#{asset.tag}", parameters, asset.location) do |response|
    parse_response response, :expects => 200, :as => :status, :raise => strict?, :default => false
  end
end

#exists?(asset_or_tag, status = nil) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/collins/api/asset.rb', line 35

def exists? asset_or_tag, status = nil
  begin
    asset = get(asset_or_tag)
    if asset && status && asset.status.downcase == status.downcase then
      true
    elsif asset && status.nil? then
      true
    else
      false
    end
  rescue Collins::RequestError => e
    if e.code.to_i == 404 then
      false
    else
      # if strict? is true, should still return a boolean for exists?
      logger.info("Exception getting asset: #{e.class} #{e}")
      false
    end
  rescue Exception => e
    if e.class.to_s == "WebMock::NetConnectNotAllowedError" then
      raise e
    end
    # if strict? is true, should still return a boolean for exists?
    logger.info("Exception getting asset: #{e.class} - #{e}")
    false
  end
end

#find(options = {}) ⇒ Array<Collins::Asset>

Find assets matching the specified criteria

In general the options hash corresponds to asset key/value pairs. Reserved keys in the selector are treated with care. These keys are in Collins::Asset::Find::DATE_PARAMS and in Collins::Asset::Find::GENERAL_PARAMS. All other keys are assumed to be asset attributes.

Parameters:

  • options (Hash) (defaults to: {})

    Query options

Returns:

Raises:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/collins/api/asset.rb', line 83

def find options = {}
  query = asset_hash_to_find_query options
  params = query.to_a.map do |param|
    key, val = param
    if val.is_a?(Array) then
      val.map{|v| "#{key}=#{asset_escape_attribute(v)}"}.join("&")
    else
      "#{key}=#{asset_escape_attribute(val)}"
    end
  end.reject{|s| s.empty?}
  logger.debug("Finding assets using params #{params.inspect}")
  http_get("/api/assets", params) do |response|
    parse_response response, :expects => 200, :as => :paginated do |json|
      json.map { |j| Collins::Asset.from_json(j) }
    end
  end
end

#find_similar(asset_or_tag, size = 50, sort = "ASC", sort_type = "distance", only_unallocated = true) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/collins/api/asset.rb', line 112

def find_similar asset_or_tag, size = 50, sort = "ASC", sort_type = "distance", only_unallocated = true
  asset = get_asset_or_tag asset_or_tag
  params = {
    :size => size,
    :sort => sort,
    :sortType => sort_type,
    :onlyUnallocated => only_unallocated
  }
  logger.debug("Finding similar assets for #{asset.tag}")
  http_get("/api/asset/#{asset.tag}/similar", params) do |response|
    parse_response response, :expects => 200, :as => :paginated do |json|
      json.map { |j| Collins::Asset.from_json(j) }
    end
  end
end

#get(asset_or_tag, options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/collins/api/asset.rb', line 63

def get asset_or_tag, options = {}
  asset = get_asset_or_tag asset_or_tag
  parameters = {
    :location => get_option(:location, options, nil)
  }
  parameters = select_non_empty_parameters parameters
  logger.debug("Getting asset #{asset.tag} with params #{parameters.inspect}")
  http_get("/api/asset/#{asset.tag}", parameters, asset.location) do |response|
    parse_response response, :as => :asset, :expects => 200, :raise => strict?, :default => false
  end
end

#search(query, size = 50, sort = "ASC", sort_field = "tag", options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/collins/api/asset.rb', line 101

def search query, size = 50, sort = "ASC", sort_field = "tag", options = {}
  logger.warn("client method \"search\" is deprecated, please use find instead")
  params = {
    :query => query,
    :size => size,
    :sort => sort,
    :sortField => sort_field
  }
  find params
end