Skip to content

Commit

Permalink
refactor: storage declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Aug 28, 2024
1 parent 1115a3f commit 567fb07
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
6 changes: 6 additions & 0 deletions internal/core/plugin_daemon/backwards_invocation/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ var (
},
"error": "permission denied, you need to enable app access in plugin manifest",
},
dify_invocation.INVOKE_TYPE_STORAGE: {
"func": func(declaration *plugin_entities.PluginDeclaration) bool {
return declaration.Resource.Permission.AllowInvokeStorage()
},
"error": "permission denied, you need to enable storage access in plugin manifest",
},
}
)

Expand Down
1 change: 0 additions & 1 deletion internal/core/plugin_manager/remote_manager/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ func TestAcceptConnection(t *testing.T) {
CreatedAt: time.Now(),
Resource: plugin_entities.PluginResourceRequirement{
Memory: 1,
Storage: 1,
Permission: nil,
},
Plugins: []string{
Expand Down
12 changes: 10 additions & 2 deletions internal/types/entities/plugin_entities/plugin_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type PluginPermissionRequirement struct {
Node *PluginPermissionNodeRequirement `json:"node" yaml:"node" validate:"omitempty"`
Endpoint *PluginPermissionEndpointRequirement `json:"endpoint" yaml:"endpoint" validate:"omitempty"`
App *PluginPermissionAppRequirement `json:"app" yaml:"app" validate:"omitempty"`
Storage *PluginPermissionStorageRequirement `json:"storage" yaml:"storage" validate:"omitempty"`
}

func (p *PluginPermissionRequirement) AllowInvokeTool() bool {
Expand Down Expand Up @@ -64,6 +65,10 @@ func (p *PluginPermissionRequirement) AllowRegistryEndpoint() bool {
return p != nil && p.Endpoint != nil && p.Endpoint.Enabled
}

func (p *PluginPermissionRequirement) AllowInvokeStorage() bool {
return p != nil && p.Storage != nil && p.Storage.Enabled
}

type PluginPermissionToolRequirement struct {
Enabled bool `json:"enabled" yaml:"enabled"`
}
Expand All @@ -90,11 +95,14 @@ type PluginPermissionAppRequirement struct {
Enabled bool `json:"enabled" yaml:"enabled"`
}

type PluginPermissionStorageRequirement struct {
Enabled bool `json:"enabled" yaml:"enabled"`
Size uint64 `json:"size" yaml:"size" validate:"min=1024,max=1073741824"` // min 1024 bytes, max 1G
}

type PluginResourceRequirement struct {
// Memory in bytes
Memory int64 `json:"memory" yaml:"memory" validate:"required"`
// Storage in bytes
Storage int64 `json:"storage" yaml:"storage" validate:"required"`
// Permission requirements
Permission *PluginPermissionRequirement `json:"permission" yaml:"permission" validate:"omitempty"`
}
Expand Down
32 changes: 30 additions & 2 deletions internal/types/entities/plugin_entities/plugin_declaration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ func preparePluginDeclaration() PluginDeclaration {
Author: "test",
CreatedAt: time.Now(),
Resource: PluginResourceRequirement{
Memory: 1,
Storage: 1,
Memory: 1,
Permission: &PluginPermissionRequirement{
Tool: &PluginPermissionToolRequirement{
Enabled: true,
Expand All @@ -28,6 +27,10 @@ func preparePluginDeclaration() PluginDeclaration {
Node: &PluginPermissionNodeRequirement{
Enabled: true,
},
Storage: &PluginPermissionStorageRequirement{
Enabled: true,
Size: 1024,
},
},
},
Plugins: []string{},
Expand Down Expand Up @@ -96,6 +99,10 @@ func TestPluginDeclarationFullTest(t *testing.T) {
return
}

if new_declaration.Resource.Permission.Storage == nil {
t.Errorf("storage permission is nil")
return
}
}

func TestPluginDeclarationIncorrectVersion(t *testing.T) {
Expand Down Expand Up @@ -134,6 +141,27 @@ func TestPluginUnsupportedArch(t *testing.T) {
}
}

func TestPluginStorageSizeTooSmallOrTooLarge(t *testing.T) {
declaration := preparePluginDeclaration()
declaration.Resource.Permission.Storage.Size = 1023
declaration_bytes := parser.MarshalJsonBytes(declaration)

_, err := parser.UnmarshalJsonBytes[PluginDeclaration](declaration_bytes)
if err == nil {
t.Errorf("failed to validate storage size")
return
}

declaration.Resource.Permission.Storage.Size = 1073741825
declaration_bytes = parser.MarshalJsonBytes(declaration)

_, err = parser.UnmarshalJsonBytes[PluginDeclaration](declaration_bytes)
if err == nil {
t.Errorf("failed to validate storage size")
return
}
}

func TestPluginDeclarationIncorrectType(t *testing.T) {
declaration := preparePluginDeclaration()
declaration.Type = "test"
Expand Down

0 comments on commit 567fb07

Please sign in to comment.