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

hardware reservation moving between projects #106

Merged
merged 2 commits into from
Oct 9, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/modules/metal_hardware_reservation.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ It doesn't allow to create or update hardware_reservations.

```

```yaml
# Move hardware reservation between projects
- name: fetch hw reservation resource
equinix.cloud.metal_hardware_reservation:
id: "{{ metal_hardware_reservation_id }}"
register: hwres

- name: create new project to move the hw res to
equinix.cloud.metal_project:
name: "destination-project"
register: project

- name: move hw reservation to new project
equinix.cloud.metal_hardware_reservation:
id: "{{ metal_hardware_reservation_id }}"
project_id: "{{ project.id }}"

- name: move hw reservation to original project
equinix.cloud.metal_hardware_reservation:
id: "{{ metal_hardware_reservation_id }}"
project_id: "{{ hwres.project_id }}"

```




Expand All @@ -36,6 +60,7 @@ It doesn't allow to create or update hardware_reservations.
| Field | Type | Required | Description |
|-----------|------|----------|------------------------------------------------------------------------------|
| `id` | <center>`str`</center> | <center>**Required**</center> | UUID of the hardware_reservation. |
| `project_id` | <center>`str`</center> | <center>Optional</center> | UUID of parent project containing the hardware_reservation. It can be changed. **(Updatable)** |



Expand Down
5 changes: 5 additions & 0 deletions plugins/module_utils/metal/api_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,9 @@ def get_routes(mpc):
{},
equinix_metal.InterconnectionUpdateInput,
),
('metal_hardware_reservation', action.UPDATE): spec_types.Specs(
equinix_metal.HardwareReservationsApi(mpc).move_hardware_reservation,
{},
equinix_metal.MoveHardwareReservationRequest,
),
}
55 changes: 54 additions & 1 deletion plugins/modules/metal_hardware_reservation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
- UUID of the hardware_reservation.
required: true
type: str
project_id:
description:
- UUID of parent project containing the hardware_reservation. It can be changed.
required: false
type: str
requirements: null
short_description: Lookup a single hardware_reservation by ID in Equinix Metal
'''
Expand All @@ -29,6 +34,22 @@
tasks:
- equinix.cloud.metal_hardware_reservation:
id: 7624f0f7-75b6-4271-bc64-632b80f87de2
- name: fetch hw reservation resource
equinix.cloud.metal_hardware_reservation:
id: '{{ metal_hardware_reservation_id }}'
register: hwres
- name: create new project to move the hw res to
equinix.cloud.metal_project:
name: destination-project
register: project
- name: move hw reservation to new project
equinix.cloud.metal_hardware_reservation:
id: '{{ metal_hardware_reservation_id }}'
project_id: '{{ project.id }}'
- name: move hw reservation to original project
equinix.cloud.metal_hardware_reservation:
id: '{{ metal_hardware_reservation_id }}'
project_id: '{{ hwres.project_id }}'
'''
RETURN = '''
metal_hardware_reservation:
Expand Down Expand Up @@ -65,6 +86,12 @@
required=True,
description=['UUID of the hardware_reservation.'],
),
project_id=SpecField(
type=FieldType.string,
description=["UUID of parent project containing the hardware_reservation. It can be changed."],
required=False,
editable=True,
),
)


Expand All @@ -76,6 +103,28 @@
- equinix.cloud.metal_hardware_reservation:
id: 7624f0f7-75b6-4271-bc64-632b80f87de2
''',
'''
# Move hardware reservation between projects
- name: fetch hw reservation resource
equinix.cloud.metal_hardware_reservation:
id: "{{ metal_hardware_reservation_id }}"
register: hwres

- name: create new project to move the hw res to
equinix.cloud.metal_project:
name: "destination-project"
register: project

- name: move hw reservation to new project
equinix.cloud.metal_hardware_reservation:
id: "{{ metal_hardware_reservation_id }}"
project_id: "{{ project.id }}"

- name: move hw reservation to original project
equinix.cloud.metal_hardware_reservation:
id: "{{ metal_hardware_reservation_id }}"
project_id: "{{ hwres.project_id }}"
'''
]

result_sample = ['''
Expand Down Expand Up @@ -110,6 +159,7 @@
},
)

MUTABLE_ATTRIBUTES = [a for a, v in module_spec.items() if v.editable]

def main():
module = EquinixModule(
Expand All @@ -124,7 +174,10 @@ def main():
try:
module.params_syntax_check()
fetched = module.get_by_id("metal_hardware_reservation", False)

diff = get_diff(module.params, fetched, MUTABLE_ATTRIBUTES)
if diff:
fetched = module.update_by_id(diff, "metal_hardware_reservation")
changed = True
if fetched:
module.params['id'] = fetched['id']
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# You need a hw reservation to properly run this test.
# The variable can be passed from envvar METAL_HARDWARE_RESERVATION_ID
# If you run this test without setting it, it will skip most of the tasks and exit with success, it's set like this for the CI.
# If you set those it, you can run this test as:
# $ make test_target=metal_hardware_reservation_move test
- name: metal_hardware_reservation_move
module_defaults:
equinix.cloud.metal_hardware_reservation:
metal_api_token: '{{ metal_api_token }}'
equinix.cloud.metal_project:
metal_api_token: '{{ metal_api_token }}'
equinix.cloud.metal_project_info:
metal_api_token: '{{ metal_api_token }}'
block:
- set_fact:
test_resource_name_prefix: 'ansible-integration-test-hw-res-move'
- set_fact:
unique_id: "{{ lookup('password', '/dev/null chars=ascii_lowercase,digits length=8') }}"
- set_fact:
test_prefix: "{{ test_resource_name_prefix }}-{{ unique_id }}"

- name: test fetch hw reservation resource
equinix.cloud.metal_hardware_reservation:
id: "{{ metal_hardware_reservation_id }}"
register: hwres
when: metal_hardware_reservation_id is defined

- name: create project for test
equinix.cloud.metal_project:
name: "{{ test_prefix }}-project1"
register: project
when: metal_hardware_reservation_id is defined

- name: check that hw reservation is not in the test project
assert:
that:
- "metal_hardware_reservation_id == hwres.id"
- "project.id != hwres.project_id"
when: metal_hardware_reservation_id is defined


- name: move hardware reservation to new project
equinix.cloud.metal_hardware_reservation:
id: "{{ metal_hardware_reservation_id }}"
project_id: "{{ project.id }}"
register: hwres_moved
when: metal_hardware_reservation_id is defined

- name: assert that hw reservation has been moved
assert:
that:
- "metal_hardware_reservation_id == hwres_moved.id"
- "project.id == hwres_moved.project_id"
when: metal_hardware_reservation_id is defined

- name: move reservation to original project
equinix.cloud.metal_hardware_reservation:
id: "{{ metal_hardware_reservation_id }}"
project_id: "{{ hwres.project_id }}"
register: hwres_moved_back
when: metal_hardware_reservation_id is defined

- name: assert that hw reservation has been moved back
assert:
that:
- "hwres_moved_back.project_id == hwres.project_id"
when: metal_hardware_reservation_id is defined

- name: delete test project
equinix.cloud.metal_project:
id: "{{ project.id }}"
state: absent
when: metal_hardware_reservation_id is defined

always:
- name: Announce teardown start
debug:
msg: "***** TESTING COMPLETE. COMMENCE TEARDOWN *****"

- name: list test projects
equinix.cloud.metal_project_info:
name: "{{ test_prefix }}"
register: test_projects_listed

- name: delete test projects
equinix.cloud.metal_project:
id: "{{ item.id }}"
state: absent
loop: "{{ test_projects_listed.resources }}"
ignore_errors: yes