Skip to content

Commit f33abb9

Browse files
authored
Merge pull request #2 from merizrizal/improve-logic-of-create-vm-or-net
Add handling import error; and ansible-test into Makefile
2 parents c1417ff + 7034a92 commit f33abb9

File tree

11 files changed

+110
-10
lines changed

11 files changed

+110
-10
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Run ansible test for lint sanity
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
python-version: ["3.11", "3.12"]
17+
18+
steps:
19+
- uses: actions/checkout@v4
20+
21+
- name: Set up Python ${{ matrix.python-version }}
22+
uses: actions/setup-python@v3
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
26+
- name: Install dependencies
27+
run: |
28+
python -m pip install --upgrade pip
29+
python -m pip install ansible
30+
31+
- name: Run Ansible test
32+
run: |
33+
source envrc.github
34+
make ansible-sanity-test-lint

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ install:
55
@version=`(yq '.version' merizrizal/idcloudhost/galaxy.yml)`; \
66
ansible-galaxy collection install -f merizrizal-idcloudhost-$$version.tar.gz; \
77
rm merizrizal-idcloudhost-$$version.tar.gz
8+
9+
ansible-sanity-test-lint: install
10+
@dir=`(ansible-doc -F merizrizal.idcloudhost | head -n 1 | awk '//{print $$2}' | sed 's/merizrizal/ /g' | awk '//{print $$1}')`; \
11+
cd $$dir/merizrizal/idcloudhost; \
12+
python=`(which python3)`; \
13+
ansible-test sanity --python-interpreter $$python --lint;

envrc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
export ROOT_DIR=`(pwd)`
1+
rootdir=`dirname ${BASH_SOURCE[0]-$0}`
2+
rootdir=`cd $rootdir && pwd`
3+
4+
cp -f ${rootdir}/githooks/* ${rootdir}/.git/hooks/
5+
6+
export ROOT_DIR=${rootdir}

envrc.github

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
rootdir=`dirname ${BASH_SOURCE[0]-$0}`
2+
rootdir=`cd $rootdir && pwd`
3+
4+
export ROOT_DIR=${rootdir}

githooks/pre-commit

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
3+
BASE_DIR=$SCRIPT_DIR/../..
4+
5+
cd ${BASE_DIR}
6+
make ansible-sanity-test-lint
7+
if [[ $? -gt 0 ]]; then
8+
echo "We have sanity test failed. Commit is aborted. Please fix it first"
9+
exit 1
10+
fi

merizrizal/idcloudhost/plugins/module_utils/__init__.py

Whitespace-only changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (c) 2025, Mei Rizal (@merizrizal) <meriz.rizal@gmail.com>
3+
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
4+
from ansible.module_utils.basic import AnsibleModule
5+
6+
7+
def ensure_requests(module: AnsibleModule):
8+
try:
9+
import requests
10+
HAS_REQUESTS = True
11+
except ImportError:
12+
HAS_REQUESTS = False
13+
14+
if HAS_REQUESTS:
15+
return requests
16+
17+
module.fail_json(
18+
msg='Could not import the requests Python module',
19+
results=[]
20+
)

merizrizal/idcloudhost/plugins/modules/create_network.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@
6363
returned: success
6464
'''
6565

66-
import requests
6766
from ansible.module_utils.basic import AnsibleModule
67+
from ansible_collections.merizrizal.idcloudhost.plugins.module_utils.ensure_packages import \
68+
ensure_requests
69+
70+
requests = None
6871

6972

7073
class CreateNetwork():
@@ -77,7 +80,7 @@ def __init__(self):
7780

7881
def main(self):
7982
argument_spec = dict(
80-
api_key=dict(type='str', required=True),
83+
api_key=dict(type='str', required=True, no_log=True),
8184
name=dict(type='str', required=True),
8285
location=dict(type='str', required=True, choices=['jkt01', 'jkt02', 'jkt03', 'sgp01'])
8386
)
@@ -87,6 +90,8 @@ def main(self):
8790
supports_check_mode=True,
8891
)
8992

93+
requests = ensure_requests(module)
94+
9095
self.name = module.params['name']
9196
self.api_key = module.params['api_key']
9297
self.location = module.params['location']

merizrizal/idcloudhost/plugins/modules/create_vm.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
- If windows [ 2019 ]
5959
required: true
6060
type: str
61-
choices: [ ... ]
61+
choices: [ '8.x', '8.4', '9.x', '32', '24.04-lts', 'linux_8.4', 'server_7.9', '11', '2019', '34', '7.9',
62+
'freebsd_12.2', '15.3', 'server_8.4', '12', '21.04', '36', '22.04-lts', '20.04-lts' ]
6263
disks:
6364
description: Size of main storage in GB.
6465
required: true
@@ -123,8 +124,11 @@
123124
returned: success
124125
'''
125126

126-
import requests
127127
from ansible.module_utils.basic import AnsibleModule
128+
from ansible_collections.merizrizal.idcloudhost.plugins.module_utils.ensure_packages import \
129+
ensure_requests
130+
131+
requests = None
128132

129133

130134
class CreateVM():
@@ -159,7 +163,7 @@ def main(self):
159163
all_os_version_choices = list(set(all_os_version_choices))
160164

161165
argument_spec = dict(
162-
api_key=dict(type='str', required=True),
166+
api_key=dict(type='str', required=True, no_log=True),
163167
location=dict(type='str', required=True, choices=['jkt01', 'jkt02', 'jkt03', 'sgp01']),
164168
network_uuid=dict(type='str', required=True),
165169
name=dict(type='str', required=True),
@@ -177,6 +181,8 @@ def main(self):
177181
supports_check_mode=True,
178182
)
179183

184+
requests = ensure_requests(module)
185+
180186
self.api_key = module.params['api_key']
181187
self.location = module.params['location']
182188

merizrizal/idcloudhost/plugins/modules/get_network.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@
5454
returned: success
5555
'''
5656

57-
import requests
5857
from ansible.module_utils.basic import AnsibleModule
58+
from ansible_collections.merizrizal.idcloudhost.plugins.module_utils.ensure_packages import \
59+
ensure_requests
60+
61+
requests = None
5962

6063

6164
class GetNetwork():
@@ -67,7 +70,7 @@ def __init__(self):
6770

6871
def main(self):
6972
argument_spec = dict(
70-
api_key=dict(type='str', required=True),
73+
api_key=dict(type='str', required=True, no_log=True),
7174
location=dict(type='str', required=True, choices=['jkt01', 'jkt02', 'jkt03', 'sgp01'])
7275
)
7376

@@ -76,6 +79,8 @@ def main(self):
7679
supports_check_mode=True,
7780
)
7881

82+
requests = ensure_requests(module)
83+
7984
self.api_key = module.params['api_key']
8085
self.location = module.params['location']
8186

0 commit comments

Comments
 (0)