Instance operations can be performed with the SkyAtlas admin interface. Skyatlas API can provide:

  • Automating the infrastructure completely,

  • Server on-off operations,

  • Block storage or managing entire network services with the application.

 

All operations that can be done from SkyAtlas admin interface can be done with SkyAtlas API. It is necessary to complete certain steps to open an instance. There is no need to repeat all the steps to open 20 instances instead of 1 with the same features. In such cases, transactions are automated using API. The source published by OpenStack is used for the API.

Basic instance operations that can be done with SkyAtlas API:

  • Creating an instance,

  • Detailed list of instances,

  • Removing an instance,

  • Updating the instance,

  • Instance reboot.

Actions that can be done with HTTP Requests

 

This document includes the operations that can be done in the API with HTTP requests. The steps that can be done to use the API are as follows. To use these codes;

 

  • requests

  • json

  • os

  • time

 

libraries are installed and should be included in the code. Do not forget to install these libraries, then continue. After installing the necessary libraries, you must have various permissions.

 

To be logged in:

 

In order for the user to use the features on Openstack, she/he must login with the access information transmitted by SkyAtlas. The user must use his/her username, password and tenant ID information to access his/her account via the API.

 

 

Code Block ‖ Language = py

 

credentials = {"auth": {
    "passwordCredentials":{
        "username":"<email>",
        "password":"<password>"
    },
    "tenantId":"<tenant_id>"
}
}
response = requests.post("http://185.48.212.8:5000/v2.0/tokens", data=json.dumps(credentials), headers={"content-type":"application/json"})
response_dict = response.json()
token_id = response_dict["access"]["token"]["id"]
CODE

After login you will be able to run API codes. Now we can work creating instances. We cant access to instance via Internet when we first created. These are steps that should followed:

  • Creating Network

  • Creating Subnet

  • Creating Router

  • Adding Interface

  • Creating Security Rules

  • Create&Add Key Pair

  • Creating Port

  • Adding Public IP

  • Creating Images

  • Creating Flavor

  • Creating Instance

  • Assigning Public IP to Instance

Creation Process

Creating Network:

To start an instance (server) and connect, we must first create a network. With the created network, more than one instance will be able to communicate over this network. The following commands should be used to create a network: When creating a network name, use a name that does not contain unicode and Turkish characters.

 


network_dict = {
    "network": {
        "name": "<network_name>",
        "admin_state_up": True
    }
}
network_response=requests.post("http://185.48.212.8:9696/v2.0/networks",
                      headers = {"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(network_dict))
CODE

After creating the network, we can proceed to the subnet creation step.

Creating Subnet:

The following commands should be used to create subnet. We use the network id we created when creating subnet.


subnet_dict = {
    "subnet": {
        "network_id": network_id,
        "name": "<subnet_name>",
        "ip_version": 4,
        "cidr": "10.0.0.1/24",
        "gateway_ip": "10.0.0.1"
    }
}
subnet_response=requests.post("http://185.48.212.8:9696/v2.0/subnets",
                              headers={"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(subnet_dict))
CODE

After creating a subnet, we can proceed to the router creation step.

Creating Router:

A router is needed for instances in different networks to exchange information with each other. The following commands should be used to create a router: It should be noted that an external network id should be given to the network id.

router_dict = {
    "router": {
        "name": "<router_name",
        "external_gateway_info": {
            "network_id": external_network_id
        },
        "admin_state_up": True
    }
}
router_response=requests.post("http://185.48.212.8:9696/v2.0/routers",
                       headers = {"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(router_dict))
CODE

After creating the router, you can proceed to the interface creation step.

Creating Interface:

The following commands should be used to create the interface: We use the id of the subnet we created above to create an interface.

interface_dict = {
    "subnet_id": subnet_id
}
interface_response = requests.put("http://185.48.212.8:9696/v2.0/routers/"+router_id+"/add_router_interface",
                        headers={"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(interface_dict))
CODE

To access and connect to the Instance to be created, the security group creation process must be done before the Instance is created.

Creating Security Group:

The following commands should be used to create a security group:

security_group_response=requests.get("http://185.48.212.8:9696/v2.0/security-groups",
                      headers={"x-auth-token": token_id, "content-type":"application/json"})
CODE

From which ports the servers are required to be allowed access, they can be added under this group.

Adding Rule:

We can add rules that will enable the instance to access inward and outward. For example, we need to add the SSH rule to access linux instances via SSH via port 22.

rules_dict = {
    "security_group_rule": {
        "direction": "ingress",
        "port_range_min": "22",
        "ethertype": "IPv4",
        "port_range_max": "22",
        "protocol": "tcp",
        "security_group_id": security_group_id
    }
}
 
rules_response=requests.post("http://185.48.212.8:9696/v2.0/security-group-rules",
                      headers={"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(rules_dict))
CODE

Adding Keypair:

Keypair must be added for secure connection to Linux servers. In order to add a keypair, a public key must first be created from the local system. This public key is given to the public key parameter. While creating a Keypair name, it is important to use names that do not contain unicode and Turkish characters without leaving spaces.

home_directory = os.getenv("HOME")
key = open(home_directory + "/.ssh/id_rsa.pub")
public_key = key.read()
 
keypair_dict = {
    "keypair": {
        "name": "<keypair_name>",
        "public_key": public_key
    }
}
keypair_response=requests.post("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/os-keypairs",
                      headers={"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(keypair_dict))
CODE

The keypair we have created will be used to connect to a server with a public IP via ssh.

Creating Port:

port_dict = {
    "port": {
        "network_id": network_id,
        "name": "<port_name>",
        "admin_state_up": True
    }
}
port_response=requests.post("http://185.48.212.8:9696/v2.0/ports",
                            headers={"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(port_dict))
CODE

Creating Public IP:

We need to create a public ip to access the instance to be created over the internet. The following commands should be used to create a public ip:

floating_dict = {
    "floatingip": {
        "floating_network_id": external_network_id,
        "port_id": port_id
    }
}
floating_response=requests.post("http://185.48.212.8:9696/v2.0/floatingips",
                                headers={"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(floating_dict))
CODE

Creating Images:

We choose which operating system the server will use by creating images. We created Ubuntu here. CentOS, Debian, Fedora and Windows can also be used as required.

images_response = requests.get("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/images",
                               headers={"x-auth-token": token_id})
images = images_response.json()["images"]
 
for image in images:
    if image["name"] ==  "Ubuntu Trusty":
        image_id = image["id"]
CODE

Creating Flavor:

While creating a flavor, we determine the size of the server we will open. We can create servers in sizes S, M, L, XL and XXL.

flavor_response = requests.get("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/flavors",
                               headers = {"x-auth-token": token_id})
 
flavors = flavor_response.json()["flavors"]
for flavor in flavors:
    if flavor["name"] == "General S":
        flavor_id = flavor["id"]
CODE

Creating Server:

To create a server, a server name, image id, flavor id, keypair name and network id parameters should be given as shown below.

server_info = {
    "server": {
        "name":"<server_name>",
      "imageRef": image_id,
      "flavorRef": flavor_id,
        "key_name": "<keypair_name>",
        "networks": [
            {
                "uuid": network_id
            }
        ]
    }
}
 
compute_response = requests.post("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/servers",
                                 headers = {"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(server_info))
CODE

Adding Public IP to Instance:

In order to access a server over the internet, any ip address parameter that has previously added floating strings is given.

floating_ip_dict = {
    "addFloatingIp": {
        "address": floating_ip
    }
}
 
 
response=requests.post("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/servers/"+server_id+"/action",
                      headers={"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(floating_ip_dict))
CODE

With these steps, we created a server that can be connected via SSH.

Building an Externally Accessible Server from Scratch:

You can perform the steps we separated in the previous title with a single code in this section.

import requests
import json
import os
import time

credentials = {"auth": {
    "passwordCredentials":{
        "username":"<email>",
        "password":"<password>"
    },
    "tenantId":"<tenant_id>"
}
}
response = requests.post("http://185.48.212.8:5000/v2.0/tokens", data=json.dumps(credentials), headers={"content-type":"application/json"})
response_dict = response.json()
token_id = response_dict["access"]["token"]["id"]
network_dict = {
    "network": {
        "name": "<network_name>",
        "admin_state_up": True
    }
}
print "Creating Network"
network_response=requests.post("http://185.48.212.8:9696/v2.0/networks",
                      headers = {"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(network_dict))

network_id = network_response.json()["network"]["id"]

subnet_dict = {
    "subnet": {
        "network_id": network_id,
        "name": "<subnet_name>",
        "ip_version": 4,
        "cidr": "10.0.0.1/24",
        "gateway_ip": "10.0.0.1"
    }
}
print "Creating Subnet"
subnet_response=requests.post("http://185.48.212.8:9696/v2.0/subnets",
                              headers={"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(subnet_dict))

subnet_id = subnet_response.json()["subnet"]["id"]

list_network_response=requests.get("http://185.48.212.8:9696/v2.0/networks",
                      headers={"x-auth-token": token_id, "content-type":"application/json"})
networks = list_network_response.json()["networks"]
for network in networks:
    if network["router:external"]:
        external_network_id = network["id"]

router_dict = {
    "router": {
        "name": "<router_name",
        "external_gateway_info": {
            "network_id": external_network_id
        },
        "admin_state_up": True
    }
}

print "Creating Router"
router_response=requests.post("http://185.48.212.8:9696/v2.0/routers",
                       headers = {"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(router_dict))

router_id = router_response.json()["router"]["id"]

interface_dict = {
    "subnet_id": subnet_id
}
print "Creating Interface"
interface_response = requests.put("http://185.48.212.8:9696/v2.0/routers/"+router_id+"/add_router_interface",
                        headers={"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(interface_dict))
print "Creating Security Group"
security_group_response=requests.get("http://185.48.212.8:9696/v2.0/security-groups",
                      headers={"x-auth-token": token_id, "content-type":"application/json"})
security_group_id = security_group_response.json()["security_groups"][0]["id"]

rules_dict = {
    "security_group_rule": {
        "direction": "ingress",
        "port_range_min": "22",
        "ethertype": "IPv4",
        "port_range_max": "22",
        "protocol": "tcp",
        "security_group_id": security_group_id
    }
}

rules_response=requests.post("http://185.48.212.8:9696/v2.0/security-group-rules",
                      headers={"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(rules_dict))

home_directory = os.getenv("HOME")
key = open(home_directiry + "/.ssh/id_rsa.pub")
public_key = key.read()

keypair_dict = {
    "keypair": {
        "name": "<keypair_name>",
        "public_key": public_key
    }
}
print "Creating Keypair"
keypair_response=requests.post("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/os-keypairs",
                      headers={"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(keypair_dict))

port_dict = {
    "port": {
        "network_id": network_id,
        "name": "<port_name>",
        "admin_state_up": True
    }
}
print "Creating Port"
port_response=requests.post("http://185.48.212.8:9696/v2.0/ports",
                            headers={"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(port_dict))
port_id = port_response.json()["port"]["id"]


floating_dict = {
    "floatingip": {
        "floating_network_id": external_network_id,
        "port_id": port_id
    }
}
floating_response=requests.post("http://185.48.212.8:9696/v2.0/floatingips",
                                headers={"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(floating_dict))

floating_ip = floating_response.json()["floatingip"]["floating_ip_address"]

print "Creating Images"
images_response = requests.get("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/images",
                               headers={"x-auth-token": token_id})
images = images_response.json()["images"]

for image in images:
    if image["name"] ==  "Ubuntu Trusty":
        image_id = image["id"]
print "Creating Flavor"
flavor_response = requests.get("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/flavors",
                               headers = {"x-auth-token": token_id})

flavors = flavor_response.json()["flavors"]
for flavor in flavors:
    if flavor["name"] == "General S":
        flavor_id = flavor["id"]

print "Creating Server"

server_info = {
    "server": {
        "name":"<server_name>",
      "imageRef": image_id,
      "flavorRef": flavor_id,
        "key_name": "<keypair_name>",
        "networks": [
            {
                "uuid": network_id
            }
        ]
    }
}

compute_response = requests.post("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/servers",
                                 headers = {"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(server_info))
server_id = compute_response.json()["server"]["id"]
server_ready = False
while not server_ready:
    time.sleep(5)
    print "Waiting for Server Ready"
    compute_detail_response = requests.get("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/servers/"+server_id,
                                 headers = {"x-auth-token": token_id})
    status = compute_detail_response.json()["server"]["status"]
    server_ready = (status == "ACTIVE")


print "Creating Floating Ip"

floating_ip_dict = {
    "addFloatingIp": {
        "address": floating_ip
    }
}


response=requests.post("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/servers/"+server_id+"/action",
                      headers={"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(floating_ip_dict))
CODE

Deletion Operations

The following command lines are run to delete the existing routers, networks and servers.

import requests
import json
import pprint

credentials = {"auth": {
    "passwordCredentials":{
        "username":"<email>",
         "password":"<password>"
      },
      "tenantId":"<tenant_id>"
      }

}
response = requests.post("http://185.48.212.8:5000/v2.0/tokens", data=json.dumps(credentials),
                         headers = {"content-type":"application/json"})
response_dict = response.json()
token_id = response_dict["access"]["token"]["id"]

response_floatingIp_list = requests.get("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/os-floating-ips",
                                             headers = {"x-auth-token": token_id})
for i in response_floatingIp_list.json()["floating_ips"]:
    deallocate_floatingIp = requests.delete("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/os-floating-ips/"+i["id"],
                                             headers = {"x-auth-token": token_id, "content-type":"application/json"})

response_port_list = requests.get("http://185.48.212.8:9696/v2.0/ports",
                                  headers={"x-auth-token": token_id})
response_ports = response_port_list.json()["ports"]
response_router_list = requests.get("http://185.48.212.8:9696/v2.0/routers",
                                    headers = {"x-auth-token": token_id})
for port in response_ports:
    fixed_ips = port["fixed_ips"]
    for fixed_ip in fixed_ips:
        subnet_id = fixed_ip["subnet_id"]

        interface_dict = {
            "subnet_id": subnet_id
        }
        for router in response_router_list.json()["routers"]:
            requests.put("http://185.48.212.8:9696/v2.0/routers/"+router["id"]+"/remove_router_interface",
                                                headers = {"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(interface_dict))

for i in response_router_list.json()["routers"]:
    delete_router = requests.delete("http://185.48.212.8:9696/v2.0/routers/"+i["id"],
                                    headers={"x-auth-token": token_id, "content-type":"application/json"})
for i in response_port_list.json()["ports"]:
    delete_ports = requests.delete("http://185.48.212.8:9696/v2.0/ports/"+i["id"],
                                   headers={"x-auth-token": token_id})
    pprint.pprint(delete_ports.text)

response_subnet_list = requests.get("http://185.48.212.8:9696/v2.0/subnets",
                                  headers={"x-auth-token": token_id})

for i in response_subnet_list.json()["subnets"]:
    delete_subnet = requests.delete("http://185.48.212.8:9696/v2.0/subnets/"+i["id"],
                                   headers={"x-auth-token": token_id})
    pprint.pprint(delete_subnet.text)

response_network_list = requests.get("http://185.48.212.8:9696/v2.0/networks",
                      headers={"x-auth-token": token_id})


for i in response_network_list.json()["networks"]:
    delete_network = requests.delete("http://185.48.212.8:9696/v2.0/networks/"+i["id"],headers={"x-auth-token": token_id} )
    pprint.pprint(delete_network.text)


response_list_servers=requests.get("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/servers",
                      headers = {"x-auth-token": token_id})
for i in response_list_servers.json()["servers"]:
    delete_servers = requests.delete("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/servers/"+i["id"],
                                     headers={"x-auth-token": token_id})
CODE

Deleting a Server:

compute_response = requests.delete("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/servers/<"sunucu_id>",
                                   headers={"x-auth-token": token_id, "content-type":"application/json"})
CODE

Listing Operations

Image Listing:

images_response = requests.get("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/images",
	headers={"x-auth-token": token_id})
CODE

Detailed Listing of Desired Server:

response=requests.get("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/servers/<server_id>", 
	headers = {"x-auth-token": token_id}) 
CODE

Keypair Listing:

response=requests.get("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/os-keypairs",
	headers = {"x-auth-token": token_id, "content-type":"application/json"})
CODE

Flavor Listing:

flavor_response = requests.get("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/flavors",
	headers={"x-auth-token": token_id})
CODE

Network Listing:

response=requests.get("http://185.48.212.8:9696/v2.0/networks",
	headers = {"x-auth-token": token_id, "content-type":"application/json"})
CODE

Port Listing:

response_port_list = requests.get("http://185.48.212.8:9696/v2.0/ports",
    headers={"x-auth-token": token_id, "content-type":"application/json"})
CODE

Security Group Listing:

 

response=requests.get("http://185.48.212.8:9696/v2.0/security-groups",
    headers = {"x-auth-token": token_id, "content-type":"application/json"})
CODE

Listing Servers:

response=requests.get("https://compute.istanbul-1.endpoint.skyatlas.com/v2/6<tenant_id>/servers", 
	headers = {"x-auth-token": token_id})
CODE

Listing Rules:

response=requests.get("https://compute.istanbul-1.endpoint.skyatlas.com/v2/6<tenant_id>/os-security-groups-default-rules",
    headers = {"x-auth-token": token_id, "content-type":"application/json"})
CODE

Listing Details of Servers:

response = requests.get("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/servers/detail",
                      headers={"x-auth-token": token_id}) 

pprint.pprint(response.json()) 
CODE

Listing Details of the Desired Server:

response=requests.get("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/servers/<server_id>", 
	headers = {"x-auth-token": token_id}) 
CODE

Update Procedures

Instance Update:

servers = {
    "server": {
        "name": "<yeni_isim>"
    }
}

response=requests.put("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/servers/<server_id>", 
                 headers={"content-type":"application/json","x-auth-token": token_id}, data=json.dumps(servers))
CODE

Password Update:

server_info = {
    "changePassword": {
        "adminPass": "<new_password>"
    }
}

compute_response = requests.post("https://compute.istanbul-1.endpoint.skyatlas.com/v2/6a97dceaa064484eb2be90867684bf7b/servers/ba4fb7c8-5625-4820-aaf7-f6a01a72bb28/action", headers = {"x-auth-token": token_id, "content-type":"application/json"}, data=json.dumps(server_info))
CODE

Reboot Server:

servers = {
    "reboot": {
        "type": "SOFT"
    }
}
response=requests.post("https://compute.istanbul-1.endpoint.skyatlas.com/v2/<tenant_id>/servers/<server_id>/action", 
	headers = {"content-type":"application/json","x-auth-token": token_id}, data=json.dumps(servers)) 
CODE

Basic Server Operations with Python Novaclient

Creating a Server:

from keystoneclient.auth.identity import v2
from keystoneclient import session
from novaclient.v2 import client
auth = v2.Password(auth_url="http://185.48.212.8:5000/v2.0",
                   username="<email>",
                   password="<password>",
                   tenant_id="<tenant_id>")
sess = session.Session(auth=auth)
nova = client.Client("http://185.48.212.8:5000/v2.0", session=sess)

fl = nova.flavors.find(ram=2048)
img = nova.images.find(name="Ubuntu Trusty")
nova.servers.create("my-server", flavor=fl, image=img)
CODE

Server Listing:

from keystoneclient.auth.identity import v2
from keystoneclient import session
from novaclient.v2 import client
auth = v2.Password(auth_url="http://185.48.212.8:5000/v2.0",
                   username="<email>",
                   password="<password>",
                   tenant_id="<tenant_id>")
sess = session.Session(auth=auth)
nova = client.Client("http://185.48.212.8:5000/v2.0", session=sess)
for i in nova.servers.list(detailed=True) :
    print i.id, i.name, i.status
CODE

Server Update:

from keystoneclient.auth.identity import v2
from keystoneclient import session
from novaclient.v2 import client
auth = v2.Password(auth_url="http://185.48.212.8:5000/v2.0",
                   username="<email>",
                   password="<password>",
                   tenant_id="<tenant_id>")
sess = session.Session(auth=auth)
nova = client.Client("http://185.48.212.8:5000/v2.0", session=sess)
server = nova.servers.find(name="<eski_isim>")

server.update(name="<yeni_isim>")
CODE