Skip to content

Commit

Permalink
Merge pull request #267 from kthcloud/dev
Browse files Browse the repository at this point in the history
add discovery route, fix many bugs with user and team notifications
  • Loading branch information
saffronjam authored Nov 15, 2023
2 parents cdaf681 + 8e0e4a0 commit f07dc3a
Show file tree
Hide file tree
Showing 31 changed files with 344 additions and 150 deletions.
2 changes: 1 addition & 1 deletion models/config/helpers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
roleModel "go-deploy/models/config/role"
roleModel "go-deploy/models/sys/role"
)

func (e *ConfigType) GetRole(roleName string) *roleModel.Role {
Expand Down
4 changes: 3 additions & 1 deletion models/config/models.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"go-deploy/models/config/role"
"go-deploy/models/sys/role"
"k8s.io/client-go/kubernetes"
)

Expand Down Expand Up @@ -71,6 +71,8 @@ type VM struct {
}

type ConfigType struct {
Version string `yaml:"version"`

Port int `yaml:"port"`
ExternalUrl string `yaml:"externalUrl"`
Manager string `yaml:"manager"`
Expand Down
26 changes: 0 additions & 26 deletions models/config/role/models.go

This file was deleted.

6 changes: 6 additions & 0 deletions models/dto/body/discover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package body

type DiscoverRead struct {
Version string `json:"version"`
Roles []Role `json:"roles"`
}
2 changes: 1 addition & 1 deletion models/dto/body/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ type NotificationRead struct {
}

type NotificationUpdate struct {
ReadAt *time.Time `json:"readAt"`
Read bool `json:"read"`
}
8 changes: 8 additions & 0 deletions models/dto/body/role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package body

type Role struct {
Name string `json:"name"`
Description string `json:"description"`
Permissions []string `json:"permissions"`
Quota *Quota `json:"quota,omitempty"`
}
17 changes: 10 additions & 7 deletions models/dto/body/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ type PublicKey struct {
}

type Quota struct {
Deployments int `json:"deployments"`
CpuCores int `json:"cpuCores"`
RAM int `json:"ram"`
DiskSize int `json:"diskSize"`
Snapshots int `json:"snapshots"`
GpuLeaseDuration float64 `json:"gpuLeaseDuration"` // in hours
}

type Usage struct {
Deployments int `json:"deployments"`
CpuCores int `json:"cpuCores"`
RAM int `json:"ram"`
DiskSize int `json:"diskSize"`
Snapshots int `json:"snapshots"`
}

type Role struct {
Name string `json:"name"`
Description string `json:"description"`
Permissions []string `json:"permissions"`
}

type SmallUserRead struct {
ID string `json:"id"`
Username string `json:"username"`
Expand All @@ -40,7 +43,7 @@ type UserRead struct {
Admin bool `json:"admin"`

Quota Quota `json:"quota"`
Usage Quota `json:"usage"`
Usage Usage `json:"usage"`

StorageURL *string `json:"storageUrl,omitempty"`
}
Expand Down
10 changes: 10 additions & 0 deletions models/sys/discover/discover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package discover

import (
"go-deploy/models/sys/role"
)

type Discover struct {
Version string
Roles []role.Role
}
15 changes: 15 additions & 0 deletions models/sys/discover/dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package discover

import "go-deploy/models/dto/body"

func (d *Discover) ToDTO() *body.DiscoverRead {
roles := make([]body.Role, len(d.Roles))
for i, r := range d.Roles {
roles[i] = r.ToDTO(true)
}

return &body.DiscoverRead{
Version: d.Version,
Roles: roles,
}
}
4 changes: 2 additions & 2 deletions models/sys/notification/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (client *Client) AddPagination(page, pageSize int) *Client {
return client
}

func (client *Client) RestrictToOwner(ownerID string) *Client {
client.ResourceClient.AddExtraFilter(bson.D{{"ownerId", ownerID}})
func (client *Client) RestrictToUserID(ownerID string) *Client {
client.ResourceClient.AddExtraFilter(bson.D{{"userId", ownerID}})
client.RestrictUserID = &ownerID

return client
Expand Down
6 changes: 5 additions & 1 deletion models/sys/notification/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ func (client *Client) UpdateWithParamsByID(id string, params *UpdateParams) erro

models.AddIfNotNil(&update, "readAt", params.ReadAt)

return client.UpdateWithBsonByID(id, update)
if len(update) == 0 {
return nil
}

return client.SetWithBsonByID(id, update)
}
5 changes: 4 additions & 1 deletion models/sys/notification/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ type UpdateParams struct {
}

func (u *UpdateParams) FromDTO(dto *body.NotificationUpdate) {
u.ReadAt = dto.ReadAt
if dto.Read {
now := time.Now()
u.ReadAt = &now
}
}
41 changes: 41 additions & 0 deletions models/sys/role/dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package role

import (
"github.com/fatih/structs"
"go-deploy/models/dto/body"
)

func (r *Role) ToDTO(includeQuota bool) body.Role {
permissionsStructMap := structs.Map(r.Permissions)
permissions := make([]string, 0)
for name, value := range permissionsStructMap {
hasPermission, ok := value.(bool)
if ok && hasPermission {
permissions = append(permissions, name)
}
}

var quota *body.Quota
if includeQuota {
dto := r.Quotas.ToDTO()
quota = &dto
}

return body.Role{
Name: r.Name,
Description: r.Description,
Permissions: permissions,
Quota: quota,
}
}

func (q *Quotas) ToDTO() body.Quota {
return body.Quota{
Deployments: q.Deployments,
CpuCores: q.CpuCores,
RAM: q.RAM,
DiskSize: q.DiskSize,
Snapshots: q.Snapshots,
GpuLeaseDuration: q.GpuLeaseDuration,
}
}
26 changes: 26 additions & 0 deletions models/sys/role/role.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package role

type Quotas struct {
Deployments int `yaml:"deployments" structs:"deployments"`
CpuCores int `yaml:"cpuCores" structs:"cpuCores"`
RAM int `yaml:"ram" structs:"ram"`
DiskSize int `yaml:"diskSize" structs:"diskSize"`
Snapshots int `yaml:"snapshots" structs:"snapshots"`
GpuLeaseDuration float64 `yaml:"gpuLeaseDuration" structs:"gpuLeaseDuration"` // in hours
}

type Permissions struct {
ChooseZone bool `yaml:"chooseZone" structs:"chooseZone"`
ChooseGPU bool `yaml:"chooseGpu" structs:"chooseGpu"`
UseCustomDomains bool `yaml:"useCustomDomains" structs:"useCustomDomains"`
UseGPUs bool `yaml:"useGpus" structs:"useGpus"`
UsePrivilegedGPUs bool `yaml:"usePrivilegedGpus" structs:"usePrivilegedGpus"`
}

type Role struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
IamGroup string `yaml:"iamGroup"`
Permissions Permissions `yaml:"permissions"`
Quotas Quotas `yaml:"quotas"`
}
19 changes: 6 additions & 13 deletions models/sys/team/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (t *Team) ToDTO(getMember func(*Member) *body.TeamMember, getResourceName f
}
}

func (params *CreateParams) FromDTO(teamCreateDTO *body.TeamCreate, getResourceFunc func(string) *Resource) {
func (params *CreateParams) FromDTO(teamCreateDTO *body.TeamCreate, getResourceFunc func(string) *Resource, getMemberFunc func(*body.TeamMemberCreate) *Member) {
params.Name = teamCreateDTO.Name
params.MemberMap = make(map[string]Member)

Expand All @@ -44,19 +44,16 @@ func (params *CreateParams) FromDTO(teamCreateDTO *body.TeamCreate, getResourceF
}
}

for _, member := range teamCreateDTO.Members {
params.MemberMap[member.ID] = Member{
ID: member.ID,
TeamRole: member.TeamRole,
}
for _, memberDTO := range teamCreateDTO.Members {
params.MemberMap[memberDTO.ID] = *getMemberFunc(&memberDTO)
}
}

func (params *JoinParams) FromDTO(teamJoinDTO *body.TeamJoin) {
params.InvitationCode = teamJoinDTO.InvitationCode
}

func (params *UpdateParams) FromDTO(teamUpdateDTO *body.TeamUpdate, getResourceFunc func(string) *Resource) {
func (params *UpdateParams) FromDTO(teamUpdateDTO *body.TeamUpdate, getResourceFunc func(string) *Resource, getMemberFunc func(*body.TeamMemberUpdate) *Member) {
params.Name = teamUpdateDTO.Name
params.Description = teamUpdateDTO.Description

Expand All @@ -72,12 +69,8 @@ func (params *UpdateParams) FromDTO(teamUpdateDTO *body.TeamUpdate, getResourceF

if teamUpdateDTO.Members != nil {
memberMap := make(map[string]Member)
for _, member := range *teamUpdateDTO.Members {
memberMap[member.ID] = Member{
ID: member.ID,
// temporary until we have a use case for this
TeamRole: MemberRoleAdmin,
}
for _, memberDTO := range *teamUpdateDTO.Members {
memberMap[memberDTO.ID] = *getMemberFunc(&memberDTO)
}
params.MemberMap = &memberMap
}
Expand Down
9 changes: 9 additions & 0 deletions models/sys/team/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ func (t *Team) GetMemberMap() map[string]Member {
return t.MemberMap
}

func (t *Team) GetMember(id string) *Member {
res, ok := t.GetMemberMap()[id]
if !ok {
return nil
}

return &res
}

func (t *Team) GetResourceMap() map[string]Resource {
if t.ResourceMap == nil {
t.ResourceMap = make(map[string]Resource)
Expand Down
44 changes: 14 additions & 30 deletions models/sys/user/dto.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package user

import (
"github.com/fatih/structs"
roleModel "go-deploy/models/config/role"
"go-deploy/models/dto/body"
roleModel "go-deploy/models/sys/role"
"log"
)

Expand All @@ -28,15 +27,6 @@ func (user *User) ToDTO(effectiveRole *roleModel.Role, usage *Usage, storageURL
}
}

permissionsStructMap := structs.Map(effectiveRole.Permissions)
permissions := make([]string, 0)
for name, value := range permissionsStructMap {
hasPermission, ok := value.(bool)
if ok && hasPermission {
permissions = append(permissions, name)
}
}

userRead := body.UserRead{
ID: user.ID,
Username: user.Username,
Expand All @@ -46,30 +36,24 @@ func (user *User) ToDTO(effectiveRole *roleModel.Role, usage *Usage, storageURL
PublicKeys: publicKeys,
Onboarded: user.Onboarded,

Role: body.Role{
Name: effectiveRole.Name,
Description: effectiveRole.Description,
Permissions: permissions,
},
Role: effectiveRole.ToDTO(false),
Admin: user.IsAdmin,

Quota: body.Quota{
Deployments: effectiveRole.Quotas.Deployments,
CpuCores: effectiveRole.Quotas.CpuCores,
RAM: effectiveRole.Quotas.RAM,
DiskSize: effectiveRole.Quotas.DiskSize,
Snapshots: effectiveRole.Quotas.Snapshots,
},
Usage: body.Quota{
Deployments: usage.Deployments,
CpuCores: usage.CpuCores,
RAM: usage.RAM,
DiskSize: usage.DiskSize,
Snapshots: usage.Snapshots,
},
Quota: effectiveRole.Quotas.ToDTO(),
Usage: usage.ToDTO(),

StorageURL: storageURL,
}

return userRead
}

func (usage *Usage) ToDTO() body.Usage {
return body.Usage{
Deployments: usage.Deployments,
CpuCores: usage.CpuCores,
RAM: usage.RAM,
DiskSize: usage.DiskSize,
Snapshots: usage.Snapshots,
}
}
2 changes: 1 addition & 1 deletion pkg/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func Create(options *Options) *App {
}

go func() {
err := httpServer.ListenAndServe()
err = httpServer.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalln(fmt.Errorf("failed to start http server. details: %w", err))
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func SetupEnvironment() {
log.Fatalf(makeError(err).Error())
}

log.Println("go-deploy", Config.Version)

log.Println("loaded", len(Config.Roles), "roles in order:")
for _, role := range Config.Roles {
log.Println("\t", role.Name)
Expand Down
Loading

0 comments on commit f07dc3a

Please sign in to comment.