Add organizationAdmin
[ZITADOPER-1]
This commit is contained in:
@@ -26,6 +26,7 @@ import (
|
||||
condition "bitbucket.org/topmanage-software-engineering/zitadel-k8s-operator/src/pkg/condition"
|
||||
"bitbucket.org/topmanage-software-engineering/zitadel-k8s-operator/src/pkg/controller/zitadel"
|
||||
"github.com/zitadel/zitadel-go/v2/pkg/client/management"
|
||||
"github.com/zitadel/zitadel-go/v2/pkg/client/middleware"
|
||||
pb "github.com/zitadel/zitadel-go/v2/pkg/client/zitadel/management"
|
||||
"k8s.io/client-go/util/workqueue"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
@@ -90,7 +91,28 @@ func newWrappedOrganizationReconciler(client client.Client, refResolver *zitadel
|
||||
}
|
||||
}
|
||||
|
||||
type orgReconcilePhase struct {
|
||||
Name string
|
||||
Reconcile func(context.Context, *management.Client) error
|
||||
}
|
||||
|
||||
func (wr *wrappedOrganizationReconciler) Reconcile(ctx context.Context, ztdClient *management.Client) error {
|
||||
phases := []orgReconcilePhase{
|
||||
{
|
||||
Name: "organization",
|
||||
Reconcile: wr.reconcileOrg,
|
||||
},
|
||||
}
|
||||
for _, p := range phases {
|
||||
err := p.Reconcile(ctx, ztdClient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (wr *wrappedOrganizationReconciler) reconcileOrg(ctx context.Context, ztdClient *management.Client) error {
|
||||
zitadelCluster, err := wr.refResolver.ZitadelCluster(ctx, &wr.organization.Spec.ZitadelClusterRef, wr.organization.Namespace)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -103,7 +125,6 @@ func (wr *wrappedOrganizationReconciler) Reconcile(ctx context.Context, ztdClien
|
||||
return fmt.Errorf("Error getting org: %v", err)
|
||||
}
|
||||
}
|
||||
// TODO: add initial user
|
||||
if orgRes == nil {
|
||||
resp, err := ztdClient.AddOrg(ctx, &pb.AddOrgRequest{
|
||||
Name: strings.ToLower(wr.organization.Name),
|
||||
@@ -120,6 +141,70 @@ func (wr *wrappedOrganizationReconciler) Reconcile(ctx context.Context, ztdClien
|
||||
return wr.Client.Status().Patch(ctx, wr.organization, patch)
|
||||
}
|
||||
|
||||
func (wr *wrappedOrganizationReconciler) reconcileInitialAdmin(ctx context.Context, ztdClient *management.Client) error {
|
||||
zitadelCluster, err := wr.refResolver.ZitadelCluster(ctx, &wr.organization.Spec.ZitadelClusterRef, wr.organization.Namespace)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
adminUser, err := ztdClient.GetUserByLoginNameGlobal(ctx, &pb.GetUserByLoginNameGlobalRequest{
|
||||
LoginName: strings.ToLower(fmt.Sprintf("%s@%s.%s", wr.organization.Spec.OrganizationAdmin.UserName, wr.organization.Name, zitadelCluster.Spec.Host)),
|
||||
})
|
||||
if err != nil {
|
||||
if !strings.Contains(err.Error(), "could not be found") {
|
||||
return fmt.Errorf("Error getting admin user: %v", err)
|
||||
}
|
||||
}
|
||||
ctx = middleware.SetOrgID(ctx, wr.organization.Status.OrgId)
|
||||
var userid string
|
||||
if adminUser == nil {
|
||||
resp, err := ztdClient.AddHumanUser(ctx, &pb.AddHumanUserRequest{
|
||||
UserName: wr.organization.Spec.OrganizationAdmin.UserName,
|
||||
Profile: &pb.AddHumanUserRequest_Profile{
|
||||
FirstName: wr.organization.Spec.OrganizationAdmin.FirstName,
|
||||
LastName: wr.organization.Spec.OrganizationAdmin.LastName,
|
||||
},
|
||||
Email: &pb.AddHumanUserRequest_Email{
|
||||
Email: wr.organization.Spec.OrganizationAdmin.Email,
|
||||
IsEmailVerified: false,
|
||||
},
|
||||
})
|
||||
userid = resp.UserId
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error adding human user: %v", err)
|
||||
}
|
||||
{
|
||||
if _, err := ztdClient.AddOrgMember(ctx, &pb.AddOrgMemberRequest{
|
||||
UserId: userid,
|
||||
Roles: []string{
|
||||
"ORG_OWNER",
|
||||
},
|
||||
}); err != nil {
|
||||
if !strings.Contains(err.Error(), "Errors.Org.Member.RolesNotChanged") {
|
||||
return fmt.Errorf("Error adding org member: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
userid = adminUser.User.Id
|
||||
}
|
||||
|
||||
{
|
||||
if _, err := ztdClient.UpdateOrgMember(ctx, &pb.UpdateOrgMemberRequest{
|
||||
UserId: userid,
|
||||
Roles: []string{
|
||||
"ORG_OWNER",
|
||||
},
|
||||
}); err != nil {
|
||||
if !strings.Contains(err.Error(), "Errors.Org.Member.RolesNotChanged") {
|
||||
return fmt.Errorf("Error updating org member: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
patch := client.MergeFrom(wr.organization.DeepCopy())
|
||||
wr.organization.Status.AdminId = userid
|
||||
return wr.Status().Patch(ctx, wr.organization, patch)
|
||||
}
|
||||
|
||||
func (wr *wrappedOrganizationReconciler) PatchStatus(ctx context.Context, patcher condition.Patcher) error {
|
||||
patch := client.MergeFrom(wr.organization.DeepCopy())
|
||||
patcher(&wr.organization.Status)
|
||||
|
||||
Reference in New Issue
Block a user