Get the newest image from a given family

This sample retrieves the newest image that is part of a given family in a project.

Code sample

Go

Before trying this sample, follow the Go setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Go API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

import(
"context"
"fmt"
"io"
compute"cloud.google.com/go/compute/apiv1"
computepb"cloud.google.com/go/compute/apiv1/computepb"
)
// Geg a disk image from family for the given project
funcgetDiskImageFromFamily(
wio.Writer,
projectID,familystring,
)(*computepb.Image,error){
// projectID := "your_project_id"
// family := "my_family"
ctx:=context.Background()
imagesClient,err:=compute.NewImagesRESTClient (ctx)
iferr!=nil{
returnnil,fmt.Errorf("NewImagesRESTClient: %w",err)
}
deferimagesClient.Close()
source_req:=&computepb.GetFromFamilyImageRequest{
Project:projectID,
Family:family,
}
newestImage,err:=imagesClient.GetFromFamily(ctx,source_req)
iferr!=nil{
returnnil,fmt.Errorf("unable to get image: %w",err)
}
fmt.Fprintf(w,"Newest disk image was found: %s\n",*newestImage.Name )
returnnewestImage,nil
}

Java

Before trying this sample, follow the Java setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Java API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


importcom.google.cloud.compute.v1.GetFromFamilyImageRequest ;
importcom.google.cloud.compute.v1.Image ;
importcom.google.cloud.compute.v1.ImagesClient ;
importjava.io.IOException;
publicclass GetImageFromFamily{
publicstaticvoidmain(String[]args)throwsIOException{
// TODO(developer): Replace these variables before running the sample.
// Project ID or project number of the Google Cloud project you want to use.
StringprojectId="debian-cloud";
// Name of the image family you want to retrieve the image from.
// List of public operating system (OS) images:
// https://cloud.google.com/compute/docs/images/os-details
Stringfamily="debian-11";
getImageFromFamily(projectId,family);
}
// Retrieve the newest image that is part of a given family in a project.
publicstaticImage getImageFromFamily(StringprojectId,Stringfamily)throwsIOException{
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try(ImagesClient client=ImagesClient .create()){
GetFromFamilyImageRequest request=GetFromFamilyImageRequest .newBuilder()
.setProject(projectId)
.setFamily(family)
.build();
Image image=client.getFromFamily (request);
System.out.printf("Image '%s' has been retrieved successfully",image.getName ());
returnimage;
}
}
}

Python

Before trying this sample, follow the Python setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Python API reference documentation.

To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

fromgoogle.cloudimport compute_v1
defget_image_from_family(project: str, family: str) -> compute_v1.Image:
"""
 Retrieve the newest image that is part of a given family in a project.
 Args:
 project: project ID or project number of the Cloud project you want to get image from.
 family: name of the image family you want to get image from.
 Returns:
 An Image object.
 """
 image_client = compute_v1.ImagesClient()
 # List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details
 newest_image = image_client.get_from_family(project=project, family=family)
 return newest_image

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.