Skip to content

Commit

Permalink
Add changes from kd/issue-202
Browse files Browse the repository at this point in the history
  • Loading branch information
mamercad committed Oct 6, 2022
1 parent 180ebcb commit 03a0eb8
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions plugins/modules/digital_ocean_droplet.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,10 @@ def add_droplet_to_firewalls(self):
if rule is None:
err = "Failed to find firewalls: {0}".format(self.module.params["firewall"])
return err
json_data = self.get_droplet()
if not self.unique_name:
json_data = self.get_droplet_by_name()
else:
json_data = self.get_droplet()
if json_data is not None:
request_params = {}
droplet = json_data.get("droplet", None)
Expand All @@ -387,13 +390,17 @@ def add_droplet_to_firewalls(self):
err = "Failed to add droplet {0} to firewall {1}".format(
droplet_id, rule[firewall]["id"]
)
return err, changed
changed = True
return None, changed
return err
else:
err = f"Failed to find droplet data. got: {json_data}"
return err
return None

def remove_droplet_from_firewalls(self):
changed = False
json_data = self.get_droplet()
if self.unique_name:
json_data = self.get_droplet()
else:
json_data = self.get_droplet_by_name()
if json_data is not None:
request_params = {}
droplet = json_data.get("droplet", None)
Expand Down Expand Up @@ -497,6 +504,12 @@ def get_droplet(self):
json_data = self.get_by_name(self.module.params["name"])
return json_data

def get_droplet_by_name(self):
json_data = self.get_by_id(self.module.params["id"])
if not json_data:
json_data = self.get_by_name(self.module.params["name"])
return json_data

def resize_droplet(self, state, droplet_id):
if self.status != "off":
self.module.fail_json(
Expand Down Expand Up @@ -542,7 +555,7 @@ def wait_status(self, droplet_id, desired_statuses):
status_code = response.status_code
message = json_data.get("message", "no error message")
droplet = json_data.get("droplet", None)
droplet_status = droplet.get("status", None) if droplet else None
droplet_status = droplet.get("status", None)

if droplet is None or droplet_status is None:
self.module.fail_json(
Expand Down Expand Up @@ -776,7 +789,8 @@ def create(self, state):

# Get updated Droplet data (fallback to current data)
if self.wait:
json_data = self.get_by_id(droplet_id)
json_data = self.get_droplet()
# Without unique_name json_data is None
if json_data:
droplet = json_data.get("droplet", droplet)

Expand All @@ -795,7 +809,6 @@ def create(self, state):
)
# Add droplet to firewall if specified
if self.module.params["firewall"] is not None:
# raise Exception(self.module.params["firewall"])
firewall_add = self.add_droplet_to_firewalls()
if firewall_add is not None:
self.module.fail_json(
Expand All @@ -815,13 +828,9 @@ def create(self, state):
self.module.exit_json(changed=True, data={"droplet": droplet})

def delete(self):
# to delete a droplet we need to know the droplet id or unique name, ie
# name is not None and unique_name is True, but as "id or name" is
# enforced elsewhere, we only need to enforce "id or unique_name" here
if not self.module.params["id"] and not self.unique_name:
if not self.unique_name:
self.module.fail_json(
changed=False,
msg="id must be set or unique_name must be true for deletes",
changed=False, msg="unique_name must be set for deletes"
)
json_data = self.get_droplet()
if json_data is None:
Expand Down Expand Up @@ -906,6 +915,7 @@ def main():
("state", "present", ["name", "size", "image", "region"]),
("state", "active", ["name", "size", "image", "region"]),
("state", "inactive", ["name", "size", "image", "region"]),
("state", "absent", ["name", "unique_name"]),
]
),
supports_check_mode=True,
Expand Down

0 comments on commit 03a0eb8

Please sign in to comment.