|
4 | 4 | "encoding/base64" |
5 | 5 | "fmt" |
6 | 6 |
|
| 7 | + "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb" |
7 | 8 | "github.com/milvus-io/milvus-proto/go-api/v2/schemapb" |
| 9 | + "github.com/samber/lo" |
8 | 10 | "google.golang.org/protobuf/proto" |
9 | 11 |
|
10 | 12 | "github.com/zilliztech/milvus-backup/core/proto/backuppb" |
@@ -42,15 +44,15 @@ func Functions(bakFuncs []*backuppb.FunctionSchema) []*schemapb.FunctionSchema { |
42 | 44 | funcs := make([]*schemapb.FunctionSchema, 0, len(bakFuncs)) |
43 | 45 | for _, bakFunc := range bakFuncs { |
44 | 46 | fun := &schemapb.FunctionSchema{ |
45 | | - Name: bakFunc.Name, |
46 | | - Id: bakFunc.Id, |
47 | | - Description: bakFunc.Description, |
48 | | - Type: schemapb.FunctionType(bakFunc.Type), |
49 | | - InputFieldNames: bakFunc.InputFieldNames, |
50 | | - InputFieldIds: bakFunc.InputFieldIds, |
51 | | - OutputFieldNames: bakFunc.OutputFieldNames, |
52 | | - OutputFieldIds: bakFunc.OutputFieldIds, |
53 | | - Params: pbconv.BakKVToMilvusKV(bakFunc.Params), |
| 47 | + Name: bakFunc.GetName(), |
| 48 | + Id: bakFunc.GetId(), |
| 49 | + Description: bakFunc.GetDescription(), |
| 50 | + Type: schemapb.FunctionType(bakFunc.GetType()), |
| 51 | + InputFieldNames: bakFunc.GetInputFieldNames(), |
| 52 | + InputFieldIds: bakFunc.GetInputFieldIds(), |
| 53 | + OutputFieldNames: bakFunc.GetOutputFieldNames(), |
| 54 | + OutputFieldIds: bakFunc.GetOutputFieldIds(), |
| 55 | + Params: pbconv.BakKVToMilvusKV(bakFunc.GetParams()), |
54 | 56 | } |
55 | 57 | funcs = append(funcs, fun) |
56 | 58 | } |
@@ -138,3 +140,118 @@ func Schema(bakSchema *backuppb.CollectionSchema) (*schemapb.CollectionSchema, e |
138 | 140 |
|
139 | 141 | return schema, nil |
140 | 142 | } |
| 143 | + |
| 144 | +func Users(bacUsers []*backuppb.UserInfo, curUsers []*milvuspb.UserInfo) []*milvuspb.UserInfo { |
| 145 | + um := lo.SliceToMap(curUsers, func(user *milvuspb.UserInfo) (string, struct{}) { |
| 146 | + return user.User, struct{}{} |
| 147 | + }) |
| 148 | + |
| 149 | + users := make([]*milvuspb.UserInfo, 0, len(bacUsers)) |
| 150 | + for _, user := range bacUsers { |
| 151 | + // skip if user already exist |
| 152 | + if _, ok := um[user.GetUser()]; ok { |
| 153 | + continue |
| 154 | + } |
| 155 | + |
| 156 | + ur := lo.Map(user.GetRoles(), func(role *backuppb.RoleEntity, index int) *milvuspb.RoleEntity { |
| 157 | + return &milvuspb.RoleEntity{Name: role.Name} |
| 158 | + }) |
| 159 | + userEntity := &milvuspb.UserInfo{ |
| 160 | + User: user.User, |
| 161 | + Password: user.Password, |
| 162 | + Roles: ur, |
| 163 | + } |
| 164 | + users = append(users, userEntity) |
| 165 | + } |
| 166 | + |
| 167 | + return users |
| 168 | +} |
| 169 | + |
| 170 | +func Roles(bakRoles []*backuppb.RoleEntity, curRoles []*milvuspb.RoleEntity) []*milvuspb.RoleEntity { |
| 171 | + rm := lo.SliceToMap(curRoles, func(role *milvuspb.RoleEntity) (string, struct{}) { |
| 172 | + return role.Name, struct{}{} |
| 173 | + }) |
| 174 | + |
| 175 | + roles := make([]*milvuspb.RoleEntity, 0, len(bakRoles)) |
| 176 | + for _, role := range bakRoles { |
| 177 | + // skip if role already exist |
| 178 | + if _, ok := rm[role.GetName()]; ok { |
| 179 | + continue |
| 180 | + } |
| 181 | + |
| 182 | + roleEntity := &milvuspb.RoleEntity{ |
| 183 | + Name: role.GetName(), |
| 184 | + } |
| 185 | + roles = append(roles, roleEntity) |
| 186 | + } |
| 187 | + |
| 188 | + return roles |
| 189 | +} |
| 190 | + |
| 191 | +func backupGrantKey(grant *backuppb.GrantEntity) string { |
| 192 | + return fmt.Sprintf("%s/%s/%s/%s/%s/%s", |
| 193 | + grant.GetObject().GetName(), |
| 194 | + grant.GetObjectName(), |
| 195 | + grant.GetRole().GetName(), |
| 196 | + grant.GetGrantor().GetUser().GetName(), |
| 197 | + grant.GetGrantor().GetPrivilege().GetName(), |
| 198 | + grant.GetDbName()) |
| 199 | +} |
| 200 | + |
| 201 | +func milvusGrantKey(grant *milvuspb.GrantEntity) string { |
| 202 | + return fmt.Sprintf("%s/%s/%s/%s/%s/%s", |
| 203 | + grant.GetObject().GetName(), |
| 204 | + grant.GetObjectName(), |
| 205 | + grant.GetRole().GetName(), |
| 206 | + grant.GetGrantor().GetUser().GetName(), |
| 207 | + grant.GetGrantor().GetPrivilege().GetName(), |
| 208 | + grant.GetDbName()) |
| 209 | +} |
| 210 | + |
| 211 | +func Grants(bakGrants []*backuppb.GrantEntity, curGrants []*milvuspb.GrantEntity) []*milvuspb.GrantEntity { |
| 212 | + gm := lo.SliceToMap(curGrants, func(grant *milvuspb.GrantEntity) (string, struct{}) { |
| 213 | + return milvusGrantKey(grant), struct{}{} |
| 214 | + }) |
| 215 | + grants := make([]*milvuspb.GrantEntity, 0, len(bakGrants)) |
| 216 | + for _, grant := range bakGrants { |
| 217 | + // skip if grant already exist |
| 218 | + if _, ok := gm[backupGrantKey(grant)]; ok { |
| 219 | + continue |
| 220 | + } |
| 221 | + |
| 222 | + grantEntity := &milvuspb.GrantEntity{ |
| 223 | + Role: &milvuspb.RoleEntity{Name: grant.GetRole().GetName()}, |
| 224 | + Object: &milvuspb.ObjectEntity{Name: grant.GetObject().GetName()}, |
| 225 | + ObjectName: grant.GetObjectName(), |
| 226 | + Grantor: &milvuspb.GrantorEntity{ |
| 227 | + User: &milvuspb.UserEntity{Name: grant.GetGrantor().GetUser().GetName()}, |
| 228 | + Privilege: &milvuspb.PrivilegeEntity{Name: grant.GetGrantor().GetPrivilege().GetName()}, |
| 229 | + }, |
| 230 | + DbName: grant.GetDbName(), |
| 231 | + } |
| 232 | + grants = append(grants, grantEntity) |
| 233 | + } |
| 234 | + |
| 235 | + return grants |
| 236 | +} |
| 237 | + |
| 238 | +func PrivilegeGroups(bakGroups []*backuppb.PrivilegeGroupInfo, curGroups []*milvuspb.PrivilegeGroupInfo) []*milvuspb.PrivilegeGroupInfo { |
| 239 | + curGroupName := lo.SliceToMap(curGroups, func(group *milvuspb.PrivilegeGroupInfo) (string, struct{}) { |
| 240 | + return group.GetGroupName(), struct{}{} |
| 241 | + }) |
| 242 | + |
| 243 | + groups := make([]*milvuspb.PrivilegeGroupInfo, 0, len(bakGroups)) |
| 244 | + for _, group := range bakGroups { |
| 245 | + if _, ok := curGroupName[group.GetGroupName()]; ok { |
| 246 | + continue |
| 247 | + } |
| 248 | + |
| 249 | + privileges := lo.Map(group.GetPrivileges(), func(privilege *backuppb.PrivilegeEntity, _ int) *milvuspb.PrivilegeEntity { |
| 250 | + return &milvuspb.PrivilegeEntity{Name: privilege.GetName()} |
| 251 | + }) |
| 252 | + g := &milvuspb.PrivilegeGroupInfo{GroupName: group.GetGroupName(), Privileges: privileges} |
| 253 | + groups = append(groups, g) |
| 254 | + } |
| 255 | + |
| 256 | + return groups |
| 257 | +} |
0 commit comments