客户在软件层控制中订购本地负载均衡器后,客户执行一些管理操作(添加组,添加服务,取消负载均衡器),然后将配置保存在控制中。
我可以使用VirtualIpAddress getObject(id)方法获取负载均衡器的详细信息,但我不知道如何使用API更新(保存)我更改的配置文件?
我附上了softlayer control的屏幕截图 哪个api可用于管理负载均衡器?
答案 0 :(得分:0)
你只需要editObject方法。
"""
Add a new service group.
Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress/editObject
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Routing_Type
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Routing_Type/getAllObjects
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Routing_Method
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Routing_Method/getAllObjects
License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
import SoftLayer
import json
# Your SoftLayer API username and key.
USERNAME = 'set me'
API_KEY = 'set me'
loadBalancerId = 79945
# The new allocation value for the service groups.
# The sum of the allocation of all your services group must be 100.
# If you add a new service group it is required to change the allocation
# value of the previous service groups into the load balancer.
# note: the last value is for the new service group you wish to add.
newAllocations = [40, 20, 20, 10, 10]
# The port must unique among the service groups into the load balancer
port = 4040
notes = 'my group note'
method = 'Shortest Response'
group = 'HTTP'
timeout = ""
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
loadBalancerService = client['SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress']
groupService = client['SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Routing_Type']
methodService = client['SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Routing_Method']
objectMask = "mask[virtualServers[serviceGroups]]"
groupFilter = {"name": {"operation": group}}
methodFilter = {"name": {"operation": method}}
try:
group = groupService.getAllObjects(filter=groupFilter)
except SoftLayer.SoftLayerAPIError as e:
print("Unable to get the group. faultCode=%s, faultString=%s" % (e.faultCode, e.faultString))
exit(1)
try:
method = methodService.getAllObjects(filter=methodFilter)
except SoftLayer.SoftLayerAPIError as e:
print("Unable to get the method. faultCode=%s, faultString=%s" % (e.faultCode, e.faultString))
exit(1)
try:
virtualServer = {
'port': port,
'serviceGroups': [
{
'routingMethodId': group[0]['id'],
'routingTypeId': method[0]['id'],
'notes': notes,
'timeout': timeout
}
]
}
loadBalancer = loadBalancerService.getObject(id=loadBalancerId, mask=objectMask)
result = loadBalancerService.editObject(loadBalancer, id=loadBalancerId)
if 'virtualServers' not in loadBalancer:
loadBalancer['virtualServers'] = []
loadBalancer['virtualServers'].append(virtualServer)
if len(loadBalancer['virtualServers']) == len(newAllocations):
for index in range(len(newAllocations)):
loadBalancer['virtualServers'][index]['allocation'] = newAllocations[index]
result = loadBalancerService.editObject(loadBalancer, id=loadBalancerId)
print(json.dumps(result, sort_keys=True, indent=2, separators=(',', ': ')))
else:
print ("Error the number of configured allocations is not the same as the service groups in the load balancer.")
except SoftLayer.SoftLayerAPIError as e:
print("Unable to add the service group. faultCode=%s, faultString=%s" % (e.faultCode, e.faultString))
exit(1)
-
"""
Add a new service to a service group.
Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress/editObject
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Health_Check_Type
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Health_Check_Type/getAllObjects
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Subnet_IpAddress
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Subnet_IpAddress/findByIpv4Address
License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
import SoftLayer
import json
USERNAME = 'set me'
API_KEY = 'set me'
loadBalancerId = 79945
serviceGroupId = 138193
destinationIpAddress = "169.53.15.53"
destinationPort = 40
weight = 10
notes = "my service group note"
enabled = True
healthCheck = "Ping"
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
loadBalancerService = client['SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress']
healthService = client['SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Health_Check_Type']
ipService = client['SoftLayer_Network_Subnet_IpAddress']
heaqlthFilter = {"name": {"operation": healthCheck}}
objectMask = "mask[sslActiveFlag, notes, sslEnabledFlag, loadBalancerHardware[hostname,datacenterName,location,locationPathString], ipAddress, virtualServers[notes,port,id,allocation,serviceGroups[services[groupReferences[weight],healthChecks[type[id,name],attributes],ipAddress[id,ipAddress]],routingMethod,routingType]]]"
try:
healthCheck = healthService.getAllObjects(filter=heaqlthFilter)
except SoftLayer.SoftLayerAPIError as e:
print("Unable to get the health check. faultCode=%s, faultString=%s" % (e.faultCode, e.faultString))
exit(1)
try:
destinationIpAddress = ipService.findByIpv4Address(destinationIpAddress)
except SoftLayer.SoftLayerAPIError as e:
print("Unable to get the IP address. faultCode=%s, faultString=%s" % (e.faultCode, e.faultString))
exit(1)
try:
loadBalancer = loadBalancerService.getObject(id=loadBalancerId, mask=objectMask)
except SoftLayer.SoftLayerAPIError as e:
print("Unable to get the load balancer. faultCode=%s, faultString=%s" % (e.faultCode, e.faultString))
exit(1)
serviceGroup = {}
for i in range(len(loadBalancer['virtualServers'])):
for j in range(len(loadBalancer['virtualServers'][i]['serviceGroups'])):
if loadBalancer['virtualServers'][i]['serviceGroups'][j]['id'] == serviceGroupId:
serviceGroup = loadBalancer['virtualServers'][i]['serviceGroups'][j]
break
if 'id' not in serviceGroup:
print("Unable to get the service group id: " + str(serviceGroupId) + " in the load balancer")
else:
newService = {}
newService['enabled'] = enabled
newService['groupReferences'] = []
groupReferences = {}
groupReferences['weight'] = weight
newService['groupReferences'].append(groupReferences)
newService['healthChecks'] = []
healthChecks = {}
healthChecks['healthCheckTypeId'] = healthCheck[0]['id']
newService['healthChecks'].append(healthChecks)
newService['ipAddressId'] = destinationIpAddress['id']
newService['port'] = destinationPort
newService['notes'] = notes
loadBalancer['virtualServers'][i]['serviceGroups'][j]['services'].append(newService)
try:
result = loadBalancerService.editObject(loadBalancer, id=loadBalancerId)
print(json.dumps(result, sort_keys=True, indent=2, separators=(',', ': ')))
except SoftLayer.SoftLayerAPIError as e:
print("Unable to add the service to the service group. faultCode=%s, faultString=%s" % (e.faultCode, e.faultString))
exit(1)
-
"""
Cancel load balancer.
Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Billing_Item_Cancellation_Request
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Billing_Item_Cancellation_Request
http://sldn.softlayer.com/reference/services/SoftLayer_Billing_Item_Cancellation_Request/createObject
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress
License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
import SoftLayer
import json
# Your SoftLayer API username and key.
USERNAME = 'set me'
API_KEY = 'set me'
loadBalancerId = 109225
immediateCancellationFlag = True
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
loadBalancerService = client['SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualIpAddress']
cancellationService = client['SoftLayer_Billing_Item_Cancellation_Request']
objectMask = "mask[accountId, dedicatedFlag,dedicatedBillingItem[id],billingItem[id]]"
try:
loadBalancer = loadBalancerService.getObject(id=loadBalancerId, mask=objectMask)
billingItemId = 0
if loadBalancer['dedicatedFlag']:
billingItemId = loadBalancer['dedicatedBillingItem']['id']
else:
billingItemId = loadBalancer['billingItem']['id']
cancelTemplate = {
'accountId': loadBalancer['accountId'],
'items': [
{
'billingItemId': billingItemId,
'immediateCancellationFlag': immediateCancellationFlag
}
]
}
result = cancellationService.createObject(cancelTemplate)
print(json.dumps(result, sort_keys=True, indent=2, separators=(',', ': ')))
except SoftLayer.SoftLayerAPIError as e:
print("Unable to cancel the load balancer. faultCode=%s, faultString=%s" % (e.faultCode, e.faultString))
exit(1)
-
"""
Remove a service group from a load balancer.
Important manual pages:
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualServer
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualServer/
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualServer/deleteObject
License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
import SoftLayer
import json
# Your SoftLayer API username and key.
USERNAME = 'set me'
API_KEY = 'set me'
# The virtual server which contains the service group to delete.
virtualServerId = 193563
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
virtualService = client['SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_VirtualServer']
try:
result = virtualService.deleteObject(id=virtualServerId)
print(json.dumps(result, sort_keys=True, indent=2, separators=(',', ': ')))
except SoftLayer.SoftLayerAPIError as e:
print("Unable to remove the service group. faultCode=%s, faultString=%s" % (e.faultCode, e.faultString))
exit(1)
-
"""
Remove a service from a load balancer.
Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Service
http://sldn.softlayer.com/reference/services/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Service/deleteObject
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Service
License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
import SoftLayer
import json
# Your SoftLayer API username and key.
USERNAME = 'set me'
API_KEY = 'set me'
# The service id to delete.
serviceId = 367111
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
service = client['SoftLayer_Network_Application_Delivery_Controller_LoadBalancer_Service']
try:
result = service.deleteObject(id=serviceId)
print(json.dumps(result, sort_keys=True, indent=2, separators=(',', ': ')))
except SoftLayer.SoftLayerAPIError as e:
print("Unable to remove the service. faultCode=%s, faultString=%s" % (e.faultCode, e.faultString))
exit(1)
此致