Exemplo n.º 1
0
def reserve_floating_ip(network_interface_id):

    # Required payload for reserving a floating ip
    payload = f'''
        {{
            "target": {{
                "id": "{network_interface_id}"
            }}
        }}
    '''

    try:
        # Connect to api endpoint for reserving a floating ip
        conn.request("POST", "/v1/floating_ips?version=" + version, payload, headers)

        # Get and read response data
        res = conn.getresponse()
        data = res.read()

        # Print and return response data
        print_json(data.decode("utf-8"))
        return data.decode("utf-8")

    # If an error happens while reserving a floating ip
    except Exception as error:
        print(f"Error reserving floating ip. {error}")
        raise
def create_security_group(name, protocol, direction, vpc_id):

    # Required payload for creating a security group
    payload = f'''
        {{
            "name": "{name}",
            "rules": [{{
                "protocol": "{protocol}",
                "direction": "{direction}"
            }}],
            "vpc": {{
                "id": "{vpc_id}"
            }}
        }}
    '''

    try:
        # Connect to rias endpoint for creating a security group
        conn.request("POST", "/v1/security_groups?version=" + version, payload,
                     headers)

        # Get and read response data
        res = conn.getresponse()
        data = res.read()

        # Print and return response data
        print_json(data.decode("utf-8"))
        return data.decode("utf-8")

    # If an error happens while creating a security group
    except Exception as error:
        print(f"Error creating a security group. {error}")
        raise
Exemplo n.º 3
0
def createSSHKey(name, public_key, key_type):

    # Required payload for creating an ssh key
    payload = f'''
        {{
            "name": "{name}",
            "public_key": "{public_key}",
            "type": "{key_type}"
        }}
    '''

    try:
        # Connect to api endpoint for ssh keys
        conn.request("POST", "/v1/keys?version=" + version, payload, headers)

        # Get and read response data
        res = conn.getresponse()
        data = res.read()

        # Print and return response data
        print_json(data.decode("utf-8"))
        return data.decode("utf-8")

    # If an error happens while creating an ssh key
    except Exception as error:
        print(f"Error creating SSH key. {error}")
        raise
Exemplo n.º 4
0
def create_subnet(name, vpc_id, total_ipv4_address_count, zone):

    # Required payload for creating a subnet
    payload = f'''
        {{
            "name": "{name}",
            "vpc": {{
                "id": "{vpc_id}"
            }},
            "total_ipv4_address_count": {total_ipv4_address_count},
            "zone": {{
                "name": "{zone}"
            }}
        }}
    '''

    try:
        # Connect to api endpoint for subnets
        conn.request("POST", "/v1/subnets?version=" + version, payload,
                     headers)

        # Get and read response data
        res = conn.getresponse()
        data = res.read()

        # Print and return response data
        print_json(data.decode("utf-8"))
        return data.decode("utf-8")

    # If an error happens while creating a subnet
    except Exception as error:
        print(f"Error creating subnet. {error}")
        raise
Exemplo n.º 5
0
def create_instance(image, name, profile, key_id, port_speed, subnet_id, vpc_id, zone):

    # Required payload for creating an instance
    payload = f'''
        {{
            "image": {{
                "id": "{image}"
            }},
            "name": "{name}",
            "profile": {{
                "name": "{profile}"
            }},
            "keys": [{{
                "id": "{key_id}"
            }}],
            "primary_network_interface": {{
                "port_speed": {port_speed},
                "subnet": {{
                    "id": "{subnet_id}"
                }}
            }},
            "vpc": {{
                "id": "{vpc_id}"
            }},
            "zone": {{
                "name": "{zone}"
            }}
        }}
    '''

    try:
        # Connect to rias endpoint for instances
        conn.request("POST", "/v1/instances?version=" + version, payload, headers)

        # Get and read response data
        res = conn.getresponse()
        data = res.read()
        
        # Print and return response data
        print_json(data.decode("utf-8"))
        return data.decode("utf-8")
    
    # If an error happens while creating an instance
    except Exception as error:
        print(f"Error creating instance. {error}")
        raise
Exemplo n.º 6
0
def fetch_images():

    payload = ""

    try:
        # Connect to api endpoint for images
        conn.request("GET", "/v1/images?version=" + version, payload, headers)

        # Get and read response data
        res = conn.getresponse()
        data = res.read()

        # Print and return response data
        print_json(data.decode("utf-8"))
        return data.decode("utf-8")

    except Exception as error:
        print(f"Error fetching Images. {error}")
        raise
Exemplo n.º 7
0
def fetch_floating_ips():

    payload = ""

    try:
        # Connect to api endpoint for floating ips
        conn.request("GET", "/v1/floating_ips?version=" + version, payload, headers)

        # Get and read response data
        res = conn.getresponse()
        data = res.read()

        # Print and return response data
        print_json(data.decode("utf-8"))
        return data.decode("utf-8")

    # If an error happens while fetching floating ips
    except Exception as error:
        print(f"Error fetching floating ips. {error}")
        raise
Exemplo n.º 8
0
def create_vpc(name):

    # Required payload for creating a VPC
    payload = f'{{"name": "{name}"}}'

    try:
        # Connect to rias endpoint for vpcs
        conn.request("POST", "/v1/vpcs?version=" + version, payload, headers)

        # Get and read response data
        res = conn.getresponse()
        data = res.read()

        # Print and return response data
        print_json(data.decode("utf-8"))
        return data.decode("utf-8")

    # If an error happens while creating a VPC
    except Exception as error:
        print(f"Error creating VPC. {error}")
        raise