Skip to content

Commit fcb6493

Browse files
Sample's for stackdriver's uptime check api. (#1478)
* Sample's for stackdriver's uptime check api. * Add doc tags.
1 parent 4d584ac commit fcb6493

File tree

5 files changed

+391
-0
lines changed

5 files changed

+391
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Stackdriver Uptime Checks API Python Samples
4+
===============================================================================
5+
6+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
7+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=monitoring/api/v3/uptime-check-client/README.rst
8+
9+
10+
This directory contains samples for Google Stackdriver Uptime Checks API. Stackdriver Monitoring collects metrics, events, and metadata from Google Cloud Platform, Amazon Web Services (AWS), hosted uptime probes, application instrumentation, and a variety of common application components including Cassandra, Nginx, Apache Web Server, Elasticsearch and many others. Stackdriver's Uptime Checks API allows you to create, delete, and list your project's Uptime Checks.
11+
12+
13+
14+
15+
.. _Google Stackdriver Uptime Checks API: https://cloud.google.com/monitoring/uptime-checks/management
16+
17+
Setup
18+
-------------------------------------------------------------------------------
19+
20+
21+
Authentication
22+
++++++++++++++
23+
24+
This sample requires you to have authentication setup. Refer to the
25+
`Authentication Getting Started Guide`_ for instructions on setting up
26+
credentials for applications.
27+
28+
.. _Authentication Getting Started Guide:
29+
https://cloud.google.com/docs/authentication/getting-started
30+
31+
Install Dependencies
32+
++++++++++++++++++++
33+
34+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
35+
36+
.. code-block:: bash
37+
38+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
39+
40+
#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions.
41+
42+
.. _Python Development Environment Setup Guide:
43+
https://cloud.google.com/python/setup
44+
45+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
46+
47+
.. code-block:: bash
48+
49+
$ virtualenv env
50+
$ source env/bin/activate
51+
52+
#. Install the dependencies needed to run the samples.
53+
54+
.. code-block:: bash
55+
56+
$ pip install -r requirements.txt
57+
58+
.. _pip: https://pip.pypa.io/
59+
.. _virtualenv: https://virtualenv.pypa.io/
60+
61+
Samples
62+
-------------------------------------------------------------------------------
63+
64+
Snippets
65+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
66+
67+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
68+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=monitoring/api/v3/uptime-check-client/snippets.py,monitoring/api/v3/uptime-check-client/README.rst
69+
70+
71+
72+
73+
To run this sample:
74+
75+
.. code-block:: bash
76+
77+
$ python snippets.py
78+
79+
usage: snippets.py [-h]
80+
{list-uptime-check-configs,list-uptime-check-ips,create-uptime-check,get-uptime-check-config,delete-uptime-check-config}
81+
...
82+
83+
Demonstrates Uptime Check API operations.
84+
85+
positional arguments:
86+
{list-uptime-check-configs,list-uptime-check-ips,create-uptime-check,get-uptime-check-config,delete-uptime-check-config}
87+
list-uptime-check-configs
88+
list-uptime-check-ips
89+
create-uptime-check
90+
get-uptime-check-config
91+
delete-uptime-check-config
92+
93+
optional arguments:
94+
-h, --help show this help message and exit
95+
96+
97+
98+
99+
100+
The client library
101+
-------------------------------------------------------------------------------
102+
103+
This sample uses the `Google Cloud Client Library for Python`_.
104+
You can read the documentation for more details on API usage and use GitHub
105+
to `browse the source`_ and `report issues`_.
106+
107+
.. _Google Cloud Client Library for Python:
108+
https://googlecloudplatform.github.io/google-cloud-python/
109+
.. _browse the source:
110+
https://github.com/GoogleCloudPlatform/google-cloud-python
111+
.. _report issues:
112+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
113+
114+
115+
.. _Google Cloud SDK: https://cloud.google.com/sdk/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Stackdriver Uptime Checks API
5+
short_name: Stackdriver Uptime Checks API
6+
url: https://cloud.google.com/monitoring/uptime-checks/management
7+
description: >
8+
Stackdriver Monitoring collects metrics, events, and metadata from Google
9+
Cloud Platform, Amazon Web Services (AWS), hosted uptime probes,
10+
application instrumentation, and a variety of common application
11+
components including Cassandra, Nginx, Apache Web Server, Elasticsearch
12+
and many others. Stackdriver's Uptime Checks API allows you to create,
13+
delete, and list your project's Uptime Checks.
14+
15+
setup:
16+
- auth
17+
- install_deps
18+
19+
samples:
20+
- name: Snippets
21+
file: snippets.py
22+
show_help: true
23+
24+
cloud_client_library: true
25+
26+
folder: monitoring/api/v3/uptime-check-client
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
google-cloud-monitoring==0.29.0
2+
tabulate==0.8.2
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import print_function
16+
17+
import argparse
18+
import os
19+
import pprint
20+
21+
from google.cloud import monitoring_v3
22+
import tabulate
23+
24+
25+
# [START monitoring_uptime_check_create]
26+
def create_uptime_check_config(project_name, host_name=None,
27+
display_name=None):
28+
config = monitoring_v3.types.uptime_pb2.UptimeCheckConfig()
29+
config.display_name = display_name or 'New uptime check'
30+
config.monitored_resource.type = 'uptime_url'
31+
config.monitored_resource.labels.update(
32+
{'host': host_name or 'example.com'})
33+
config.http_check.path = '/'
34+
config.http_check.port = 80
35+
config.timeout.seconds = 10
36+
config.period.seconds = 300
37+
38+
client = monitoring_v3.UptimeCheckServiceClient()
39+
new_config = client.create_uptime_check_config(project_name, config)
40+
pprint.pprint(new_config)
41+
return new_config
42+
# [END monitoring_uptime_check_create]
43+
44+
45+
# [START monitoring_uptime_check_list_configs]
46+
def list_uptime_check_configs(project_name):
47+
client = monitoring_v3.UptimeCheckServiceClient()
48+
configs = client.list_uptime_check_configs(project_name)
49+
50+
for config in configs:
51+
pprint.pprint(config)
52+
# [END monitoring_uptime_check_list_configs]
53+
54+
55+
# [START monitoring_uptime_check_list_ips]
56+
def list_uptime_check_ips():
57+
client = monitoring_v3.UptimeCheckServiceClient()
58+
ips = client.list_uptime_check_ips()
59+
print(tabulate.tabulate(
60+
[(ip.region, ip.location, ip.ip_address) for ip in ips],
61+
('region', 'location', 'ip_address')
62+
))
63+
# [END monitoring_uptime_check_list_ips]
64+
65+
66+
# [START monitoring_uptime_check_get]
67+
def get_uptime_check_config(config_name):
68+
client = monitoring_v3.UptimeCheckServiceClient()
69+
config = client.get_uptime_check_config(config_name)
70+
pprint.pprint(config)
71+
# [END monitoring_uptime_check_get]
72+
73+
74+
# [START monitoring_uptime_check_delete]
75+
def delete_uptime_check_config(config_name):
76+
client = monitoring_v3.UptimeCheckServiceClient()
77+
client.delete_uptime_check_config(config_name)
78+
print('Deleted ', config_name)
79+
# [END monitoring_uptime_check_delete]
80+
81+
82+
class MissingProjectIdError(Exception):
83+
pass
84+
85+
86+
def project_id():
87+
"""Retreieves the project id from the environment variable.
88+
89+
Raises:
90+
MissingProjectIdError -- When not set.
91+
92+
Returns:
93+
str -- the project name
94+
"""
95+
project_id = os.environ['GCLOUD_PROJECT']
96+
97+
if not project_id:
98+
raise MissingProjectIdError(
99+
'Set the environment variable ' +
100+
'GCLOUD_PROJECT to your Google Cloud Project Id.')
101+
return project_id
102+
103+
104+
def project_name():
105+
return 'projects/' + project_id()
106+
107+
108+
if __name__ == '__main__':
109+
110+
parser = argparse.ArgumentParser(
111+
description='Demonstrates Uptime Check API operations.')
112+
113+
subparsers = parser.add_subparsers(dest='command')
114+
115+
list_uptime_check_configs_parser = subparsers.add_parser(
116+
'list-uptime-check-configs',
117+
help=list_uptime_check_configs.__doc__
118+
)
119+
120+
list_uptime_check_ips_parser = subparsers.add_parser(
121+
'list-uptime-check-ips',
122+
help=list_uptime_check_ips.__doc__
123+
)
124+
125+
create_uptime_check_config_parser = subparsers.add_parser(
126+
'create-uptime-check',
127+
help=create_uptime_check_config.__doc__
128+
)
129+
create_uptime_check_config_parser.add_argument(
130+
'-d', '--display_name',
131+
required=False,
132+
)
133+
create_uptime_check_config_parser.add_argument(
134+
'-o', '--host_name',
135+
required=False,
136+
)
137+
138+
get_uptime_check_config_parser = subparsers.add_parser(
139+
'get-uptime-check-config',
140+
help=get_uptime_check_config.__doc__
141+
)
142+
get_uptime_check_config_parser.add_argument(
143+
'-m', '--name',
144+
required=True,
145+
)
146+
147+
delete_uptime_check_config_parser = subparsers.add_parser(
148+
'delete-uptime-check-config',
149+
help=delete_uptime_check_config.__doc__
150+
)
151+
delete_uptime_check_config_parser.add_argument(
152+
'-m', '--name',
153+
required=True,
154+
)
155+
156+
args = parser.parse_args()
157+
158+
if args.command == 'list-uptime-check-configs':
159+
list_uptime_check_configs(project_name())
160+
161+
elif args.command == 'list-uptime-check-ips':
162+
list_uptime_check_ips()
163+
164+
elif args.command == 'create-uptime-check':
165+
create_uptime_check_config(project_name(), args.host_name,
166+
args.display_name)
167+
168+
elif args.command == 'get-uptime-check-config':
169+
get_uptime_check_config(args.name)
170+
171+
elif args.command == 'delete-uptime-check-config':
172+
delete_uptime_check_config(args.name)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import print_function
16+
17+
import random
18+
import string
19+
20+
import pytest
21+
22+
import snippets
23+
24+
25+
def random_name(length):
26+
return ''.join(
27+
[random.choice(string.ascii_lowercase) for i in range(length)])
28+
29+
30+
class UptimeFixture:
31+
"""A test fixture that creates uptime check config.
32+
"""
33+
34+
def __init__(self):
35+
self.project_id = snippets.project_id()
36+
self.project_name = snippets.project_name()
37+
38+
def __enter__(self):
39+
# Create an uptime check config.
40+
self.config = snippets.create_uptime_check_config(
41+
self.project_name, display_name=random_name(10))
42+
return self
43+
44+
def __exit__(self, type, value, traceback):
45+
# Delete the config.
46+
snippets.delete_uptime_check_config(self.config.name)
47+
48+
49+
@pytest.fixture(scope='session')
50+
def uptime():
51+
with UptimeFixture() as uptime:
52+
yield uptime
53+
54+
55+
def test_create_and_delete(capsys):
56+
# create and delete happen in uptime fixture.
57+
with UptimeFixture():
58+
pass
59+
60+
61+
def test_get_uptime_check_config(capsys, uptime):
62+
snippets.get_uptime_check_config(uptime.config.name)
63+
out, _ = capsys.readouterr()
64+
assert uptime.config.display_name in out
65+
66+
67+
def test_list_uptime_check_configs(capsys, uptime):
68+
snippets.list_uptime_check_configs(uptime.project_name)
69+
out, _ = capsys.readouterr()
70+
assert uptime.config.display_name in out
71+
72+
73+
def test_list_uptime_check_ips(capsys):
74+
snippets.list_uptime_check_ips()
75+
out, _ = capsys.readouterr()
76+
assert 'Singapore' in out

0 commit comments

Comments
 (0)