数据库安全服务 DBSS-添加RDS数据库:Python

时间:2024-11-04 16:36:10

Python

 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkdbss.v1.region.dbss_region import DbssRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkdbss.v1 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.getenv("CLOUD_SDK_AK")
    sk = os.getenv("CLOUD_SDK_SK")

    credentials = BasicCredentials(ak, sk) \

    client = DbssClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(DbssRegion.value_of("xx-xx")) \
        .build()

    try:
        request = AddRdsNoAgentDatabaseRequest()
        listDatabasesbody = [
            RdsNoAgentDbRequestDatabases(
                id="123751d3ee2f47aea64822e98318c6a8in01",
                db_name="rds1",
                status="ACTIVE",
                port="3306",
                ip="192.168.0.119",
                instance_name="rds1",
                version="8.0",
                type="MySQL",
                enterprise_id="0",
                enterprise_name="default"
            ),
            RdsNoAgentDbRequestDatabases(
                id="2343f7285d684fed8b09fac201c3fc7ain01",
                db_name="rds2",
                status="ACTIVE",
                port="3306",
                ip="192.168.0.92",
                instance_name="rds2",
                version="8.0",
                type="MySQL",
                enterprise_id="0",
                enterprise_name="default"
            )
        ]
        request.body = RdsNoAgentDbRequest(
            databases=listDatabasesbody
        )
        response = client.add_rds_no_agent_database(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)
support.huaweicloud.com/api-dbss/AddRdsNoAgentDatabase.html