Skip to content

Commit

Permalink
feature. update organization admin_id, when organization created
Browse files Browse the repository at this point in the history
  • Loading branch information
ktkfree committed Mar 14, 2024
1 parent 1864618 commit 4889e6c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/openinfradev/tks-batch/internal/cluster"
"github.com/openinfradev/tks-batch/internal/database"
"github.com/openinfradev/tks-batch/internal/organization"
"github.com/openinfradev/tks-batch/internal/user"
)

const INTERVAL_SEC = 5
Expand All @@ -26,6 +27,7 @@ var (
applicationAccessor *application.ApplicationAccessor
cloudAccountAccessor *cloudAccount.CloudAccountAccessor
organizationAccessor *organization.OrganizationAccessor
userAccessor *user.UserAccessor
apiClient _apiClient.ApiClient
)

Expand Down Expand Up @@ -68,6 +70,7 @@ func main() {
applicationAccessor = application.New(db)
cloudAccountAccessor = cloudAccount.New(db)
organizationAccessor = organization.New(db)
userAccessor = user.New(db)

// initialize external clients
argowfClient, err = argo.New(viper.GetString("argo-address"), viper.GetInt("argo-port"), false, "")
Expand Down
15 changes: 15 additions & 0 deletions cmd/server/organization_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ func processOrganizationStatus() error {
continue
}
}

if newStatus == domain.OrganizationStatus_CREATED {
user, err := userAccessor.GetOrganizationAdmin(organizationId)
if err != nil {
log.Error("Failed to get organization admin err : ", err)
continue
}

log.Debug(fmt.Sprintf("update organization admin!! organizationId [%s], adminId [%s]", organizationId, user.ID.String()))
err = organizationAccessor.UpdateOrganizationAdmin(organizationId, user.ID)
if err != nil {
log.Error("Failed to update organization admin err : ", err)
continue
}
}
}
return nil
}
Binary file modified cmd/server/server
Binary file not shown.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/openinfradev/tks-batch
go 1.18

require (
github.com/gofrs/uuid v4.0.0+incompatible
github.com/openinfradev/tks-api v0.0.0-20231023034343-1580951459e9
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.15.0
gorm.io/driver/postgres v1.4.5
Expand All @@ -28,7 +30,6 @@ require (
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/openinfradev/tks-api v0.0.0-20231023034343-1580951459e9 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
Expand Down
13 changes: 13 additions & 0 deletions internal/organization/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"gorm.io/gorm"

"github.com/gofrs/uuid"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/log"
)
Expand Down Expand Up @@ -59,3 +60,15 @@ func (x *OrganizationAccessor) UpdateOrganizationStatus(organizationId string, s
}
return nil
}

func (x *OrganizationAccessor) UpdateOrganizationAdmin(organizationId string, adminId uuid.UUID) error {
log.Info(fmt.Sprintf("UpdateOrganizationAdmin. organizationId[%s], adminId[%s]", organizationId, adminId))
res := x.db.Model(Organization{}).
Where("ID = ?", organizationId).
Updates(map[string]interface{}{"admin_id": adminId})

if res.Error != nil || res.RowsAffected == 0 {
return fmt.Errorf("nothing updated in organization with id %s", organizationId)
}
return nil
}
52 changes: 52 additions & 0 deletions internal/user/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package user

import (
"time"

"gorm.io/gorm"

"github.com/gofrs/uuid"
)

type User struct {
ID uuid.UUID `gorm:"primarykey;type:uuid" json:"id"`
AccountId string `json:"accountId"`
Password string `gorm:"-:all" json:"password"`
Name string `json:"name"`
Token string `json:"token"`
RoleId string
OrganizationId string
Creator string `json:"creator"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
PasswordUpdatedAt time.Time `json:"passwordUpdatedAt"`
PasswordExpired bool `json:"passwordExpired"`
Email string `json:"email"`
Department string `json:"department"`
Description string `json:"description"`
}

type UserAccessor struct {
db *gorm.DB
}

func New(db *gorm.DB) *UserAccessor {
return &UserAccessor{
db: db,
}
}

func (x *UserAccessor) GetDb() *gorm.DB {
return x.db
}

func (x *UserAccessor) GetOrganizationAdmin(organizationId string) (out User, err error) {
res := x.db.
Where("organization_id = ?", organizationId).
First(&out)

if res.Error != nil {
return out, res.Error
}
return
}

0 comments on commit 4889e6c

Please sign in to comment.