Skip to content

Commit

Permalink
Azure stateful import support (#16)
Browse files Browse the repository at this point in the history
* Draft import flow with example added

* ansible-lint fixes

* Update Changelog.rst

* Review comment fixes, Updating example and Version Bump.

* Adding "execution-environment.yml"

* Bump python version used for CI Environment

* Correcting Python Version

* publishing branch

* Files not to be checked-in

* Added logic to return delete SUCCESS when node to be deleted is non-existent
  • Loading branch information
anuragsharma-123 authored Jul 26, 2023
1 parent ea26112 commit b022985
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
16 changes: 15 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,18 @@ v1.3.0
Release Summary
---------------

Adding support to create Azure Stateful Node by Importing an Azure VM.
Adding support to create Azure Stateful Node by Importing an Azure VM.


v1.3.1
======

Release Summary
---------------

Made delete operation idempotent for azure_stateful_node

Bugfixes
--------

- spot.cloud_modules.azure_stateful_node - When 'delete by name' operation is triggered for a non-existent node, return success, not exception.
10 changes: 10 additions & 0 deletions changelogs/changelog.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
ancestor: null
releases:
1.3.1:
changes:
release_summary: Making delete operation idempotent
fragments:
- 1-3-1-summary.yml
modules:
- description: Create, update, delete or Import Spot Azure Stateful Nodes
name: azure_stateful_node
namespace: ''
release_date: '2023-07-20'
1.3.0:
changes:
release_summary: Adding ability to create Azure Stateful Node by importing a VM.
Expand Down
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace: spot
name: cloud_modules

# The version of the collection. Must be compatible with semantic versioning
version: 1.3.0
version: 1.3.1

# The path to the Markdown (.md) readme file. This path is relative to the root of the collection
readme: README.md
Expand Down
23 changes: 13 additions & 10 deletions plugins/modules/azure_stateful_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
short_description: Create, update or delete Spot Azure Stateful Nodes
author: Spot by NetApp (@anuragsharma-123)
description: >
Create, update, delete, import or perform actions (pause, resume, recycle) on Spot Azure
Create, update, delete, import or perform actions (pause, resume, recycle) on Spot Azure
Stateful Nodes.
You will have to have a credentials file in this location - <home>/.spotinst/credentials
The credentials file must contain a row that looks like this
Expand Down Expand Up @@ -804,7 +804,7 @@ def turn_to_model(content, field_name: str, curr_path=None):


def find_ssn_with_same_name(stateful_nodes, name):
ret_val=[]
ret_val = []
for node in stateful_nodes:
if node["name"] == name:
ret_val.append(node)
Expand Down Expand Up @@ -887,9 +887,7 @@ def get_id_and_operation(client, state: str, module):
msg = f"Failed deleting stateful node - 'uniqueness_by' is set to 'name' but there's more than one stateful node with the name '{name}'"
module.fail_json(changed=False, msg=msg)
elif len(nodes_with_name) == 0:
msg = f"Failed deleting stateful node - 'uniqueness_by' is set to 'name' but there is no stateful node with the name '{name}'"
module.fail_json(changed=False, msg=msg)

id = None
else:
msg = f"Spot Ansible Module error: got unknown state {state}"
module.fail_json(changed=False, msg=msg)
Expand Down Expand Up @@ -926,9 +924,14 @@ def handle_delete_stateful_node(client, ssn_id, module):
handle_deletion_config(delete_args, module)

try:
client.delete_stateful_node(**delete_args)
message = f"Stateful node {stateful_node_id} deleted successfully"
has_changed = True
# 'id' will be 'None' when delete operation by name is triggered and stateful node doesn't exist
if stateful_node_id is None:
message = "Failed deleting stateful node - Stateful Node doesn't exist"
has_changed = False
else:
client.delete_stateful_node(**delete_args)
message = f"Stateful node {stateful_node_id} deleted successfully"
has_changed = True
except SpotinstClientException as exc:
if "STATEFUL_NODE_DOES_NOT_EXIST" in exc.message:
message = f"Failed deleting stateful node - Stateful Node with ID {stateful_node_id} doesn't exist"
Expand Down Expand Up @@ -991,7 +994,7 @@ def handle_create_stateful_node(client, stateful_node_module_copy, module):
message = f"Failed creating stateful node, error: {exc.message}"
has_changed = False
module.fail_json(msg=message)

return has_changed, stateful_node_id, message


Expand Down Expand Up @@ -1032,7 +1035,7 @@ def handle_import_stateful_node(client, ssn_models, module):
draining_timeout = import_vm_config["draining_timeout"]
else:
draining_timeout = None

if "resource_retention_time" in import_vm_config:
resource_retention_time = import_vm_config["resource_retention_time"]
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/test_azure_elastigroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def test_all_fields(self):

expected_network = Network(resource_group_name="AutomationResourceGroup",
virtual_network_name="Automation-VirtualNetwork",
network_interfaces=[NetworkInterface(is_primary=True, assign_public_ip=True, public_ip_sku="Standard",
network_interfaces=[NetworkInterface(is_primary=True, assign_public_ip=True, public_ip_sku="Standard",
subnet_name="Automation-PrivateSubnet", enable_ip_forwarding=True)])

actual_network = eg.compute.launch_specification.network
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/test_azure_stateful_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def test_all_fields(self):

expected_network = Network(resource_group_name="AutomationResourceGroup",
virtual_network_name="Automation-VirtualNetwork",
network_interfaces=[NetworkInterface(is_primary=True, assign_public_ip=True, public_ip_sku="Standard",
network_interfaces=[NetworkInterface(is_primary=True, assign_public_ip=True, public_ip_sku="Standard",
subnet_name="Automation-PrivateSubnet", enable_ip_forwarding=True)])

actual_network = ssn.compute.launch_specification.network
Expand Down

0 comments on commit b022985

Please sign in to comment.