capability.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2011 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 capability exposes information about outages and scheduled downtime
  6. for specific API capabilities.
  7. This package does not work in App Engine "flexible environment".
  8. Example:
  9. if !capability.Enabled(c, "datastore_v3", "write") {
  10. // show user a different page
  11. }
  12. */
  13. package capability // import "google.golang.org/appengine/capability"
  14. import (
  15. "golang.org/x/net/context"
  16. "google.golang.org/appengine/internal"
  17. "google.golang.org/appengine/log"
  18. pb "google.golang.org/appengine/internal/capability"
  19. )
  20. // Enabled returns whether an API's capabilities are enabled.
  21. // The wildcard "*" capability matches every capability of an API.
  22. // If the underlying RPC fails (if the package is unknown, for example),
  23. // false is returned and information is written to the application log.
  24. func Enabled(ctx context.Context, api, capability string) bool {
  25. req := &pb.IsEnabledRequest{
  26. Package: &api,
  27. Capability: []string{capability},
  28. }
  29. res := &pb.IsEnabledResponse{}
  30. if err := internal.Call(ctx, "capability_service", "IsEnabled", req, res); err != nil {
  31. log.Warningf(ctx, "capability.Enabled: RPC failed: %v", err)
  32. return false
  33. }
  34. switch *res.SummaryStatus {
  35. case pb.IsEnabledResponse_ENABLED,
  36. pb.IsEnabledResponse_SCHEDULED_FUTURE,
  37. pb.IsEnabledResponse_SCHEDULED_NOW:
  38. return true
  39. case pb.IsEnabledResponse_UNKNOWN:
  40. log.Errorf(ctx, "capability.Enabled: unknown API capability %s/%s", api, capability)
  41. return false
  42. default:
  43. return false
  44. }
  45. }