samanage 2.1.20 → 2.1.21

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2123d5a06d5316e8f8c68eeddc675da008aaaaad062208796f8f89bc142ce51e
4
- data.tar.gz: d8e19971dbadfdfe6b102fb40931786a30506f70090db7a56e2ada9babe539d2
3
+ metadata.gz: dbd3b3da593b36d83d02a91c0d79e3573a6e51a01ccd7e78460ea3493d64a271
4
+ data.tar.gz: 98953bce3408c07e8b56dce8f9833f7dae8679dbaa15c395fb457bc950ffc234
5
5
  SHA512:
6
- metadata.gz: 8444a5784699c2b93ee52ac126c4221d4c6cc55bdc5053c2c930a91c6b0474a046b04b28e2dd68bf419ad1453d5a42c838724ff5a0406cc08e2ce88d75214b6e
7
- data.tar.gz: f3e85b2df019e7776b286a58c0dff2631d125c1b0dca583ddae4e95f3736044d8069defa862642f9fa600422854da560deceafc075c880a984dc3fb5238e96a2
6
+ metadata.gz: f4f89a6c6afcf8ab99fa0c91b9e8c2a1a6ac7109d4010c1874ef3e4c2130e2eea0d0d95a1c0d0d63efe5c728e861ecacad0da36ad68573426bc3a8c53d819df2
7
+ data.tar.gz: 0e55006ee3345f13c5a2cfb7673031cb1f85e65209d8853b316db9451887a87b1c628bc3c0eb00e1104da60e5571c4de528e94e817b63a8b53a3e7787c12381c
data/Gemfile CHANGED
@@ -2,18 +2,22 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rspec"
6
5
  gem "httparty", "0.16.4"
7
6
  gem "ffi", "1.9.24"
8
7
 
9
8
  group :development do
10
- gem "guard-rspec", require: false
9
+ gem "rspec"
11
10
  gem "rubocop", require: false
12
11
  gem "rubocop-performance"
13
12
  end
14
13
 
15
14
 
16
- group :test do
15
+ group :development, :test do
17
16
  gem "guard-rspec", require: false
17
+ gem "guard"
18
+ gem "dotenv"
19
+ end
20
+
21
+ group :test do
18
22
  gem "faker"
19
23
  end
@@ -5,6 +5,7 @@ GEM
5
5
  coderay (1.1.2)
6
6
  concurrent-ruby (1.1.5)
7
7
  diff-lcs (1.3)
8
+ dotenv (2.7.5)
8
9
  faker (2.1.2)
9
10
  i18n (>= 0.8)
10
11
  ffi (1.9.24)
@@ -85,8 +86,10 @@ PLATFORMS
85
86
  ruby
86
87
 
87
88
  DEPENDENCIES
89
+ dotenv
88
90
  faker
89
91
  ffi (= 1.9.24)
92
+ guard
90
93
  guard-rspec
91
94
  httparty (= 0.16.4)
92
95
  rspec
data/README.md CHANGED
@@ -15,43 +15,50 @@
15
15
 
16
16
  Initialize API controller
17
17
  ```ruby
18
- api_controller = Samanage::Api.new(token: 'abc123')
18
+ samanage = Samanage::Api.new(token: 'abc123')
19
19
  ```
20
20
 
21
21
  - Create a user
22
22
  ```ruby
23
23
  user = {user: { name: 'John Doe', email: 'john.doe@example.com'}}
24
- api_controller.create_user(payload: user)
24
+ samanage.create_user(payload: user)
25
25
  ```
26
26
 
27
27
 
28
28
  - Find a user by email
29
29
  ```ruby
30
- my_user = api_controller.find_user_by_email(email: 'user@example.com')
30
+ my_user = samanage.find_user_by_email(email: 'user@example.com')
31
31
  ```
32
32
 
33
33
 
34
34
  - Update incident by ID
35
35
  ```ruby
36
36
  incident_data = {incident: { priority: 'Critical' }}
37
- incident_update = api_controller.update_incident(id: 123, payload: incident_data)
37
+ incident_update = samanage.update_incident(id: 123, payload: incident_data)
38
38
  ```
39
39
 
40
+
40
41
  - Update hardware
41
42
  ```ruby
42
- hardware = {hardware: {name: 'My Computer'}}
43
- result = api_controller.update_hardware(id: 123, payload: hardware)
43
+ hardware = {hardware: {name: 'Cool New Hostname'}}
44
+ result = samanage.update_hardware(id: 123, payload: hardware)
45
+ ```
46
+
47
+ - Use Filters
48
+ ```ruby
49
+ incidents_updated_today = samanage.incidents(options: {'updated' => 1})
50
+ expired_hardwares = samanage.hardwares(options: {'warranty_status[]' => 'Expired'})
44
51
  ```
45
52
 
46
53
 
47
54
  ### Listing and Searching
48
55
  - Find All Users
49
56
  ```ruby
50
- users = api_controller.users # returns all users
57
+ users = samanage.users # returns all users
51
58
  ```
52
59
  - Filtering Incidents matching custom field and updated in the last 7 days
53
60
  ```ruby
54
- incidents = api_controller.incidents(options: {'Custom Field' => 'Some Value', 'updated[]' => 7})
61
+ incidents = samanage.incidents(options: {'Custom Field' => 'Some Value', 'updated[]' => 7})
55
62
  ```
56
63
 
57
64
  - Listing / Searching also responds to blocks
@@ -8,6 +8,7 @@ require "samanage/api/attachments"
8
8
  require "samanage/api/category"
9
9
  require "samanage/api/changes"
10
10
  require "samanage/api/comments"
11
+ require "samanage/api/configuration_items"
11
12
  require "samanage/api/contracts"
12
13
  require "samanage/api/custom_fields"
13
14
  require "samanage/api/custom_forms"
@@ -19,11 +20,11 @@ require "samanage/api/mobiles"
19
20
  require "samanage/api/other_assets"
20
21
  require "samanage/api/problems"
21
22
  require "samanage/api/purchase_orders"
22
- require "samanage/api/requester"
23
23
  require "samanage/api/releases"
24
+ require "samanage/api/requester"
24
25
  require "samanage/api/sites"
25
- require "samanage/api/tasks"
26
26
  require "samanage/api/solutions"
27
+ require "samanage/api/tasks"
27
28
  require "samanage/api/time_tracks"
28
29
  require "samanage/api/users"
29
30
  require "samanage/api/utils"
@@ -12,7 +12,10 @@ module Samanage
12
12
  category: "categories.json",
13
13
  change: "changes.json",
14
14
  contract: "contracts.json",
15
+ configuration_item: "configuration_items.json",
16
+ custom_field: "custom_fields.json",
15
17
  custom_fields: "custom_fields.json",
18
+ custom_form: "custom_forms.json",
16
19
  custom_forms: "custom_forms.json",
17
20
  department: "departments.json",
18
21
  group: "groups.json",
@@ -114,6 +117,7 @@ module Samanage
114
117
  response = e.class
115
118
  raise Samanage::InvalidRequest.new(error: error, response: response, options: options)
116
119
  end
120
+
117
121
  end
118
122
 
119
123
  response = {}
@@ -148,6 +152,10 @@ module Samanage
148
152
  response[:data] = api_call.body
149
153
  error = response[:response]
150
154
  raise Samanage::InvalidRequest.new(error: error, response: response, options: options)
155
+ when 429
156
+ response[:data] = api_call.body
157
+ error = response[:response]
158
+ raise Samanage::InvalidRequest.new(error: error, response: response, options: options)
151
159
  else
152
160
  response[:data] = api_call.body
153
161
  error = response[:response]
@@ -162,9 +170,13 @@ module Samanage
162
170
 
163
171
  # Return all admins in the account
164
172
  def list_admins
165
- admin_role_id = execute(path: "roles.json")[:data].select { |role| role["name"] == "Administrator" }.first["id"]
173
+ admin_role_id = execute(path: "roles.json")[:data]
174
+ .find { |role| role["name"] == "Administrator" }
175
+ .dig("id")
176
+ admin_path = "users.json?role=#{admin_role_id}"
166
177
  admins.push(
167
- execute(path: "users.json?role=#{admin_role_id}")[:data].map { |u| u["email"] }
178
+ execute(path: admin_path)[:data]
179
+ .map { |u| u["email"] }
168
180
  ).flatten
169
181
  end
170
182
  end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Samanage
4
+ class Api
5
+ # Default get configuration_item path
6
+ def get_configuration_items(path: PATHS[:configuration_item], options: {})
7
+ path = "configuration_items.json?"
8
+ self.execute(path: path, options: options)
9
+ end
10
+
11
+
12
+ # Returns all configuration_items.
13
+ # Options:
14
+ # - audit_archives: true
15
+ # - layout: 'long'
16
+ def collect_configuration_items(options: {})
17
+ configuration_items = Array.new
18
+ total_pages = self.get_configuration_items(options: options)[:total_pages]
19
+ 1.upto(total_pages) do |page|
20
+ options[:page] = page
21
+
22
+ puts "Collecting Configuration Items page: #{page}/#{total_pages}" if options[:verbose]
23
+ path = "configuration_items.json?"
24
+ request = self.execute(http_method: "get", path: path, options: options)
25
+ request[:data].each do |configuration_item|
26
+ if block_given?
27
+ yield configuration_item
28
+ end
29
+ configuration_items << configuration_item
30
+ end
31
+ end
32
+ configuration_items
33
+ end
34
+
35
+
36
+ # Create an configuration_item given json
37
+ def create_configuration_item(payload: nil, options: {})
38
+ self.execute(path: PATHS[:configuration_item], http_method: "post", payload: payload)
39
+ end
40
+
41
+ # Find configuration_item by ID
42
+ def find_configuration_item(id:, options: {})
43
+ path = "configuration_items/#{id}.json"
44
+ if options[:layout] == "long"
45
+ path += "?layout=long"
46
+ end
47
+ self.execute(path: path)
48
+ end
49
+
50
+ # Update an configuration_item given id and json
51
+ def update_configuration_item(payload:, id:, options: {})
52
+ path = "configuration_items/#{id}.json"
53
+ self.execute(path: path, http_method: "put", payload: payload)
54
+ end
55
+
56
+ def delete_configuration_item(id:)
57
+ self.execute(path: "configuration_items/#{id}.json", http_method: "delete")
58
+ end
59
+
60
+
61
+ alias_method :configuration_items, :collect_configuration_items
62
+ end
63
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Samanage
4
- VERSION = "2.1.20"
4
+ VERSION = "2.1.21"
5
5
  end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "samanage"
4
+ require "faker"
5
+
6
+ describe Samanage::Api do
7
+ context "configuration_items" do
8
+ describe "API Functions" do
9
+ before(:all) do
10
+ TOKEN ||= ENV["SAMANAGE_TEST_API_TOKEN"]
11
+ @samanage = Samanage::Api.new(token: TOKEN)
12
+ @configuration_items = @samanage.configuration_items(options: { verbose: true })
13
+ @users = @samanage.get_users[:data]
14
+ end
15
+ it "get_configuration_items: it returns API call of configuration_items" do
16
+ api_call = @samanage.get_configuration_items
17
+ expect(api_call).to be_a(Hash)
18
+ expect(api_call[:total_count]).to be_an(Integer)
19
+ expect(api_call).to have_key(:response)
20
+ expect(api_call).to have_key(:code)
21
+ end
22
+ it "collect_configuration_items: collects array of configuration_items" do
23
+ configuration_item_count = @samanage.get_configuration_items[:total_count]
24
+ expect(@configuration_items.size).to eq(configuration_item_count)
25
+ expect(@configuration_items).to be_an(Array)
26
+ end
27
+ it "create_configuration_item(payload: json): creates a configuration_item" do
28
+ users_email = @users.sample["email"]
29
+ configuration_item_name = Faker::Device.model_name
30
+ json = {
31
+ configuration_item: {
32
+ name: configuration_item_name,
33
+ priority: "Low",
34
+ description: "Description",
35
+ state: "Operational",
36
+ user: { email: users_email },
37
+ }
38
+ }
39
+ configuration_item_create = @samanage.create_configuration_item(payload: json)
40
+
41
+ expect(configuration_item_create[:data]["id"]).to be_an(Integer)
42
+ expect(configuration_item_create[:data]["name"]).to eq(configuration_item_name)
43
+ expect(configuration_item_create[:code]).to eq(200).or(201)
44
+ end
45
+ it "create_configuration_item: fails if no name/title" do
46
+ json = {
47
+ configuration_item: {
48
+ description: "Description"
49
+ }
50
+ }
51
+ expect { @samanage.create_configuration_item(payload: json) }.to raise_error(Samanage::InvalidRequest)
52
+ end
53
+ it "find_configuration_item: returns a configuration_item card by known id" do
54
+ sample_id = @configuration_items.sample["id"]
55
+ configuration_item = @samanage.find_configuration_item(id: sample_id)
56
+ expect(configuration_item[:data]["id"]).to eq(sample_id) # id should match found configuration_item
57
+ expect(configuration_item[:data]).to have_key("name")
58
+ expect(configuration_item[:data]).to have_key("id")
59
+ end
60
+ it "find_configuration_item: returns more keys with layout=long" do
61
+ sample_id = @configuration_items.sample["id"]
62
+ layout_regular_configuration_item = @samanage.find_configuration_item(id: sample_id)
63
+ layout_long_configuration_item = @samanage.find_configuration_item(id: sample_id, options: { layout: "long" })
64
+
65
+ expect(layout_long_configuration_item[:data]["id"]).to eq(sample_id) # id should match found configuration_item
66
+ expect(layout_long_configuration_item[:data].keys.size).to be > (layout_regular_configuration_item.keys.size)
67
+ expect(layout_long_configuration_item[:data].keys - layout_regular_configuration_item[:data].keys).to_not be([])
68
+ end
69
+ it "update_configuration_item: update_configuration_item by id" do
70
+ sample_configuration_item = @configuration_items
71
+ .reject { |i| ["Closed", "Resolved"].include? i["state"] }
72
+ .sample
73
+ sample_id = sample_configuration_item["id"]
74
+ description = (0...500).map { ("a".."z").to_a[rand(26)] }.join
75
+ configuration_item_json = {
76
+ configuration_item: {
77
+ description: description
78
+ }
79
+ }
80
+ configuration_item_update = @samanage.update_configuration_item(payload: configuration_item_json, id: sample_id)
81
+ # expect(configuration_item_update[:data]['description']).to eq(description) # configuration_item bug #00044569
82
+ expect(configuration_item_update[:code]).to eq(200).or(201)
83
+ end
84
+ it 'finds more data for option[:layout] = "long"' do
85
+ full_layout_configuration_item_keys = @samanage.configuration_items(options: { layout: "long" }).first.keys
86
+ basic_configuration_item_keys = @samanage.configuration_items.sample.keys
87
+ expect(basic_configuration_item_keys.size).to be < full_layout_configuration_item_keys.size
88
+ end
89
+ it "deletes a valid configuration_item" do
90
+ sample_configuration_item_id = @configuration_items.sample["id"]
91
+ configuration_item_delete = @samanage.delete_configuration_item(id: sample_configuration_item_id)
92
+ expect(configuration_item_delete[:code]).to eq(200).or(201)
93
+ end
94
+ end
95
+ end
96
+ end
@@ -25,7 +25,7 @@ describe Samanage::Api do
25
25
  department_name = [
26
26
  Faker::Internet.username,
27
27
  Faker::Internet.domain_word
28
- ].shuffle.join(' ')
28
+ ].shuffle.join(" ")
29
29
  department_description = Faker::Book.author
30
30
  payload = {
31
31
  department: {
@@ -2,6 +2,8 @@
2
2
 
3
3
  require "samanage"
4
4
  require "faker"
5
+ require "dotenv"
6
+ Dotenv.load
5
7
  describe Samanage::Api do
6
8
  context "Task" do
7
9
  before(:all) do
@@ -54,8 +56,6 @@ describe Samanage::Api do
54
56
  end
55
57
  it "deletes a valid task" do
56
58
  sample_task = @tasks.sample
57
- pp sample_task
58
- puts "sample_task: #{sample_task.inspect}"
59
59
  sample_task_id = sample_task["id"]
60
60
  incident_id = sample_task.dig("parent", "id")
61
61
  task_delete = @samanage.delete_task(
@@ -64,5 +64,14 @@ describe Samanage::Api do
64
64
  )
65
65
  expect(task_delete[:code]).to eq(200).or(201)
66
66
  end
67
+ it "collects all tasks from 90 days in demo" do
68
+ @samanage = Samanage::Api.new(token: ENV["DEMOTOKEN"])
69
+ opts = { verbose: true, 'created[]': 90 }
70
+ task_count = @samanage.get_tasks(options: opts)[:total_count]
71
+ @tasks = @samanage.tasks(options: opts)
72
+ expect(task_count).to be_a(Integer)
73
+ expect(task_count).to be > 0
74
+ expect(task_count).to eq(@tasks.count)
75
+ end
67
76
  end
68
77
  end
@@ -10,3 +10,5 @@ $LOAD_PATH.uniq!
10
10
 
11
11
  require "samanage"
12
12
  require "faker"
13
+ require "dotenv"
14
+ Dotenv.load
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: samanage
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.20
4
+ version: 2.1.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Walls
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-14 00:00:00.000000000 Z
11
+ date: 2019-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -58,6 +58,7 @@ files:
58
58
  - lib/samanage/api/category.rb
59
59
  - lib/samanage/api/changes.rb
60
60
  - lib/samanage/api/comments.rb
61
+ - lib/samanage/api/configuration_items.rb
61
62
  - lib/samanage/api/contracts.rb
62
63
  - lib/samanage/api/custom_fields.rb
63
64
  - lib/samanage/api/custom_forms.rb
@@ -89,6 +90,7 @@ files:
89
90
  - spec/api/samanage_category_spec.rb
90
91
  - spec/api/samanage_change_spec.rb
91
92
  - spec/api/samanage_comments_spec.rb
93
+ - spec/api/samanage_configuration_item_spec.rb
92
94
  - spec/api/samanage_contract_spec.rb
93
95
  - spec/api/samanage_custom_field_spec.rb
94
96
  - spec/api/samanage_custom_form_spec.rb