Skip to content

Commit 1d6d19f

Browse files
modular-magicianansibot
authored andcommitted
New Module: gcp_tpu_node_facts (ansible#57730)
1 parent a85750d commit 1d6d19f

File tree

2 files changed

+216
-6
lines changed

2 files changed

+216
-6
lines changed

lib/ansible/module_utils/gcp_utils.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def remove_nones_from_dict(obj):
4949
value = obj[key]
5050
if value is not None and value != {} and value != []:
5151
new_obj[key] = value
52+
53+
# Blank dictionaries should return None or GCP API may complain.
54+
if not new_obj:
55+
return None
5256
return new_obj
5357

5458

@@ -76,12 +80,7 @@ def __init__(self, module, product):
7680
def get(self, url, body=None, **kwargs):
7781
kwargs.update({'json': body, 'headers': self._headers()})
7882
try:
79-
# Ignore the google-auth library warning for user credentials. More
80-
# details: https://github.com/googleapis/google-auth-library-python/issues/271
81-
import warnings
82-
with warnings.catch_warnings():
83-
warnings.filterwarnings("ignore", "Your application has authenticated using end user credentials")
84-
return self.session().get(url, **kwargs)
83+
return self.session().get(url, **kwargs)
8584
except getattr(requests.exceptions, 'RequestException') as inst:
8685
self.module.fail_json(msg=inst.message)
8786

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (C) 2017 Google
5+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6+
# ----------------------------------------------------------------------------
7+
#
8+
# *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
9+
#
10+
# ----------------------------------------------------------------------------
11+
#
12+
# This file is automatically generated by Magic Modules and manual
13+
# changes will be clobbered when the file is regenerated.
14+
#
15+
# Please read more about how to change this file at
16+
# https://www.github.com/GoogleCloudPlatform/magic-modules
17+
#
18+
# ----------------------------------------------------------------------------
19+
20+
from __future__ import absolute_import, division, print_function
21+
22+
__metaclass__ = type
23+
24+
################################################################################
25+
# Documentation
26+
################################################################################
27+
28+
ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ["preview"], 'supported_by': 'community'}
29+
30+
DOCUMENTATION = '''
31+
---
32+
module: gcp_tpu_node_facts
33+
description:
34+
- Gather facts for GCP Node
35+
short_description: Gather facts for GCP Node
36+
version_added: 2.9
37+
author: Google Inc. (@googlecloudplatform)
38+
requirements:
39+
- python >= 2.6
40+
- requests >= 2.18.4
41+
- google-auth >= 1.3.0
42+
options:
43+
zone:
44+
description:
45+
- The GCP location for the TPU.
46+
required: true
47+
extends_documentation_fragment: gcp
48+
'''
49+
50+
EXAMPLES = '''
51+
- name: " a node facts"
52+
gcp_tpu_node_facts:
53+
zone: us-central1-b
54+
project: test_project
55+
auth_kind: serviceaccount
56+
service_account_file: "/tmp/auth.pem"
57+
state: facts
58+
'''
59+
60+
RETURN = '''
61+
resources:
62+
description: List of resources
63+
returned: always
64+
type: complex
65+
contains:
66+
name:
67+
description:
68+
- The immutable name of the TPU.
69+
returned: success
70+
type: str
71+
description:
72+
description:
73+
- The user-supplied description of the TPU. Maximum of 512 characters.
74+
returned: success
75+
type: str
76+
acceleratorType:
77+
description:
78+
- The type of hardware accelerators associated with this node.
79+
returned: success
80+
type: str
81+
tensorflowVersion:
82+
description:
83+
- The version of Tensorflow running in the Node.
84+
returned: success
85+
type: str
86+
network:
87+
description:
88+
- The name of a network to peer the TPU node to. It must be a preexisting Compute
89+
Engine network inside of the project on which this API has been activated.
90+
If none is provided, "default" will be used.
91+
returned: success
92+
type: str
93+
cidrBlock:
94+
description:
95+
- The CIDR block that the TPU node will use when selecting an IP address. This
96+
CIDR block must be a /29 block; the Compute Engine networks API forbids a
97+
smaller block, and using a larger block would be wasteful (a node can only
98+
consume one IP address).
99+
- Errors will occur if the CIDR block has already been used for a currently
100+
existing TPU node, the CIDR block conflicts with any subnetworks in the user's
101+
provided network, or the provided network is peered with another network that
102+
is using that CIDR block.
103+
returned: success
104+
type: str
105+
serviceAccount:
106+
description:
107+
- The service account used to run the tensor flow services within the node.
108+
To share resources, including Google Cloud Storage data, with the Tensorflow
109+
job running in the Node, this account must have permissions to that data.
110+
returned: success
111+
type: str
112+
schedulingConfig:
113+
description:
114+
- Sets the scheduling options for this TPU instance.
115+
returned: success
116+
type: complex
117+
contains:
118+
preemptible:
119+
description:
120+
- Defines whether the TPU instance is preemptible.
121+
returned: success
122+
type: bool
123+
networkEndpoints:
124+
description:
125+
- The network endpoints where TPU workers can be accessed and sent work.
126+
- It is recommended that Tensorflow clients of the node first reach out to the
127+
first (index 0) entry.
128+
returned: success
129+
type: complex
130+
contains:
131+
ipAddress:
132+
description:
133+
- The IP address of this network endpoint.
134+
returned: success
135+
type: str
136+
port:
137+
description:
138+
- The port of this network endpoint.
139+
returned: success
140+
type: int
141+
labels:
142+
description:
143+
- Resource labels to represent user provided metadata.
144+
returned: success
145+
type: dict
146+
zone:
147+
description:
148+
- The GCP location for the TPU.
149+
returned: success
150+
type: str
151+
'''
152+
153+
################################################################################
154+
# Imports
155+
################################################################################
156+
from ansible.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule, GcpRequest
157+
import json
158+
159+
################################################################################
160+
# Main
161+
################################################################################
162+
163+
164+
def main():
165+
module = GcpModule(argument_spec=dict(zone=dict(required=True, type='str')))
166+
167+
if not module.params['scopes']:
168+
module.params['scopes'] = ['https://www.googleapis.com/auth/cloud-platform']
169+
170+
items = fetch_list(module, collection(module))
171+
if items.get('nodes'):
172+
items = items.get('nodes')
173+
else:
174+
items = []
175+
return_value = {'resources': items}
176+
module.exit_json(**return_value)
177+
178+
179+
def collection(module):
180+
return "https://tpu.googleapis.com/v1/projects/{project}/locations/{zone}/nodes".format(**module.params)
181+
182+
183+
def fetch_list(module, link):
184+
auth = GcpSession(module, 'tpu')
185+
response = auth.get(link)
186+
return return_if_object(module, response)
187+
188+
189+
def return_if_object(module, response):
190+
# If not found, return nothing.
191+
if response.status_code == 404:
192+
return None
193+
194+
# If no content, return nothing.
195+
if response.status_code == 204:
196+
return None
197+
198+
try:
199+
module.raise_for_status(response)
200+
result = response.json()
201+
except getattr(json.decoder, 'JSONDecodeError', ValueError) as inst:
202+
module.fail_json(msg="Invalid JSON response with error: %s" % inst)
203+
204+
if navigate_hash(result, ['error', 'errors']):
205+
module.fail_json(msg=navigate_hash(result, ['error', 'errors']))
206+
207+
return result
208+
209+
210+
if __name__ == "__main__":
211+
main()

0 commit comments

Comments
 (0)