Python SDK Authentication Modes

Python SDK supports two authentication modes: token-based authentication and AK/SK authentication.

Token Authentication

For details about the code for token-based authentication, see Table 1.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# -*- coding:utf-8 -*-
from openstack import connection

# create connection
username = "replace-with-your-username" # username
password = "replace-with-your-password" # user password
projectId = "replace-with-your-projectId"    # project ID
userDomainId = "replace-with-your-domainId"  # account ID
auth_url = "https://iam.example.com/v3"    # endpoint url
conn = connection.Connection(auth_url=auth_url,
                             user_domain_id=userDomainId,
                             project_id=projectId,
                             username=username,
                             password=password)

# set parameters
limit = 5

# define function for listing servers
def list_servers():
    # get server list with params
    servers = conn.compute.servers(limit=limit)
    # iterate servers list
    for server in servers:
        print(server)

# visit API
list_servers()
Table 1 Parameter description

Parameter

Description

Example Value

auth_url

Specifies the endpoint of the IAM service.

example in the https://iam.example.com/v3, indicates the Region.Cloud platform domain name. For details about the parameter, see here.

https://iam.cn-north-1.myhuaweicloud.com/v3

username

Specifies the IAM username. For details about how to obtain the username, see How Do I Obtain the IAM Username, Account ID, and Project ID?.

N/A

password

Specifies the IAM user password.

N/A

projectId

Specifies the project ID. For details about how to obtain the project ID, see How Do I Obtain the IAM Username, Account ID, and Project ID?.

N/A

userDomainId

Specifies the account ID. For details about how to obtain the account ID, see How Do I Obtain the IAM Username, Account ID, and Project ID?.

N/A

AK/SK Authentication

For details about the code for AK/SK authentication, see Table 2.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#encoding=utf-8

from openstack import connection

projectId = "***"
cloud = "myhuaweicloud.com"
region= "***"    # example: region = "cn-north-1"
AK = "***"
SK = "***"

conn = connection.Connection(
              project_id=projectId,
              cloud=cloud,
              region=region,
              ak = AK,
              sk = SK)

def test_compute():
    servers = conn.compute.servers(limit = 3)
    for server in servers:
        print server

if __name__ == "__main__":
    test_compute()
Table 2 Parameter description

Parameter

Description

Example Value

ak/sk

Specifies the AK/SK access key.

NOTE:
  • AK/SK generation description: Log in to the management console, choose My Credentials, and click Access Keys to create an AK and SK.
  • The time error between the AK/SK signature time and UTC time cannot exceed 15 minutes. Otherwise, the authentication fails.
  • If the AK/SK signature fails for more than five consecutive times, the AK/SK request of the source IP address is locked for 5 minutes.

N/A

project_id

Specifies the project ID. For details about how to obtain the project ID, see How Do I Obtain the IAM Username, Account ID, and Project ID?.

N/A

region

Specifies the region name.

cn-north-1

cloud

Specifies the cloud platform domain name.

myhuaweicloud.com