Skip to content

Commit fe4efc2

Browse files
authored
betterutime_status_page :: arg for id (#14)
* betteruptime_status_page :: allow passing id * bump versions * small fixes on build * fix * fix tests * restore exit * update doc * use ansible-core
1 parent 8bcba1e commit fe4efc2

File tree

6 files changed

+44
-12
lines changed

6 files changed

+44
-12
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ANSIBLE_COLLECTION_VERSION=`cat galaxy.yml | grep -oP 'version: \K(\d.\d.\d)$$'`
99

1010
BUILD_PATH="build"
1111

12-
DOCKER_BUILD_IMAGE="python:3.10.5-alpine3.16"
12+
DOCKER_BUILD_IMAGE="python:3.11.6-alpine3.18"
1313
DOCKER_CMD=docker run --rm -v `pwd`:/data --workdir /data ${DOCKER_BUILD_IMAGE} sh -c
1414

1515
build: set-env
@@ -19,7 +19,7 @@ clean:
1919
@rm -rf ${BUILD_PATH}
2020

2121
docker-build:
22-
@${DOCKER_CMD} "python -m pip install -r ${DEPLOY_REQ_FILE} && ln -s /usr/local/bin/python /usr/bin/python3 && ansible-galaxy collection build --force --output-path ${BUILD_PATH} && chown -R `id -u`:`id -g` ${BUILD_PATH}"
22+
@${DOCKER_CMD} "python -m pip install -r ${DEPLOY_REQ_FILE} && ansible-galaxy collection build --force --output-path ${BUILD_PATH} && chown -R `id -u`:`id -g` ${BUILD_PATH}"
2323

2424
docker-test:
2525
@${DOCKER_CMD} "python -m pip install -r ${DEPLOY_REQ_FILE} && PYTHONPATH="${PYTHONPATH}:${pwd}" pytest"

ansible-python-requirements.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ansible==2.9.27
2-
requests==2.21.0
3-
pytest==7.1.2
4-
mock==4.0.3
1+
ansible-core==2.15.5
2+
requests==2.31.0
3+
pytest==7.4.3
4+
mock==5.1.0

docs/toucantoco.toucantoco.betteruptime_status_page.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Manage BetterUptime status pages
1010
| state | True | str | present/absent | |
1111
| subdomain | True | str | | |
1212
| scope | True | str | | |
13+
| id | False | str | | |
1314
| sections | False | list | | See below |
1415
| company_name | False | str | | |
1516
| company_url | False | str | | |

galaxy.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ license:
1717
dependencies: {}
1818

1919
repository: https://github.com/ToucanToco/ansible-collection
20+
21+
build_ignore:
22+
- .ansible-venv*
23+
- scripts
24+
- .pytest_cache
25+
- .gitignore
26+
- Jenkinsfile
27+
- build
28+
- tests
29+
- docs

plugins/modules/betteruptime_status_page.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"state": {"required": True, "type": "str", "choices": ["present", "absent"]},
3333
"subdomain": {"required": True, "type": "str"},
3434
"scope": {"required": True, "type": "str"},
35+
"id": {"required": False, "type": "str"},
3536
"sections": {"required": False, "type": "list", "elements": "dict", "options": STATUS_PAGES_SECTION_FIELDS},
3637
"company_name": {"required": False, "type": "str"},
3738
"company_url": {"required": False, "type": "str"},
@@ -169,7 +170,11 @@ def __init__(self, module):
169170
self.scope = self.payload.pop("scope")
170171
self.headers = {"Authorization": f"Bearer {self.payload.pop('api_key')}"}
171172

172-
self.id = None
173+
if "id" in self.payload and self.payload["id"] != "":
174+
self.id = self.payload.pop("id")
175+
else:
176+
self.id = None
177+
173178
self.retrieved_attributes = None
174179

175180
self.sectionList = []
@@ -242,6 +247,15 @@ def manage_resources(self):
242247
resource.delete()
243248
self.changed = True
244249

250+
def get(self):
251+
""" Get a status page from the id """
252+
resp = requests.get(f"{API_STATUS_PAGES_BASE_URL}/{self.id}", headers=self.headers)
253+
if resp.status_code == HTTPStatus.OK:
254+
json_object = resp.json()
255+
self.retrieved_attributes = json_object["data"]["attributes"]
256+
else:
257+
self.module.fail_json(msg=resp.content)
258+
245259
def create(self):
246260
""" Create a new status page """
247261
resp = requests.post(API_STATUS_PAGES_BASE_URL, headers=self.headers, json=self.payload)
@@ -272,7 +286,11 @@ def delete(self):
272286

273287
def manage_status_page(self):
274288
""" Manage state of a status page """
275-
self.retrieve_id(API_STATUS_PAGES_BASE_URL)
289+
290+
if self.id is None:
291+
self.retrieve_id(API_STATUS_PAGES_BASE_URL)
292+
else:
293+
self.get()
276294

277295
if self.state == "present":
278296
if not self.id:
@@ -290,7 +308,7 @@ def manage_status_page(self):
290308
else:
291309
self.delete()
292310

293-
self.module.exit_json(changed=self.changed)
311+
self.module.exit_json(changed=self.changed, id=self.id)
294312

295313

296314
def main():

tests/unit/plugins/modules/test_betteruptime_status_page.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@ def test_manage_sections(mock_module, mock_betteruptime_page_create, mock_better
9999
assert mock_betteruptime_page_create.call_count == expected_create
100100
assert mock_betteruptime_page_delete.call_count == expected_delete
101101

102+
102103
@mock.patch('plugins.modules.betteruptime_status_page.BetterUptimeStatusPage.retrieve_id')
103104
@mock.patch('plugins.modules.betteruptime_status_page.BetterUptimeStatusPage.create')
104105
@mock.patch('plugins.modules.betteruptime_status_page.AnsibleModule')
105-
def test_manage_status_page_create(mock_module, mock_create, mock_retrieve_id):
106+
def test_manage_status_page_create(mock_module, mock_create, _):
106107
status_page_object = betteruptime_status_page.BetterUptimeStatusPage(mock_module)
107108
status_page_object.state = "present"
108109
status_page_object.sections = None
@@ -111,10 +112,11 @@ def test_manage_status_page_create(mock_module, mock_create, mock_retrieve_id):
111112

112113
assert mock_create.call_count == 1
113114

115+
114116
@mock.patch('plugins.modules.betteruptime_status_page.BetterUptimeStatusPage.retrieve_id')
115117
@mock.patch('plugins.modules.betteruptime_status_page.BetterUptimeStatusPage.update')
116118
@mock.patch('plugins.modules.betteruptime_status_page.AnsibleModule')
117-
def test_manage_status_page_update(mock_module, mock_update, mock_retrieve_id):
119+
def test_manage_status_page_update(mock_module, mock_update, _):
118120
status_page_object = betteruptime_status_page.BetterUptimeStatusPage(mock_module)
119121
status_page_object.state = "present"
120122
status_page_object.id = "1234"
@@ -124,10 +126,11 @@ def test_manage_status_page_update(mock_module, mock_update, mock_retrieve_id):
124126

125127
assert mock_update.call_count == 1
126128

129+
127130
@mock.patch('plugins.modules.betteruptime_status_page.BetterUptimeStatusPage.retrieve_id')
128131
@mock.patch('plugins.modules.betteruptime_status_page.BetterUptimeStatusPage.delete')
129132
@mock.patch('plugins.modules.betteruptime_status_page.AnsibleModule')
130-
def test_manage_status_page_delete(mock_module, mock_delete, mock_retrieve_id):
133+
def test_manage_status_page_delete(mock_module, mock_delete, _):
131134
status_page_object = betteruptime_status_page.BetterUptimeStatusPage(mock_module)
132135
status_page_object.state = "absent"
133136
status_page_object.id = "1234"

0 commit comments

Comments
 (0)