# huaweicloud-organizations-tag-java **Repository Path**: HuaweiCloudDeveloper/huaweicloud-organizations-tag-java ## Basic Information - **Project Name**: huaweicloud-organizations-tag-java - **Description**: Organizations服务典型场景示例库。提供向创建的资源共享示例添加、删除、列出标签 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master-dev - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-04-10 - **Last Updated**: 2025-06-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 版本说明 本示例配套的SDK版本为:3.1.35及以上版本 ### 示例简介 本示例展示如何使用组织(Organizations)相关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及其以上版本。 ### SDK获取和安装 您可以通过Maven配置所依赖的组织服务SDK 具体的SDK版本号请参见 [SDK开发中心](https://sdkcenter.developer.huaweicloud.com?language=java) (产品类别:组织) ### 代码示例 以下代码展示如何使用组织(Organizations)相关SDK ``` java // 用户身份认证 import com.huaweicloud.sdk.core.auth.GlobalCredentials; import com.huaweicloud.sdk.core.auth.ICredential; // 请求异常类 import com.huaweicloud.sdk.core.exception.ClientRequestException; // 导入待请求接口的 request 和 response 类 import com.huaweicloud.sdk.organizations.v1.OrganizationsClient; import com.huaweicloud.sdk.organizations.v1.model.CreateOrganizationRequest; import com.huaweicloud.sdk.organizations.v1.model.CreateOrganizationResponse; import com.huaweicloud.sdk.organizations.v1.model.DeleteOrganizationRequest; import com.huaweicloud.sdk.organizations.v1.model.DeleteOrganizationResponse; import com.huaweicloud.sdk.organizations.v1.model.ListRootsRequest; import com.huaweicloud.sdk.organizations.v1.model.ListRootsResponse; import com.huaweicloud.sdk.organizations.v1.model.ListTagsForResourceRequest; import com.huaweicloud.sdk.organizations.v1.model.ListTagsForResourceResponse; import com.huaweicloud.sdk.organizations.v1.model.ShowOrganizationRequest; import com.huaweicloud.sdk.organizations.v1.model.ShowOrganizationResponse; import com.huaweicloud.sdk.organizations.v1.model.TagDto; import com.huaweicloud.sdk.organizations.v1.model.TagResourceReqBody; import com.huaweicloud.sdk.organizations.v1.model.TagResourceRequest; import com.huaweicloud.sdk.organizations.v1.model.TagResourceResponse; import com.huaweicloud.sdk.organizations.v1.model.UntagResourceReqBody; import com.huaweicloud.sdk.organizations.v1.model.UntagResourceRequest; import com.huaweicloud.sdk.organizations.v1.model.UntagResourceResponse; // 日志打印 import com.huaweicloud.sdk.organizations.v1.region.OrganizationsRegion; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Collections; public class OrganizationsTagManagementDemo { private static final Logger logger = LoggerFactory.getLogger(OrganizationsTagManagementDemo.class.getName()); public static void main(String[] args) { // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 String ak = System.getenv("HUAWEICLOUD_SDK_AK"); String sk = System.getenv("HUAWEICLOUD_SDK_SK"); ICredential auth = new GlobalCredentials().withAk(ak).withSk(sk); OrganizationsClient organizationsClient = OrganizationsClient.newBuilder() .withCredential(auth) .withRegion(OrganizationsRegion.valueOf("cn-north-4")) .build(); OrganizationsTagManagementDemo demo = new OrganizationsTagManagementDemo(); // 1、创建组织 demo.createOrganization(organizationsClient); // 2、查询所属组织信息 demo.showOrganization(organizationsClient); // 3、列出组织的根 ListRootsResponse listRootsResponse = demo.listRoots(organizationsClient); // 4、为指定资源添加标签 demo.tagResource(organizationsClient, listRootsResponse.getRoots().get(0).getId()); // 5、列出绑定到指定资源的标签 demo.listTagsForResource(organizationsClient, listRootsResponse.getRoots().get(0).getId()); // 6、从指定资源中删除指定主键标签 demo.unTagResource(organizationsClient, listRootsResponse.getRoots().get(0).getId()); // 7、删除组织 demo.deleteOrganization(organizationsClient); } public void createOrganization(OrganizationsClient organizationsClient) { CreateOrganizationRequest createOrganizationRequest = new CreateOrganizationRequest(); try { CreateOrganizationResponse createOrganizationResponse = organizationsClient.createOrganization(createOrganizationRequest); logger.info(createOrganizationResponse.toString()); } catch (ClientRequestException e) { LogError(e); } } public void showOrganization(OrganizationsClient organizationsClient) { ShowOrganizationRequest showOrganizationRequest = new ShowOrganizationRequest(); try { ShowOrganizationResponse showOrganizationResponse = organizationsClient.showOrganization(showOrganizationRequest); logger.info(showOrganizationResponse.toString()); } catch (ClientRequestException e) { LogError(e); } } public ListRootsResponse listRoots(OrganizationsClient organizationsClient) { ListRootsRequest listRootsRequest = new ListRootsRequest(); ListRootsResponse listRootsResponse = null; try { listRootsResponse = organizationsClient.listRoots(listRootsRequest); logger.info(listRootsResponse.toString()); } catch (ClientRequestException e) { LogError(e); } return listRootsResponse; } public void deleteOrganization(OrganizationsClient organizationsClient) { DeleteOrganizationRequest deleteOrganizationRequest = new DeleteOrganizationRequest(); try { DeleteOrganizationResponse deleteOrganizationResponse = organizationsClient.deleteOrganization(deleteOrganizationRequest); logger.info(deleteOrganizationResponse.toString()); } catch (ClientRequestException e) { LogError(e); } } public void tagResource(OrganizationsClient organizationsClient, String resourceId) { if (resourceId == null || resourceId.length() == 0) { return; } TagResourceRequest tagResourceRequest = new TagResourceRequest() .withBody(new TagResourceReqBody() .withTags(Collections.singletonList(new TagDto().withKey("{key}").withValue("{value}")))) // key标签键的密钥标识符或名称;value与标签键关联的字符串值 .withResourceId(resourceId); // resourceId根、组织单元、帐号或策略的唯一标识符 try { TagResourceResponse tagResourceResponse = organizationsClient.tagResource(tagResourceRequest); logger.info(tagResourceResponse.toString()); } catch (ClientRequestException e) { LogError(e); } } public void unTagResource(OrganizationsClient organizationsClient, String resourceId) { if (resourceId == null || resourceId.length() == 0) { return; } UntagResourceRequest untagResourceRequest = new UntagResourceRequest() .withBody(new UntagResourceReqBody().withTagKeys(Collections.singletonList("{tag_keys}"))) // tag_keys标签列表 .withResourceId(resourceId); //resourceId根、组织单元、帐号或策略的唯一标识符 try { UntagResourceResponse untagResourceResponse = organizationsClient.untagResource(untagResourceRequest); logger.info(untagResourceResponse.toString()); } catch (ClientRequestException e) { LogError(e); } } public void listTagsForResource(OrganizationsClient organizationsClient, String resourceId) { if (resourceId == null || resourceId.length() == 0) { return; } ListTagsForResourceRequest listTagsForResourceRequest = new ListTagsForResourceRequest().withResourceId(resourceId); //resourceId根、组织单元、帐号或策略的唯一标识符 try { ListTagsForResourceResponse listTagsForResourceResponse = organizationsClient.listTagsForResource(listTagsForResourceRequest); logger.info(listTagsForResourceResponse.toString()); } catch (ClientRequestException e) { LogError(e); } } private static void LogError(ClientRequestException e) { logger.error("HttpStatusCode: " + e.getHttpStatusCode()); logger.error("RequestId: " + e.getRequestId()); logger.error("ErrorCode: " + e.getErrorCode()); logger.error("ErrorMsg: " + e.getErrorMsg()); } } ``` 您可以在 [组织Organizations服务文档](https://support.huaweicloud.com/productdesc-organizations/org_01_0011.html) 和[API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/Organizations/doc?api=CreateTagResource) 查看具体信息。 ### 修订记录 发布日期 | 文档版本 | 修订说明 :----: | :-----: | :------: 2023/4/10 |1.0 | 文档首次发布