更新时间:2024-09-12 GMT+08:00
分享

创建连接

功能介绍

创建连接接口。

请求参数

响应参数

状态码: 200

状态码: 400

状态码: 500

请求示例

创建一个名为mysql_link的数据连接。

POST /v1.1/1551c7f6c808414d8e9f3c514a170f2e/clusters/6ec9a0a4-76be-4262-8697-e7af1fac7920/cdm/link

{
  "links" : [ {
    "link-config-values" : {
      "configs" : [ {
        "inputs" : [ {
          "name" : "linkConfig.databaseType",
          "value" : "MYSQL"
        }, {
          "name" : "linkConfig.host",
          "value" : "100.94.8.163"
        }, {
          "name" : "linkConfig.port",
          "value" : "3306"
        }, {
          "name" : "linkConfig.database",
          "value" : "DB_name"
        }, {
          "name" : "linkConfig.username",
          "value" : "username"
        }, {
          "name" : "linkConfig.password",
          "value" : "DB_password"
        }, {
          "name" : "linkConfig.fetchSize",
          "value" : "100000"
        }, {
          "name" : "linkConfig.usingNative",
          "value" : "false"
        } ],
        "name" : "linkConfig"
      } ]
    },
    "name" : "mysql_link",
    "creation-date" : 1496654788622,
    "connector-name" : "generic-jdbc-connector",
    "update-date" : 1496654788622,
    "enabled" : true
  } ]
}

响应示例

状态码: 200

OK。

{
  "name" : "rdb_link",
  "validation-result" : [ { } ]
}

状态码: 400

请求错误。

{
  "code" : "Cdm.0315",
  "errCode" : "Cdm.0315",
  "message" : "Link name [ftp_link] already exist or created by other user.",
  "externalMessage" : "Link name [ftp_link] already exist or created by other user."
}

状态码: 500

服务内部错误,具体返回错误码请参考错误码。

{
  "validation-result" : [ {
    "linkConfig" : [ {
      "message" : "Can't connect to the database with given credentials: The authentication type 12 is not supported. Check that you have configured the pg_hba.conf file to include the client's IP address or subnet, and that it is using an authentication scheme supported by the driver.",
      "status" : "ERROR"
    } ]
  } ]
}

SDK代码示例

SDK代码示例如下。

创建一个名为mysql_link的数据连接。

  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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.cdm.v1.region.CdmRegion;
import com.huaweicloud.sdk.cdm.v1.*;
import com.huaweicloud.sdk.cdm.v1.model.*;

import java.util.List;
import java.util.ArrayList;

public class CreateLinkSolution {

    public static void main(String[] args) {
        // 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
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");
        String projectId = "{project_id}";

        ICredential auth = new BasicCredentials()
                .withProjectId(projectId)
                .withAk(ak)
                .withSk(sk);

        CdmClient client = CdmClient.newBuilder()
                .withCredential(auth)
                .withRegion(CdmRegion.valueOf("<YOUR REGION>"))
                .build();
        CreateLinkRequest request = new CreateLinkRequest();
        request.withClusterId("{cluster_id}");
        CdmCreateAndUpdateLinkReq body = new CdmCreateAndUpdateLinkReq();
        List<Input> listConfigsInputs = new ArrayList<>();
        listConfigsInputs.add(
            new Input()
                .withName("linkConfig.databaseType")
                .withValue("MYSQL")
        );
        listConfigsInputs.add(
            new Input()
                .withName("linkConfig.host")
                .withValue("100.94.8.163")
        );
        listConfigsInputs.add(
            new Input()
                .withName("linkConfig.port")
                .withValue("3306")
        );
        listConfigsInputs.add(
            new Input()
                .withName("linkConfig.database")
                .withValue("DB_name")
        );
        listConfigsInputs.add(
            new Input()
                .withName("linkConfig.username")
                .withValue("username")
        );
        listConfigsInputs.add(
            new Input()
                .withName("linkConfig.password")
                .withValue("DB_password")
        );
        listConfigsInputs.add(
            new Input()
                .withName("linkConfig.fetchSize")
                .withValue("100000")
        );
        listConfigsInputs.add(
            new Input()
                .withName("linkConfig.usingNative")
                .withValue("false")
        );
        List<Configs> listLinkConfigValuesConfigs = new ArrayList<>();
        listLinkConfigValuesConfigs.add(
            new Configs()
                .withInputs(listConfigsInputs)
                .withName("linkConfig")
        );
        LinksLinkconfigvalues linkConfigValuesLinks = new LinksLinkconfigvalues();
        linkConfigValuesLinks.withConfigs(listLinkConfigValuesConfigs);
        List<Links> listbodyLinks = new ArrayList<>();
        listbodyLinks.add(
            new Links()
                .withLinkConfigValues(linkConfigValuesLinks)
                .withName("mysql_link")
                .withCreationDate(1496654788622L)
                .withConnectorName("generic-jdbc-connector")
                .withUpdateDate(1496654788622L)
                .withEnabled(true)
        );
        body.withLinks(listbodyLinks);
        request.withBody(body);
        try {
            CreateLinkResponse response = client.createLink(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

创建一个名为mysql_link的数据连接。

 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcdm.v1.region.cdm_region import CdmRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcdm.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.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]
    projectId = "{project_id}"

    credentials = BasicCredentials(ak, sk, projectId)

    client = CdmClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(CdmRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CreateLinkRequest()
        request.cluster_id = "{cluster_id}"
        listInputsConfigs = [
            Input(
                name="linkConfig.databaseType",
                value="MYSQL"
            ),
            Input(
                name="linkConfig.host",
                value="100.94.8.163"
            ),
            Input(
                name="linkConfig.port",
                value="3306"
            ),
            Input(
                name="linkConfig.database",
                value="DB_name"
            ),
            Input(
                name="linkConfig.username",
                value="username"
            ),
            Input(
                name="linkConfig.password",
                value="DB_password"
            ),
            Input(
                name="linkConfig.fetchSize",
                value="100000"
            ),
            Input(
                name="linkConfig.usingNative",
                value="false"
            )
        ]
        listConfigsLinkConfigValues = [
            Configs(
                inputs=listInputsConfigs,
                name="linkConfig"
            )
        ]
        linkConfigValuesLinks = LinksLinkconfigvalues(
            configs=listConfigsLinkConfigValues
        )
        listLinksbody = [
            Links(
                link_config_values=linkConfigValuesLinks,
                name="mysql_link",
                creation_date=1496654788622,
                connector_name="generic-jdbc-connector",
                update_date=1496654788622,
                enabled=True
            )
        ]
        request.body = CdmCreateAndUpdateLinkReq(
            links=listLinksbody
        )
        response = client.create_link(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

创建一个名为mysql_link的数据连接。

  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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    cdm "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cdm/v1"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cdm/v1/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cdm/v1/region"
)

func 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")
    projectId := "{project_id}"

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        WithProjectId(projectId).
        Build()

    client := cdm.NewCdmClient(
        cdm.CdmClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.CreateLinkRequest{}
	request.ClusterId = "{cluster_id}"
	valueInputs:= "MYSQL"
	var valueInputsInterface interface{} = valueInputs
	valueInputs1:= "100.94.8.163"
	var valueInputsInterface1 interface{} = valueInputs1
	valueInputs2:= "3306"
	var valueInputsInterface2 interface{} = valueInputs2
	valueInputs3:= "DB_name"
	var valueInputsInterface3 interface{} = valueInputs3
	valueInputs4:= "username"
	var valueInputsInterface4 interface{} = valueInputs4
	valueInputs5:= "DB_password"
	var valueInputsInterface5 interface{} = valueInputs5
	valueInputs6:= "100000"
	var valueInputsInterface6 interface{} = valueInputs6
	valueInputs7:= "false"
	var valueInputsInterface7 interface{} = valueInputs7
	var listInputsConfigs = []model.Input{
        {
            Name: "linkConfig.databaseType",
            Value: &valueInputsInterface,
        },
        {
            Name: "linkConfig.host",
            Value: &valueInputsInterface1,
        },
        {
            Name: "linkConfig.port",
            Value: &valueInputsInterface2,
        },
        {
            Name: "linkConfig.database",
            Value: &valueInputsInterface3,
        },
        {
            Name: "linkConfig.username",
            Value: &valueInputsInterface4,
        },
        {
            Name: "linkConfig.password",
            Value: &valueInputsInterface5,
        },
        {
            Name: "linkConfig.fetchSize",
            Value: &valueInputsInterface6,
        },
        {
            Name: "linkConfig.usingNative",
            Value: &valueInputsInterface7,
        },
    }
	var listConfigsLinkConfigValues = []model.Configs{
        {
            Inputs: listInputsConfigs,
            Name: "linkConfig",
        },
    }
	linkConfigValuesLinks := &model.LinksLinkconfigvalues{
		Configs: listConfigsLinkConfigValues,
	}
	creationDateLinks:= int64(1496654788622)
	updateDateLinks:= int64(1496654788622)
	enabledLinks:= true
	var listLinksbody = []model.Links{
        {
            LinkConfigValues: linkConfigValuesLinks,
            Name: "mysql_link",
            CreationDate: &creationDateLinks,
            ConnectorName: "generic-jdbc-connector",
            UpdateDate: &updateDateLinks,
            Enabled: &enabledLinks,
        },
    }
	request.Body = &model.CdmCreateAndUpdateLinkReq{
		Links: listLinksbody,
	}
	response, err := client.CreateLink(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

错误码

请参见错误码

相关文档