foreman_discovery 1.4.1 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +5 -13
  2. data/README.md +52 -96
  3. data/app/controllers/api/v2/discovered_hosts_controller.rb +104 -3
  4. data/app/controllers/api/v2/discovery_rules_controller.rb +79 -0
  5. data/app/controllers/concerns/foreman/controller/discovered_extensions.rb +55 -0
  6. data/app/controllers/discovered_hosts_controller.rb +82 -19
  7. data/app/controllers/discovery_rules_controller.rb +73 -0
  8. data/app/helpers/discovered_hosts_helper.rb +18 -6
  9. data/app/helpers/discovery_rules_helper.rb +36 -0
  10. data/app/lib/puppet_fact_parser_extensions.rb +9 -3
  11. data/app/models/concerns/discovery_subnet.rb +7 -0
  12. data/app/models/discovery_attribute_set.rb +8 -0
  13. data/app/models/discovery_rule.rb +24 -0
  14. data/app/models/host/discovered.rb +64 -7
  15. data/app/models/host/managed_extensions.rb +3 -8
  16. data/app/models/hostgroup_extensions.rb +7 -0
  17. data/app/models/setting/discovered.rb +14 -0
  18. data/app/overrides/subnet_form_with_discovery_proxy.rb +9 -0
  19. data/app/views/api/v2/discovered_hosts/auto_provision.json.rabl +3 -0
  20. data/app/views/api/v2/discovered_hosts/auto_provision_all.json.rabl +3 -0
  21. data/app/views/api/v2/discovered_hosts/main.json.rabl +1 -1
  22. data/app/views/api/v2/discovery_rules/base.json.rabl +3 -0
  23. data/app/views/api/v2/discovery_rules/index.json.rabl +3 -0
  24. data/app/views/api/v2/discovery_rules/main.json.rabl +5 -0
  25. data/app/views/api/v2/discovery_rules/show.json.rabl +3 -0
  26. data/app/views/dashboard/_discovery_widget.html.erb +5 -11
  27. data/app/views/dashboard/_discovery_widget_host_list.html.erb +17 -0
  28. data/app/views/discovered_hosts/_discovered_host.html.erb +12 -0
  29. data/app/views/discovered_hosts/_discovered_hosts_list.html.erb +11 -2
  30. data/app/views/discovered_hosts/index.html.erb +1 -0
  31. data/app/views/discovery_rules/_form.html.erb +14 -0
  32. data/app/views/discovery_rules/edit.html.erb +3 -0
  33. data/app/views/discovery_rules/index.html.erb +26 -0
  34. data/app/views/discovery_rules/new.html.erb +3 -0
  35. data/config/routes.rb +19 -0
  36. data/db/migrate/20141107091416_create_discovery_rules.rb +15 -0
  37. data/db/migrate/20141107091417_add_discovery_rule_to_host.rb +9 -0
  38. data/db/migrate/20141126165451_add_discovery_id_to_subnet.rb +9 -0
  39. data/db/migrate/20141223101707_create_discovery_attribute_sets.rb +14 -0
  40. data/db/migrate/20141223142759_fill_discovery_attribute_sets_for_existing_hosts.rb +8 -0
  41. data/db/seeds.d/60_discovery_proxy_feature.rb +2 -0
  42. data/lib/discovery.rake +0 -82
  43. data/lib/foreman_discovery/engine.rb +81 -14
  44. data/lib/foreman_discovery/proxy_operations.rb +18 -0
  45. data/lib/foreman_discovery/version.rb +1 -1
  46. data/locale/de/LC_MESSAGES/foreman_discovery.mo +0 -0
  47. data/locale/de/foreman_discovery.po +1 -1
  48. data/locale/en_GB/LC_MESSAGES/foreman_discovery.mo +0 -0
  49. data/locale/en_GB/foreman_discovery.po +1 -1
  50. data/locale/es/LC_MESSAGES/foreman_discovery.mo +0 -0
  51. data/locale/es/foreman_discovery.po +3 -3
  52. data/locale/fr/LC_MESSAGES/foreman_discovery.mo +0 -0
  53. data/locale/fr/foreman_discovery.po +1 -1
  54. data/locale/gl/LC_MESSAGES/foreman_discovery.mo +0 -0
  55. data/locale/gl/foreman_discovery.po +1 -1
  56. data/locale/it/LC_MESSAGES/foreman_discovery.mo +0 -0
  57. data/locale/it/foreman_discovery.po +1 -1
  58. data/locale/ja/LC_MESSAGES/foreman_discovery.mo +0 -0
  59. data/locale/ja/foreman_discovery.po +1 -1
  60. data/locale/ko/LC_MESSAGES/foreman_discovery.mo +0 -0
  61. data/locale/ko/foreman_discovery.po +1 -1
  62. data/locale/pt_BR/LC_MESSAGES/foreman_discovery.mo +0 -0
  63. data/locale/pt_BR/foreman_discovery.po +1 -1
  64. data/locale/ru/LC_MESSAGES/foreman_discovery.mo +0 -0
  65. data/locale/ru/foreman_discovery.po +1 -1
  66. data/locale/sv_SE/LC_MESSAGES/foreman_discovery.mo +0 -0
  67. data/locale/sv_SE/foreman_discovery.po +1 -1
  68. data/locale/zh_CN/LC_MESSAGES/foreman_discovery.mo +0 -0
  69. data/locale/zh_CN/foreman_discovery.po +1 -1
  70. data/locale/zh_TW/LC_MESSAGES/foreman_discovery.mo +0 -0
  71. data/locale/zh_TW/foreman_discovery.po +1 -1
  72. data/test/factories/discovery_rule_related.rb +20 -0
  73. data/test/functional/api/v2/discovered_hosts_controller_test.rb +81 -0
  74. data/test/functional/api/v2/discovery_rules_controller_test.rb +51 -0
  75. data/test/functional/discovered_hosts_controller_test.rb +96 -3
  76. data/test/functional/discovery_rules_controller_test.rb +62 -0
  77. data/test/test_helper.rb +0 -1
  78. data/test/unit/discovered_extensions_test.rb +106 -0
  79. data/test/unit/discovery_attribute_set_test.rb +43 -0
  80. data/test/unit/facts.json +4 -1
  81. data/test/unit/host_discovered_test.rb +44 -4
  82. metadata +100 -75
  83. data/Rakefile +0 -110
  84. data/lib/foreman_discovery/facts.rb +0 -12
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- NWZjNjE1NzM2ODY0MjNhNzA5NTA3YTUwNDg5ZDIxNDEwNWJhYzVmYQ==
5
- data.tar.gz: !binary |-
6
- YzI1Yjc3N2VkMTBkODgyYWE2Nzg4ZDZkNzgzYTFmYWJkZGViZDlmOA==
2
+ SHA1:
3
+ metadata.gz: 4b30c810a94650d5f72b8592168e2f2afd5d0854
4
+ data.tar.gz: 77645786d40659c4205c4f02ed2eb0a426f3fb62
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- MzkwZTcyZGNlYjZkYmQ0OWM5YTBmNTI0MTAyMzQwNDYwMzJhZDc5NDY3ODc5
10
- OWIxYjA4OWNmYzVjZGQwN2NiOWY4ZDYwOGI2MmJmNGNlOWViNDNlODkxNTA4
11
- ZmJjNzIwMTYxOGFmZmE0YTE4MjRjMmVkNGE4YThjZjQyMmVkNzc=
12
- data.tar.gz: !binary |-
13
- ODcxNDVjZDQxYzNmM2MxZTNkMDhhNDI1ZDY2NTBkNmJlMjE1MDZmNjk2NjFi
14
- YmY1OTk0Yzc2YzA5NmVlMWIxMzE4NzVlODY2MjNmYTdjNTczNGNhZjllNjg5
15
- OTUyODA1MTY0ZDkzOWQ3YzBhY2E4N2Y5YTA4NjE5ZTZiM2UyOTM=
6
+ metadata.gz: fc9544ac861a5a08b494b8a9089fbc4ce25bc2641f79877f85636927cb4d702ffd9b01d6bf266b0574068ae69d66884e7c71223159984e6e61fe8d445d7b4e49
7
+ data.tar.gz: bee1405da6e16b59b45fad2d4eb88da763b3af79e12de6fd1b1e24e95931ee69054d49796d83a58dac87ed1107a0d055b2434ad78853248a4174bc60792b9f94
data/README.md CHANGED
@@ -11,14 +11,9 @@ artifacts (and terms) used in this document:
11
11
 
12
12
  ## Minimal hardware requirements
13
13
 
14
- Since the bootable image runs from memory, keep in mind the following minimum
15
- requirements on the hardware that is being discovered:
16
-
17
- * 700 MB RAM for the CentOS image
18
- * 900 MB RAM for the Fedora image
19
-
20
- When testing those images on virtual machines, make sure they have allocated
21
- enough memory, otherwise kernel panic can be seen during boot sequence.
14
+ Since the bootable image runs from memory, keep in mind the minimum
15
+ requirement of 500 MB RAM otherwise kernel panic can be seen during boot
16
+ sequence.
22
17
 
23
18
  # Installation (plugin)
24
19
 
@@ -33,12 +28,13 @@ RPM users can install the "ruby193-rubygem-foreman_discovery" or
33
28
 
34
29
  ## Compatibility
35
30
 
36
- | Foreman Version | Plugin Version | oVirt Image Version |
31
+ | Foreman Version | Plugin Version | Image Version |
37
32
  | --------------- | --------------:| -------------------:|
38
33
  | <= 1.2 | 1.0.2 | N/A |
39
34
  | = 1.3 | 1.1.0 | 0.1.0 |
40
35
  | = 1.4 | 1.2.0 | 0.3.0-1 |
41
- | >= 1.5 | 1.3.0 | 0.5.0-1 |
36
+ | = 1.5 | 1.3.0 | 0.5.0-1 |
37
+ | >= 1.6 | 1.4.0 | 0.6 or 2.0 |
42
38
 
43
39
  ## Latest code
44
40
 
@@ -49,28 +45,20 @@ branch of the plugin can be obtained by updating the Gemfile in this way:
49
45
 
50
46
  # Installation (image)
51
47
 
52
- The image is based on CentOS 6 & Fedora 18 (there is an image for each) and it
53
- leverages the oVirt Node open-source project. The download site provides stable
54
- releases as well as nightly builds for those who want to live on the edge, or
55
- alternatively a local image can be rebuilt by the user.
56
-
57
- The recommended option is to download a prebuilt image from the Foreman
58
- repositories.
59
-
60
- Nightly builds have the ssh daemon enabled and root password set to "redhat";
61
- logging is also increased. Conversely, releases have the root account locked
62
- and there is no ssh access. Note that the first console (tty1) is reserved for
63
- Discovery output. For a login prompt (nightly images only) use tty2.
48
+ The image is based on CentOS 7 and about 150 MB in size. The recommended
49
+ option is to download a prebuilt image from the Foreman repositories.
64
50
 
65
51
  ## Downloading an image
66
52
 
67
53
  ### Download via installer
68
54
 
69
55
  As of Foreman 1.6, the foreman-installer is able to automatically download
70
- latest stable CentOS6 images. For this, re-run the installer with the
71
- following option:
56
+ latest stable image. For this, re-run the installer with the following
57
+ option:
72
58
 
73
- # foreman-installer --foreman-plugin-discovery-install-images=true
59
+ # foreman-installer \
60
+ --enable-foreman-plugin-discovery \
61
+ --foreman-plugin-discovery-install-images=true
74
62
 
75
63
  ### Manual download
76
64
 
@@ -78,39 +66,23 @@ Images are available from:
78
66
 
79
67
  * http://downloads.theforeman.org/discovery/
80
68
 
81
- A choice of using an ISO or a kernel/initrd pair is available.
82
-
83
- Kernel/initrd (recommended):
69
+ To download the latest release to the expected location, do the following:
84
70
 
85
- * ovirt-node-iso-3.X.0-0.999.201404XXXXXX.el6.iso-img
86
- * ovirt-node-iso-3.X.0-0.999.201404XXXXXX.el6.iso-vmlinuz
71
+ # wget http://downloads.theforeman.org/discovery/releases/latest/fdi-image-latest.tar \
72
+ -O - | tar x --overwrite -C /var/lib/tftpboot/boot
87
73
 
88
- or ISO file:
74
+ After download, verify checksums:
89
75
 
90
- * ovirt-node-iso-3.X.0-0.999.201404XXXXXX.el6.iso
91
-
92
- When downloading the ISO file, it must be extracted before proceeding:
93
-
94
- # yum -y install livecd-tools
95
- # ln -sf ovirt-node-iso-3.X.0-0.999.201404170648.el6.iso foreman.iso
96
- # sudo livecd-iso-to-pxeboot foreman.iso
97
- # find tftpboot/
98
- tftpboot/
99
- tftpboot/vmlinuz0
100
- tftpboot/pxelinux.0
101
- tftpboot/pxelinux.cfg
102
- tftpboot/pxelinux.cfg/default
103
- tftpboot/initrd0.img
104
-
105
- Once the initrd/vmlinuz pair (whether from direct download or from ISO
106
- extraction) is available, copy them to the TFTP BOOT directory and rename them
107
- to *vmlinuz0* and *initrd0.img*.
76
+ # cat /var/lib/tftpboot/boot/fdi-image/SHA256SUM
77
+ beb3cfba7d9fb9d71481c0c8f... initrd0.img
78
+ f03bce150d2473a28697d2988... vmlinuz0
79
+ # sha256sum /var/lib/tftpboot/boot/fdi-image/*
108
80
 
109
81
  ### Building an image
110
82
 
111
83
  To build a discovery image, please visit the
112
- [ovirt-node-plugin-foreman](https://github.com/theforeman/ovirt-node-plugin-foreman)
113
- page for further instructions.
84
+ [foreman-discovery-image](https://github.com/theforeman/foreman-discovery-image)
85
+ git repository and find the README for further instructions.
114
86
 
115
87
  # Configuration (image)
116
88
 
@@ -119,19 +91,26 @@ not registered in Foreman. To achieve this, the PXE default.cfg file needs to
119
91
  be altered to instruct new machines to boot the discovery image.
120
92
 
121
93
  In the Foreman UI, go to Provisioning Templates, edit *PXELinux global default*
122
- template and add:
94
+ template and add the following after the "LABEL local" block of options:
123
95
 
124
96
  LABEL discovery
125
97
  MENU LABEL Foreman Discovery
126
98
  MENU DEFAULT
127
- KERNEL boot/tftpboot/vmlinuz0
128
- APPEND rootflags=loop initrd=boot/tftpboot/initrd0.img root=live:/foreman.iso rootfstype=auto ro rd.live.image rd.live.check rd.lvm=0 rootflags=ro crashkernel=128M elevator=deadline max_loop=256 rd.luks=0 rd.md=0 rd.dm=0 nomodeset selinux=0 stateless foreman.url=https://foreman.example.com
99
+ KERNEL boot/fdi-image/vmlinuz0
100
+ APPEND initrd=boot/fdi-image/initrd0.img rootflags=loop root=live:/fdi.iso rootfstype=auto ro rd.live.image acpi=force rd.luks=0 rd.md=0 rd.dm=0 rd.lvm=0 rd.bootif=0 rd.neednet=0 nomodeset proxy.url=http://xyz proxy.type=proxy
129
101
  IPAPPEND 2
130
102
 
131
- to the end of the file. Note the `foreman.url` option on the APPEND line,
132
- which defines where foreman instance is. Make sure this is set correctly or
133
- discovered hosts will not register to Foreman. Then make this the default by
134
- altering the `ONTIMEOUT` option:
103
+ The `proxy.type` option can be either `proxy` or `foreman`. In the first case
104
+ all communication goes through Smart Proxy, in the latter case the
105
+ communication goes directly to Foreman (legacy mode). This is the default when
106
+ not specified.
107
+
108
+ The `proxy.url` specifies URL of the Smart Proxy or Foreman depending on the
109
+ previous setting. Both http and https schemes are supported. For backward
110
+ compatibility, `foreman.url` is an alias for this setting.
111
+
112
+ Once the APPEND line is modified properly, set the entry to be default via the
113
+ `ONTIMEOUT` option:
135
114
 
136
115
  ONTIMEOUT discovery
137
116
 
@@ -155,21 +134,11 @@ booted from.
155
134
  _Warning_: For now, the selinux=0 option *must* be provided, the image is read
156
135
  only anyway but we plan to enable and test with SELinux too.
157
136
 
158
- An example complete *PXELinux global default* template for Discovery might look
159
- like this:
137
+ ## Updating the on-disk `default` file
160
138
 
161
- DEFAULT menu
162
- PROMPT 0
163
- MENU TITLE PXE Menu
164
- TIMEOUT 200
165
- TOTALTIMEOUT 6000
166
- ONTIMEOUT discovery
167
-
168
- LABEL discovery
169
- MENU LABEL Foreman Discovery
170
- KERNEL boot/tftpboot/vmlinuz0
171
- APPEND rootflags=loop initrd=boot/tftpboot/initrd0.img root=live:/foreman.iso rootfstype=auto ro rd.live.image rd.live.check rd.lvm=0 rootflags=ro crashkernel=128M elevator=deadline max_loop=256 rd.luks=0 rd.md=0 rd.dm=0 nomodeset selinux=0 stateless foreman.url=https://foreman.example.com
172
- IPAPPEND 2
139
+ Once the template has been updated, click the Build PXE Default button at the top
140
+ of the Provisioning Templates page. This will instruct the TFTP proxy to rewrite
141
+ the `pxelinux.cfg/default` file.
173
142
 
174
143
  # Configuration (plugin)
175
144
 
@@ -179,7 +148,7 @@ configuration is available.
179
148
  If Locations and/or Organisations are enabled, Foreman will default to using
180
149
  the first Location and first Organisation for Discovered hosts. If hosts should
181
150
  be placed in some other Location/Organization, alter the default Loc/Org in
182
- `More->Settings->Discovery Settings`
151
+ `More->Settings->Discovered`
183
152
 
184
153
  There is also setting called `discovery_fact` which defaults to
185
154
  `discovery_bootif` which specifies which incoming fact should be used to get
@@ -189,11 +158,11 @@ the `IPAPPEND 2` option is set correctly in the Foreman template.
189
158
 
190
159
  # Usage
191
160
 
192
- Boot a machine using the new PXE config above. It should register with Foreman.
193
- The new Host should show up in `More->Provisioning->Discovered Hosts`. Then
194
- select a Discovered Host and choose Provision. This will redirect to the normal
195
- Edit page for a Host, with the discovered data filled in where possible. Fill
196
- in the details as normal.
161
+ Boot a machine using the new PXE config above. It should register with
162
+ Foreman. The new Host should show up in `Hosts->Discovered Hosts`. Then select
163
+ a Discovered Host and choose Provision. This will redirect to the normal Edit
164
+ page for a Host, with the discovered data filled in where possible. Fill in
165
+ the details as normal.
197
166
 
198
167
  On save, a reboot is sent to the discovered host, after which it should reboot into
199
168
  the installer for the chosen OS, and finally into the installed OS.
@@ -217,15 +186,11 @@ If the booted machine fails to register with Foreman, then there are a number of
217
186
  * Check DNS is working for that image, or use an IP in `foreman.url`
218
187
  * Check DHCP is handing IPs to the booted image correctly
219
188
 
220
- When working with production images (no root password set), it is still
221
- possible to log on as root. Generate some root password:
189
+ To log in the running image you need to set root password which is locked by
190
+ default. In addition, you can enable ssh server as well. To do this, provide
191
+ the following kernel command line options:
222
192
 
223
- $ openssl passwd -salt RH redhat
224
- RHhwCLrQXB8zE
225
-
226
- And then provide it as an kernel command line option:
227
-
228
- ... stateless rootpw=RHhwCLrQXB8zE
193
+ APPEND ... fdi.ssh=1 fdi.rootpw=redhat
229
194
 
230
195
  # API
231
196
 
@@ -242,18 +207,9 @@ If you only wish to test the plugin code itself, you don't need to create the PX
242
207
  image above, or have a TFTP server to run it from. Simply POST a hash of Host Facts to
243
208
  `/api/v2/discovered_hosts/facts` to create a Discovered Host in the UI.
244
209
 
245
- # TODO
246
-
247
- * Support for downloading shell script for oVirt Node image
248
- * Add more Tests
249
- * Add proper Location/Organization handling (via a Wizard maybe?)
250
- * Add per-subnet discovery
251
- * Rake Task for ISO build
252
- * Easy way to add custom facts to build/runtime
253
-
254
210
  # Copyright
255
211
 
256
- Copyright (c) 2012-2013 Greg Sutcliffe
212
+ Copyright (c) 2012-2014 Greg Sutcliffe and The Foreman developers
257
213
 
258
214
  This program is free software: you can redistribute it and/or modify
259
215
  it under the terms of the GNU General Public License as published by
@@ -1,8 +1,9 @@
1
1
  module Api
2
2
  module V2
3
3
  class DiscoveredHostsController < ::Api::V2::BaseController
4
+ include Foreman::Controller::DiscoveredExtensions
4
5
 
5
- before_filter :find_resource, :except => %w{index create facts}
6
+ before_filter :find_resource, :except => %w{index create facts auto_provision_all}
6
7
  skip_before_filter :authorize, :only => :facts
7
8
 
8
9
  resource_description do
@@ -80,7 +81,13 @@ module Api
80
81
  @host.managed = true
81
82
  @host.build = true
82
83
  forward_request_url
83
- process_response @host.update_attributes(params[:discovered_host])
84
+ update_response = @host.update_attributes(params[:discovered_host])
85
+ Host.transaction do
86
+ if update_response
87
+ delete_discovery_attribute_set(@host.id)
88
+ end
89
+ end
90
+ process_response update_response
84
91
  end
85
92
 
86
93
  api :DELETE, "/discovered_hosts/:id/", N_("Delete a discovered host")
@@ -94,12 +101,94 @@ module Api
94
101
  param :facts, Hash, :required => true, :desc => N_("hash containing facts for the host")
95
102
 
96
103
  def facts
97
- @discovered_host, state = Host::Discovered.import_host_and_facts(params[:facts])
104
+ state = true
105
+ Host.transaction do
106
+ @discovered_host, state = Host::Discovered.import_host_and_facts(params[:facts])
107
+ end
108
+ @discovered_host.transaction do
109
+ if state && rule = find_discovery_rule(@discovered_host)
110
+ state = perform_auto_provision(@discovered_host, rule) if Setting['discovery_auto']
111
+ else
112
+ Rails.logger.warn "Discovered facts import unsuccessful, skipping auto provisioning" if Setting['discovery_auto']
113
+ end
114
+ end
98
115
  process_response state
99
116
  rescue ::Foreman::Exception => e
100
117
  render :json => {'message'=>e.to_s}, :status => :unprocessable_entity
101
118
  end
102
119
 
120
+ api :POST, "/discovered_hosts/:id/auto_provision", N_("Execute rules against a discovered host")
121
+ param :id, :identifier, :required => true
122
+
123
+ def auto_provision
124
+ @discovered_host.transaction do
125
+ if rule = find_discovery_rule(@discovered_host)
126
+ msg = _("Host %s was provisioned with rule %s") % [@discovered_host.name, rule.name]
127
+ process_response perform_auto_provision(@discovered_host, rule), msg
128
+ else
129
+ process_success _("No rule found for host %s") % @discovered_host.name
130
+ end
131
+ end
132
+ rescue ::Foreman::Exception => e
133
+ render :json => {'message' => e.to_s}, :status => :unprocessable_entity
134
+ end
135
+
136
+ api :POST, "/discovered_hosts/auto_provision_all", N_("Execute rules against all currently discovered hosts")
137
+
138
+ def auto_provision_all
139
+ result = true
140
+ error_message = _("Errors during auto provisioning: %s")
141
+
142
+ if Host::Discovered.count == 0
143
+ error_message = _("No discovered hosts to provision")
144
+ result = false
145
+ end
146
+
147
+ Host.transaction do
148
+ overall_errors = ""
149
+ Host::Discovered.all.each do |discovered_host|
150
+ if rule = find_discovery_rule(discovered_host)
151
+ result &= perform_auto_provision(discovered_host, rule)
152
+ unless discovered_host.errors.empty?
153
+ errors = discovered_host.errors.full_messages.join(' ')
154
+ logger.warn "Failed to auto provision host %s: %s" % [discovered_host.name, errors]
155
+ overall_errors << "#{discovered_host.name}: #{errors} "
156
+ end
157
+ else
158
+ logger.warn "No rule found for host %s" % discovered_host.name
159
+ end
160
+ end
161
+
162
+ if result
163
+ process_success _("Discovered hosts are provisioning now")
164
+ else
165
+ render_error :custom_error,
166
+ :status => :unprocessable_entity,
167
+ :locals => {
168
+ :message => error_message % overall_errors
169
+ }
170
+ end
171
+ end
172
+ end
173
+
174
+ api :PUT, "/discovered_hosts/:id/refresh_facts", N_("Refreshing the facts of a discovered host")
175
+ param :id, :identifier, :required => true
176
+
177
+ def refresh_facts
178
+ process_response @discovered_host.refresh_facts
179
+ rescue ::Foreman::Exception => e
180
+ render :json => { 'message' => e.to_s }
181
+ end
182
+
183
+ api :PUT, "/discovered_hosts/:id/reboot", N_("Rebooting a discovered host")
184
+ param :id, :identifier, :required => true
185
+
186
+ def reboot
187
+ process_response @discovered_host.reboot
188
+ rescue ::Foreman::Exception => e
189
+ render :json => {'message'=>e.to_s}
190
+ end
191
+
103
192
  private
104
193
 
105
194
  def resource_class
@@ -110,6 +199,18 @@ module Api
110
199
  @discovered_host.request_url = request.host_with_port if @discovered_host.respond_to?(:request_url)
111
200
  end
112
201
 
202
+ def action_permission
203
+ case params[:action]
204
+ when 'auto_provision'
205
+ :auto_provision
206
+ when 'auto_provision_all'
207
+ :auto_provision_all
208
+ when 'refresh_facts', 'reboot'
209
+ :edit
210
+ else
211
+ super
212
+ end
213
+ end
113
214
  end
114
215
  end
115
216
  end
@@ -0,0 +1,79 @@
1
+ module Api
2
+ module V2
3
+ class DiscoveryRulesController < ::Api::V2::BaseController
4
+
5
+ before_filter :find_resource, :except => %w{index create facts}
6
+
7
+ resource_description do
8
+ resource_id 'discovery_rules'
9
+ api_version 'v2'
10
+ api_base_url "/api/v2"
11
+ end
12
+
13
+ api :GET, "/discovery_rules/", N_("List all discovery rules")
14
+ param :search, String, :desc => N_("filter results")
15
+ param :order, String, :desc => N_("sort results")
16
+ param :page, String, :desc => N_("paginate results")
17
+ param :per_page, String, :desc => N_("number of entries per request")
18
+
19
+ def index
20
+ @discovery_rules = resource_scope.search_for(*search_options).paginate(paginate_options)
21
+ end
22
+
23
+ api :GET, "/discovery_rules/:id/", N_("Show a discovery rule")
24
+ param :id, :identifier_dottable, :required => true
25
+
26
+ def show
27
+ end
28
+
29
+ def_param_group :discovery_rule do
30
+ param :discovery_rule, Hash, :action_aware => true do
31
+ param :name, String, :required => true
32
+ param :search, String, :required => true
33
+ param :hostgroup_id, Integer, :required => true
34
+ param :hostname, String, :required => true
35
+ param :max_count, Integer
36
+ param :priority, Integer
37
+ param :enabled, :bool
38
+ end
39
+ end
40
+
41
+ api :POST, "/discovery_rules/", N_("Create a discovery rule")
42
+ param_group :discovery_rule, :as => :create
43
+
44
+ def create
45
+ @discovery_rule = DiscoveryRule.new(params[:discovery_rule])
46
+ process_response @discovery_rule.save
47
+ end
48
+
49
+ api :PUT, "/discovery_rules/:id/", N_("Update a rule")
50
+ param :id, :identifier, :required => true
51
+ param :discovery_rule, Hash, :action_aware => true do
52
+ param :name, String, :required => true
53
+ param :search, String, :required => true
54
+ param :hostgroup_id, Integer, :required => true
55
+ param :hostname, String, :required => true
56
+ param :max_count, Integer
57
+ param :priority, Integer
58
+ param :enabled, :bool
59
+ end
60
+
61
+ def update
62
+ process_response @discovery_rule.update_attributes(params[:discovery_rule])
63
+ end
64
+
65
+ api :DELETE, "/discovery_rules/:id/", N_("Delete a rule")
66
+ param :id, :identifier, :required => true
67
+
68
+ def destroy
69
+ process_response @discovery_rule.destroy
70
+ end
71
+
72
+ private
73
+
74
+ def resource_class
75
+ DiscoveryRule
76
+ end
77
+ end
78
+ end
79
+ end