gcp-notifier 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gcp_notifier-0.1.2/LICENSE +21 -0
- gcp_notifier-0.1.2/MANIFEST.in +2 -0
- gcp_notifier-0.1.2/PKG-INFO +140 -0
- gcp_notifier-0.1.2/README.md +103 -0
- gcp_notifier-0.1.2/gcp_notifier/__init__.py +124 -0
- gcp_notifier-0.1.2/gcp_notifier.egg-info/PKG-INFO +140 -0
- gcp_notifier-0.1.2/gcp_notifier.egg-info/SOURCES.txt +10 -0
- gcp_notifier-0.1.2/gcp_notifier.egg-info/dependency_links.txt +1 -0
- gcp_notifier-0.1.2/gcp_notifier.egg-info/requires.txt +3 -0
- gcp_notifier-0.1.2/gcp_notifier.egg-info/top_level.txt +1 -0
- gcp_notifier-0.1.2/pyproject.toml +23 -0
- gcp_notifier-0.1.2/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Marcellus Montilla
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gcp_notifier
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: A simple notification library for Google Cloud projects. Sends alerts via Email and Google Chat.
|
|
5
|
+
Author-email: Marcellus Montilla <marcellusmontilla@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 Marcellus Montilla
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/marcellusmontilla/gcp_notifier
|
|
29
|
+
Project-URL: Repository, https://github.com/marcellusmontilla/gcp_notifier
|
|
30
|
+
Requires-Python: >=3.8
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
License-File: LICENSE
|
|
33
|
+
Requires-Dist: google-cloud-secret-manager
|
|
34
|
+
Requires-Dist: google-auth
|
|
35
|
+
Requires-Dist: requests
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# GCP Notifier
|
|
39
|
+
|
|
40
|
+
A simple notification library for Google Cloud projects. Send alerts via Email and Google Chat with a single function call. Designed to be imported and used as a Python module in your own code.
|
|
41
|
+
|
|
42
|
+
## Features
|
|
43
|
+
|
|
44
|
+
- Send notifications via Email and Google Chat (Webhook)
|
|
45
|
+
- Unified, simple Python API: `notify(subject, body, channels)`
|
|
46
|
+
- Tenacity-compatible error callback: `notify_on_failure`
|
|
47
|
+
- Secrets are securely loaded from Google Secret Manager
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
Install from PyPI (recommended):
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
pip install gcp-notifier
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Or, to test the latest version from TestPyPI:
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
pip install -i https://test.pypi.org/simple/ gcp-notifier
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Or, for local development:
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
pip install .
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Or add to your requirements.txt:
|
|
70
|
+
|
|
71
|
+
```text
|
|
72
|
+
gcp_notifier @ git+https://github.com/marcellusmontilla/gcp_notifier.git
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Quick Start
|
|
76
|
+
|
|
77
|
+
1. Install the package (see Installation above).
|
|
78
|
+
|
|
79
|
+
2. The account (personal or service) running this code must have the 'Secret Manager Secret Accessor' role in your GCP project.
|
|
80
|
+
|
|
81
|
+
3. The required secrets must be in the same GCP project where your Python script or notebook is running.
|
|
82
|
+
|
|
83
|
+
4. Add your required secrets to Google Secret Manager in your GCP project:
|
|
84
|
+
|
|
85
|
+
- `GCHAT_WEBHOOK_URL` (for Google Chat)
|
|
86
|
+
- `EMAIL_SENDER` (sender email address for Email)
|
|
87
|
+
- `EMAIL_PASSWORD` (password or app password for sender)
|
|
88
|
+
- `EMAIL_RECIPIENTS` (comma-separated list of recipient email addresses)
|
|
89
|
+
|
|
90
|
+
5. Import and use `notify` in your code as shown below.
|
|
91
|
+
|
|
92
|
+
## Usage
|
|
93
|
+
|
|
94
|
+
Import and use in your Python code:
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
from gcp_notifier import notify, notify_on_failure
|
|
98
|
+
|
|
99
|
+
# Send a notification (choose channels: "email", "gchat", or both)
|
|
100
|
+
notify(
|
|
101
|
+
subject="Alert Subject",
|
|
102
|
+
body="Alert body text",
|
|
103
|
+
channels=["email", "gchat"] # or ["email"] or ["gchat"]
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
# Use as tenacity retry_error_callback
|
|
107
|
+
from tenacity import retry, stop_after_attempt
|
|
108
|
+
|
|
109
|
+
@retry(stop=stop_after_attempt(3), retry_error_callback=notify_on_failure)
|
|
110
|
+
def always_fails():
|
|
111
|
+
# This function will always fail, triggering notify_on_failure after retries
|
|
112
|
+
raise ValueError("This is a test error for notification.")
|
|
113
|
+
|
|
114
|
+
always_fails()
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Building and Publishing
|
|
118
|
+
|
|
119
|
+
This project uses a modern Python packaging workflow with `pyproject.toml`.
|
|
120
|
+
|
|
121
|
+
To build the package:
|
|
122
|
+
|
|
123
|
+
```sh
|
|
124
|
+
python -m pip install --upgrade build
|
|
125
|
+
python -m build
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
To check and upload to PyPI:
|
|
129
|
+
|
|
130
|
+
```sh
|
|
131
|
+
python -m pip install --upgrade twine
|
|
132
|
+
twine check dist/*
|
|
133
|
+
twine upload dist/*
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
See [Python Packaging User Guide](https://packaging.python.org/en/latest/tutorials/packaging-projects/) for more details.
|
|
137
|
+
|
|
138
|
+
## License
|
|
139
|
+
|
|
140
|
+
MIT
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# GCP Notifier
|
|
2
|
+
|
|
3
|
+
A simple notification library for Google Cloud projects. Send alerts via Email and Google Chat with a single function call. Designed to be imported and used as a Python module in your own code.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Send notifications via Email and Google Chat (Webhook)
|
|
8
|
+
- Unified, simple Python API: `notify(subject, body, channels)`
|
|
9
|
+
- Tenacity-compatible error callback: `notify_on_failure`
|
|
10
|
+
- Secrets are securely loaded from Google Secret Manager
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
Install from PyPI (recommended):
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
pip install gcp-notifier
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Or, to test the latest version from TestPyPI:
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
pip install -i https://test.pypi.org/simple/ gcp-notifier
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Or, for local development:
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
pip install .
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Or add to your requirements.txt:
|
|
33
|
+
|
|
34
|
+
```text
|
|
35
|
+
gcp_notifier @ git+https://github.com/marcellusmontilla/gcp_notifier.git
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Quick Start
|
|
39
|
+
|
|
40
|
+
1. Install the package (see Installation above).
|
|
41
|
+
|
|
42
|
+
2. The account (personal or service) running this code must have the 'Secret Manager Secret Accessor' role in your GCP project.
|
|
43
|
+
|
|
44
|
+
3. The required secrets must be in the same GCP project where your Python script or notebook is running.
|
|
45
|
+
|
|
46
|
+
4. Add your required secrets to Google Secret Manager in your GCP project:
|
|
47
|
+
|
|
48
|
+
- `GCHAT_WEBHOOK_URL` (for Google Chat)
|
|
49
|
+
- `EMAIL_SENDER` (sender email address for Email)
|
|
50
|
+
- `EMAIL_PASSWORD` (password or app password for sender)
|
|
51
|
+
- `EMAIL_RECIPIENTS` (comma-separated list of recipient email addresses)
|
|
52
|
+
|
|
53
|
+
5. Import and use `notify` in your code as shown below.
|
|
54
|
+
|
|
55
|
+
## Usage
|
|
56
|
+
|
|
57
|
+
Import and use in your Python code:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from gcp_notifier import notify, notify_on_failure
|
|
61
|
+
|
|
62
|
+
# Send a notification (choose channels: "email", "gchat", or both)
|
|
63
|
+
notify(
|
|
64
|
+
subject="Alert Subject",
|
|
65
|
+
body="Alert body text",
|
|
66
|
+
channels=["email", "gchat"] # or ["email"] or ["gchat"]
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
# Use as tenacity retry_error_callback
|
|
70
|
+
from tenacity import retry, stop_after_attempt
|
|
71
|
+
|
|
72
|
+
@retry(stop=stop_after_attempt(3), retry_error_callback=notify_on_failure)
|
|
73
|
+
def always_fails():
|
|
74
|
+
# This function will always fail, triggering notify_on_failure after retries
|
|
75
|
+
raise ValueError("This is a test error for notification.")
|
|
76
|
+
|
|
77
|
+
always_fails()
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Building and Publishing
|
|
81
|
+
|
|
82
|
+
This project uses a modern Python packaging workflow with `pyproject.toml`.
|
|
83
|
+
|
|
84
|
+
To build the package:
|
|
85
|
+
|
|
86
|
+
```sh
|
|
87
|
+
python -m pip install --upgrade build
|
|
88
|
+
python -m build
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
To check and upload to PyPI:
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
python -m pip install --upgrade twine
|
|
95
|
+
twine check dist/*
|
|
96
|
+
twine upload dist/*
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
See [Python Packaging User Guide](https://packaging.python.org/en/latest/tutorials/packaging-projects/) for more details.
|
|
100
|
+
|
|
101
|
+
## License
|
|
102
|
+
|
|
103
|
+
MIT
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""
|
|
2
|
+
gcp_notifier: Notification microservice for Email and Google Chat.
|
|
3
|
+
"""
|
|
4
|
+
EMAIL_HOST = "smtp.gmail.com"
|
|
5
|
+
EMAIL_PORT = 587
|
|
6
|
+
__version__ = "0.1.2"
|
|
7
|
+
|
|
8
|
+
def get_secret(project_id: str, secret_id: str, version_id: str = "latest") -> str:
|
|
9
|
+
"""
|
|
10
|
+
Retrieve a secret from Google Secret Manager.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
project_id: GCP project ID
|
|
14
|
+
secret_id: Secret ID in Secret Manager
|
|
15
|
+
version_id: Version of the secret to retrieve
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
The secret value as a string
|
|
19
|
+
"""
|
|
20
|
+
from google.cloud import secretmanager
|
|
21
|
+
client = secretmanager.SecretManagerServiceClient()
|
|
22
|
+
name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"
|
|
23
|
+
response = client.access_secret_version(request={"name": name})
|
|
24
|
+
secret_value = response.payload.data.decode("UTF-8")
|
|
25
|
+
return secret_value
|
|
26
|
+
|
|
27
|
+
# Detect GCP project and fetch secrets
|
|
28
|
+
try:
|
|
29
|
+
import google.auth
|
|
30
|
+
credentials, project_id = google.auth.default()
|
|
31
|
+
|
|
32
|
+
def _fetch_secret_or_none(secret_id: str) -> str:
|
|
33
|
+
if not project_id:
|
|
34
|
+
return None
|
|
35
|
+
try:
|
|
36
|
+
return get_secret(project_id, secret_id)
|
|
37
|
+
except Exception as e:
|
|
38
|
+
print(f"Failed to fetch secret '{secret_id}': {e}")
|
|
39
|
+
return None
|
|
40
|
+
|
|
41
|
+
GCHAT_WEBHOOK_URL = _fetch_secret_or_none("GCHAT_WEBHOOK_URL")
|
|
42
|
+
EMAIL_SENDER = _fetch_secret_or_none("EMAIL_SENDER")
|
|
43
|
+
EMAIL_PASSWORD = _fetch_secret_or_none("EMAIL_PASSWORD")
|
|
44
|
+
EMAIL_RECIPIENTS = _fetch_secret_or_none("EMAIL_RECIPIENTS")
|
|
45
|
+
except Exception:
|
|
46
|
+
credentials, project_id = None, None
|
|
47
|
+
GCHAT_WEBHOOK_URL = EMAIL_SENDER = EMAIL_PASSWORD = EMAIL_RECIPIENTS = None
|
|
48
|
+
|
|
49
|
+
def _send_email(subject: str, message: str) -> None:
|
|
50
|
+
"""Send notification email using configured SMTP credentials."""
|
|
51
|
+
try:
|
|
52
|
+
import smtplib
|
|
53
|
+
from email.message import EmailMessage
|
|
54
|
+
|
|
55
|
+
if not EMAIL_RECIPIENTS:
|
|
56
|
+
print("EMAIL_RECIPIENTS is not set. Cannot send email.")
|
|
57
|
+
return
|
|
58
|
+
recipients = [email.strip() for email in EMAIL_RECIPIENTS.split(",") if email.strip()]
|
|
59
|
+
if not recipients:
|
|
60
|
+
print("EMAIL_RECIPIENTS is empty. Cannot send email.")
|
|
61
|
+
return
|
|
62
|
+
if not EMAIL_SENDER:
|
|
63
|
+
print("EMAIL_SENDER is not set. Cannot send email.")
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
msg = EmailMessage()
|
|
67
|
+
msg['From'] = EMAIL_SENDER
|
|
68
|
+
msg['To'] = ", ".join(recipients)
|
|
69
|
+
msg['Subject'] = subject
|
|
70
|
+
msg.set_content(message)
|
|
71
|
+
|
|
72
|
+
with smtplib.SMTP(EMAIL_HOST, EMAIL_PORT) as server:
|
|
73
|
+
server.ehlo()
|
|
74
|
+
server.starttls()
|
|
75
|
+
server.ehlo()
|
|
76
|
+
server.login(EMAIL_SENDER, EMAIL_PASSWORD)
|
|
77
|
+
server.send_message(msg)
|
|
78
|
+
print("Email sent successfully!")
|
|
79
|
+
except Exception as e:
|
|
80
|
+
print(f"Failed to send email: {e}")
|
|
81
|
+
|
|
82
|
+
def _send_gchat_alert(message: str) -> None:
|
|
83
|
+
"""Send a notification to Google Chat via Webhook."""
|
|
84
|
+
try:
|
|
85
|
+
import requests
|
|
86
|
+
payload = {"text": message}
|
|
87
|
+
headers = {"Content-Type": "application/json"}
|
|
88
|
+
response = requests.post(GCHAT_WEBHOOK_URL, json=payload, headers=headers)
|
|
89
|
+
if response.status_code == 200:
|
|
90
|
+
print("GChat alert sent successfully!")
|
|
91
|
+
else:
|
|
92
|
+
print(f"Failed to send GChat alert: {response.text}")
|
|
93
|
+
except Exception as e:
|
|
94
|
+
print(f"Failed to send GChat alert: {e}")
|
|
95
|
+
|
|
96
|
+
def notify(subject: str, body: str, channels: list[str] = ["email", "gchat"]) -> None:
|
|
97
|
+
"""
|
|
98
|
+
Send a notification via email, Google Chat, or both.
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
subject: The subject for email (ignored for gchat)
|
|
102
|
+
body: The message body
|
|
103
|
+
channels: List of channels to send to ("email", "gchat")
|
|
104
|
+
"""
|
|
105
|
+
if not channels:
|
|
106
|
+
print("No channels specified. Nothing to send.")
|
|
107
|
+
return
|
|
108
|
+
if "email" in channels:
|
|
109
|
+
_send_email(subject, body)
|
|
110
|
+
if "gchat" in channels:
|
|
111
|
+
_send_gchat_alert(body)
|
|
112
|
+
|
|
113
|
+
# Expose functions for import
|
|
114
|
+
def notify_on_failure(retry_state) -> None:
|
|
115
|
+
"""
|
|
116
|
+
Tenacity retry_error_callback to send notification on final failure.
|
|
117
|
+
Usage: @retry(..., retry_error_callback=notify_on_failure)
|
|
118
|
+
"""
|
|
119
|
+
fn_name = getattr(retry_state.fn, "__name__", str(retry_state.fn))
|
|
120
|
+
exception = retry_state.outcome.exception() if retry_state.outcome else None
|
|
121
|
+
msg = f"Function '{fn_name}' failed after retries. Exception: {exception}"
|
|
122
|
+
notify(subject=f"Failure in {fn_name}", body=msg, channels=["email", "gchat"])
|
|
123
|
+
|
|
124
|
+
__all__ = ["notify", "project_id", "notify_on_failure"]
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gcp_notifier
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: A simple notification library for Google Cloud projects. Sends alerts via Email and Google Chat.
|
|
5
|
+
Author-email: Marcellus Montilla <marcellusmontilla@gmail.com>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025 Marcellus Montilla
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Homepage, https://github.com/marcellusmontilla/gcp_notifier
|
|
29
|
+
Project-URL: Repository, https://github.com/marcellusmontilla/gcp_notifier
|
|
30
|
+
Requires-Python: >=3.8
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
License-File: LICENSE
|
|
33
|
+
Requires-Dist: google-cloud-secret-manager
|
|
34
|
+
Requires-Dist: google-auth
|
|
35
|
+
Requires-Dist: requests
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# GCP Notifier
|
|
39
|
+
|
|
40
|
+
A simple notification library for Google Cloud projects. Send alerts via Email and Google Chat with a single function call. Designed to be imported and used as a Python module in your own code.
|
|
41
|
+
|
|
42
|
+
## Features
|
|
43
|
+
|
|
44
|
+
- Send notifications via Email and Google Chat (Webhook)
|
|
45
|
+
- Unified, simple Python API: `notify(subject, body, channels)`
|
|
46
|
+
- Tenacity-compatible error callback: `notify_on_failure`
|
|
47
|
+
- Secrets are securely loaded from Google Secret Manager
|
|
48
|
+
|
|
49
|
+
## Installation
|
|
50
|
+
|
|
51
|
+
Install from PyPI (recommended):
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
pip install gcp-notifier
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Or, to test the latest version from TestPyPI:
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
pip install -i https://test.pypi.org/simple/ gcp-notifier
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Or, for local development:
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
pip install .
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Or add to your requirements.txt:
|
|
70
|
+
|
|
71
|
+
```text
|
|
72
|
+
gcp_notifier @ git+https://github.com/marcellusmontilla/gcp_notifier.git
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Quick Start
|
|
76
|
+
|
|
77
|
+
1. Install the package (see Installation above).
|
|
78
|
+
|
|
79
|
+
2. The account (personal or service) running this code must have the 'Secret Manager Secret Accessor' role in your GCP project.
|
|
80
|
+
|
|
81
|
+
3. The required secrets must be in the same GCP project where your Python script or notebook is running.
|
|
82
|
+
|
|
83
|
+
4. Add your required secrets to Google Secret Manager in your GCP project:
|
|
84
|
+
|
|
85
|
+
- `GCHAT_WEBHOOK_URL` (for Google Chat)
|
|
86
|
+
- `EMAIL_SENDER` (sender email address for Email)
|
|
87
|
+
- `EMAIL_PASSWORD` (password or app password for sender)
|
|
88
|
+
- `EMAIL_RECIPIENTS` (comma-separated list of recipient email addresses)
|
|
89
|
+
|
|
90
|
+
5. Import and use `notify` in your code as shown below.
|
|
91
|
+
|
|
92
|
+
## Usage
|
|
93
|
+
|
|
94
|
+
Import and use in your Python code:
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
from gcp_notifier import notify, notify_on_failure
|
|
98
|
+
|
|
99
|
+
# Send a notification (choose channels: "email", "gchat", or both)
|
|
100
|
+
notify(
|
|
101
|
+
subject="Alert Subject",
|
|
102
|
+
body="Alert body text",
|
|
103
|
+
channels=["email", "gchat"] # or ["email"] or ["gchat"]
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
# Use as tenacity retry_error_callback
|
|
107
|
+
from tenacity import retry, stop_after_attempt
|
|
108
|
+
|
|
109
|
+
@retry(stop=stop_after_attempt(3), retry_error_callback=notify_on_failure)
|
|
110
|
+
def always_fails():
|
|
111
|
+
# This function will always fail, triggering notify_on_failure after retries
|
|
112
|
+
raise ValueError("This is a test error for notification.")
|
|
113
|
+
|
|
114
|
+
always_fails()
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Building and Publishing
|
|
118
|
+
|
|
119
|
+
This project uses a modern Python packaging workflow with `pyproject.toml`.
|
|
120
|
+
|
|
121
|
+
To build the package:
|
|
122
|
+
|
|
123
|
+
```sh
|
|
124
|
+
python -m pip install --upgrade build
|
|
125
|
+
python -m build
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
To check and upload to PyPI:
|
|
129
|
+
|
|
130
|
+
```sh
|
|
131
|
+
python -m pip install --upgrade twine
|
|
132
|
+
twine check dist/*
|
|
133
|
+
twine upload dist/*
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
See [Python Packaging User Guide](https://packaging.python.org/en/latest/tutorials/packaging-projects/) for more details.
|
|
137
|
+
|
|
138
|
+
## License
|
|
139
|
+
|
|
140
|
+
MIT
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pyproject.toml
|
|
5
|
+
gcp_notifier/__init__.py
|
|
6
|
+
gcp_notifier.egg-info/PKG-INFO
|
|
7
|
+
gcp_notifier.egg-info/SOURCES.txt
|
|
8
|
+
gcp_notifier.egg-info/dependency_links.txt
|
|
9
|
+
gcp_notifier.egg-info/requires.txt
|
|
10
|
+
gcp_notifier.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
gcp_notifier
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "gcp_notifier"
|
|
7
|
+
version = "0.1.2"
|
|
8
|
+
description = "A simple notification library for Google Cloud projects. Sends alerts via Email and Google Chat."
|
|
9
|
+
authors = [
|
|
10
|
+
{ name = "Marcellus Montilla", email = "marcellusmontilla@gmail.com" }
|
|
11
|
+
]
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
license = { file = "LICENSE" }
|
|
14
|
+
requires-python = ">=3.8"
|
|
15
|
+
dependencies = [
|
|
16
|
+
"google-cloud-secret-manager",
|
|
17
|
+
"google-auth",
|
|
18
|
+
"requests"
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://github.com/marcellusmontilla/gcp_notifier"
|
|
23
|
+
Repository = "https://github.com/marcellusmontilla/gcp_notifier"
|