-
Notifications
You must be signed in to change notification settings - Fork 7
Playbook and Role to move objects from one storage bucket into another #4
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
Open
abikouo
wants to merge
7
commits into
redhat-cop:main
Choose a base branch
from
abikouo:move_objects_v1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9acafd6
A role and playbook to move object from one storage bucket to another
abikouo 9767d8b
add changelog
abikouo 298494e
fix: issue with changelog
abikouo 97d91af
ansible-lint fix: add changelogs/changelog.yaml
abikouo f586e5b
Revert update for tox.ini
abikouo 890a8b0
Remove reference to gcp_setup_credentials
abikouo 9a56cec
remove .yamllint
abikouo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
changelogs/fragments/20230817-move-objects-between-storage-buckets.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
minor_changes: | ||
- add new role and playbook to move objects between GCP Storage buckets (https://github.com/redhat-cop/cloud.gcp_ops/pull/4). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
## cloud.gcp_ops.move_objects_between_storage_buckets | ||
|
||
A playbook to move objects between GCP Storage buckets. | ||
|
||
Variables | ||
-------------- | ||
|
||
* **move_objects_between_storage_buckets_source_bucket**: The name of the GCP storage bucket to retrieve objects from. **Required** | ||
* **move_objects_between_storage_buckets_dest_bucket**: The name of the GCP storage bucket to download objects to. **Required** | ||
* **move_objects_between_storage_buckets_objects**: A list of existing objects from the source bucket. **Required** | ||
|
||
See [cloud.gcp_ops.gcp_setup_credentials](https://github.com/redhat-cop/cloud.gcp_ops/blob/main/roles/gcp_setup_credentials/README.md) for required credentials variables. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
- name: Move objects between Storage buckets | ||
hosts: localhost | ||
gather_facts: false | ||
|
||
roles: | ||
- role: cloud.gcp_ops.move_objects_between_storage_buckets |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
move_objects_between_storage_buckets | ||
================== | ||
|
||
A role to move an object from one GCP storage bucket to another. | ||
|
||
Requirements | ||
------------ | ||
|
||
GCP credentials with permission to upload, download and delete objects from GCP storage bucket. | ||
|
||
|
||
Role Variables | ||
-------------- | ||
|
||
* **move_objects_between_storage_buckets_source_bucket**: The name of the GCP storage bucket to retrieve objects from. **Required** | ||
* **move_objects_between_storage_buckets_dest_bucket**: The name of the GCP storage bucket to download objects to. **Required** | ||
* **move_objects_between_storage_buckets_objects**: A list of existing objects from the source bucket. **Required** | ||
|
||
Dependencies | ||
------------ | ||
|
||
- role: [gcp_setup_credentials](../gcp_setup_credentials/README.md) | ||
|
||
## Example: | ||
``` | ||
--- | ||
- name: Playbook for moving one object from one GCP storage bucket into another. | ||
hosts: localhost | ||
gather_facts: false | ||
|
||
roles: | ||
- role: cloud.gcp_ops.move_objects_between_storage_buckets | ||
move_objects_between_storage_buckets_source_bucket: my-src-storage | ||
move_objects_between_storage_buckets_dest_bucket: my-dest-storage | ||
move_objects_between_storage_buckets_objects: | ||
- object-1 | ||
- object-2 | ||
``` | ||
|
||
License | ||
------- | ||
|
||
GNU General Public License v3.0 or later | ||
|
||
See [LICENCE](https://github.com/redhat-cop/cloud.gcp_ops/blob/main/LICENSE) to see the full text. | ||
|
||
Author Information | ||
------------------ | ||
|
||
- Ansible Cloud Content Team |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
- name: Delete temporary directory | ||
ansible.builtin.file: | ||
state: absent | ||
path: "{{ move_objects_between_storage_buckets__tmpdir.path }}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
dependencies: | ||
- role: cloud.gcp_ops.gcp_setup_credentials |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
- name: Fail when 'move_objects_between_storage_buckets_source_bucket' is undefined | ||
ansible.builtin.fail: | ||
msg: GCP storage bucket source should be defined as move_objects_between_storage_buckets_source_bucket | ||
when: move_objects_between_storage_buckets_source_bucket is undefined | ||
|
||
- name: Fail when 'move_objects_between_storage_buckets_dest_bucket' is undefined | ||
ansible.builtin.fail: | ||
msg: GCP storage bucket destination should be defined as move_objects_between_storage_buckets_dest_bucket | ||
when: move_objects_between_storage_buckets_dest_bucket is undefined | ||
|
||
- name: Fail when 'move_objects_between_storage_buckets_objects' is undefined | ||
ansible.builtin.fail: | ||
msg: Objects to move should be defined as move_objects_between_storage_buckets_objects | ||
when: move_objects_between_storage_buckets_objects is undefined | ||
|
||
- name: Create temporary directory to download objects in | ||
ansible.builtin.tempfile: | ||
state: directory | ||
suffix: .storage | ||
register: move_objects_between_storage_buckets__tmpdir | ||
notify: | ||
- 'Delete temporary directory' | ||
|
||
- name: Include tasks 'move_object.yml' | ||
ansible.builtin.include_tasks: move_object.yml | ||
with_items: "{{ move_objects_between_storage_buckets_objects }}" |
24 changes: 24 additions & 0 deletions
24
roles/move_objects_between_storage_buckets/tasks/move_object.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
- name: 'Move single object from source bucket into destination bucket' | ||
module_defaults: | ||
group/gcp: "{{ gcp_setup_credentials__output }}" | ||
block: | ||
- name: "Download object from bucket source bucket" | ||
google.cloud.gcp_storage_object: | ||
action: download | ||
src: "{{ item }}" | ||
dest: "{{ move_objects_between_storage_buckets__tmpdir.path }}/{{ item }}" | ||
bucket: "{{ move_objects_between_storage_buckets_source_bucket }}" | ||
|
||
- name: "Updload object into destination bucket" | ||
google.cloud.gcp_storage_object: | ||
action: upload | ||
src: "{{ move_objects_between_storage_buckets__tmpdir.path }}/{{ item }}" | ||
dest: "{{ item }}" | ||
bucket: "{{ move_objects_between_storage_buckets_dest_bucket }}" | ||
|
||
- name: "Delete object from source bucket" | ||
google.cloud.gcp_storage_object: | ||
action: delete | ||
src: "{{ item }}" | ||
bucket: "{{ move_objects_between_storage_buckets_source_bucket }}" |
3 changes: 3 additions & 0 deletions
3
tests/integration/targets/test_move_objects_between_storage_buckets/aliases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
cloud/gcp | ||
role/move_objects_between_storage_buckets | ||
time=10s |
8 changes: 8 additions & 0 deletions
8
tests/integration/targets/test_move_objects_between_storage_buckets/defaults/main.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
test_source_storage_bucket: "{{ resource_prefix }}-bucket-src" | ||
test_dest_storage_bucket: "{{ resource_prefix }}-bucket-dest" | ||
test_bucket_objects: | ||
- name: "{{ resource_prefix }}-obj-1" | ||
value: "This has been created using Ansible Seeded content Role" | ||
- name: "{{ resource_prefix }}-obj-2" | ||
value: "Ansible roles for managing GCP resources" |
42 changes: 42 additions & 0 deletions
42
tests/integration/targets/test_move_objects_between_storage_buckets/tasks/create_buckets.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
- name: Create GCP Storage bucket | ||
google.cloud.gcp_storage_bucket: | ||
name: "{{ item }}" | ||
auth_kind: "{{ gcp_auth_kind }}" | ||
service_account_file: "{{ gcp_cred_file }}" | ||
project: "{{ gcp_project }}" | ||
with_items: | ||
- "{{ test_source_storage_bucket }}" | ||
- "{{ test_dest_storage_bucket }}" | ||
|
||
- name: Create temporary directory to store data | ||
ansible.builtin.tempfile: | ||
state: directory | ||
suffix: .upload | ||
register: _tmpdir | ||
|
||
- name: Upload objects into source bucket | ||
block: | ||
- name: Copy content into files | ||
ansible.builtin.copy: | ||
dest: "{{ _tmpdir.path }}/{{ item.name }}.txt" | ||
content: "{{ item.value }}" | ||
mode: '0755' | ||
with_items: "{{ test_bucket_objects }}" | ||
|
||
- name: Upload object into source bucket | ||
google.cloud.gcp_storage_object: | ||
action: upload | ||
bucket: "{{ test_source_storage_bucket }}" | ||
src: "{{ _tmpdir.path }}/{{ item.name }}.txt" | ||
dest: "{{ item.name }}" | ||
auth_kind: "{{ gcp_auth_kind }}" | ||
service_account_file: "{{ gcp_cred_file }}" | ||
project: "{{ gcp_project }}" | ||
with_items: "{{ test_bucket_objects }}" | ||
|
||
always: | ||
- name: Delete temporary directory | ||
ansible.builtin.file: | ||
state: absent | ||
path: "{{ _tmpdir.path }}" |
35 changes: 35 additions & 0 deletions
35
tests/integration/targets/test_move_objects_between_storage_buckets/tasks/delete_buckets.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
- name: Delete objects from source buckets | ||
google.cloud.gcp_storage_object: | ||
action: delete | ||
bucket: "{{ test_source_storage_bucket }}" | ||
src: "{{ item.name }}" | ||
auth_kind: "{{ gcp_auth_kind }}" | ||
service_account_file: "{{ gcp_cred_file }}" | ||
project: "{{ gcp_project }}" | ||
with_items: "{{ test_bucket_objects }}" | ||
register: delete_result | ||
failed_when: (delete_result is failed) and (delete_result.msg != "File does not exist in bucket") | ||
|
||
- name: Delete objects from destination buckets | ||
google.cloud.gcp_storage_object: | ||
action: delete | ||
bucket: "{{ test_dest_storage_bucket }}" | ||
src: "{{ item.name }}" | ||
auth_kind: "{{ gcp_auth_kind }}" | ||
service_account_file: "{{ gcp_cred_file }}" | ||
project: "{{ gcp_project }}" | ||
with_items: "{{ test_bucket_objects }}" | ||
register: delete_result | ||
failed_when: (delete_result is failed) and (delete_result.msg != "File does not exist in bucket") | ||
|
||
- name: Delete GCP Storage bucket | ||
google.cloud.gcp_storage_bucket: | ||
name: "{{ item }}" | ||
auth_kind: "{{ gcp_auth_kind }}" | ||
service_account_file: "{{ gcp_cred_file }}" | ||
project: "{{ gcp_project }}" | ||
state: absent | ||
with_items: | ||
- "{{ test_source_storage_bucket }}" | ||
- "{{ test_dest_storage_bucket }}" |
36 changes: 36 additions & 0 deletions
36
tests/integration/targets/test_move_objects_between_storage_buckets/tasks/main.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
- name: Test Role move_objects_between_storage_buckets | ||
block: | ||
- name: Create buckets | ||
ansible.builtin.include_tasks: create_buckets.yml | ||
|
||
- name: Move objects from source to destination storage bucket | ||
ansible.builtin.include_role: | ||
name: cloud.gcp_ops.move_objects_between_storage_buckets | ||
vars: | ||
move_objects_between_storage_buckets_source_bucket: "{{ test_source_storage_bucket }}" | ||
move_objects_between_storage_buckets_dest_bucket: "{{ test_dest_storage_bucket }}" | ||
move_objects_between_storage_buckets_objects: "{{ test_bucket_objects | map(attribute='name') | list }}" | ||
|
||
# Validate that objects were deleted from Source bucket | ||
- name: Validate that objects have been removed from Source bucket | ||
google.cloud.gcp_storage_object: | ||
action: download | ||
bucket: "{{ test_dest_storage_bucket }}" | ||
src: "{{ item }}" | ||
dest: "{{ item }}.txt" | ||
auth_kind: "{{ gcp_auth_kind }}" | ||
service_account_file: "{{ gcp_cred_file }}" | ||
project: "{{ gcp_project }}" | ||
with_items: "{{ test_bucket_objects }}" | ||
register: _download | ||
failed_when: (_download is not failed) or (_download.msg != "File does not exist in bucket") | ||
|
||
# Validate that objects from destination are stored as expected | ||
- name: Validate objects move | ||
ansible.builtin.include_tasks: validate_objects_from_bucket.yml | ||
with_items: "{{ test_bucket_objects }}" | ||
|
||
always: | ||
- name: Delete buckets | ||
ansible.builtin.include_tasks: delete_buckets.yml |
30 changes: 30 additions & 0 deletions
30
.../targets/test_move_objects_between_storage_buckets/tasks/validate_objects_from_bucket.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
- name: Download content from bucket | ||
block: | ||
- name: Create temporary file | ||
ansible.builtin.tempfile: | ||
suffix: .object | ||
register: _tmpfile | ||
|
||
- name: Download objects from destination bucket | ||
google.cloud.gcp_storage_object: | ||
action: download | ||
bucket: "{{ test_dest_storage_bucket }}" | ||
src: "{{ item.name }}" | ||
dest: "{{ _tmpfile.path }}" | ||
auth_kind: "{{ gcp_auth_kind }}" | ||
service_account_file: "{{ gcp_cred_file }}" | ||
project: "{{ gcp_project }}" | ||
|
||
- name: Assert that object value from Storage bucket is as expected | ||
ansible.builtin.assert: | ||
that: | ||
- item.value == object_data | ||
vars: | ||
object_data: "{{ lookup('file', _tmpfile.path) }}" | ||
|
||
always: | ||
- name: Delete temporary file | ||
ansible.builtin.file: | ||
state: absent | ||
path: "{{ _tmpfile.path }}" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't you use the role for creds setting here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can also do that, I was just using it for collection role, but it should work too