softlayer / softlayer-python

A set of Python libraries that assist in calling the SoftLayer API.
http://softlayer.github.io/softlayer-python/
MIT License
152 stars 193 forks source link

Minimum Example Request #1560

Closed PKehnel closed 3 years ago

PKehnel commented 3 years ago

Hey,

currently struggling to use the function: addDedicatedHostAccess https://sldn.softlayer.com/reference/services/SoftLayer_User_Customer/addDedicatedHostAccess/

Thought maybe I can get a minimum example here or a hint where I can find support / examples for functions like this.

My first guess was somewhere along the lines:

 resourceObject = [
            {
                "dedicatedHostId": "xyz",
            }
        ]
        client["SoftLayer_User_Customer"].addDedicatedHostAccess(
            resourceObject,
            # dedicatedHostId=xyt, or this instead of resource Object
            id=user_id,
        )

If I follow the documentation strictly, all I need is the dedicatedHostId, but I guess I also need a user_id?

ATGE commented 3 years ago

Hi @PKehnel , as the dedicatedHostId parameter is an integer identifier, you just need to pass it, like below:

    user_id = 12345
    dedicated_host_id = 654321
    result = client['SoftLayer_User_Customer'].addDedicatedHostAccess(dedicated_host_id, id=user_id)
    print(result)

Also, If you ever find yourself wishing there was an example of how to do something in the SoftLayer API, please make a github issue on the githubio_source repository.

PKehnel commented 3 years ago

Hey @ATGE,

thanks for the fast response and the link to the other repo. This is one of the users:

user

When running the suggested code:

        dedicated_host_id = 443490
        user_id = user["id"]
        client["SoftLayer_User_Customer"].addDedicatedHostAccess(
            dedicated_host_id,
            id=user_id,
        )

I get following error:

SoftLayer.exceptions.SoftLayerAPIError: SoftLayerAPIError(SoftLayer_Exception_Public): DedicatedHost 443490 not found.

My assumption was, that the _dedicated_host_id = devices.id from the cloud portal, which matches with the screenshot above.

Above user was generated via (first user):

all_users = client.call(
        "SoftLayer_Account",
        "getUsers",
        mask=objectMask,
        iter=False,
        limit=2,
        offset=35,
    )
ATGE commented 3 years ago

@PKehnel , looks like the id 443490 is a hardware server identifier, and the SoftLayer_User_Customer::addDedicatedHostAccess, just works with a dedicated host identifier, you can get the valid dedicated hosts with SoftLayer_Account::getDedicatedHosts, so like the id 443490 is a hardware server, try with :

    user_id = user["id"]
    hardware_id = 443490
    result = client['SoftLayer_User_Customer'].addHardwareAccess(hardware_id, id=user_id)
    print(result)
PKehnel commented 3 years ago

Thanks a lot @ATGE.

Works like a charm.