Skip to content

Commit

Permalink
Merge pull request #167 from kthcloud/dev
Browse files Browse the repository at this point in the history
add permissions to user dto, hotfix bug privileged gpu was not fetched
  • Loading branch information
saffronjam authored Sep 5, 2023
2 parents 799da51 + 477b267 commit b83605f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 19 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ require (
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ github.com/emicklei/go-restful/v3 v3.8.0 h1:eCZ8ulSerjdAiaNpF7GxXIE7ZCMo1moN1qX+
github.com/emicklei/go-restful/v3 v3.8.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gin-contrib/cors v1.4.0 h1:oJ6gwtUl3lqV0WEIwM/LxPF1QZ5qe2lGWdY2+bz7y0g=
Expand Down
15 changes: 9 additions & 6 deletions models/dto/body/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ type Quota struct {
}

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

type UserRead struct {
ID string `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Role Role `json:"role"`
Admin bool `json:"admin"`
Quota Quota `json:"quota"`
Usage Quota `json:"usage"`
PublicKeys []PublicKey `json:"publicKeys"`

Role Role `json:"role"`
Admin bool `json:"admin"`

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

type UserUpdate struct {
Expand Down
26 changes: 14 additions & 12 deletions models/sys/enviroment/role/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ type Quotas struct {
Snapshots int `yaml:"snapshots"`
}

type Permissions struct {
ChooseZone bool `yaml:"chooseZone" structs:"chooseZone"`
ChooseGPU bool `yaml:"chooseGpu" structs:"chooseGpu"`
UseGPUs bool `yaml:"useGpus" structs:"useGpus"`
UsePrivilegedGPUs bool `yaml:"usePrivilegedGpus" structs:"usePrivilegedGpus"`
// in hours
GpuLeaseDuration float64 `yaml:"gpuLeaseDuration" structs:"gpuLeaseDuration"`
}

type Role struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
IamGroup string `yaml:"iamGroup"`
Permissions struct {
ChooseZone bool `yaml:"chooseZone"`
ChooseGPU bool `yaml:"chooseGpu"`
UseGPUs bool `yaml:"useGpus"`
UsePrivilegedGPUs bool `yaml:"usePrivilegedGpus"`
// in hours
GpuLeaseDuration float64 `yaml:"gpuLeaseDuration"`
}
Quotas Quotas `yaml:"quotas"`
Name string `yaml:"name"`
Description string `yaml:"description"`
IamGroup string `yaml:"iamGroup"`
Permissions Permissions `yaml:"permissions"`
Quotas Quotas `yaml:"quotas"`
}
11 changes: 11 additions & 0 deletions models/sys/user/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package user
import (
"context"
"fmt"
"github.com/fatih/structs"
"go-deploy/models"
"go-deploy/models/dto/body"
roleModel "go-deploy/models/sys/enviroment/role"
Expand Down Expand Up @@ -31,6 +32,15 @@ func (u *User) ToDTO(effectiveRole *roleModel.Role, usage *Usage) body.UserRead
}
}

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: u.ID,
Username: u.Username,
Expand All @@ -39,6 +49,7 @@ func (u *User) ToDTO(effectiveRole *roleModel.Role, usage *Usage) body.UserRead
Role: body.Role{
Name: effectiveRole.Name,
Description: effectiveRole.Description,
Permissions: permissions,
},
Quota: body.Quota{
Deployments: effectiveRole.Quotas.Deployments,
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/v1_vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func attachGPU(context *sys.ClientContext, requestBody *body.VmUpdate, auth *ser
return
}

gpu, err := vm_service.GetGpuByID(*requestBody.GpuID, false)
gpu, err := vm_service.GetGpuByID(*requestBody.GpuID, true)
if err != nil {
context.ErrorResponse(http.StatusInternalServerError, status_codes.Error, fmt.Sprintf("Failed to get gpu: %s", err))
return
Expand Down

0 comments on commit b83605f

Please sign in to comment.