diff --git a/cmd/server/main.go b/cmd/server/main.go index ea1a858..93e444a 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -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 @@ -26,6 +27,7 @@ var ( applicationAccessor *application.ApplicationAccessor cloudAccountAccessor *cloudAccount.CloudAccountAccessor organizationAccessor *organization.OrganizationAccessor + userAccessor *user.UserAccessor apiClient _apiClient.ApiClient ) @@ -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, "") diff --git a/cmd/server/organization_status.go b/cmd/server/organization_status.go index 497a171..911b2db 100644 --- a/cmd/server/organization_status.go +++ b/cmd/server/organization_status.go @@ -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 } diff --git a/cmd/server/server b/cmd/server/server index cea31af..a2aee27 100755 Binary files a/cmd/server/server and b/cmd/server/server differ diff --git a/go.mod b/go.mod index 02ba31d..e9fee8f 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/internal/organization/organization.go b/internal/organization/organization.go index 1db73d7..2c0300c 100644 --- a/internal/organization/organization.go +++ b/internal/organization/organization.go @@ -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" ) @@ -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 +} diff --git a/internal/user/user.go b/internal/user/user.go new file mode 100644 index 0000000..de3509a --- /dev/null +++ b/internal/user/user.go @@ -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 +}