github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/google.golang.org/appengine/module/module.go (about)

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