// getFromFamily calls GCE API and retrieves the size of provided image. func (s *Stack) getDiskSize(currentProject string, computeService *compute.Service) func(string, string) int { if computeService == nil { return nil } imagesService := compute.NewImagesService(computeService) return func(project, image string) int { if project == "" { project = currentProject } computeImg, err := imagesService.Get(project, image).Do() if err != nil { s.Log.Warning("cannot find image %q: %v (project: %q)", image, err, project) if project == currentProject { return 0 } project = currentProject // Fall-back to current project. if computeImg, err = imagesService.Get(project, image).Do(); err != nil { s.Log.Warning("cannot find image %q in stack's project: %v", image, err) return 0 } } if computeImg == nil || computeImg.DiskSizeGb == 0 { s.Log.Warning("no size metadata for image: %q (project: %q)", image, project) return 0 } return int(computeImg.DiskSizeGb) } }
// 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 }
// getFromFamily calls GCE API and retrieves the latest image that is a part of // provided family. If an error occurs, this function is no-op. func (s *Stack) getFromFamily(computeService *compute.Service) func(string, string) string { if computeService == nil { return nil } imagesService := compute.NewImagesService(computeService) return func(project, family string) string { image, err := imagesService.GetFromFamily(project, family).Do() if err != nil { s.Log.Warning("cannot create image service: %s", err) return "" } if image == nil || image.Name == "" { s.Log.Warning("no image for family: %s (project: %s)", family, project) return "" } return image.Name } }