Thanks to visit codestin.com
Credit goes to github.com

Skip to content

createObjects methods do not work with REST API #885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rlrossiter opened this issue Oct 12, 2017 · 3 comments
Closed

createObjects methods do not work with REST API #885

rlrossiter opened this issue Oct 12, 2017 · 3 comments

Comments

@rlrossiter
Copy link

Please triple-check to make sure that you have properly masked out user credentials like usernames, passwords and API keys before submitting your issue

Expected Behavior

createObjects with the RestTransport should operate the same way as the XmlRpcTransport. Calling createObjects with multiple objects should create each object, and return all created objects

Actual Behavior

Here is a script that I wrote to attempt creating multiple VSIs as the comments in the VS manager's create_instances function instructs. The endpoint I was pointing to was endpoint_url = https://api.softlayer.com/rest/v3.1/

import os

import SoftLayer

config_file = os.path.expanduser('~/.softlayer')

client = SoftLayer.create_client_from_env(config_file=config_file)
vs = SoftLayer.VSManager(client)

new_vsi = {
    'domain': 'sl.test',
    'hostname': 'sl-test',
    'datacenter': 'dal13',
    'cpus': 1,
    'os_code' : 'UBUNTU_LATEST',
    'hourly': True,
    'disks': ('100',),
    'local_disk': True,
    'memory': 1024
}

instances = [new_vsi.copy(), new_vsi.copy()]
instances[0]['hostname'] = 'sl-test0'
instances[1]['hostname'] = 'sl-test1'

vsis = vs.create_instances(instances)
for vsi in vsis:
    print vsi

The error output was:

Traceback (most recent call last):
  File "test_createObjects.py", line 26, in <module>
    vsis = vs.create_instances(instances)
  File "/Users/rlrossit/venv/lib/python2.7/site-packages/SoftLayer/managers/vs.py", line 635, in create_instances
    for kwargs in config_list])
  File "/Users/rlrossit/venv/lib/python2.7/site-packages/SoftLayer/API.py", line 392, in call_handler
    return self(name, *args, **kwargs)
  File "/Users/rlrossit/venv/lib/python2.7/site-packages/SoftLayer/API.py", line 360, in call
    return self.client.call(self.name, name, *args, **kwargs)
  File "/Users/rlrossit/venv/lib/python2.7/site-packages/SoftLayer/API.py", line 263, in call
    return self.transport(request)
  File "/Users/rlrossit/venv/lib/python2.7/site-packages/SoftLayer/transports.py", line 315, in __call__
    message)
SoftLayer.exceptions.SoftLayerAPIError: SoftLayerAPIError(500): The property 'hostname' must be set to create an instance of 'SoftLayer_Virtual_Guest'.

The hostname on each instance is set, so this isn't the actual error that's happening. This is also happening for security groups createObjects, and what's happening with security groups is it is just doing a create of one security group with no attributes set on it.

For security groups, we attempted to make the REST call using curl:

curl --user "$SOFTLAYER_USERNAME:$SOFTLAYER_API_KEY" -k -X POST 'https://api.softlayer.com/rest/v3/SoftLayer_Network_SecurityGroup' -d '{"parameters": [{"name": "some name1","description":"some description"},{"name": "some name2","description":"some description"}]}'

This only created one security group instead of both. The call, in order to make it work, was:

curl -s --user "$SOFTLAYER_USERNAME:$SOFTLAYER_API_KEY" -k -X POST 'https://api.softlayer.com/rest/v3/SoftLayer_Network_SecurityGroup/createObjects' -d '{"parameters": [[{"name": "some name1","description":"some description"},{"name": "some name2","description":"some description"}]]}'

The solution was to call createObjects directly, where softlayer-python is just calling https://api.softlayer.com/rest/v3/SoftLayer_Network_SecurityGroup with POST. This probably means that if a "special method" (https://github.com/softlayer/softlayer-python/blob/master/SoftLayer/transports.py#L32-L38) is encountered, we should change it to POST, but we shouldn't remove the method name in case it is the plural API that is being called.

Environment Information

Operating System: Mac OS 10.12.6
softlayer-python version (slcli --version): slcli (SoftLayer Command-line), version 5.2.14

@rlrossiter
Copy link
Author

I used debugging the the VSI createObjects call and got the following:

(Pdb) url
'https://api.softlayer.com/rest/v3.1/SoftLayer_Virtual_Guest.json'
(Pdb) raw_body
'{"parameters": [[{"datacenter": {"name": "dal13"}, "domain": "sl.test", "hourlyBillingFlag": true, "localDiskFlag": true, "maxMemory": 1024, "hostname": "sl-test0", "startCpus": 1, "blockDevices": [{"device": "0", "diskImage": {"capacity": "100"}}], "operatingSystemReferenceCode": "UBUNTU_LATEST"}, {"datacenter": {"name": "dal13"}, "domain": "sl.test", "hourlyBillingFlag": true, "localDiskFlag": true, "maxMemory": 1024, "hostname": "sl-test1", "startCpus": 1, "blockDevices": [{"device": "0", "diskImage": {"capacity": "100"}}], "operatingSystemReferenceCode": "UBUNTU_LATEST"}]]}'
(Pdb) method
'POST'

The result showed that hostname was missing:

(Pdb) resp.text
u'{"error":"The property \'hostname\' must be set to create an instance of \'SoftLayer_Virtual_Guest\'.","code":"SoftLayer_Exception_MissingCreationProperty"}'

Changing the URL to have createObjects on the end made it work properly:

(Pdb) url
'https://api.softlayer.com/rest/v3.1/SoftLayer_Virtual_Guest/createObjects'
(Pdb) raw_body
'{"parameters": [[{"datacenter": {"name": "dal13"}, "domain": "sl.test", "hourlyBillingFlag": true, "localDiskFlag": true, "maxMemory": 1024, "hostname": "sl-test0", "startCpus": 1, "blockDevices": [{"device": "0", "diskImage": {"capacity": "100"}}], "operatingSystemReferenceCode": "UBUNTU_LATEST"}, {"datacenter": {"name": "dal13"}, "domain": "sl.test", "hourlyBillingFlag": true, "localDiskFlag": true, "maxMemory": 1024, "hostname": "sl-test1", "startCpus": 1, "blockDevices": [{"device": "0", "diskImage": {"capacity": "100"}}], "operatingSystemReferenceCode": "UBUNTU_LATEST"}]]}'
(Pdb) method
'POST'
(Pdb) resp.text
u'[{"accountId":1273203,"createDate":"2017-10-12T09:28:26-06:00","domain":"sl.test","fullyQualifiedDomainName":"sl-test0.sl.test","hostname":"sl-test0","id":41629079,"lastPowerStateId":null,"lastVerifiedDate":null,"maxCpu":1,"maxCpuUnits":"CORE","maxMemory":1024,"metricPollDate":null,"modifyDate":null,"provisionDate":null,"startCpus":1,"statusId":1001,"uuid":"b9ab43b7-0c19-4a14-9409-4068762565e0"},{"accountId":1273203,"createDate":"2017-10-12T09:28:26-06:00","domain":"sl.test","fullyQualifiedDomainName":"sl-test1.sl.test","hostname":"sl-test1","id":41629081,"lastPowerStateId":null,"lastVerifiedDate":null,"maxCpu":1,"maxCpuUnits":"CORE","maxMemory":1024,"metricPollDate":null,"modifyDate":null,"provisionDate":null,"startCpus":1,"statusId":1001,"uuid":"a66405a2-06de-4ba3-a235-1ed3dc8e6c34"}]'

@rlrossiter
Copy link
Author

Another suggestion was to remove the double array, but that only results in the first VSI being created, the second is ignored:

(Pdb) url
'https://api.softlayer.com/rest/v3.1/SoftLayer_Virtual_Guest.json'
(Pdb) method
'POST'
(Pdb) raw_body
'{"parameters": [{"datacenter": {"name": "dal13"}, "domain": "sl.test", "hourlyBillingFlag": true, "localDiskFlag": true, "maxMemory": 1024, "hostname": "sl-test0", "startCpus": 1, "blockDevices": [{"device": "0", "diskImage": {"capacity": "100"}}], "operatingSystemReferenceCode": "UBUNTU_LATEST"}, {"datacenter": {"name": "dal13"}, "domain": "sl.test", "hourlyBillingFlag": true, "localDiskFlag": true, "maxMemory": 1024, "hostname": "sl-test1", "startCpus": 1, "blockDevices": [{"device": "0", "diskImage": {"capacity": "100"}}], "operatingSystemReferenceCode": "UBUNTU_LATEST"}]}'

Response:

(Pdb) resp.text
u'{"accountId":1273203,"createDate":"2017-10-12T09:31:54-06:00","domain":"sl.test","fullyQualifiedDomainName":"sl-test0.sl.test","hostname":"sl-test0","id":41629137,"lastPowerStateId":null,"lastVerifiedDate":null,"maxCpu":1,"maxCpuUnits":"CORE","maxMemory":1024,"metricPollDate":null,"modifyDate":null,"provisionDate":null,"startCpus":1,"statusId":1001,"uuid":"afcacee7-2ba5-4d57-8731-063812017144","globalIdentifier":"7c9cebf0-3bbe-4d9d-86ab-cef697a1b10d"}'

@allmightyspiff
Copy link
Member

this should be fixed in 5.2.15 and up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants