collins_client 0.2.9 → 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ 1.9.2-p290
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.9
1
+ 0.2.10
@@ -5,17 +5,18 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "collins_client"
8
- s.version = "0.2.9"
8
+ s.version = "0.2.10"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Blake Matheny"]
12
- s.date = "2012-11-19"
12
+ s.date = "2012-12-17"
13
13
  s.description = "Provides ruby support for interacting with the Collins API"
14
14
  s.email = "bmatheny@tumblr.com"
15
15
  s.extra_rdoc_files = [
16
16
  "README.md"
17
17
  ]
18
18
  s.files = [
19
+ ".rbenv-version",
19
20
  "Gemfile",
20
21
  "Gemfile.lock",
21
22
  "README.md",
@@ -39,6 +40,8 @@ Gem::Specification.new do |s|
39
40
  "lib/collins/api/util/responses.rb",
40
41
  "lib/collins/asset.rb",
41
42
  "lib/collins/asset_client.rb",
43
+ "lib/collins/asset_find.rb",
44
+ "lib/collins/asset_update.rb",
42
45
  "lib/collins/client.rb",
43
46
  "lib/collins/errors.rb",
44
47
  "lib/collins/ipmi.rb",
@@ -79,11 +79,6 @@ module Collins; module Api
79
79
  # @return [Array<Collins::Asset>] An array of assets matching the query
80
80
  # @raise [UnexpectedResponseError] If the HTTP response code is not a 200
81
81
  def find options = {}
82
- solrquery = options.delete(:query)
83
- if solrquery != nil then
84
- return search solrquery, (options[:size] || 50), (options[:sort] || "ASC"), "tag", options
85
- end
86
- use_api_version "1.1"
87
82
  query = asset_hash_to_find_query options
88
83
  params = query.to_a.map do |param|
89
84
  key, val = param
@@ -102,32 +97,14 @@ module Collins; module Api
102
97
  end
103
98
 
104
99
  def search query, size = 50, sort = "ASC", sort_field = "tag", options = {}
105
- use_api_version "1.2"
106
- if query.start_with? "\"" and query.end_with? "\"" then
107
- query = query[1..-2]
108
- end
100
+ logger.warn("client method \"search\" is deprecated, please use find instead")
109
101
  params = {
110
102
  :query => query,
111
103
  :size => size,
112
104
  :sort => sort,
113
- :sort_field => sort_field
105
+ :sortField => sort_field
114
106
  }
115
- query = asset_hash_to_find_query options.merge(params)
116
- params = query.to_a.map do |(key,val)|
117
- if val.is_a?(Array) then
118
- val.map{|v| "#{key}=#{asset_escape_attribute(v)}"}.join("&")
119
- elsif val.nil? then
120
- ""
121
- else
122
- "#{key}=#{asset_escape_attribute(val)}"
123
- end
124
- end.reject{|s| s.empty?}
125
- logger.debug("perform asset search using query #{query} - #{params.inspect}")
126
- http_get("/api/assets",params) do |response|
127
- parse_response response, :expects => 200, :as => :paginated do |json|
128
- json.map { |j| Collins::Asset.from_json(j) }
129
- end
130
- end
107
+ find params
131
108
  end
132
109
 
133
110
  def find_similar asset_or_tag, size = 50, sort = "ASC", sort_type = "distance", only_unallocated = true
@@ -152,7 +129,7 @@ module Collins; module Api
152
129
  end
153
130
 
154
131
  def asset_hash_to_find_query opts = {}
155
- options = opts.clone
132
+ options = deep_copy_hash opts
156
133
  hash = {:attribute => []}
157
134
  okeys = options.keys
158
135
  Collins::Asset::Find::DATE_PARAMS.each do |query_key|
@@ -1,6 +1,7 @@
1
1
  module Collins; module Api
2
2
 
3
3
  module Attributes
4
+
4
5
  def delete_attribute! asset_or_tag, attribute, group_id = nil
5
6
  asset = get_asset_or_tag asset_or_tag
6
7
  parameters = {
@@ -12,14 +13,36 @@ module Collins; module Api
12
13
  parse_response response, :expects => 202, :as => :status, :raise => strict?, :default => false
13
14
  end
14
15
  end
16
+
15
17
  def set_attribute! asset_or_tag, key, value, group_id = nil
18
+ set_multi_attribute! asset_or_tag, {key => value}, group_id
19
+ end
20
+
21
+ def set_multi_attribute! asset_or_tag, kv_hash, group_id = nil
16
22
  asset = get_asset_or_tag asset_or_tag
23
+ http_overrides = {}
17
24
  parameters = {
18
- :attribute => "#{key};#{value}",
19
- :groupId => group_id
25
+ :groupId => group_id,
26
+ :attribute => []
20
27
  }
28
+ kv_hash.each do |key,value|
29
+ if ::Collins::Asset::Update.is_attribute?(key) then
30
+ parameters[:attribute] << "#{key};#{value}"
31
+ else
32
+ p = ::Collins::Asset::Update.get_param(key)
33
+ parameters[p.to_sym] = ::Collins::Asset::Update.get_param_value(key, value)
34
+ end
35
+ end
36
+ if parameters[:attribute].empty? then
37
+ parameters[:attribute] = nil
38
+ elsif parameters[:attribute].size == 1 then
39
+ parameters[:attribute] = parameters[:attribute].first
40
+ else
41
+ http_overrides[:query_string_normalizer] = HTTParty::Request::NON_RAILS_QUERY_STRING_NORMALIZER
42
+ end
21
43
  parameters = select_non_empty_parameters parameters
22
- logger.debug("Setting attribute #{key} to #{value} on #{asset.tag}")
44
+ logger.debug("Setting attributes #{parameters.inspect} on #{asset.tag}")
45
+ parameters[:http_options] = http_overrides unless http_overrides.empty?
23
46
  http_post("/api/asset/#{asset.tag}", parameters, asset.location) do |response|
24
47
  parse_response response, :expects => 200, :as => :status, :raise => strict?, :default => false
25
48
  end
@@ -109,7 +109,8 @@ module Collins; module Api
109
109
  # new solr interface
110
110
  def search_logs options = {}
111
111
  parameters = get_page_options(options).merge(
112
- :query => get_option(:query, options, nil)
112
+ :query => get_option(:query, options, nil),
113
+ :sortField => get_option(:sortField, options, 'ID')
113
114
  )
114
115
  parameters = select_non_empty_parameters parameters
115
116
  logger.debug("Fetching logs for all assets with parameters #{parameters.inspect}")
@@ -118,7 +119,6 @@ module Collins; module Api
118
119
  json.map{|j| OpenStruct.new(symbolize_hash(j))}
119
120
  end
120
121
  end
121
-
122
122
  end
123
123
 
124
124
  # Same as logs but for all assets
@@ -37,8 +37,9 @@ module Collins; module Api; module Util
37
37
 
38
38
  def http_post uri, parameters = {}, remote = nil
39
39
  http_call(uri) {
40
+ http_overrides = parameters.delete(:http_options) || {}
40
41
  params = strip_request :body, :body => parameters
41
- result = self.class.post(uri, http_options(params, remote))
42
+ result = self.class.post(uri, http_options(params, remote, http_overrides))
42
43
  if block_given? then
43
44
  yield(result)
44
45
  else
@@ -59,7 +60,7 @@ module Collins; module Api; module Util
59
60
  }
60
61
  end
61
62
 
62
- def http_options opts = {}, remote = nil
63
+ def http_options opts = {}, remote = nil, overrides = {}
63
64
  if remote then
64
65
  host_info = get_location_information remote
65
66
  auth = {:username => host_info.username, :password => host_info.password}
@@ -70,6 +71,7 @@ module Collins; module Api; module Util
70
71
  end
71
72
  http_opts = opts.merge!({:basic_auth => auth, :base_uri => base_uri, :timeout => timeout_i})
72
73
  http_opts[:headers] = headers unless headers.empty?
74
+ http_opts.merge!(overrides)
73
75
  http_opts[:debug_output] = $stderr if (logger.level < 0 and Module.const_defined?(:HTTP_DEBUG) and HTTP_DEBUG)
74
76
  http_opts
75
77
  end
@@ -1,4 +1,6 @@
1
1
  require 'collins/address'
2
+ require 'collins/asset_find'
3
+ require 'collins/asset_update'
2
4
  require 'collins/ipmi'
3
5
  require 'collins/power'
4
6
  require 'collins/state'
@@ -14,33 +16,8 @@ module Collins
14
16
 
15
17
  # Asset finder related parameter descriptions
16
18
  # @note these exist here instead of the API module for convenience
17
- module Find
18
- # Find API parameters that are dates
19
- # @return [Array<String>] Date related query parameters
20
- DATE_PARAMS = [
21
- "createdAfter", "createdBefore", "updatedAfter", "updatedBefore"
22
- ]
23
- # Find API parameters that are not dates
24
- # This list exists so that when assets are being queries, we know what keys in the find hash
25
- # are attributes of the asset (such as hostname), and which are nort (such as sort or page).
26
- # @return [Array,<String>] Non-date related query parameters that are 'reserved'
27
- GENERAL_PARAMS = [
28
- "details", "tag", "type", "status", "page", "size", "sort", "state", "operation", "remoteLookup", "query",
29
- "sort_field"
30
- ]
31
- # @return [Array<String>] DATE_PARAMS plus GENERAL_PARAMS
32
- ALL_PARAMS = DATE_PARAMS + GENERAL_PARAMS
33
-
34
- class << self
35
- def to_a
36
- Collins::Asset::Find::ALL_PARAMS
37
- end
38
- def valid? key
39
- to_a.include?(key.to_s)
40
- end
41
- end
42
- end
43
-
19
+ include Collins::Asset::Find
20
+ include Collins::Asset::Update
44
21
  include Collins::Util
45
22
 
46
23
  # @return [Array<CollinsAddress>] Addresses associated with asset
@@ -0,0 +1,29 @@
1
+ module Collins
2
+ class Asset
3
+ module Find
4
+ # Find API parameters that are dates
5
+ # @return [Array<String>] Date related query parameters
6
+ DATE_PARAMS = [
7
+ "createdAfter", "createdBefore", "updatedAfter", "updatedBefore"
8
+ ]
9
+ # Find API parameters that are not dates
10
+ # This list exists so that when assets are being queries, we know what keys in the find hash
11
+ # are attributes of the asset (such as hostname), and which are nort (such as sort or page).
12
+ # @return [Array,<String>] Non-date related query parameters that are 'reserved'
13
+ GENERAL_PARAMS = [
14
+ "details", "tag", "type", "status", "page", "size", "sort", "state", "operation", "remoteLookup", "query",
15
+ "sortField"
16
+ ]
17
+ # @return [Array<String>] DATE_PARAMS plus GENERAL_PARAMS
18
+ ALL_PARAMS = DATE_PARAMS + GENERAL_PARAMS
19
+ class << self
20
+ def to_a
21
+ Collins::Asset::Find::ALL_PARAMS
22
+ end
23
+ def valid? key
24
+ to_a.include?(key.to_s)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,69 @@
1
+ module Collins
2
+ class Asset
3
+ # Params we know about for updates, others come in via attribute hash
4
+ module Update
5
+
6
+ NON_ATTRIBUTE_PARAMS = [
7
+ "CHASSIS_TAG", "RACK_POSITION", /^POWER_(.*)_(.*)/i
8
+ ]
9
+ FILE_PARAMS = [
10
+ "lshw", "lldp"
11
+ ]
12
+ ALL_PARAMS = NON_ATTRIBUTE_PARAMS + FILE_PARAMS
13
+
14
+ class << self
15
+
16
+ def to_a
17
+ Collins::Asset::Update::ALL_PARAMS
18
+ end
19
+
20
+ def get_param_value key, value
21
+ if is_file_param?(key) then
22
+ if value.start_with?('@') then
23
+ filename = File.expand_path(value[1..-1])
24
+ if !File.readable?(filename) then
25
+ msg = "Could not read file '#{filename}' for key '#{key}'"
26
+ raise ::Collins::ExpectationFailedError.new msg
27
+ else
28
+ File.read(filename)
29
+ end
30
+ else
31
+ value
32
+ end
33
+ else
34
+ value
35
+ end
36
+ end # get_param_value
37
+
38
+ def get_param key
39
+ to_a.each do |k|
40
+ if k.is_a?(Regexp) && !k.match(key).nil? then
41
+ # Assume it's a power setting until we have >1 regexp
42
+ return key.upcase
43
+ elsif key.to_s.downcase == k.to_s.downcase then
44
+ return k
45
+ end
46
+ end
47
+ return key
48
+ end # get_param
49
+
50
+ def is_file_param? key
51
+ FILE_PARAMS.map{|k|k.to_s.downcase}.include?(key.to_s.downcase)
52
+ end # is_file_param?
53
+
54
+ def is_attribute? key
55
+ to_a.each do |k|
56
+ if k.is_a?(Regexp) && !k.match(key).nil? then
57
+ return false
58
+ elsif key.to_s.downcase == k.to_s.downcase then
59
+ return false
60
+ end
61
+ end
62
+ return true
63
+ end # is_attribute?
64
+
65
+ end # class << self
66
+
67
+ end # module Update
68
+ end # class Asset
69
+ end # module Collins
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collins_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.2.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-19 00:00:00.000000000 Z
12
+ date: 2012-12-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -34,6 +34,7 @@ extensions: []
34
34
  extra_rdoc_files:
35
35
  - README.md
36
36
  files:
37
+ - .rbenv-version
37
38
  - Gemfile
38
39
  - Gemfile.lock
39
40
  - README.md
@@ -57,6 +58,8 @@ files:
57
58
  - lib/collins/api/util/responses.rb
58
59
  - lib/collins/asset.rb
59
60
  - lib/collins/asset_client.rb
61
+ - lib/collins/asset_find.rb
62
+ - lib/collins/asset_update.rb
60
63
  - lib/collins/client.rb
61
64
  - lib/collins/errors.rb
62
65
  - lib/collins/ipmi.rb
@@ -84,7 +87,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
87
  version: '0'
85
88
  segments:
86
89
  - 0
87
- hash: -117054897362145987
90
+ hash: -2685565510305324275
88
91
  required_rubygems_version: !ruby/object:Gem::Requirement
89
92
  none: false
90
93
  requirements: