Skip to content

Conversation

@hammerstefan
Copy link
Contributor

@hammerstefan hammerstefan commented Jul 24, 2025

Description

IBM library get_instance returns a DetailedResponse object, not an instance dict directly. This fix gets the instance dict result from that object before passing it to from_existing.

Additional Context and Relevant Issues

Previous behavior throws a TypeError

  File "/home/shammer/Canonical/git/tools/ubuntu-old-fashioned/scripts/ubuntu-bartender/ibm-provider.py", line 34, in <module>
    delete(args.id)
  File "/home/shammer/Canonical/git/tools/ubuntu-old-fashioned/scripts/ubuntu-bartender/ibm-provider.py", line 22, in delete
    instance = ibm.get_instance(instance_id)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shammer/ubuntu-bartender-multipass.pedbPM1KcM/.venv/lib/python3.12/site-packages/pycloudlib/ibm/cloud.py", line 230, in get_instance
    return IBMInstance.find_existing(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shammer/ubuntu-bartender-multipass.pedbPM1KcM/.venv/lib/python3.12/site-packages/pycloudlib/ibm/instance.py", line 702, in find_existing
    return cls.from_existing(
           ^^^^^^^^^^^^^^^^^^
  File "/home/shammer/ubuntu-bartender-multipass.pedbPM1KcM/.venv/lib/python3.12/site-packages/pycloudlib/ibm/instance.py", line 675, in from_existing
    floating_ip = kwargs.pop("floating_ip", None) or cls._discover_floating_ip(client, instance)
                                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shammer/ubuntu-bartender-multipass.pedbPM1KcM/.venv/lib/python3.12/site-packages/pycloudlib/ibm/instance.py", line 773, in _discover_floating_ip
    nic_id = instance["primary_network_interface"]["id"]
             ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'DetailedResponse' object is not subscriptable

Test Steps

Create an instance and perform a get_instance

import pycloudlib

ibm = pycloudlib.IBM("test")
imge = ibm.daily_image(release="noble")
instance = ibm.launch(image_id=daily, instance_type="bx3d-2x10", name="test")
id = instance._instance["id"]
instance_find = ibm.get_instance(id)  # previously would throw an exception here

IBM library get_instance returns a DetailedResponse object, not an
instance dict directly. This fix gets the instance dict result from that
object before passing it to from_existing.
@hammerstefan
Copy link
Contributor Author

mypy linting failure is unrelated to this PR

mypy: commands[0]> .tox/.testenv/bin/python -m mypy pycloudlib examples setup.py
pycloudlib/azure/instance.py:317: error: Incompatible types in assignment (expression has type "None", variable has type "NetworkInterface")
Found 1 error in 1 file (checked 64 source files)

@hammerstefan hammerstefan marked this pull request as ready for review July 24, 2025 15:30
@hammerstefan hammerstefan requested a review from blackboxsw July 24, 2025 15:36
Copy link
Collaborator

@blackboxsw blackboxsw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM based on .tox/.testenv/lib/python3.12/site-packages/ibm_cloud_sdk_core/detailed_response.py values.

Also we can fix mypy with

--- a/pycloudlib/azure/instance.py
+++ b/pycloudlib/azure/instance.py
@@ -301,6 +301,7 @@ class AzureInstance(BaseInstance):
         vm_nics_ids = [nic.id for nic in self._instance["vm"].network_profile.network_interfaces]
         all_nics: List[NetworkInterface] = list(self._network_client.network_interfaces.list_all())
         vm_nics = [nic for nic in all_nics if nic.id in vm_nics_ids]
+        primary_nic : Optional[NetworkInterface] = None
         primary_nic = [nic for nic in vm_nics if nic.primary][0]
         nic_params = []
         nic_to_remove: Optional[NetworkInterface] = None

@hammerstefan
Copy link
Contributor Author

Let me get a separate PR up to fix the linting error.

@hammerstefan hammerstefan merged commit ad3e7ef into canonical:main Jul 25, 2025
7 of 8 checks passed
Gisaldjo pushed a commit to Gisaldjo/pycloudlib that referenced this pull request Jul 28, 2025
## Description

IBM library get_instance returns a DetailedResponse object, not an
instance dict directly. This fix gets the instance dict result from that
object before passing it to from_existing.


## Additional Context and Relevant Issues

Previous behavior throws a TypeError

```
  File "/home/shammer/Canonical/git/tools/ubuntu-old-fashioned/scripts/ubuntu-bartender/ibm-provider.py", line 34, in <module>
    delete(args.id)
  File "/home/shammer/Canonical/git/tools/ubuntu-old-fashioned/scripts/ubuntu-bartender/ibm-provider.py", line 22, in delete
    instance = ibm.get_instance(instance_id)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shammer/ubuntu-bartender-multipass.pedbPM1KcM/.venv/lib/python3.12/site-packages/pycloudlib/ibm/cloud.py", line 230, in get_instance
    return IBMInstance.find_existing(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shammer/ubuntu-bartender-multipass.pedbPM1KcM/.venv/lib/python3.12/site-packages/pycloudlib/ibm/instance.py", line 702, in find_existing
    return cls.from_existing(
           ^^^^^^^^^^^^^^^^^^
  File "/home/shammer/ubuntu-bartender-multipass.pedbPM1KcM/.venv/lib/python3.12/site-packages/pycloudlib/ibm/instance.py", line 675, in from_existing
    floating_ip = kwargs.pop("floating_ip", None) or cls._discover_floating_ip(client, instance)
                                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/shammer/ubuntu-bartender-multipass.pedbPM1KcM/.venv/lib/python3.12/site-packages/pycloudlib/ibm/instance.py", line 773, in _discover_floating_ip
    nic_id = instance["primary_network_interface"]["id"]
             ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'DetailedResponse' object is not subscriptable
```


## Test Steps

Create an instance and perform a get_instance

```python
import pycloudlib

ibm = pycloudlib.IBM("test")
imge = ibm.daily_image(release="noble")
instance = ibm.launch(image_id=daily, instance_type="bx3d-2x10", name="test")
id = instance._instance["id"]
instance_find = ibm.get_instance(id)  # previously would throw an exception here
```
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

Successfully merging this pull request may close these issues.

2 participants