Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes for GCP (as of 2019-07-09T06:06:53Z) #59132

Merged
merged 2 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
Allowed characters are: lowercase and uppercase letters, numbers, hyphen, single-quote,
double-quote, space, and exclamation point.'
required: false
type: str
labels:
description:
- The labels associated with this Project.
Expand All @@ -65,25 +66,30 @@
- Clients should store labels in a representation such as JSON that does not depend
on specific characters being disallowed .
required: false
type: dict
parent:
description:
- A parent organization.
required: false
type: dict
suboptions:
type:
description:
- Must be organization.
required: false
type: str
id:
description:
- Id of the organization.
required: false
type: str
id:
description:
- The unique, user-assigned ID of the Project. It must be 6 to 30 lowercase letters,
digits, or hyphens. It must start with a letter.
- Trailing hyphens are prohibited.
required: true
type: str
extends_documentation_fragment: gcp
'''

Expand Down Expand Up @@ -329,7 +335,7 @@ def wait_for_operation(module, response):
return {}
status = navigate_hash(op_result, ['done'])
wait_done = wait_for_completion(status, op_result, module)
raise_if_errors(op_result, ['error'], module)
raise_if_errors(wait_done, ['error'], module)
return navigate_hash(wait_done, ['response'])


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
- The repo name may contain slashes. eg, projects/myproject/repos/name/with/slash
.
required: true
type: str
extends_documentation_fragment: gcp
notes:
- 'API Reference: U(https://cloud.google.com/source-repositories/docs/reference/rest/v1/projects.repos)'
Expand Down
48 changes: 42 additions & 6 deletions lib/ansible/modules/cloud/google/gcp_spanner_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@
- A unique identifier for the database, which cannot be changed after the instance
is created. Values are of the form [a-z][-a-z0-9]*[a-z0-9].
required: true
type: str
extra_statements:
description:
- 'An optional list of DDL statements to run inside the newly created database.
Statements can create tables, indexes, etc. These statements execute atomically
with the creation of the database: if there is an error in any statement, the
database is not created.'
required: false
type: list
instance:
description:
- The instance to create the database on.
Expand All @@ -68,6 +70,7 @@
to a gcp_spanner_instance task and then set this instance field to "{{ name-of-resource
}}"'
required: true
type: dict
extends_documentation_fragment: gcp
notes:
- 'API Reference: U(https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances.databases)'
Expand Down Expand Up @@ -127,6 +130,7 @@

from ansible.module_utils.gcp_utils import navigate_hash, GcpSession, GcpModule, GcpRequest, replace_resource_dict
import json
import time

################################################################################
# Main
Expand Down Expand Up @@ -177,7 +181,7 @@ def main():

def create(module, link):
auth = GcpSession(module, 'spanner')
return return_if_object(module, auth.post(link, resource_to_request(module)))
return wait_for_operation(module, auth.post(link, resource_to_request(module)))


def update(module, link):
Expand All @@ -191,11 +195,7 @@ def delete(module, link):


def resource_to_request(module):
request = {
u'instance': replace_resource_dict(module.params.get(u'instance', {}), 'name'),
u'name': module.params.get('name'),
u'extraStatements': module.params.get('extra_statements'),
}
request = {u'name': module.params.get('name'), u'extraStatements': module.params.get('extra_statements')}
request = encode_request(request, module)
return_vals = {}
for k, v in request.items():
Expand Down Expand Up @@ -268,6 +268,42 @@ def response_to_hash(module, response):
return {u'name': module.params.get('name'), u'extraStatements': module.params.get('extra_statements')}


def async_op_url(module, extra_data=None):
if extra_data is None:
extra_data = {}
url = "https://spanner.googleapis.com/v1/{op_id}"
combined = extra_data.copy()
combined.update(module.params)
return url.format(**combined)


def wait_for_operation(module, response):
op_result = return_if_object(module, response)
if op_result is None:
return {}
status = navigate_hash(op_result, ['done'])
wait_done = wait_for_completion(status, op_result, module)
raise_if_errors(wait_done, ['error'], module)
return navigate_hash(wait_done, ['response'])


def wait_for_completion(status, op_result, module):
op_id = navigate_hash(op_result, ['name'])
op_uri = async_op_url(module, {'op_id': op_id})
while not status:
raise_if_errors(op_result, ['error'], module)
time.sleep(1.0)
op_result = fetch_resource(module, op_uri, False)
status = navigate_hash(op_result, ['done'])
return op_result


def raise_if_errors(response, err_path, module):
errors = navigate_hash(response, err_path)
if errors is not None:
module.fail_json(msg=errors)


def decode_response(response, module):
if not response:
return response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
to a gcp_spanner_instance task and then set this instance field to "{{ name-of-resource
}}"'
required: true
type: dict
extends_documentation_fragment: gcp
'''

Expand Down
9 changes: 7 additions & 2 deletions lib/ansible/modules/cloud/google/gcp_spanner_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
- A unique identifier for the instance, which cannot be changed after the instance
is created. The name must be between 6 and 30 characters in length.
required: true
type: str
config:
description:
- The name of the instance's configuration (similar but not quite the same as
Expand All @@ -61,21 +62,25 @@
- In order to obtain a valid list please consult the [Configuration section of
the docs](U(https://cloud.google.com/spanner/docs/instances)).
required: true
type: str
display_name:
description:
- The descriptive name for this instance as it appears in UIs. Must be unique
per project and between 4 and 30 characters in length.
required: true
type: str
node_count:
description:
- The number of nodes allocated to this instance.
required: false
default: '1'
type: int
labels:
description:
- 'An object containing a list of "key": value pairs.'
- 'Example: { "name": "wrench", "mass": "1.3kg", "count": "3" }.'
required: false
type: dict
extends_documentation_fragment: gcp
notes:
- 'API Reference: U(https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances)'
Expand Down Expand Up @@ -202,7 +207,7 @@ def update(module, link):

def delete(module, link):
auth = GcpSession(module, 'spanner')
return wait_for_operation(module, auth.delete(link))
return return_if_object(module, auth.delete(link))


def resource_to_request(module):
Expand Down Expand Up @@ -303,7 +308,7 @@ def wait_for_operation(module, response):
return {}
status = navigate_hash(op_result, ['done'])
wait_done = wait_for_completion(status, op_result, module)
raise_if_errors(op_result, ['error'], module)
raise_if_errors(wait_done, ['error'], module)
return navigate_hash(wait_done, ['response'])


Expand Down
4 changes: 4 additions & 0 deletions lib/ansible/modules/cloud/google/gcp_sql_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,23 @@
description:
- The MySQL charset value.
required: false
type: str
collation:
description:
- The MySQL collation value.
required: false
type: str
name:
description:
- The name of the database in the Cloud SQL instance.
- This does not include the project ID or instance name.
required: true
type: str
instance:
description:
- The name of the Cloud SQL instance. This does not include the project ID.
required: true
type: str
extends_documentation_fragment: gcp
'''

Expand Down
1 change: 1 addition & 0 deletions lib/ansible/modules/cloud/google/gcp_sql_database_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
description:
- The name of the Cloud SQL instance. This does not include the project ID.
required: true
type: str
extends_documentation_fragment: gcp
'''

Expand Down