Can't append with Error on Python Code

Lucky Tham
Lucky Tham used Ask the Experts™
on
Hi,

how to solve this error. ps help. Tks.

line 68, in <module>
    volume_id.Apppend(result['volume']['id'])
AttributeError: 'list' object has no attribute 'Apppend'


i need more than one system-volumes but created on volume only in the cloud platform. Ps advice. Tks


start_number = 1
number_of_volumes = 10
volume_id = []

for i in range(number_of_volumes):
    vol_no = start_number + i
    create_volume_url = "https://domain.com/v2/cd088007d3b84e7fa894478e6fe667c4/volumes"
    body = {"volume":
        {
            "size": 60,
            "availability_zone": "az0.dc0",
            "volume_type": "ssd",
            "name": "vol" + str(vol_no),
            "multiattach": False,
            "imageRef": "c54d05fa-5ad8-425e-be56-e60ede395230"
        }
    }
    headers = {
        'content-type': "application/json",
        'X-Auth-Token': token
    }

    result = requests.post(create_volume_url, json=body, headers=headers, verify=False)
    volume_id.apppend(result['volume']['id'])
    print(result.json())
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NorieAnalyst Assistant

Commented:
Python is case-sensitive, that error message mentions 'Append'.

Are you sure you haven't used 'Append' instead of 'append' somewhere in the code?
Lucky ThamFreelancer

Author

Commented:
yes. is the same error no matter i tried "append" or "Append" is the same error.
NorieAnalyst Assistant

Commented:
Is this definitely the line of code causing the error?
    volume_id.apppend(result['volume']['id'])

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Lucky ThamFreelancer

Author

Commented:
i am not quite sure just based on the error message. Can you shine some light? Tks.
NorieAnalyst Assistant

Commented:
Does this work?
    volume_id += [result['volume']['id']]

Open in new window

Lucky ThamFreelancer

Author

Commented:
no. tks

line 69, in <module>
    volume_id += [result['volume']['id']]
TypeError: 'Response' object is not subscriptable
NorieAnalyst Assistant

Commented:
How about without the outside []?
 volume_id +=  result['volume']['id']

Open in new window

Lucky ThamFreelancer

Author

Commented:
still the same. tks

line 70, in <module>
    volume_id += result['volume']['id']
TypeError: 'Response' object is not subscriptable
NorieAnalyst Assistant

Commented:
What happens if you save and close the file, without the changes I suggested, and then try it again?
Lucky ThamFreelancer

Author

Commented:
save all the files. restarted pycharm but still the same error. tks
Lucky ThamFreelancer

Author

Commented:
could it be require times to create the system volumes from the cloud. But it shouldn't be like this. When use POSTMAN to create one system volume and the response is 1550 ms and check the system volume still creating on the cloud platform.

Could it be this is the reason?

How to add a delay to the python code?

Tks.
NorieAnalyst Assistant

Commented:
If you want to add a delay you could try using sleep, not sure if it'll help though.
from time import sleep

# other code

# wait for a second
sleep(1)

# continue

Open in new window

Lucky ThamFreelancer

Author

Commented:
Thanks. Still can't
nociSoftware Engineer
Distinguished Expert 2018

Commented:
the error message indicated apppend (3 p's)  is used not append (2 p's) which should be available in a list
NorieAnalyst Assistant

Commented:
Doh, can't believe I missed that.
Lucky ThamFreelancer

Author

Commented:
is this okay. Tks.

2.jpg
Lucky ThamFreelancer

Author

Commented:
sorry for my typo error and copied over without checking.
Lucky ThamFreelancer

Author

Commented:
# Store the urls we want to use
url = "https://ip/silvan/apigateway/v1.0/"
get_apis = "apis_include_throttles"

get_token_url = "https://iam-apigateway-proxy.domain/v3/auth/tokens"

body = {
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "domain": {
                        "name": "STIC"
                    },
                    "name": "admin",
                    "password": "P@ssw0rd"
                }
            }
        },
        "scope": {
            "project": {
                "id": "cd088007d3b84e7fa894478e6fe667c4",
                "domain": {
                    "name": "SITC"
                }
            }
        }
    }
}

results = requests.post(get_token_url, json=body, verify=False)

token = results.headers['X-Subject-Token']

#print(results.headers)

start_number = 2
number_of_volumes = 2
volume_id = []

for i in range(number_of_volumes):
    vol_no = start_number + i
    create_volume_url = "https://evs.domain.com/v2/cd088007d3b84e7fa894478e6fe667c4/volumes"
    body = {"volume":
        {
            "size": 60,
            "availability_zone": "az0.dc0",
            "volume_type": "ssd",
            "name": "SITC-v" + str(vol_no),
            "multiattach": False,
            "imageRef": "c54d05fa-5ad8-425e-be56-e60ede395230"
        }
    }
    headers = {
        'content-type': "application/json",
        'X-Auth-Token': token
    }

    result = requests.post(create_volume_url, json=body, headers=headers, verify=False)
    volume_id.append(result['volume']['id'])
    print(result.json())


print(volume_id)
NorieAnalyst Assistant

Commented:
How about this?

result = requests.post(create_volume_url, json=body, headers=headers, verify=False)

response_data = result.json()

volume_id.append(response_data['volume']['id'])

Open in new window

Lucky ThamFreelancer

Author

Commented:
just in case the code may have error so copy whole thing.  I think should be oaky just that the append reporting issue as i can still create one VM on the cloud platform except I can't create more than 2 VM.

Thanks for helping!
Lucky ThamFreelancer

Author

Commented:
Thanks. Norie. You are great.

Thanks everyone helping.
Lucky ThamFreelancer

Author

Commented:
May I know what is wrong to the logic? Tks.
Lucky ThamFreelancer

Author

Commented:
can ask is there a better way to delete all the volume id being created from the above code without manually copy one by one to paste it below to delete it. Tks  

# POST to the API
results = requests.post(get_token_url, json=body, verify=False)
token = results.headers['X-Subject-Token']

#Deletion

volume_id = [
    '2336ed71-5d8f-410a-9dc7-01b452ab6ad9',
    '3cf77fd9-8ef8-4c4f-82ec-b249eb2e8df0',
    '65115cf6-743c-4225-83c7-451095e58ff0',
    'f09d0fd8-ae1c-45f7-94b0-d5436a80748e',
    'eb3d9e0a-1e10-4acb-bc55-6ce83344c625',
    '32d2106b-d8cd-4e4b-91a3-e33ade200ac3'
]

delete_url = "https://evs.domain.com/v2/cd088007d3b84e7fa894478e6fe667c4/volumes/"
headers = {
    'content-type': "application/json",
    'X-Auth-Token': token
}
for id in volume_id:
    r = requests.delete(delete_url + id, headers=headers, verify=False)
    print(r.text)
Lucky ThamFreelancer

Author

Commented:
I hope someone can help for the one-to-one live support (pay as a service) from Expert Exchange.

I need some help for coding just to perform this simple item as follow.

1. create 1000 VMs for script 1
2. delete all the system volumes for script 2
3. delete all the VM for script 3

This can help me to speed up the infra and API call testing on the cloud platform by this week and use this script as a template for future deployment.

Ps advice accordingly if there is any better suggestion from the above coding.

Just to take note that I'm not a developer or good programmer and i am a system administrator only and still learning to cope with this new job.

Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial