module.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2013 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. /*
  5. Package module provides functions for interacting with modules.
  6. The appengine package contains functions that report the identity of the app,
  7. including the module name.
  8. */
  9. package module // import "google.golang.org/appengine/module"
  10. import (
  11. "github.com/golang/protobuf/proto"
  12. "golang.org/x/net/context"
  13. "google.golang.org/appengine/internal"
  14. pb "google.golang.org/appengine/internal/modules"
  15. )
  16. // List returns the names of modules belonging to this application.
  17. func List(c context.Context) ([]string, error) {
  18. req := &pb.GetModulesRequest{}
  19. res := &pb.GetModulesResponse{}
  20. err := internal.Call(c, "modules", "GetModules", req, res)
  21. return res.Module, err
  22. }
  23. // NumInstances returns the number of instances of the given module/version.
  24. // If either argument is the empty string it means the default.
  25. func NumInstances(c context.Context, module, version string) (int, error) {
  26. req := &pb.GetNumInstancesRequest{}
  27. if module != "" {
  28. req.Module = &module
  29. }
  30. if version != "" {
  31. req.Version = &version
  32. }
  33. res := &pb.GetNumInstancesResponse{}
  34. if err := internal.Call(c, "modules", "GetNumInstances", req, res); err != nil {
  35. return 0, err
  36. }
  37. return int(*res.Instances), nil
  38. }
  39. // SetNumInstances sets the number of instances of the given module.version to the
  40. // specified value. If either module or version are the empty string it means the
  41. // default.
  42. func SetNumInstances(c context.Context, module, version string, instances int) error {
  43. req := &pb.SetNumInstancesRequest{}
  44. if module != "" {
  45. req.Module = &module
  46. }
  47. if version != "" {
  48. req.Version = &version
  49. }
  50. req.Instances = proto.Int64(int64(instances))
  51. res := &pb.SetNumInstancesResponse{}
  52. return internal.Call(c, "modules", "SetNumInstances", req, res)
  53. }
  54. // Versions returns the names of the versions that belong to the specified module.
  55. // If module is the empty string, it means the default module.
  56. func Versions(c context.Context, module string) ([]string, error) {
  57. req := &pb.GetVersionsRequest{}
  58. if module != "" {
  59. req.Module = &module
  60. }
  61. res := &pb.GetVersionsResponse{}
  62. err := internal.Call(c, "modules", "GetVersions", req, res)
  63. return res.GetVersion(), err
  64. }
  65. // DefaultVersion returns the default version of the specified module.
  66. // If module is the empty string, it means the default module.
  67. func DefaultVersion(c context.Context, module string) (string, error) {
  68. req := &pb.GetDefaultVersionRequest{}
  69. if module != "" {
  70. req.Module = &module
  71. }
  72. res := &pb.GetDefaultVersionResponse{}
  73. err := internal.Call(c, "modules", "GetDefaultVersion", req, res)
  74. return res.GetVersion(), err
  75. }
  76. // Start starts the specified version of the specified module.
  77. // If either module or version are the empty string, it means the default.
  78. func Start(c context.Context, module, version string) error {
  79. req := &pb.StartModuleRequest{}
  80. if module != "" {
  81. req.Module = &module
  82. }
  83. if version != "" {
  84. req.Version = &version
  85. }
  86. res := &pb.StartModuleResponse{}
  87. return internal.Call(c, "modules", "StartModule", req, res)
  88. }
  89. // Stop stops the specified version of the specified module.
  90. // If either module or version are the empty string, it means the default.
  91. func Stop(c context.Context, module, version string) error {
  92. req := &pb.StopModuleRequest{}
  93. if module != "" {
  94. req.Module = &module
  95. }
  96. if version != "" {
  97. req.Version = &version
  98. }
  99. res := &pb.StopModuleResponse{}
  100. return internal.Call(c, "modules", "StopModule", req, res)
  101. }