# huaweicloud-dc-virtualinterface-samples-java **Repository Path**: HuaweiCloudDeveloper/huaweicloud-dc-virtualinterface-samples-java ## Basic Information - **Project Name**: huaweicloud-dc-virtualinterface-samples-java - **Description**: 云专线相关SDK 虚拟接口使用示例 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master-dev - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-12-16 - **Last Updated**: 2025-06-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 版本说明 本示例配套的SDK版本为:3.1.11及以上版本 ### 示例简介 本示例展示如何使用云专线(DirectConnection)相关SDK ### 功能介绍 云专线虚拟接口增、删、改、查功能 ### 前置条件 1.已 [注册](https://id1.cloud.huawei.com/UnifiedIDMPortal/portal/userRegister/regbyphone.html?themeName=red&access_type=offline&clientID=103493351&loginChannel=88000000&loginUrl=https%3A%2F%2Fauth.huaweicloud.com%2Fauthui%2Flogin.html%23&service=https%3A%2F%2Fauth.huaweicloud.com%2Fauthui%2FcasLogin&countryCode=cn&scope=https%3A%2F%2Fwww.huawei.com%2Fauth%2Faccount%2Funified.profile+https%3A%2F%2Fwww.huawei.com%2Fauth%2Faccount%2Frisk.idstate&reqClientType=88&state=cf496c0cb6c7414894276bebbfe1068c&lang=zh-cn) 华为云,并完成 [实名认证](https://account.huaweicloud.com/usercenter/?region=cn-north-4#/accountindex/realNameAuth) 2.获取华为云开发工具包(SDK),您也可以查看安装JAVA SDK。 3.已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK)。请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 [访问密钥](https://support.huaweicloud.com/usermanual-ca/zh-cn_topic_0046606340.html) 。 4.已具备开发环境 ,支持Java JDK 1.8及其以上版本。 5.虚拟接口的创建 ,需要提前准备好物理连接和虚拟网关。 ### SDK获取和安装 您可以通过Maven配置所依赖的云专线服务SDK 具体的SDK版本号请参见 [SDK开发中心](https://sdkcenter.developer.huaweicloud.com?language=java) (产品类别:云专线) ### 代码示例 以下代码展示如何使用云专线(DirectConnection)相关SDK ``` java import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ClientRequestException; import com.huaweicloud.sdk.core.exception.ServerResponseException; import com.huaweicloud.sdk.dc.v3.DcClient; import com.huaweicloud.sdk.dc.v3.model.CreateVirtualInterface; import com.huaweicloud.sdk.dc.v3.model.CreateVirtualInterfaceRequest; import com.huaweicloud.sdk.dc.v3.model.CreateVirtualInterfaceRequestBody; import com.huaweicloud.sdk.dc.v3.model.CreateVirtualInterfaceResponse; import com.huaweicloud.sdk.dc.v3.model.DeleteVirtualInterfaceRequest; import com.huaweicloud.sdk.dc.v3.model.DeleteVirtualInterfaceResponse; import com.huaweicloud.sdk.dc.v3.model.ListVirtualInterfacesRequest; import com.huaweicloud.sdk.dc.v3.model.ListVirtualInterfacesResponse; import com.huaweicloud.sdk.dc.v3.model.ShowVirtualInterfaceRequest; import com.huaweicloud.sdk.dc.v3.model.ShowVirtualInterfaceResponse; import com.huaweicloud.sdk.dc.v3.model.UpdateVirtualInterface; import com.huaweicloud.sdk.dc.v3.model.UpdateVirtualInterfaceRequest; import com.huaweicloud.sdk.dc.v3.model.UpdateVirtualInterfaceRequestBody; import com.huaweicloud.sdk.dc.v3.model.UpdateVirtualInterfaceResponse; import com.huaweicloud.sdk.dc.v3.region.DcRegion; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Arrays; public class VirtualInterfaceDemo { private static final Logger logger = LoggerFactory.getLogger(VirtualInterfaceDemo.class); // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份认证为例,运行示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 private static final String AK = System.getenv("HUAWEICLOUD_SDK_AK"); private static final String SK = System.getenv("HUAWEICLOUD_SDK_SK"); private static final String PROJECT_ID = ""; private static DcClient client = getDcClient(); /* 获取DC请求客户端 */ public static DcClient getDcClient() { return DcClient.newBuilder() .withCredential(new BasicCredentials() .withAk(AK) .withSk(SK) .withProjectId(PROJECT_ID) ).withRegion(DcRegion.CN_NORTH_4) .build(); } public static void main(String[] args) { // 1,创建虚拟接口 CreateVirtualInterfaceResponse response = createVirtualInterface(); // 2,查询虚拟接口详情 showVirtualInterface(response.getVirtualInterface().getId()); // 3,修改虚拟接口 updateVirtualInterface(response.getVirtualInterface().getId()); // 4,查询虚拟接口列表 listVirtualInterfaces(response.getVirtualInterface().getId()); // 5,删除虚拟接口 deleteVirtualInterface(response.getVirtualInterface().getId()); } private static CreateVirtualInterfaceResponse createVirtualInterface() { CreateVirtualInterface createVirtualInterface = new CreateVirtualInterface() .withName("") .withDescription("") .withDirectConnectId("") .withType(CreateVirtualInterface.TypeEnum.valueOf("private")) .withServiceType(CreateVirtualInterface.ServiceTypeEnum.valueOf("vpc")) .withBandwidth(10) .withVlan(10) .withVgwId("") .withRouteMode(CreateVirtualInterface.RouteModeEnum.valueOf("static")) .withLocalGatewayV4Ip("") .withRemoteGatewayV4Ip("") .withRemoteEpGroup(Arrays.asList("")); CreateVirtualInterfaceRequest request = new CreateVirtualInterfaceRequest() .withBody(new CreateVirtualInterfaceRequestBody().withVirtualInterface(createVirtualInterface)); CreateVirtualInterfaceResponse response = null; try { response = client.createVirtualInterface(request); logger.info(response.toString()); } catch (ClientRequestException e) { logger.error(String.valueOf(e.getHttpStatusCode())); logger.error(e.toString()); } catch (ServerResponseException e) { logger.error(String.valueOf(e.getHttpStatusCode())); logger.error(e.getMessage()); } return response; } private static void showVirtualInterface(String virtualInterfaceId) { ShowVirtualInterfaceRequest request = new ShowVirtualInterfaceRequest().withVirtualInterfaceId(virtualInterfaceId); try { ShowVirtualInterfaceResponse response = client.showVirtualInterface(request); logger.info(response.toString()); } catch (ClientRequestException e) { logger.error(String.valueOf(e.getHttpStatusCode())); logger.error(e.toString()); } catch (ServerResponseException e) { logger.error(String.valueOf(e.getHttpStatusCode())); logger.error(e.getMessage()); } } private static void updateVirtualInterface(String virtualInterfaceId) { UpdateVirtualInterfaceRequest request = new UpdateVirtualInterfaceRequest() .withVirtualInterfaceId(virtualInterfaceId) .withBody(new UpdateVirtualInterfaceRequestBody().withVirtualInterface(new UpdateVirtualInterface() .withName("") .withDescription(""))); try { UpdateVirtualInterfaceResponse response = client.updateVirtualInterface(request); logger.info(response.toString()); } catch (ClientRequestException e) { logger.error(String.valueOf(e.getHttpStatusCode())); logger.error(e.toString()); } catch (ServerResponseException e) { logger.error(String.valueOf(e.getHttpStatusCode())); logger.error(e.getMessage()); } } private static void listVirtualInterfaces(String virtualInterfaceId) { ListVirtualInterfacesRequest request = new ListVirtualInterfacesRequest().withId(Arrays.asList(virtualInterfaceId)); try { ListVirtualInterfacesResponse response = client.listVirtualInterfaces(request); logger.info(response.toString()); } catch (ClientRequestException e) { logger.error(String.valueOf(e.getHttpStatusCode())); logger.error(e.toString()); } catch (ServerResponseException e) { logger.error(String.valueOf(e.getHttpStatusCode())); logger.error(e.getMessage()); } } private static void deleteVirtualInterface(String virtualInterfaceId) { DeleteVirtualInterfaceRequest request = new DeleteVirtualInterfaceRequest().withVirtualInterfaceId(virtualInterfaceId); try { DeleteVirtualInterfaceResponse response = client.deleteVirtualInterface(request); logger.info(response.toString()); } catch (ClientRequestException e) { logger.error(String.valueOf(e.getHttpStatusCode())); logger.error(e.toString()); } catch (ServerResponseException e) { logger.error(String.valueOf(e.getHttpStatusCode())); logger.error(e.getMessage()); } } } ``` 您可以在 [云专线DC服务文档](https://support.huaweicloud.com/dc/index.html) 和[API Explorer](https://apiexplorer.developer.huaweicloud.com/apiexplorer/doc?product=DC&api=CreateVirtualInterface) 查看具体信息。 ### 修订记录 发布日期 | 文档版本 | 修订说明 :----: | :-----: | :------: 2022/12/12 |1.0 | 文档首次发布