Skip to content

Commit a0ac42e

Browse files
Merge pull request #27 from ganeshrn/resource_model
Add support for new model format Reviewed-by: Bradley A. Thornton https://github.com/cidrblock
2 parents 610f24b + d6e2933 commit a0ac42e

File tree

16 files changed

+315
-252
lines changed

16 files changed

+315
-252
lines changed
Lines changed: 66 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,70 @@
1-
generator_version: 1.0
2-
metadata:
3-
vesion: 1.1
4-
status:
5-
- preview
6-
supported_by:
7-
- '<support_group>'
8-
copyright_str: Copyright 2019 <company_name>
9-
license: gpl-3.0.txt
10-
info:
11-
network_os: myos
12-
resource: interfaces
13-
version_added: 2.8
1+
---
2+
GENERATOR_VERSION: '1.0'
3+
ANSIBLE_METADATA: |
4+
{
5+
'metadata_version': '1.1',
6+
'status': ['preview'],
7+
'supported_by': '<support_group>'
8+
}
9+
10+
NETWORK_OS: myos
11+
RESOURCE: interfaces
12+
COPYRIGHT: Copyright 2019 Red Hat
13+
LICENSE: gpl-3.0.txt
14+
15+
DOCUMENTATION: |
16+
module: myos_interfaces
17+
version_added: 2.9
1418
short_description: 'Manages <xxxx> attributes of <network_os> <resource>.'
15-
description:
16-
- Manages <xxxx> attributes of <network_os> <resource>.
17-
authors:
18-
- Ansible Network Engineer
19-
extends_documentation_fragment: myos
19+
description: 'Manages <xxxx> attributes of <network_os> <resource>'
20+
author: Ansible Network Engineer
2021
notes:
21-
- Tested against <network_os> <version>
22-
schema:
23-
type: object
24-
description: The schema used for the argspec and docstring
25-
properties:
22+
- 'Tested against <network_os> <version>'
23+
options:
2624
config:
27-
type: array
2825
description: The provided configuration
29-
items:
30-
type: object
31-
properties:
32-
some_string:
33-
$ref: '#/definitions/some_string'
34-
some_bool:
35-
$ref: '#/definitions/some_bool'
36-
some_int:
37-
$ref: '#/definitions/some_int'
38-
some_dict:
39-
$ref: '#/definitions/some_dict'
40-
some_list_of_dicts:
41-
type: array
42-
description: An array of dicts
43-
items:
44-
$ref: '#/definitions/some_dict'
45-
required:
46-
- some_string
47-
state:
48-
$ref: '#/definitions/state'
49-
50-
definitions:
51-
some_string:
52-
type: str
53-
description:
54-
- The some_string_01
55-
enum:
56-
- choice_a
57-
- choice_b
58-
- choice_c
59-
default: choice_a
60-
some_bool:
61-
description:
62-
- The some_bool.
63-
type: bool
64-
some_int:
65-
description:
66-
- The some_int.
67-
type: int
68-
version_added: '1.1'
69-
some_dict:
70-
description:
71-
- The some_dict.
72-
type: object
73-
properties:
74-
property_01:
75-
description:
76-
- The property_01
77-
type: str
78-
property_02:
79-
$ref: '#/definitions/some_string'
80-
state:
81-
description:
82-
- The state the configuration should be left in
83-
type: str
84-
enum:
85-
- merged
86-
- replaced
87-
- overridden
88-
- deleted
89-
default: merged
90-
examples:
91-
- merged_example_01.txt
92-
- replaced_example_01.txt
93-
- overridden_example_01.txt
94-
- deleted_example_01.txt
26+
type: list
27+
elements: dict
28+
suboptions:
29+
some_string:
30+
type: str
31+
description:
32+
- The some_string_01
33+
chocies:
34+
- choice_a
35+
- choice_b
36+
- choice_c
37+
default: choice_a
38+
some_bool:
39+
description:
40+
- The some_bool.
41+
type: bool
42+
some_int:
43+
description:
44+
- The some_int.
45+
type: int
46+
version_added: '1.1'
47+
some_dict:
48+
type: dict
49+
description:
50+
- The some_dict.
51+
suboptions:
52+
property_01:
53+
description:
54+
- The property_01
55+
type: str
56+
state:
57+
description:
58+
- The state the configuration should be left in
59+
type: str
60+
choices:
61+
- merged
62+
- replaced
63+
- overridden
64+
- deleted
65+
default: merged
66+
EXAMPLES:
67+
- deleted_example_01.txt
68+
- merged_example_01.txt
69+
- overridden_example_01.txt
70+
- replaced_example_01.txt

roles/init/tasks/main.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
file: "{{ model }}"
1616
name: rm
1717

18+
- name: Set the module documentation variable
19+
set_fact:
20+
rm_docmentation: "{{ rm['DOCUMENTATION']|from_yaml }}"
21+
22+
- name: Set the module ansible_metada variable
23+
set_fact:
24+
rm_ansible_metadata: "{{ rm['ANSIBLE_METADATA']|from_yaml }}"
25+
1826
- name: "Create the {{ structure }} directory structure"
1927
file:
2028
path: "{{ parent }}/{{ item }}"
@@ -23,7 +31,7 @@
2331

2432
- name: Copy the license file to the parent directory
2533
copy:
26-
src: "{{ rm['metadata']['license'] }}"
34+
src: "{{ rm['LICENSE'] | default('gpl-3.0.txt') }}"
2735
dest: "{{ parent }}/LICENSE.txt"
2836

2937
- name: Ensure the 'collection_org' is set when 'structure' is set to collection

roles/resource_module/filter_plugins/to_argspec.py

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,58 @@
1-
# Copyright (c) 2018 Ansible Project
1+
# Copyright (c) 2019 Ansible Project
22
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
33

44
from __future__ import (absolute_import, division, print_function)
5-
import jsonref
6-
import json
5+
__metaclass__ = type
6+
7+
import yaml
78
import pprint
9+
810
from ansible.module_utils.six import iteritems
11+
from ansible.module_utils.six import string_types
12+
from ansible.utils.display import Display
13+
from ansible.errors import AnsibleFilterError
914

10-
__metaclass__ = type
15+
OPTIONS_METADATA = ('type', 'elements', 'default', 'choices', 'required')
16+
SUBOPTIONS_METADATA = ('mutually_exclusive', 'required_together', 'required_one_of', 'supports_check_mode', 'required_if')
1117

12-
from ansible.errors import AnsibleFilterError
18+
display = Display()
19+
20+
21+
def retrieve_metadata(values, out):
22+
for key in OPTIONS_METADATA:
23+
if key in values:
24+
data = values.get(key, None)
25+
if data:
26+
out[key] = data
1327

14-
def dive(obj, required=False):
28+
29+
def dive(obj, result):
30+
for k, v in iteritems(obj):
31+
result[k] = dict()
32+
retrieve_metadata(v, result[k])
33+
suboptions = v.get('suboptions')
34+
if suboptions:
35+
for item in SUBOPTIONS_METADATA:
36+
if item in v:
37+
result[k][item] = v[item]
38+
result[k]['options'] = dict()
39+
dive(suboptions, result[k]['options'])
40+
41+
42+
def to_argspec(spec):
43+
if 'DOCUMENTATION' not in spec:
44+
raise AnsibleFilterError("missing required element 'DOCUMENTATION' in model")
45+
46+
if not isinstance(spec['DOCUMENTATION'], string_types):
47+
raise AnsibleFilterError("value of element 'DOCUMENTATION' should be of type string")
1548
result = {}
16-
if not 'type' in obj:
17-
raise AnsibleFilterError('missing type key')
18-
if obj['type'] == 'object':
19-
result['options'] = {}
20-
if not 'properties' in obj:
21-
raise AnsibleFilterError('missing properties key')
22-
for propkey, propval in iteritems(obj['properties']):
23-
required = bool('required' in obj and propkey in obj['required'])
24-
result['options'][propkey] = dive(propval, required)
25-
elif obj['type'] == 'array':
26-
result['options'] = {}
27-
if obj.get('elements'):
28-
result['elements'] = obj['elements']
29-
if not 'items' in obj:
30-
raise AnsibleFilterError('missing items key in array')
31-
if not 'properties' in obj['items']:
32-
raise AnsibleFilterError('missing properties in items')
33-
for propkey, propval in iteritems(obj['items']['properties']):
34-
required = bool('required' in obj['items'] and propkey in obj['items']['required'])
35-
result['options'][propkey] = dive(propval, required)
36-
result['type'] = 'list'
37-
elif obj['type'] in ['str', 'bool', 'int']:
38-
if 'default' in obj:
39-
result['default'] = obj['default']
40-
if 'enum' in obj:
41-
result['choices'] = obj['enum']
42-
if 'version_added' in obj:
43-
result['version_added'] = obj['version_added']
44-
result['required'] = required
45-
result['type'] = obj['type']
46-
return result
49+
doc = yaml.safe_load(spec['DOCUMENTATION'])
50+
51+
dive(doc['options'], result)
4752

48-
def to_argspec(value):
49-
data = jsonref.loads(json.dumps(value))
50-
result = dive(data['schema'])
51-
return str(result['options'])
53+
result = pprint.pformat(result, indent=1)
54+
display.debug("Arguments: %s" % result)
55+
return result
5256

5357

5458
class FilterModule(object):

0 commit comments

Comments
 (0)