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

Adding support for GCP Sql Instances #42802

Merged
merged 1 commit into from
Aug 28, 2018
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
10 changes: 7 additions & 3 deletions lib/ansible/module_utils/gcp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ def put(self, url, body=None):
except getattr(requests.exceptions, 'RequestException') as inst:
self.module.fail_json(msg=inst.message)

def patch(self, url, body=None, **kwargs):
kwargs.update({'json': body, 'headers': self._headers()})
try:
return self.session().patch(url, **kwargs)
except getattr(requests.exceptions, 'RequestException') as inst:
self.module.fail_json(msg=inst.message)

def session(self):
return AuthorizedSession(
self._credentials().with_scopes(self.module.params['scopes']))
Expand All @@ -106,9 +113,6 @@ def _validate(self):
if not HAS_GOOGLE_LIBRARIES:
self.module.fail_json(msg="Please install the google-auth library")

if 'auth_kind' not in self.module.params:
self.module.fail_json(msg="Auth kind parameter is missing")

if self.module.params.get('service_account_email') is not None and self.module.params['auth_kind'] != 'machineaccount':
self.module.fail_json(
msg="Service Acccount Email only works with Machine Account-based authentication"
Expand Down
967 changes: 967 additions & 0 deletions lib/ansible/modules/cloud/google/gcp_sql_instance.py

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions lib/ansible/utils/module_docs_fragments/gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@ class ModuleDocFragment(object):
# GCP doc fragment.
DOCUMENTATION = '''
options:
state:
description:
- Whether the given zone should or should not be present.
required: true
choices: ["present", "absent"]
default: "present"
project:
description:
- The Google Cloud Platform project to use.
default: null
auth_kind:
description:
- The type of credential used.
Expand All @@ -30,7 +25,6 @@ class ModuleDocFragment(object):
scopes:
description:
- Array of scopes to be used.
required: true
notes:
- For authentication, you can set service_account_file using the
C(GCP_SERVICE_ACCOUNT_FILE) env variable.
Expand Down
2 changes: 2 additions & 0 deletions test/integration/targets/gcp_sql_instance/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cloud/gcp
unsupported
3 changes: 3 additions & 0 deletions test/integration/targets/gcp_sql_instance/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
# defaults file
resource_name: '{{resource_prefix}}'
Empty file.
131 changes: 131 additions & 0 deletions test/integration/targets/gcp_sql_instance/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
---
# ----------------------------------------------------------------------------
#
# *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
#
# ----------------------------------------------------------------------------
#
# This file is automatically generated by Magic Modules and manual
# changes will be clobbered when the file is regenerated.
#
# Please read more about how to change this file at
# https://www.github.com/GoogleCloudPlatform/magic-modules
#
# ----------------------------------------------------------------------------
# Pre-test setup
- name: delete a instance
gcp_sql_instance:
name: "{{ resource_name }}"
settings:
ip_configuration:
authorized_networks:
- name: google dns server
value: 8.8.8.8/32
tier: db-n1-standard-1
region: us-central1
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
state: absent
#----------------------------------------------------------
- name: create a instance
gcp_sql_instance:
name: "{{ resource_name }}"
settings:
ip_configuration:
authorized_networks:
- name: google dns server
value: 8.8.8.8/32
tier: db-n1-standard-1
region: us-central1
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
state: present
register: result
- name: assert changed is true
assert:
that:
- result.changed == true
- "result.kind == 'sql#instance'"
- name: verify that instance was created
shell: |
gcloud sql instances describe --project="{{ gcp_project}}" "{{ resource_name }}"
register: results
- name: verify that command succeeded
assert:
that:
- results.rc == 0
# ----------------------------------------------------------------------------
- name: create a instance that already exists
gcp_sql_instance:
name: "{{ resource_name }}"
settings:
ip_configuration:
authorized_networks:
- name: google dns server
value: 8.8.8.8/32
tier: db-n1-standard-1
region: us-central1
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
state: present
register: result
- name: assert changed is false
assert:
that:
- result.changed == false
- "result.kind == 'sql#instance'"
#----------------------------------------------------------
- name: delete a instance
gcp_sql_instance:
name: "{{ resource_name }}"
settings:
ip_configuration:
authorized_networks:
- name: google dns server
value: 8.8.8.8/32
tier: db-n1-standard-1
region: us-central1
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
state: absent
register: result
- name: assert changed is true
assert:
that:
- result.changed == true
- result.has_key('kind') == False
- name: verify that instance was deleted
shell: |
gcloud sql instances describe --project="{{ gcp_project}}" "{{ resource_name }}"
register: results
failed_when: results.rc == 0
- name: verify that command succeeded
assert:
that:
- results.rc == 1
- "\"Cloud SQL instance does not exist\" in results.stderr"
# ----------------------------------------------------------------------------
- name: delete a instance that does not exist
gcp_sql_instance:
name: "{{ resource_name }}"
settings:
ip_configuration:
authorized_networks:
- name: google dns server
value: 8.8.8.8/32
tier: db-n1-standard-1
region: us-central1
project: "{{ gcp_project }}"
auth_kind: "{{ gcp_cred_kind }}"
service_account_file: "{{ gcp_cred_file }}"
state: absent
register: result
- name: assert changed is false
assert:
that:
- result.changed == false
- result.has_key('kind') == False