1+ import * as utils from "../../common/utils.js" ;
2+ import { z } from "zod" ;
3+
4+ // 定义资源成员的Zod模式
5+ export const ResourceMemberSchema = z . object ( {
6+ username : z . string ( ) . nullable ( ) . optional ( ) . describe ( "用户名" ) ,
7+ userId : z . string ( ) . nullable ( ) . optional ( ) . describe ( "用户id" ) ,
8+ role : z . string ( ) . nullable ( ) . optional ( ) . describe ( "用户角色" ) ,
9+ } ) ;
10+
11+ // 定义API请求参数的Zod模式
12+ export const ResourceMemberBaseSchema = z . object ( {
13+ organizationId : z . string ( ) . describe ( "企业Id" ) ,
14+ resourceType : z . string ( ) . describe ( "资源类型 pipeline 流水线 hostGroup 主机组" ) ,
15+ resourceId : z . string ( ) . describe ( "资源Id" ) ,
16+ } ) ;
17+
18+ export const DeleteResourceMemberSchema = ResourceMemberBaseSchema . extend ( {
19+ userId : z . string ( ) . describe ( "用户Id" ) ,
20+ } ) ;
21+
22+ export const UpdateResourceMemberSchema = ResourceMemberBaseSchema . extend ( {
23+ roleName : z . string ( ) . describe ( "角色部署组 hostGroup: user(成员,使用权限) admin(管理员,使用编辑权限) 流水线 pipeline: admin(管理员,查看、运行、编辑权限) member(运行权限) viewer(查看权限)" ) ,
24+ userId : z . string ( ) . describe ( "用户id" ) ,
25+ } ) ;
26+
27+ export const CreateResourceMemberSchema = ResourceMemberBaseSchema . extend ( {
28+ roleName : z . string ( ) . describe ( "角色部署组 hostGroup: user(成员,使用权限) admin(管理员,使用编辑权限) owner(拥有者,所有权限) 流水线 pipeline: owner(拥有者,所有权限) admin(管理员,查看、运行、编辑权限) member(运行权限) viewer(查看权限)" ) ,
29+ userId : z . string ( ) . describe ( "用户id" ) ,
30+ } ) ;
31+
32+ export const UpdateResourceOwnerSchema = ResourceMemberBaseSchema . extend ( {
33+ newOwnerId : z . string ( ) . describe ( "新拥有者用户Id" ) ,
34+ } ) ;
35+
36+ // 定义资源成员类型
37+ export type ResourceMember = z . infer < typeof ResourceMemberSchema > ;
38+ export type DeleteResourceMemberParams = z . infer < typeof DeleteResourceMemberSchema > ;
39+ export type UpdateResourceMemberParams = z . infer < typeof UpdateResourceMemberSchema > ;
40+ export type CreateResourceMemberParams = z . infer < typeof CreateResourceMemberSchema > ;
41+ export type UpdateResourceOwnerParams = z . infer < typeof UpdateResourceOwnerSchema > ;
42+
43+ /**
44+ * 删除资源成员
45+ * @param organizationId 企业Id
46+ * @param resourceType 资源类型 pipeline 流水线 hostGroup 主机组
47+ * @param resourceId 资源Id
48+ * @param userId 用户Id
49+ * @returns 是否成功
50+ */
51+ export async function deleteResourceMemberFunc (
52+ organizationId : string ,
53+ resourceType : string ,
54+ resourceId : string ,
55+ userId : string
56+ ) : Promise < boolean > {
57+ const url = `/oapi/v1/flow/organizations/${ organizationId } /resourceMembers/resourceTypes/${ resourceType } /resourceIds/${ resourceId } ` ;
58+
59+ const queryParams = { userId } ;
60+ const fullUrl = utils . buildUrl ( url , queryParams ) ;
61+
62+ const response = await utils . yunxiaoRequest ( fullUrl , {
63+ method : "DELETE" ,
64+ } ) ;
65+
66+ return Boolean ( response ) ;
67+ }
68+
69+ /**
70+ * 获取资源成员列表
71+ * @param organizationId 企业Id
72+ * @param resourceType 资源类型 pipeline 流水线 hostGroup 主机组
73+ * @param resourceId 资源Id
74+ * @returns 资源成员列表
75+ */
76+ export async function listResourceMembersFunc (
77+ organizationId : string ,
78+ resourceType : string ,
79+ resourceId : string
80+ ) : Promise < ResourceMember [ ] > {
81+ const url = `/oapi/v1/flow/organizations/${ organizationId } /resourceMembers/resourceTypes/${ resourceType } /resourceIds/${ resourceId } ` ;
82+
83+ const response = await utils . yunxiaoRequest ( url , {
84+ method : "GET" ,
85+ } ) ;
86+
87+ if ( Array . isArray ( response ) ) {
88+ return response . map ( item => ResourceMemberSchema . parse ( item ) ) ;
89+ }
90+
91+ // 如果响应不是数组,但包含数据,尝试解析单个对象
92+ try {
93+ return [ ResourceMemberSchema . parse ( response ) ] ;
94+ } catch {
95+ return [ ] ;
96+ }
97+ }
98+
99+ /**
100+ * 更新资源成员
101+ * @param organizationId 企业Id
102+ * @param resourceType 资源类型 pipeline 流水线 hostGroup 主机组
103+ * @param resourceId 资源Id
104+ * @param roleName 角色
105+ * @param userId 用户id
106+ * @returns 是否成功
107+ */
108+ export async function updateResourceMemberFunc (
109+ organizationId : string ,
110+ resourceType : string ,
111+ resourceId : string ,
112+ roleName : string ,
113+ userId : string
114+ ) : Promise < boolean > {
115+ const url = `/oapi/v1/flow/organizations/${ organizationId } /resourceMembers/resourceTypes/${ resourceType } /resourceIds/${ resourceId } ` ;
116+
117+ const queryParams = { roleName, userId } ;
118+ const fullUrl = utils . buildUrl ( url , queryParams ) ;
119+
120+ const response = await utils . yunxiaoRequest ( fullUrl , {
121+ method : "PUT" ,
122+ } ) ;
123+
124+ return Boolean ( response ) ;
125+ }
126+
127+ /**
128+ * 插入资源成员
129+ * @param organizationId 企业Id
130+ * @param resourceType 资源类型 pipeline 流水线 hostGroup 主机组
131+ * @param resourceId 资源Id
132+ * @param roleName 角色
133+ * @param userId 用户id
134+ * @returns 是否成功
135+ */
136+ export async function createResourceMemberFunc (
137+ organizationId : string ,
138+ resourceType : string ,
139+ resourceId : string ,
140+ roleName : string ,
141+ userId : string
142+ ) : Promise < boolean > {
143+ const url = `/oapi/v1/flow/organizations/${ organizationId } /resourceMembers/resourceTypes/${ resourceType } /resourceIds/${ resourceId } ` ;
144+
145+ const queryParams = { roleName, userId } ;
146+ const fullUrl = utils . buildUrl ( url , queryParams ) ;
147+
148+ const response = await utils . yunxiaoRequest ( fullUrl , {
149+ method : "POST" ,
150+ } ) ;
151+
152+ return Boolean ( response ) ;
153+ }
154+
155+ /**
156+ * 移交资源对象拥有者
157+ * @param organizationId 企业Id
158+ * @param resourceType 资源类型 pipeline 流水线 hostGroup 主机组
159+ * @param resourceId 资源Id
160+ * @param newOwnerId 新拥有者用户Id
161+ * @returns 是否成功
162+ */
163+ export async function updateResourceOwnerFunc (
164+ organizationId : string ,
165+ resourceType : string ,
166+ resourceId : string ,
167+ newOwnerId : string
168+ ) : Promise < boolean > {
169+ const url = `/oapi/v1/flow/organizations/${ organizationId } /resourceMembers/resourceTypes/${ resourceType } /resourceIds/${ resourceId } /transfer/owner` ;
170+
171+ const queryParams = { newOwnerId } ;
172+ const fullUrl = utils . buildUrl ( url , queryParams ) ;
173+
174+ const response = await utils . yunxiaoRequest ( fullUrl , {
175+ method : "POST" ,
176+ } ) ;
177+
178+ return Boolean ( response ) ;
179+ }
0 commit comments