UpCloudLtd/upcloud-python-api

Include complete data in to_dict() methods

aqiank opened this issue · 9 comments

Hi, I couldn't seem to find the Python API for getting storage details which is documented here. Did I overlook it or is it simply not implemented yet?

Hi there! The functionality is available through the CloudManager with manager.get_storage(storage.uuid). There are some other storage examples here. Before using, CloudManager needs be be initialized and there are examples for in in the main Readme.

Could you try this out? Happy to help if you run into any problems with it.

Hi @ajmyyra, when I use manager.get_storage(storage.uuid).to_dict(), it returns this:

{
    "tier": "maxiops",
    "title": "xxx",
    "size": 16,
    "zone": "xxx"
}

But when I use the documented API via requests, it returns this:

{
    "storage": {
        "access": "private",
        "backup_rule": {},
        "backups": {
            "backup": []
        },
        "license": 0,
        "servers": {
            "server": [
                "xxx"
            ]
        },
        "size": 16,
        "state": "online",
        "tier": "maxiops",
        "title": "xxx",
        "type": "normal",
        "uuid": "xxx",
        "zone": "xxx"
    }
}

So it seems like they are not equivalent?

Hi,

As Storage is an object, getting a dict out of it is not that straightforward. Using it on its own should give you a lot more details:

>>> import upcloud_api
>>> manager = upcloud_api.CloudManager('username', 'password')
>>> storage = manager.get_storage('STORAGE_UUID')
>>> vars(storage)
{'uuid': 'STORAGE_UUID', 'title': 'Storage title', 'size': 50, 'cloud_manager': <upcloud_api.cloud_manager.CloudManager object at 0x7fc1902adbe0>, 'access': 'private', 'backup_rule': {}, 'backups': {'backup': []}, 'license': 0, 'part_of_plan': 'yes', 'servers': {'server': ['SERVER_ID']}, 'state': 'online', 'tier': 'maxiops', 'type': 'normal', 'zone': 'ZONE'}

Hmm is there a reason that those other fields are left out of the to_dict() output?

Currently to_dict() function in storage objects is used for creating a JSON object that includes the relevant items for the storage objects in the UpCloud server API. If you want your object as a dictionary, can't you just create it from the object with .__dict__? So for example:

>>> import upcloud_api
>>> manager = upcloud_api.CloudManager('username', 'password')
>>> stor = manager.get_storage('STORAGE_UUID')
>>> dictified_storage = stor.__dict__
>>> type(dictified_storage)
<class 'dict'>

Yes, I could but it seems like it's not very intuitive to have to do that when there's a method called to_dict() which seems like it should do the same thing. This information is also not documented anywhere in the documentation. Might be just me being non-Python expert though.

Yup, it's hard to disagree with you on this. The only excuse is that it's not documented as it's meant as an internal function. We should change it to work as you would expect but as this would be a breaking change, we'll need to wait for the next larger release to do it. Could you create a separate issue for this change (and adding similar to_dict() functionality to other objects as well) or modify the title of this one, so we can track this change better?

Sure, I've renamed the title. Let me know if it's a suitable title and thanks!

It is, thank you! Hopefully we can get back to this in the near future.