def ensure_ip_address_absent(nb_endpoint, data): """ :returns dict(msg, changed) """ if not isinstance(data, dict): changed = False return {"msg": data, "changed": changed} try: ip_addr = _search_ip(nb_endpoint, data) except ValueError: return _error_multiple_ip_results(data) result = {} if ip_addr: dummy, diff = delete_netbox_object(ip_addr, module.check_mode) changed = True msg = "IP Address %s deleted" % (data["address"]) result["diff"] = diff else: changed = False msg = "IP Address %s already absent" % (data["address"]) result.update({"msg": msg, "changed": changed}) return result
def ensure_device_absent(nb_endpoint, data): ''' :returns dict(msg, changed, diff) ''' nb_device = nb_endpoint.get(name=data["name"]) result = {} if nb_device: dummy, diff = delete_netbox_object(nb_device, module.check_mode) msg = 'Device %s deleted' % (data["name"]) changed = True result["diff"] = diff else: msg = 'Device %s already absent' % (data["name"]) changed = False result.update({"changed": changed, "msg": msg}) return result
def ensure_interface_absent(nb, nb_endpoint, data): """ :returns dict(msg, changed, diff) """ nb_intf = nb_endpoint.get(name=data["name"], device_id=data["device"]) result = dict() if nb_intf: dummy, diff = delete_netbox_object(nb_intf, module.check_mode) changed = True msg = "Interface %s deleted" % (data["name"]) result["diff"] = diff else: msg = "Interface %s already absent" % (data["name"]) changed = False result.update({"msg": msg, "changed": changed}) return result
def ensure_site_absent(nb, nb_endpoint, data): """ :returns dict(msg, changed) """ nb_site = nb_endpoint.get(slug=data["slug"]) result = dict() if nb_site: dummy, diff = delete_netbox_object(nb_site, module.check_mode) changed = True msg = "Site %s deleted" % (data["name"]) result["diff"] = diff else: msg = "Site %s already absent" % (data["name"]) changed = False result.update({"msg": msg, "changed": changed}) return result
def ensure_prefix_absent(nb, nb_endpoint, data): """ :returns dict(msg, changed) """ try: nb_prefix = _search_prefix(nb_endpoint, data) except ValueError: return _error_multiple_prefix_results(data) result = dict() if nb_prefix: dummy, diff = delete_netbox_object(nb_prefix, module.check_mode) changed = True msg = "Prefix %s deleted" % (nb_prefix.prefix) result["diff"] = diff else: msg = "Prefix %s already absent" % (data["prefix"]) changed = False result.update({"msg": msg, "changed": changed}) return result
def test_delete_netbox_object(nb_obj_mock, on_deletion_diff): serialized_obj, diff = delete_netbox_object(nb_obj_mock, check_mode=False) assert nb_obj_mock.delete.called_once() assert serialized_obj == nb_obj_mock.serialize() assert diff == on_deletion_diff
def test_delete_netbox_object_in_check_mode(nb_obj_mock, on_deletion_diff): serialized_obj, diff = delete_netbox_object(nb_obj_mock, check_mode=True) assert nb_obj_mock.delete.not_called() assert serialized_obj == nb_obj_mock.serialize() assert diff == on_deletion_diff