我正在尝试使用Python API FirewallManager类的cancel_firewall()函数来取消帐户中的所有防火墙。
# Connect to soflayer account
client = SoftLayer.create_client_from_env(username=user, api_key=api)
firewall_manager = SoftLayer.FirewallManager(client=client)
# Get a list of all of the firewalls in an account
firewalls = firewall_manager.get_firewalls()
# Cancel each firewall
for firewall in firewalls:
dedicated = bool(firewall["dedicatedFirewallFlag"])
firewall_manager.cancel_firewall(firewall_id=firewall["id"], dedicated=dedicated )
但是当我运行此代码时,我收到错误:
SoftLayerAPIError(SoftLayer_Exception_ObjectNotFound): Unable to find object with id of '284501'.
取消代码说它需要一个“firewall_id”,但我给它的id似乎没有用......管理员的源代码是here
答案 0 :(得分:1)
正如我在您的代码中看到的那样,您将防火墙['id'] 发送到 cancel_firewall 方法。
查看SLCLI的工作原理,根据防火墙类型是 vlan,vs还是服务器进行过滤。遵循相同的想法,您应该考虑以下链接中的现有过滤器:
https://github.com/softlayer/softlayer-python/blob/master/SoftLayer/CLI/firewall/list.py
E.g:
- VSI: firewall ['id'] == vlan ['firewallGuestNetworkComponents'] *
- 服务器:防火墙['id'] == vlan ['firewallNetworkComponents']
- Vlan: firewall ['id'] == vlan ['networkVlanFirewall'] ['id']
嗯,理解它是如何工作的是一件很乏味的事情,无论如何,我使用了下面的脚本,它使用FirewallManager来删除所有防火墙:
"""
cancels all firewall from the account
Important manual pages:
https://github.com/softlayer/softlayer-python/blob/master/SoftLayer/managers/firewall.py
https://github.com/softlayer/softlayer-python/blob/master/SoftLayer/CLI/firewall/list.py
https://github.com/softlayer/softlayer-python/blob/master/SoftLayer/CLI/firewall/cancel.py
License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
import SoftLayer
from SoftLayer.CLI import formatting
# Your SoftLayer username and apiKey
user = 'set me'
api = 'set me'
# Connect to SoftLayer
client = SoftLayer.create_client_from_env(username=user, api_key=api)
# Declare Firewall Manager
firewall_manager = SoftLayer.FirewallManager(client=client)
# Getting firewalls from the account
fwvlans = firewall_manager.get_firewalls()
# Getting Dedicated firewalls
dedicated_firewalls = [firewall for firewall in fwvlans if firewall['dedicatedFirewallFlag']]
# Define a Vlans array to store vlans
vlans = []
# Method from:
# https://github.com/softlayer/softlayer-python/blob/master/SoftLayer/CLI/firewall/list.py
def has_firewall_component(server):
"""Helper to determine whether or not a server has a firewall.
:param dict server: A dictionary representing a server
:returns: True if the Server has a firewall.
"""
if server['status'] != 'no_edit':
return True
return False
for vlan in dedicated_firewalls:
features = []
if vlan['highAvailabilityFirewallFlag']:
features.append('HA')
if features:
feature_list = formatting.listing(features, separator=',')
else:
feature_list = formatting.blank()
vlans.append('vlan:%s' % vlan['networkVlanFirewall']['id'])
shared_vlan = [firewall for firewall in fwvlans
if not firewall['dedicatedFirewallFlag']]
for vlan in shared_vlan:
vs_firewalls = [guest
for guest in vlan['firewallGuestNetworkComponents']
if has_firewall_component(guest)]
for firewall in vs_firewalls:
vlans.append('vs:%s' % firewall['id'])
server_firewalls = [server
for server in vlan['firewallNetworkComponents']
if has_firewall_component(server)]
for firewall in server_firewalls:
vlans.append('server:%s' % firewall['id'])
# Methods from:
# https://github.com/softlayer/softlayer-python/blob/master/SoftLayer/CLI/firewall/cancel.py
for item in vlans:
firewall_type, firewall_id = item.split(':')
if firewall_type in ['vs', 'server']:
print(firewall_manager.cancel_firewall(firewall_id, dedicated=False))
elif firewall_type == 'vlan':
print(firewall_manager.cancel_firewall(firewall_id, dedicated=True))
else:
raise exceptions.CLIAbort('Unknown firewall type: %s' % firewall_type)
我希望它有所帮助,如果您对此有任何疑问或意见,请与我联系。请记住,脚本只是一个想法,它可以改进。