foreman_teamdynamix 0.0.1 → 0.1.0

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
  SHA1:
3
- metadata.gz: 45a95aef7db1039e18557aa32808537189ba6bcc
4
- data.tar.gz: cda36d5138cda78d1863ca58277c91320be283fb
3
+ metadata.gz: 3fca2ef501936519c40478d00bea14fed471e379
4
+ data.tar.gz: 442cd61aa226a3b3ace8b3507963ecf02108b8ea
5
5
  SHA512:
6
- metadata.gz: 9a299726f597546ae462d497ae462bf95c276e964286ccb34aaedc56f0e83af9a574e69258e4e4d7456a00a390d9845361270bb8d3b43ea89082f69c770188c8
7
- data.tar.gz: e0d4dc4ce8f5bd3214e600686a95d6173aa60a1e4460ff10678898c3c556866cfda5252cb23a43039f822957aefa0ad08299e48d9aa25669b180974af7e94e14
6
+ metadata.gz: 3f670d6544df6d4a70c61aed2ff54a498deae3720ad0d9675a507756a2a7c2e5d64af2890d4e9a11710fe10526566e259dc28bee00732e71a3f086a79a6297dd
7
+ data.tar.gz: 1145611226ba984de9499af93b028cd479d3899e596c6e8b0ba54979370037696b0483263791ee1113101d9e9513a5a0853b55b123cd18ec13773bcf6180ef3f
data/README.md CHANGED
@@ -42,11 +42,6 @@ Example Configuration
42
42
  value: "Asset for host running on OS #{host.operatingsystem_id}"
43
43
  :delete:
44
44
  :StatusId: 642
45
- :search:
46
- AppID: 741
47
- StatusName: In Use
48
- RequestingCustomerID: 00000000-0000-0000-0000-000000000000
49
- OwningDepartmentID: 15798
50
45
  :fields:
51
46
  Asset ID: ID
52
47
  Owner: OwningCustomerName
@@ -85,11 +80,11 @@ Add the teamdynamix config to <foreman_repo>/config/settings.yaml
85
80
 
86
81
  ## Rake Task
87
82
  ```
88
- rake hosts:sync_with_teamdynamix
83
+ rake teamydynamix:sync:hosts
89
84
  ```
90
- Gets existing assets in TeamDynamix based on search params [:teamdynamix][:api][:search]. Then scans the hosts and sync them with TeamDynamix.
85
+ Scans the hosts and sync them with TeamDynamix.
91
86
  * If host has teamdynamix_asset_id, update the corresponding TeamDynamix asset.
92
- * If host name matches the asset Name or SerialNumber, update the host and the corresponding TeamDynamix asset.
87
+ * If host name matches the asset SerialNumber, update the host and the corresponding TeamDynamix asset.
93
88
  * If host has no matching asset, create an asset in TeamDynamix with configured fields.
94
89
 
95
90
  ## Test mode
@@ -7,7 +7,7 @@ module ForemanTeamdynamix
7
7
  end
8
8
 
9
9
  included do
10
- after_validation :create_teamdynamix_asset, on: :create
10
+ before_create :create_teamdynamix_asset
11
11
  before_destroy :retire_teamdynamix_asset
12
12
  validates :teamdynamix_asset_id, uniqueness: { :allow_blank => true }
13
13
  end
@@ -15,8 +15,18 @@ module ForemanTeamdynamix
15
15
  private
16
16
 
17
17
  def create_teamdynamix_asset
18
- asset = td_api.create_asset(self)
19
- self.teamdynamix_asset_id = asset['ID']
18
+ # when the asset is already in teamdynamix
19
+ assets = td_api.search_asset(SerialLike: name)
20
+
21
+ if assets.empty?
22
+ asset = td_api.create_asset(self)
23
+ self.teamdynamix_asset_id = asset['ID']
24
+ elsif assets.length > 1
25
+ raise 'Found more than 1 existing asset'
26
+ else
27
+ self.teamdynamix_asset_id = assets.first['ID']
28
+ td_api.update_asset(self)
29
+ end
20
30
  rescue StandardError => e
21
31
  errors.add(:base, _("Could not create the asset for the host in TeamDynamix: #{e.message}"))
22
32
  false
@@ -1,6 +1,6 @@
1
1
  if SETTINGS[:teamdynamix]
2
2
  Deface::Override.new(:virtual_path => 'hosts/show',
3
- :name => 'add_tab_link',
3
+ :name => 'teamdynamix_add_tab',
4
4
  :insert_bottom => 'ul#myTab',
5
5
  :text =>
6
6
  "<li><a href='#teamdynamix' data-toggle='tab'><%= _(teamdynamix_title) %></a></li>")
@@ -1,6 +1,6 @@
1
1
  if SETTINGS[:teamdynamix]
2
2
  Deface::Override.new(:virtual_path => 'hosts/show',
3
- :name => 'create_link',
3
+ :name => 'teamdynamix_add_tab_link',
4
4
  :insert_bottom => 'div#myTabContent',
5
5
  :text =>
6
6
  "\n <div id='teamdynamix' class='tab-pane'
@@ -26,6 +26,8 @@ class TeamdynamixApi
26
26
 
27
27
  def asset_exist?(asset_id)
28
28
  get_asset(asset_id).present?
29
+ rescue RuntimeError
30
+ false
29
31
  end
30
32
 
31
33
  def create_asset(host)
@@ -1,3 +1,3 @@
1
1
  module ForemanTeamdynamix
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.1.0'.freeze
3
3
  end
@@ -0,0 +1,52 @@
1
+ desc <<-DESC.strip_heredoc.squish
2
+ Scans existing hosts and creates or updates the asset in TeamDynamix.
3
+ * If found, update the fields in the TeamDynamix asset.
4
+ * If not found, create a TeamDynamix asset with desired fields.
5
+
6
+ It could be run for all the hosts as:
7
+ * rake teamydynamix:sync:hosts
8
+
9
+ DESC
10
+ namespace :teamdynamix do
11
+ namespace :sync do
12
+ task :hosts => :environment do
13
+ td_api = TeamdynamixApi.instance
14
+ errors = []
15
+ creates = 0
16
+ updates_from_serial_matching = 0
17
+ update_from_asset_id = 0
18
+
19
+ console_user = User.find_by(login: 'foreman_console_admin')
20
+ User.current = console_user
21
+
22
+ Host.all.each do |h|
23
+ # if asset exists, update it
24
+ if td_api.asset_exist?(h.teamdynamix_asset_id)
25
+ td_api.update_asset(h)
26
+ update_from_asset_id += 1
27
+ else
28
+ assets = td_api.search_asset(SerialLike: h.name)
29
+ if assets.empty?
30
+ asset = td_api.create_asset(h)
31
+ h.teamdynamix_asset_id = asset['ID']
32
+ errors.push("Could not save host: #{h.name} (#{h.id})") unless h.save
33
+ creates += 1
34
+ elsif assets.length > 1
35
+ errors.push("Could not sync: Found more than 1 asset for #{h.name} (#{h.id})")
36
+ else
37
+ h.teamdynamix_asset_id = assets.first['ID']
38
+ td_api.update_asset(h)
39
+ errors.push("Could not save host: #{h.name} (#{h.id})") unless h.save
40
+ updates_from_serial_matching += 1
41
+ end
42
+ end
43
+ end
44
+ puts "Assets created: #{creates}" unless creates.eql?(0)
45
+ unless updates_from_serial_matching.eql?(0)
46
+ puts "Assets updated from serial search: #{updates_from_serial_matching}"
47
+ end
48
+ puts "Assets updated from ID: #{update_from_asset_id}" unless update_from_asset_id.eql?(0)
49
+ puts "Errors:\n#{errors.join("\n")}" unless errors.empty?
50
+ end
51
+ end
52
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_teamdynamix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nipendar Tyagi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-14 00:00:00.000000000 Z
11
+ date: 2018-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deface
@@ -66,8 +66,8 @@ files:
66
66
  - app/controllers/concerns/foreman_teamdynamix/hosts_controller_extensions.rb
67
67
  - app/helpers/concerns/foreman_teamdynamix/hosts_helper_extensions.rb
68
68
  - app/models/concerns/foreman_teamdynamix/host_extensions.rb
69
- - app/overrides/add_tab.rb
70
- - app/overrides/add_tab_link.rb
69
+ - app/overrides/teamdynamix_add_tab.rb
70
+ - app/overrides/teamdynamix_add_tab_link.rb
71
71
  - app/services/teamdynamix_api.rb
72
72
  - app/views/foreman_teamdynamix/hosts/_teamdynamix.html.erb
73
73
  - config/routes.rb
@@ -76,7 +76,7 @@ files:
76
76
  - lib/foreman_teamdynamix/engine.rb
77
77
  - lib/foreman_teamdynamix/version.rb
78
78
  - lib/tasks/foreman_teamdynamix_tasks.rake
79
- - lib/tasks/sync_hosts_with_teamdynamix.rake
79
+ - lib/tasks/teamdynamix.rake
80
80
  - locale/Makefile
81
81
  - locale/en/foreman_teamdynamix.po
82
82
  - locale/foreman_teamdynamix.pot
@@ -1,96 +0,0 @@
1
- desc <<-DESC.strip_heredoc.squish
2
- Scans existing hosts and creates or updates the asset in TeamDynamix.
3
- * If found, update the fields in the TeamDynamix asset.
4
- * If not found, create a TeamDynamix asset with desired fields.
5
- * If host does not have a teamdynamix_asset_id or it is deleted via backend, it creates a new asset.
6
-
7
- It could be run for all the hosts as:
8
- * rake hosts:sync_with_teamdynamix
9
-
10
- Or for specific hosts as (No space b/w hostnames):
11
- * rake hosts:sync_with_teamdynamix[hostname1,hostname2,..,hostnameX]
12
- DESC
13
- namespace :hosts do
14
- task :sync_with_teamdynamix => :environment do |_task|
15
- @td_api = TeamdynamixApi.instance
16
- @errors = []
17
- @hosts_synced = []
18
-
19
- set_current_user
20
-
21
- sync_existing_assets_to_hosts
22
-
23
- create_assets_for_unmapped_hosts
24
-
25
- print_summary
26
- end
27
-
28
- def set_current_user
29
- console_user = User.find_by(login: 'foreman_console_admin')
30
- User.current = console_user
31
- end
32
-
33
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
34
- def sync_existing_assets_to_hosts
35
- # sending empty search param to get all the assets in TD at once
36
- # Note: due to a limitation(possibly a bug) in TD search asset api
37
- # each and every search payload returns all the assets.
38
- search_params = SETTINGS[:teamdynamix][:api][:search] || {}
39
- @teamdynamix_assets = @td_api.search_asset(search_params)
40
- @teamdynamix_assets.each do |asset|
41
- asset_id = asset['ID']
42
- # WHEN Asset is already mapped to a host
43
- # THEN update the asset in TD as per current configuration
44
- host = Host.find_by(teamdynamix_asset_id: asset_id)
45
- if host.present?
46
- @hosts_synced << host.id
47
- @td_api.update_asset(host)
48
- next
49
- end
50
- hosts = Host.where(name: [asset['Name'], asset['SerialNumber']])
51
- # WHEN Asset does not have a matching host, THEN Do nothing
52
- next if hosts.blank?
53
- # WHEN Asset has more than one matching host, THEN report error
54
- if hosts.count > 1
55
- @errors << "#{hosts.count} matching hosts found for asset with ID #{asset['ID']}"
56
- next
57
- end
58
- # WHEN Asset has a uniquely matching host which is not yet synced
59
- # THEN update the host with the asset ID
60
- # AND update the asset in TD as per current configuration
61
- host = hosts.first
62
- @hosts_synced << host.id
63
- host.teamdynamix_asset_id = asset_id
64
- @errors << "failed to update host ##{host.id} for asset ID ##{asset_id}" unless host.save
65
- @td_api.update_asset(host)
66
- end
67
- rescue StandardError => e
68
- @errors << "component: syncing_assets_to_hosts, asset_id: #{asset['ID']},
69
- asset_name: #{asset['Name']}, Error: #{e.message}"
70
- end
71
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
72
-
73
- # sync hosts that do not have assets created in Teamdynamix
74
- def create_assets_for_unmapped_hosts
75
- @unmapped_hosts = Host.where.not(id: @hosts_synced)
76
- @unmapped_hosts.each do |host|
77
- asset = @td_api.create_asset(host)
78
- host.teamdynamix_asset_id = asset['ID']
79
- @errors << "failed to update host ##{host.id} for asset ID ##{asset_id}" unless host.save
80
- end
81
- rescue StandardError => e
82
- @errors << "component: creating_new_assets, host: #{host.id}, hostname: #{host.name}, Error: #{e.message}"
83
- end
84
-
85
- def print_summary
86
- puts "\n Summary:"
87
- puts "\t Total Assets in TD: #{@teamdynamix_assets.count}"
88
- puts "\t Hosts with matching asset: #{@hosts_synced.uniq.count}"
89
- puts "\t Hosts with no assets: #{@unmapped_hosts}" if @unmapped_hosts.present?
90
- return if @errors.blank?
91
- puts "\n Errors: #{@errors.count}"
92
- @errors.each do |error|
93
- puts "\t#{error}"
94
- end
95
- end
96
- end