Skip to content

Commit c8f210d

Browse files
ahmetbdpebot
authored andcommitted
Add container_engine/list_clusters sample (#994)
* Add container_engine/list_clusters sample Signed-off-by: Ahmet Alp Balkan <[email protected]> * * Move samples into our directory structure /container_engine/api-client * Rename to snippets.py * Add license headers. * Add a trivial sanity test. * Use a generated readme.
1 parent d9ad1ce commit c8f210d

File tree

5 files changed

+202
-0
lines changed

5 files changed

+202
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Container Engine Python Samples
4+
===============================================================================
5+
6+
This directory contains samples for Google Container Engine. `Google Container Engine`_ runs Docker containers on Google Cloud Platform, powered by Kubernetes.
7+
8+
9+
10+
11+
.. _Google Container Engine: https://cloud.google.com/container-engine/docs/
12+
13+
Setup
14+
-------------------------------------------------------------------------------
15+
16+
17+
Authentication
18+
++++++++++++++
19+
20+
Authentication is typically done through `Application Default Credentials`_,
21+
which means you do not have to change the code to authenticate as long as
22+
your environment has credentials. You have a few options for setting up
23+
authentication:
24+
25+
#. When running locally, use the `Google Cloud SDK`_
26+
27+
.. code-block:: bash
28+
29+
gcloud auth application-default login
30+
31+
32+
#. When running on App Engine or Compute Engine, credentials are already
33+
set-up. However, you may need to configure your Compute Engine instance
34+
with `additional scopes`_.
35+
36+
#. You can create a `Service Account key file`_. This file can be used to
37+
authenticate to Google Cloud Platform services from any environment. To use
38+
the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to
39+
the path to the key file, for example:
40+
41+
.. code-block:: bash
42+
43+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
44+
45+
.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
46+
.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using
47+
.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount
48+
49+
Install Dependencies
50+
++++++++++++++++++++
51+
52+
#. Install `pip`_ and `virtualenv`_ if you do not already have them.
53+
54+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
55+
56+
.. code-block:: bash
57+
58+
$ virtualenv env
59+
$ source env/bin/activate
60+
61+
#. Install the dependencies needed to run the samples.
62+
63+
.. code-block:: bash
64+
65+
$ pip install -r requirements.txt
66+
67+
.. _pip: https://pip.pypa.io/
68+
.. _virtualenv: https://virtualenv.pypa.io/
69+
70+
Samples
71+
-------------------------------------------------------------------------------
72+
73+
Snippets
74+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
75+
76+
77+
78+
To run this sample:
79+
80+
.. code-block:: bash
81+
82+
$ python snippets.py
83+
84+
usage: snippets.py [-h] {list_clusters_and_nodepools} ...
85+
86+
positional arguments:
87+
{list_clusters_and_nodepools}
88+
list_clusters_and_nodepools
89+
Lists all clusters and associated node pools.
90+
91+
optional arguments:
92+
-h, --help show this help message and exit
93+
94+
95+
96+
97+
.. _Google Cloud SDK: https://cloud.google.com/sdk/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Container Engine
5+
short_name: Container Engine
6+
url: https://cloud.google.com/container-engine/docs/
7+
description: >
8+
`Google Container Engine`_ runs Docker containers on Google Cloud Platform,
9+
powered by Kubernetes.
10+
11+
setup:
12+
- auth
13+
- install_deps
14+
15+
samples:
16+
- name: Snippets
17+
file: snippets.py
18+
show_help: true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
google-api-python-client==1.6.2
2+
google-auth==1.0.1
3+
google-auth-httplib2==0.0.2
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2017 Google Inc. All Rights Reserved.
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+
import argparse
16+
17+
import googleapiclient.discovery
18+
19+
20+
def list_clusters_and_nodepools(project_id, zone):
21+
"""Lists all clusters and associated node pools."""
22+
service = googleapiclient.discovery.build('container', 'v1')
23+
clusters_resource = service.projects().zones().clusters()
24+
25+
clusters_response = clusters_resource.list(
26+
projectId=project_id, zone=zone).execute()
27+
28+
for cluster in clusters_response.get('clusters', []):
29+
print('Cluster: {}, Status: {}, Current Master Version: {}'.format(
30+
cluster['name'], cluster['status'],
31+
cluster['currentMasterVersion']))
32+
33+
nodepools_response = clusters_resource.nodePools().list(
34+
projectId=project_id, zone=zone,
35+
clusterId=cluster['name']).execute()
36+
37+
for nodepool in nodepools_response['nodePools']:
38+
print(
39+
' -> Pool: {}, Status: {}, Machine Type: {}, '
40+
'Autoscaling: {}'.format(
41+
nodepool['name'], nodepool['status'],
42+
nodepool['config']['machineType'],
43+
nodepool.get('autoscaling', {}).get('enabled', False)))
44+
45+
46+
if __name__ == '__main__':
47+
parser = argparse.ArgumentParser(
48+
description=__doc__,
49+
formatter_class=argparse.RawDescriptionHelpFormatter)
50+
subparsers = parser.add_subparsers(dest='command')
51+
list_clusters_and_nodepools_parser = subparsers.add_parser(
52+
'list_clusters_and_nodepools',
53+
help=list_clusters_and_nodepools.__doc__)
54+
list_clusters_and_nodepools_parser.add_argument('project_id')
55+
list_clusters_and_nodepools_parser.add_argument('zone')
56+
57+
args = parser.parse_args()
58+
59+
if args.command == 'list_clusters_and_nodepools':
60+
list_clusters_and_nodepools(args.project_id, args.zone)
61+
else:
62+
parser.print_help()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2017 Google Inc. All Rights Reserved.
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+
import os
16+
17+
import snippets
18+
19+
20+
def test_list_clusters_and_nodepools():
21+
project_id = os.environ['GCLOUD_PROJECT']
22+
snippets.list_clusters_and_nodepools(project_id, 'us-central1-f')

0 commit comments

Comments
 (0)