本文整理汇总了Golang中google/golang.org/api/compute/v1.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了New函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: projectHasInstance
// projectHasInstance checks for all the possible zones if there's already an instance for the project.
// It returns the name of the zone at the first instance it finds, if any.
func (d *Deployer) projectHasInstance() (zone string, err error) {
s, err := compute.New(d.Client)
if err != nil {
return "", err
}
// TODO(mpl): make use of the handler's cached zones.
zl, err := compute.NewZonesService(s).List(d.Conf.Project).Do()
if err != nil {
return "", fmt.Errorf("could not get a list of zones: %v", err)
}
computeService, _ := compute.New(d.Client)
var zoneOnce sync.Once
var grp syncutil.Group
errc := make(chan error, 1)
zonec := make(chan string, 1)
timeout := time.NewTimer(30 * time.Second)
defer timeout.Stop()
for _, z := range zl.Items {
z := z
grp.Go(func() error {
list, err := computeService.Instances.List(d.Conf.Project, z.Name).Do()
if err != nil {
return fmt.Errorf("could not list existing instances: %v", err)
}
if len(list.Items) > 0 {
zoneOnce.Do(func() {
zonec <- z.Name
})
}
return nil
})
}
go func() {
errc <- grp.Err()
}()
// We block until either an instance was found in a zone, or all the instance
// listing is done. Or we timed-out.
select {
case err = <-errc:
return "", err
case zone = <-zonec:
// We voluntarily ignore any listing error if we found at least one instance
// because that's what we primarily want to report about.
return zone, nil
case <-timeout.C:
return "", errors.New("timed out")
}
}
示例2: getComputeClient
func getComputeClient() (*compute.Service, error) {
const retries = 10
const backoff = time.Second * 6
// Setup the gce client for provisioning instances
// Getting credentials on gce jenkins is flaky, so try a couple times
var err error
var cs *compute.Service
for i := 0; i < retries; i++ {
if i > 0 {
time.Sleep(backoff)
}
var client *http.Client
client, err = google.DefaultClient(oauth2.NoContext, compute.ComputeScope)
if err != nil {
continue
}
cs, err = compute.New(client)
if err != nil {
continue
}
return cs, nil
}
return nil, err
}
示例3: Init
// Init creates and and initializes the compute client.
func (g *Google) Init() error {
// Initialize auth: we re-use the code from docker-machine.
oauthClient, err := newOauthClient(g.context.GCETokenPath)
if err != nil {
return util.Errorf("could not get OAuth client: %v", err)
}
cSvc, err := compute.New(oauthClient)
if err != nil {
return util.Errorf("could not get Compute service: %v", err)
}
g.computeService = cSvc
ivSvc, err := resourceviews.New(oauthClient)
if err != nil {
return util.Errorf("could not get Compute service: %v", err)
}
g.instanceGroupsService = ivSvc
if err = g.checkProjectExists(); err != nil {
return util.Errorf("invalid project %q: %v", g.project, err)
}
log.Infof("validated project name: %q", g.project)
return nil
}
示例4: Get
// Get returns the Instance corresponding to the Project, Zone, and Name defined in the
// Deployer's Conf.
func (d *Deployer) Get() (*compute.Instance, error) {
computeService, err := compute.New(d.Client)
if err != nil {
return nil, err
}
return computeService.Instances.Get(d.Conf.Project, d.Conf.Zone, d.Conf.Name).Do()
}
示例5: newComputeUtil
// NewComputeUtil creates and initializes a ComputeUtil.
func newComputeUtil(driver *Driver) (*ComputeUtil, error) {
client, err := google.DefaultClient(oauth2.NoContext, raw.ComputeScope)
if err != nil {
return nil, err
}
service, err := raw.New(client)
if err != nil {
return nil, err
}
return &ComputeUtil{
zone: driver.Zone,
instanceName: driver.MachineName,
userName: driver.SSHUser,
project: driver.Project,
diskTypeURL: driver.DiskType,
address: driver.Address,
network: driver.Network,
preemptible: driver.Preemptible,
useInternalIP: driver.UseInternalIP,
useInternalIPOnly: driver.UseInternalIPOnly,
service: service,
zoneURL: apiURL + driver.Project + "/zones/" + driver.Zone,
globalURL: apiURL + driver.Project + "/global",
SwarmMaster: driver.SwarmMaster,
SwarmHost: driver.SwarmHost,
}, nil
}
示例6: ComputeService
func (c *Cred) ComputeService() (*compute.Service, error) {
cfg, err := google.JWTConfigFromJSON([]byte(c.Credentials), compute.ComputeScope)
if err != nil {
return nil, err
}
return compute.New(cfg.Client(context.Background()))
}
示例7: computeMain
func computeMain(client *http.Client, argv []string) {
if len(argv) != 2 {
fmt.Fprintln(os.Stderr, "Usage: compute project_id instance_name (to start an instance)")
return
}
service, _ := compute.New(client)
projectId := argv[0]
instanceName := argv[1]
prefix := "https://www.googleapis.com/compute/v1/projects/" + projectId
imageURL := "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-wheezy-v20140606"
zone := "us-central1-a"
// Show the current images that are available.
res, err := service.Images.List(projectId).Do()
log.Printf("Got compute.Images.List, err: %#v, %v", res, err)
instance := &compute.Instance{
Name: instanceName,
Description: "compute sample instance",
MachineType: prefix + "/zones/" + zone + "/machineTypes/n1-standard-1",
Disks: []*compute.AttachedDisk{
{
AutoDelete: true,
Boot: true,
Type: "PERSISTENT",
InitializeParams: &compute.AttachedDiskInitializeParams{
DiskName: "my-root-pd",
SourceImage: imageURL,
},
},
},
NetworkInterfaces: []*compute.NetworkInterface{
&compute.NetworkInterface{
AccessConfigs: []*compute.AccessConfig{
&compute.AccessConfig{
Type: "ONE_TO_ONE_NAT",
Name: "External NAT",
},
},
Network: prefix + "/global/networks/default",
},
},
ServiceAccounts: []*compute.ServiceAccount{
{
Email: "default",
Scopes: []string{
compute.DevstorageFull_controlScope,
compute.ComputeScope,
},
},
},
}
op, err := service.Instances.Insert(projectId, zone, instance).Do()
log.Printf("Got compute.Operation, err: %#v, %v", op, err)
}
示例8: New
// New returns a new instance of GceImages
func New(conf *GCEConfig) (*GceImages, error) {
var err error
if conf.ProjectID == "" {
return nil, errors.New("ProjectID is not set. Please check your configuration.")
}
// increase the timeout. Also we need to pass the client with the context itself
timeout := time.Second * 30
ctx := context.WithValue(oauth2.NoContext, oauth2.HTTPClient, &http.Client{
Transport: &http.Transport{TLSHandshakeTimeout: timeout},
Timeout: timeout,
})
var client *http.Client
// allowed scopes
scopes := []string{compute.ComputeScope}
// Recommended way is explicit passing of credentials json which can be
// downloaded from console.developers.google under APIs & Auth/Credentials
// section
if conf.AccountFile != "" {
// expand shell meta character
path, err := homedir.Expand(conf.AccountFile)
if err != nil {
return nil, err
}
jsonContent, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
jtwConfig, err := google.JWTConfigFromJSON(jsonContent, scopes...)
if err != nil {
return nil, err
}
client = jtwConfig.Client(ctx)
} else {
// Look for application default credentials, for more details, see:
// https://developers.google.com/accounts/docs/application-default-credentials
client, err = google.DefaultClient(ctx, scopes...)
if err != nil {
return nil, err
}
}
svc, err := compute.New(client)
if err != nil {
return nil, err
}
return &GceImages{
svc: compute.NewImagesService(svc),
config: conf,
}, nil
}
示例9: newGCECloud
// newGCECloud creates a new instance of GCECloud.
func newGCECloud(config io.Reader) (*GCECloud, error) {
projectID, zone, err := getProjectAndZone()
if err != nil {
return nil, err
}
// TODO: if we want to use this on a machine that doesn't have the http://metadata server
// e.g. on a user's machine (not VM) somewhere, we need to have an alternative for
// instance id lookup.
instanceID, err := getInstanceID()
if err != nil {
return nil, err
}
externalID, err := getCurrentExternalID()
if err != nil {
return nil, err
}
networkName, err := getNetworkName()
if err != nil {
return nil, err
}
tokenSource := google.ComputeTokenSource("")
if config != nil {
var cfg Config
if err := gcfg.ReadInto(&cfg, config); err != nil {
glog.Errorf("Couldn't read config: %v", err)
return nil, err
}
if cfg.Global.ProjectID != "" {
projectID = cfg.Global.ProjectID
}
if cfg.Global.NetworkName != "" {
networkName = cfg.Global.NetworkName
}
if cfg.Global.TokenURL != "" {
tokenSource = newAltTokenSource(cfg.Global.TokenURL)
}
}
client := oauth2.NewClient(oauth2.NoContext, tokenSource)
svc, err := compute.New(client)
if err != nil {
return nil, err
}
containerSvc, err := container.New(client)
if err != nil {
return nil, err
}
return &GCECloud{
service: svc,
containerService: containerSvc,
projectID: projectID,
zone: zone,
instanceID: instanceID,
externalID: externalID,
networkName: networkName,
metadataAccess: getMetadata,
}, nil
}
示例10: Create
// Create sets up and starts a Google Compute Engine instance as defined in d.Conf. It
// creates the necessary Google Storage buckets beforehand.
func (d *Deployer) Create(ctx *context.Context) (*compute.Instance, error) {
if err := d.checkProjectID(); err != nil {
return nil, err
}
computeService, _ := compute.New(d.Client)
storageService, _ := storage.New(d.Client)
fwc := make(chan error, 1)
go func() {
fwc <- d.setFirewall(ctx, computeService)
}()
config := cloudConfig(d.Conf)
const maxCloudConfig = 32 << 10 // per compute API docs
if len(config) > maxCloudConfig {
return nil, fmt.Errorf("cloud config length of %d bytes is over %d byte limit", len(config), maxCloudConfig)
}
// TODO(mpl): maybe add a wipe mode where we erase other instances before attempting to create.
if zone, err := d.projectHasInstance(); zone != "" {
return nil, instanceExistsError{
project: d.Conf.Project,
zone: zone,
}
} else if err != nil {
return nil, fmt.Errorf("could not scan project for existing instances: %v", err)
}
if err := d.setBuckets(storageService, ctx); err != nil {
return nil, fmt.Errorf("could not create buckets: %v", err)
}
if err := d.setupHTTPS(storageService); err != nil {
return nil, fmt.Errorf("could not setup HTTPS: %v", err)
}
if err := d.createInstance(computeService, ctx); err != nil {
return nil, fmt.Errorf("could not create compute instance: %v", err)
}
inst, err := computeService.Instances.Get(d.Conf.Project, d.Conf.Zone, d.Conf.Name).Do()
if err != nil {
return nil, fmt.Errorf("error getting instance after creation: %v", err)
}
if Verbose {
ij, _ := json.MarshalIndent(inst, "", " ")
log.Printf("Instance: %s", ij)
}
if err = <-fwc; err != nil {
return nil, fmt.Errorf("could not create firewall rules: %v", err)
}
return inst, nil
}
示例11: refreshZones
func (h *DeployHandler) refreshZones() error {
h.zonesMu.Lock()
defer h.zonesMu.Unlock()
defer func() {
h.regions = make([]string, 0, len(h.zones))
for r, _ := range h.zones {
h.regions = append(h.regions, r)
}
}()
// TODO(mpl): get projectID and access tokens from metadata once camweb is on GCE.
accountFile := os.Getenv("CAMLI_GCE_SERVICE_ACCOUNT")
if accountFile == "" {
h.Printf("No service account to query for the zones, using hard-coded ones instead.")
h.zones = backupZones
return nil
}
project := os.Getenv("CAMLI_GCE_PROJECT")
if project == "" {
h.Printf("No project we can query on to get the zones, using hard-coded ones instead.")
h.zones = backupZones
return nil
}
data, err := ioutil.ReadFile(accountFile)
if err != nil {
return err
}
conf, err := google.JWTConfigFromJSON(data, "https://www.googleapis.com/auth/compute.readonly")
if err != nil {
return err
}
s, err := compute.New(conf.Client(oauth2.NoContext))
if err != nil {
return err
}
rl, err := compute.NewRegionsService(s).List(project).Do()
if err != nil {
return fmt.Errorf("could not get a list of regions: %v", err)
}
h.zones = make(map[string][]string)
for _, r := range rl.Items {
zones := make([]string, 0, len(r.Zones))
for _, z := range r.Zones {
zone := path.Base(z)
if zone == "europe-west1-a" {
// Because even though the docs mark it as deprecated, it still shows up here, go figure.
continue
}
zone = strings.Replace(zone, r.Name, "", 1)
zones = append(zones, zone)
}
h.zones[r.Name] = zones
}
return nil
}
示例12: newComputeService
// newComputeService returns a new Compute Engine API Client,
// to use with Google App Engine.
func newComputeService(c context.Context) (service *compute.Service, err error) {
client := &http.Client{
Transport: &oauth2.Transport{
Source: google.AppEngineTokenSource(c, compute.ComputeScope),
Base: &urlfetch.Transport{
Context: c,
},
},
}
return compute.New(client)
}
示例13: newComputeService
func newComputeService(ctx context.Context) (*compute.Service, error) {
client, err := google.DefaultClient(ctx, compute.ComputeScope)
if err != nil {
return nil, err
}
computeService, err := compute.New(client)
if err != nil {
return nil, err
}
return computeService, nil
}
示例14: initGCE
func initGCE() error {
initGCECalled = true
// Use the staging project if not on GCE. This assumes the DefaultTokenSource
// credential used below has access to that project.
if !metadata.OnGCE() {
projectID = stagingProjectID
}
var err error
projectID, err = metadata.ProjectID()
if err != nil {
return fmt.Errorf("failed to get current GCE ProjectID: %v", err)
}
inStaging = projectID == stagingProjectID
if inStaging {
log.Printf("Running in staging cluster (%q)", projectID)
}
tokenSource, _ = google.DefaultTokenSource(oauth2.NoContext)
httpClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
serviceCtx = cloud.NewContext(projectID, httpClient)
projectZone, err = metadata.Get("instance/zone")
if err != nil || projectZone == "" {
return fmt.Errorf("failed to get current GCE zone: %v", err)
}
// Convert the zone from "projects/1234/zones/us-central1-a" to "us-central1-a".
projectZone = path.Base(projectZone)
if !hasComputeScope() {
return errors.New("The coordinator is not running with access to read and write Compute resources. VM support disabled.")
}
projectRegion = projectZone[:strings.LastIndex(projectZone, "-")] // "us-central1"
externalIP, err = metadata.ExternalIP()
if err != nil {
return fmt.Errorf("ExternalIP: %v", err)
}
computeService, _ = compute.New(httpClient)
errTryDeps = checkTryBuildDeps()
if errTryDeps != nil {
log.Printf("TryBot builders disabled due to error: %v", errTryDeps)
} else {
log.Printf("TryBot builders enabled.")
}
go gcePool.pollQuotaLoop()
return nil
}
示例15: MakeGoogleCompute
func MakeGoogleCompute(email string, privateKey string, project string) (*GoogleCompute, error) {
conf := &jwt.Config{
Email: email,
PrivateKey: []byte(privateKey),
Scopes: []string{"https://www.googleapis.com/auth/compute"},
TokenURL: google.JWTTokenURL,
}
client := conf.Client(oauth2.NoContext)
service, err := gcompute.New(client)
if err != nil {
return nil, err
}
return &GoogleCompute{
service: service,
project: project,
}, nil
}