Python Error While Creating VM

Lucky Tham
Lucky Tham used Ask the Experts™
on
hi, i can get postman to POST to create a VM but using pycharm encountered this error.

code 1: obtain a token
==========================================================================
import requests
url = "https://IP/silvan/apigateway/v1.0/"
get_apis = "apis_include_throttles"

##API URLs
get_token_url = "https://iam-apigateway-proxy.domain.com/v3/auth/tokens"

create_volume_url = "https://evs.sitc-1.domain.com/v2/6d321dd88c7143ba8d6daf3e15f14be9/volumes"
delete_volume_url = "https://evs.stic-1.domain.com/v2/6d321dd88c7143ba8d6daf3e15f14be9/volumes/"
create_vm_url = "https://ecs.sitc-1.domain.com/v2/6d321dd88c7143ba8d6daf3e15f14be9/servers"



##Images Dictionary
images = {
    "Ubuntu 18.10": "5313ace4-4573-404b-abc6-8548ed14c4f7",
    "RHEL7.5-40G": "aa9d05f3-cb90-4776-9c02-617a9906b271",
    "WindowsServer2016WithGUI": "c54d05fa-5ad8-425e-be56-e60ede395230",
    "Windows10Pro": "29caef55-0617-4813-8a17-cb0bef19de16",
    "RHEL7.5": "c5ccd8a7-d8f3-4a4c-91c3-9d93303aee58",
    "Ubuntu16.04LTS": "3f8948fd-c108-48db-9951-1d617e8e5b03",
    "image-kvm-euler": "298e2912-5a7a-4178-8ac4-b260712d514c",
    "image-ManageOne": "80d9b0ee-a5b3-42fe-99ed-fc32c57da5b3",
    "esight_image": "e1e94234-f3e7-4793-8bdb-0cef9e3194cf"
}


def get_token():
    body = {
        "auth": {
            "identity": {
                "methods": [
                    "password"
                ],
                "password": {
                    "user": {
                        "domain": {
                            "name": "SITC"
                        },
                        "name": "deployment",
                        "password": "P@ssw0rd"
                    }
                }
            },
            "scope": {
                "project": {
                    "id": "6d321dd88c7143ba8d6daf3e15f14be9",
                    "domain": {
                        "name": "SITC"
                    }
                }
            }
        }
    }

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

    return token

def create_volume(name, token = ''):

    if len(token) == 0:
        token = get_token()

    body = {"volume":
        {
            "size": 60,
            "availability_zone": "az0.dc0",
            "volume_type": "ssd",
            "name": name,
            "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)
    return result.json()

def delete_volume(volume_id, token = ''):

    if len(token) == 0:
        token = get_token()

    headers = {
        'Content-Type': "application/json",
        'X-Auth-Token': token
    }

    res = requests.delete(delete_volume_url + volume_id, headers=headers, verify=False)
    if res.status_code == 202:
        return True
    else:
        return res



def create_vm(name, flavor, volume_id, network_id, iam_token):
    if len(iam_token) == 0:
        iam_token = get_token()

    body = {
        "server": {
            "flavorRef": "5313ace4-4573-404b-abc6-8548ed14c4f7",
            "name": name,
            "networks": [{
                "uuid": "2f963ced-11a4-4008-82c6-8cb1a230a785"
            }],
            "metadata": {
                "region_ID": "az0.dc0"
            },
            "adminPass": "123",
            "block_device_mapping_v2": [{
                "uuid": volume_id,
                "source_type": "volume",
                "destination_type": "volume",
                "delete_on_termination": "False",
                "boot_index": 0
            }],
            "availability_zone": "az0.dc0"
        }
    }

    headers = {
        'Content-Type': "application/json",
        'X-Auth-Token': iam_token
    }

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

Code 2: Create system volume with new ID created to output to cvs file
===================================================================================
from fusion import create_volume
from fusion import get_token

## Params that are used for configuration before running this program
output_file = "outputs/volumes_ids.csv"

number_of_volumes = 2
start_number = 1
zfil_width = 4

volume_ids = []
access_token = get_token()

for i in range(number_of_volumes):
    vol_no = start_number + i

    vol_name = "STIC-Vol-" + str(vol_no).zfill(zfil_width)
   
    res = create_volume(vol_name, access_token)
   
    ## TODO:
    ## Check json response for error
    ## Log error to some file

    single_volume_id = res['volume']['id']
    volume_ids.append(single_volume_id)


print("Total {0} created.".format(len(volume_ids)))

with open(output_file, 'w+') as output_csv:
    for vol_id in volume_ids:
        output_csv.write("{0}\n".format(vol_id))
=======================================================================================


code 3: create VM with error
============================================
import requests

url = "https://ecs.domain.com/v2/6d321dd88c7143ba8d6daf3e15f14be9/servers"

payload = "{\r\n\"server\": {\r\n\"flavorRef\": \"a80cfe5c-60c5-4d16-989a-56f47af273f2\",\r\n\"name\": \"IVH-VM-01\",\r\n\"networks\": [\r\n{\r\n\"uuid\": \"748c81c1-3c79-4974-884d-9311b7905c0d\"\r\n}\r\n],\r\n\"metadata\": {\r\n\"region_ID\": \"az0.dc0\"\r\n},\r\n\"adminPass\": \"Huawei@123\",\r\n\"block_device_mapping_v2\": [\r\n{\r\n\"uuid\": \"738bc8d4-86fe-40f2-8514-02f5a9967bdd\",\r\n\"source_type\": \"volume\",\r\n\"destination_type\": \"volume\",\r\n\"delete_on_termination\": \"False\",\r\n\"boot_index\": 0\r\n}\r\n],\r\n\"availability_zone\": \"az0.dc0\"\r\n}}"
headers = {
    'x-auth-token': "MIIElAYJKoZIhvcNAQcCoIIEhTCCBIECAQExDTALBglghkgBZQMEAgEwggL1BgkqhkiG9w0BBwGgggLmBIIC4nsidG9rZW4iOnsiZXhwaXJlc19hdCI6IjIwMTgtMTAtMjZUMTk6MjQ6MjkuMTc3MDAwWiIsIm1ldGhvZHMiOlsicGFzc3dvcmQiXSwiY2F0YWxvZyI6W10sInJvbGVzIjpbeyJuYW1lIjoidmRjX2FkbSIsImlkIjoiMjY4ZmQ1ZWU3NDE4NDc4Mjg4MTUwMWI5ZmJiMjEwZmYifSx7Im5hbWUiOiJ0ZV9hZG1pbiIsImlkIjoiMmVlMjdhZmJjNDE5NDkzYmE4OWQ2MWU3ZTg5YzFhMGEifSx7Im5hbWUiOiJ0YWdfYWRtIiwiaWQiOiI0MjE5MTA1OGMzNzM0MjcxYThlN2U1ODdmYzE1NWE0MCJ9LHsibmFtZSI6ImFwcHJvdl9hZG0iLCJpZCI6IjY5NmNmNjMyY2M4NTQ0MjlhMDA2OTY5MWJkMjNhM2NjIn0seyJuYW1lIjoidmRjX293bmVyIiwiaWQiOiJhZmVmMTg3OTE0MzY0MmJlYWZiMmE5YWVhNjRjYmM1ZCJ9XSwicHJvamVjdCI6eyJkb21haW4iOnsibmFtZSI6Ik5DU19JVkgiLCJpZCI6IjU4ZTVkYTRjZDhkNDQxMjg5Y2Y1OWQzZmViNGQ3MTBjIn0sIm5hbWUiOiJTVERfbmNzLTFfZGVwX2RlZmF1bHRfcHJvamVjdCIsImlkIjoiY2QwODgwMDdkM2I4NGU3ZmE4OTQ0NzhlNmZlNjY3YzQifSwiaXNzdWVkX2F0IjoiMjAxOC0xMC0yNVQxOToyNDoyOS4xNzcwMDBaIiwidXNlciI6eyJkb21haW4iOnsibmFtZSI6Ik5DU19JVkgiLCJpZCI6IjU4ZTVkYTRjZDhkNDQxMjg5Y2Y1OWQzZmViNGQ3MTBjIn0sIm5hbWUiOiJkZXBfYWRtaW4iLCJpZCI6ImI2NzRmNjNjZjc4ZDQ3YWY4YWQ5NWZkZDJlNjI5MmVhIn19fTGCAXIwggFuAgEBMEkwPTELMAkGA1UEBhMCQ04xDzANBgNVBAoTBkh1YXdlaTEdMBsGA1UEAxMUSHVhd2VpIElUIFByb2R1Y3QgQ0ECCBWpkSG6QZq3MAsGCWCGSAFlAwQCATANBgkqhkiG9w0BAQEFAASCAQAOwhktco+M1oyMd38szR40PrOa8eKNF7zDaKZDeWTzyjHboOWN0eiBd5DsW9e1MmP1fMZSa5HNSkGPBo0tOhMykXrYxL1BPf1j+-wh935zJNdPSEQvD2XuDpajt5PLDQdn-tb4sOWVJUD0ug7883em00f1Wjv4jffwrj58WeHtD9BQav468Uq65Ar9pjYbL4hBxfI+63z6LTP1xnw0zK3QJ1JdDluxymMwS-Wz17xIU7jkNvn70Fbl-y7qbAbR7+-bXDF07W3EAfvJzp0akrP596hc8yt6av8Mh7WwaTxZhbGgJoVBe6ClTB+jrmx2idYAhVjN7Qa6F7xWcDFcAIsn",
    'content-type': "application/json",
    'cache-control': "no-cache",
    'postman-token': "54db8c16-22b9-142e-ff6f-6143acf5de00"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)
================================================================================

error
====================================================================================
C:\Users\Owner\PycharmProjects\fusion\venv\Scripts\python.exe C:/Users/Owner/Desktop/try.py
Traceback (most recent call last):
  File "C:/Users/Owner/Desktop/try.py", line 13, in <module>
    response = requests.request("POST", url, data=payload, headers=headers)
  File "C:\Users\Owner\PycharmProjects\fusion\venv\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\Owner\PycharmProjects\fusion\venv\lib\site-packages\requests\sessions.py", line 523, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\Owner\PycharmProjects\fusion\venv\lib\site-packages\requests\sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\Owner\PycharmProjects\fusion\venv\lib\site-packages\requests\adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726)
Comment
Watch Question

Do more with

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

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