signupAndLogin will signup and login as user demo@example.com. addMemberToWorkspaceIAM adds a member as workspace role to the current workspace.
(ctx context.Context, workspace, member, role string)
| 369 | // signupAndLogin will signup and login as user demo@example.com. |
| 370 | // addMemberToWorkspaceIAM adds a member as workspace role to the current workspace. |
| 371 | func (ctl *controller) addMemberToWorkspaceIAM(ctx context.Context, workspace, member, role string) (*v1pb.IamPolicy, error) { |
| 372 | policyResp, err := ctl.workspaceServiceClient.GetIamPolicy(ctx, connect.NewRequest(&v1pb.GetIamPolicyRequest{ |
| 373 | Resource: workspace, |
| 374 | })) |
| 375 | if err != nil { |
| 376 | return nil, err |
| 377 | } |
| 378 | |
| 379 | policy := policyResp.Msg |
| 380 | found := false |
| 381 | for _, binding := range policy.Bindings { |
| 382 | if binding.Role == role { |
| 383 | binding.Members = append(binding.Members, member) |
| 384 | found = true |
| 385 | break |
| 386 | } |
| 387 | } |
| 388 | if !found { |
| 389 | policy.Bindings = append(policy.Bindings, &v1pb.Binding{ |
| 390 | Role: role, |
| 391 | Members: []string{member}, |
| 392 | }) |
| 393 | } |
| 394 | |
| 395 | updated, err := ctl.workspaceServiceClient.SetIamPolicy(ctx, connect.NewRequest(&v1pb.SetIamPolicyRequest{ |
| 396 | Etag: policy.Etag, |
| 397 | Policy: policy, |
| 398 | Resource: workspace, |
| 399 | })) |
| 400 | if err != nil { |
| 401 | return nil, err |
| 402 | } |
| 403 | return updated.Msg, nil |
| 404 | } |
| 405 | |
| 406 | func (ctl *controller) signupAndLogin(ctx context.Context) (string, error) { |
| 407 | // Use Signup API (creates principal + workspace) then Login to get a token. |