Skip to content

Commit

Permalink
add module to import puppet classes from proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeni committed Dec 8, 2020
1 parent ade931e commit 1669493
Show file tree
Hide file tree
Showing 8 changed files with 561 additions and 2 deletions.
123 changes: 123 additions & 0 deletions plugins/modules/puppetclasses_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2020 Evgeni Golov
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import, division, print_function
__metaclass__ = type


DOCUMENTATION = '''
---
module: puppetclasses_import
version_added: 1.6.0
short_description: Import Puppet Classes from a Proxy
description:
- Import Puppet Classes from a Proxy
author:
- "Evgeni Golov (@evgeni)"
options:
smart_proxy:
description:
- Smart Proxy to import Puppet Classes from
required: True
type: str
environment:
description:
- Puppet Environment to import Puppet Classes from
required: False
type: str
except:
description:
- Which types of Puppet Classes to exclude from the import.
choices:
- new
- updated
- obsolete
required: False
type: list
elements: str
extends_documentation_fragment:
- theforeman.foreman.foreman
'''

EXAMPLES = '''
- name: Import Puppet Classes
theforeman.foreman.puppetclasses_import:
username: "admin"
password: "changeme"
server_url: "https://foreman.example.com"
smart_proxy: "foreman.example.com"
'''

RETURN = '''
result:
description: Details about the Puppet Class import
returned: success
type: dict
contains:
environments_with_new_puppetclasses:
description:
- Number of Puppet Environments with new Puppet Classes
returned: when I(environment) not specificed
environments_updated_puppetclasses:
description:
- Number of Puppet Environments with updated Puppet Classes
returned: when I(environment) not specificed
environments_obsolete:
description:
- Number of Puppet Environments with removed Puppet Classes
returned: when I(environment) not specificed
environments_ignored:
description:
- Number of ignored Puppet Environments
returned: when I(environment) not specificed
results:
description:
- List of Puppet Environments and the changes made to them
type: list
returned: success
'''

from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanAnsibleModule, _flatten_entity


def main():
module = ForemanAnsibleModule(
foreman_spec={
'smart_proxy': dict(type='entity', required=True, flat_name='id'),
'environment': dict(type='entity'),
'except': dict(type='list', elements='str', choices=['new', 'updated', 'obsolete']),
},
supports_check_mode=False,
)

with module.api_connection():
module.auto_lookup_entities()

if 'except' in module.foreman_params:
module.foreman_params['except'] = ','.join(module.foreman_params.get('except'))

result = module.resource_action('smart_proxies', 'import_puppetclasses', record_change=False,
params=_flatten_entity(module.foreman_params, module.foreman_spec))
if (result.get('environments_updated_puppetclasses', 0) + result.get('environments_with_new_puppetclasses', 0)
+ result.get('environments_obsolete', 0) + result.get('environments_ignored', 0)):
module.set_changed()

module.exit_json(result=result)


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions tests/fixtures/apidoc/puppetclasses_import.json
2 changes: 1 addition & 1 deletion tests/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_crud(tmpdir, module, vcrmode):

@pytest.mark.parametrize('module', TEST_PLAYBOOKS)
def test_check_mode(tmpdir, module):
if module in ['subscription_manifest', 'templates_import']:
if module in ['subscription_manifest', 'templates_import', 'puppetclasses_import']:
pytest.skip("This module does not support check_mode.")
run = run_playbook_vcr(tmpdir, module, check_mode=True)
assert run.rc == 0
Expand Down
187 changes: 187 additions & 0 deletions tests/test_playbooks/fixtures/puppetclasses_import-0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
interactions:
- request:
body: null
headers:
Accept:
- application/json;version=2
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- apypie (https://github.com/Apipie/apypie)
method: GET
uri: https://foreman.example.org/api/status
response:
body:
string: '{"result":"ok","status":200,"version":"2.3.0","api_version":2}'
headers:
Cache-Control:
- max-age=0, private, must-revalidate
Connection:
- Keep-Alive
Content-Security-Policy:
- 'default-src ''self''; child-src ''self''; connect-src ''self'' ws: wss:;
img-src ''self'' data:; script-src ''unsafe-eval'' ''unsafe-inline'' ''self'';
style-src ''unsafe-inline'' ''self'''
Content-Type:
- application/json; charset=utf-8
Foreman_api_version:
- '2'
Foreman_current_location:
- ; ANY
Foreman_current_organization:
- ; ANY
Foreman_version:
- 2.3.0
Keep-Alive:
- timeout=15, max=100
Strict-Transport-Security:
- max-age=631139040; includeSubdomains
Vary:
- Accept-Encoding
X-Content-Type-Options:
- nosniff
X-Download-Options:
- noopen
X-Frame-Options:
- sameorigin
X-Permitted-Cross-Domain-Policies:
- none
X-XSS-Protection:
- 1; mode=block
content-length:
- '62'
status:
code: 200
message: OK
- request:
body: null
headers:
Accept:
- application/json;version=2
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
User-Agent:
- apypie (https://github.com/Apipie/apypie)
method: GET
uri: https://foreman.example.org/api/smart_proxies?search=name%3D%22centos7-foreman-2-3.yatsu.example.com%22&per_page=4294967296
response:
body:
string: "{\n \"total\": 1,\n \"subtotal\": 1,\n \"page\": 1,\n \"per_page\"\
: 4294967296,\n \"search\": \"name=\\\"centos7-foreman-2-3.yatsu.example.com\\\
\"\",\n \"sort\": {\n \"by\": null,\n \"order\": null\n },\n \"results\"\
: [{\"created_at\":\"2020-12-03 11:27:11 UTC\",\"updated_at\":\"2020-12-03\
\ 11:27:11 UTC\",\"name\":\"centos7-foreman-2-3.yatsu.example.com\",\"id\"\
:1,\"url\":\"https://centos7-foreman-2-3.yatsu.example.com:8443\",\"features\"\
:[{\"capabilities\":[],\"name\":\"Puppet CA\",\"id\":6},{\"capabilities\"\
:[],\"name\":\"Puppet\",\"id\":5},{\"capabilities\":[],\"name\":\"Logs\",\"\
id\":10},{\"capabilities\":[],\"name\":\"Registration\",\"id\":13}]}]\n}\n"
headers:
Cache-Control:
- max-age=0, private, must-revalidate
Connection:
- Keep-Alive
Content-Security-Policy:
- 'default-src ''self''; child-src ''self''; connect-src ''self'' ws: wss:;
img-src ''self'' data:; script-src ''unsafe-eval'' ''unsafe-inline'' ''self'';
style-src ''unsafe-inline'' ''self'''
Content-Type:
- application/json; charset=utf-8
Foreman_api_version:
- '2'
Foreman_current_location:
- ; ANY
Foreman_current_organization:
- ; ANY
Foreman_version:
- 2.3.0
Keep-Alive:
- timeout=15, max=99
Strict-Transport-Security:
- max-age=631139040; includeSubdomains
Vary:
- Accept-Encoding
X-Content-Type-Options:
- nosniff
X-Download-Options:
- noopen
X-Frame-Options:
- sameorigin
X-Permitted-Cross-Domain-Policies:
- none
X-XSS-Protection:
- 1; mode=block
content-length:
- '589'
status:
code: 200
message: OK
- request:
body: '{}'
headers:
Accept:
- application/json;version=2
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '2'
Content-Type:
- application/json
User-Agent:
- apypie (https://github.com/Apipie/apypie)
method: POST
uri: https://foreman.example.org/api/smart_proxies/1/import_puppetclasses
response:
body:
string: "{\n \"message\": \"Successfully updated environment and puppetclasses\
\ from the on-disk puppet installation\",\n \"environments_with_new_puppetclasses\"\
: 1,\n \"environments_updated_puppetclasses\": 0,\n \"environments_obsolete\"\
: 0,\n \"environments_ignored\": 0,\n \"results\": [{\"name\":\"production\"\
,\"actions\":[\"new\"],\"new_puppetclasses\":[\"apache::confd::no_accf\",\"\
apache::default_confd_files\",\"apache::default_mods\",\"apache\"]}]\n}\n"
headers:
Cache-Control:
- max-age=0, private, must-revalidate
Connection:
- Keep-Alive
Content-Security-Policy:
- 'default-src ''self''; child-src ''self''; connect-src ''self'' ws: wss:;
img-src ''self'' data:; script-src ''unsafe-eval'' ''unsafe-inline'' ''self'';
style-src ''unsafe-inline'' ''self'''
Content-Type:
- application/json; charset=utf-8
Foreman_api_version:
- '2'
Foreman_current_location:
- ; ANY
Foreman_current_organization:
- ; ANY
Foreman_version:
- 2.3.0
Keep-Alive:
- timeout=15, max=98
Strict-Transport-Security:
- max-age=631139040; includeSubdomains
Vary:
- Accept-Encoding
X-Content-Type-Options:
- nosniff
X-Download-Options:
- noopen
X-Frame-Options:
- sameorigin
X-Permitted-Cross-Domain-Policies:
- none
X-XSS-Protection:
- 1; mode=block
content-length:
- '418'
status:
code: 200
message: OK
version: 1
Loading

0 comments on commit 1669493

Please sign in to comment.