plugin_responses.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package types
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "sort"
  6. )
  7. // PluginsListResponse contains the response for the Engine API
  8. type PluginsListResponse []*Plugin
  9. const (
  10. authzDriver = "AuthzDriver"
  11. graphDriver = "GraphDriver"
  12. ipamDriver = "IpamDriver"
  13. networkDriver = "NetworkDriver"
  14. volumeDriver = "VolumeDriver"
  15. )
  16. // UnmarshalJSON implements json.Unmarshaler for PluginInterfaceType
  17. func (t *PluginInterfaceType) UnmarshalJSON(p []byte) error {
  18. versionIndex := len(p)
  19. prefixIndex := 0
  20. if len(p) < 2 || p[0] != '"' || p[len(p)-1] != '"' {
  21. return fmt.Errorf("%q is not a plugin interface type", p)
  22. }
  23. p = p[1 : len(p)-1]
  24. loop:
  25. for i, b := range p {
  26. switch b {
  27. case '.':
  28. prefixIndex = i
  29. case '/':
  30. versionIndex = i
  31. break loop
  32. }
  33. }
  34. t.Prefix = string(p[:prefixIndex])
  35. t.Capability = string(p[prefixIndex+1 : versionIndex])
  36. if versionIndex < len(p) {
  37. t.Version = string(p[versionIndex+1:])
  38. }
  39. return nil
  40. }
  41. // MarshalJSON implements json.Marshaler for PluginInterfaceType
  42. func (t *PluginInterfaceType) MarshalJSON() ([]byte, error) {
  43. return json.Marshal(t.String())
  44. }
  45. // String implements fmt.Stringer for PluginInterfaceType
  46. func (t PluginInterfaceType) String() string {
  47. return fmt.Sprintf("%s.%s/%s", t.Prefix, t.Capability, t.Version)
  48. }
  49. // PluginPrivilege describes a permission the user has to accept
  50. // upon installing a plugin.
  51. type PluginPrivilege struct {
  52. Name string
  53. Description string
  54. Value []string
  55. }
  56. // PluginPrivileges is a list of PluginPrivilege
  57. type PluginPrivileges []PluginPrivilege
  58. func (s PluginPrivileges) Len() int {
  59. return len(s)
  60. }
  61. func (s PluginPrivileges) Less(i, j int) bool {
  62. return s[i].Name < s[j].Name
  63. }
  64. func (s PluginPrivileges) Swap(i, j int) {
  65. sort.Strings(s[i].Value)
  66. sort.Strings(s[j].Value)
  67. s[i], s[j] = s[j], s[i]
  68. }