Google Cloud IoT Core is being retired on August 16, 2023. Contact your Google Cloud account team for more information.

List registries

Retrieve device registries given a project or region.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Go

For more information, see the Cloud IoT Core Go API reference documentation.

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


// listRegistries gets the names of device registries given a project / region.
func listRegistries(w io.Writer, projectID string, region string) ([]*cloudiot.DeviceRegistry, error) {
	// Authorize the client using Application Default Credentials.
	// See https://g.co/dv/identity/protocols/application-default-credentials
	ctx := context.Background()
	httpClient, err := google.DefaultClient(ctx, cloudiot.CloudPlatformScope)
	if err != nil {
		return nil, err
	}
	client, err := cloudiot.New(httpClient)
	if err != nil {
		return nil, err
	}

	parent := fmt.Sprintf("projects/%s/locations/%s", projectID, region)
	response, err := client.Projects.Locations.Registries.List(parent).Do()
	if err != nil {
		return nil, err
	}

	if len(response.DeviceRegistries) == 0 {
		fmt.Fprintln(w, "No registries found")
		return response.DeviceRegistries, nil
	}

	fmt.Fprintf(w, "%d registries:\n", len(response.DeviceRegistries))
	for _, registry := range response.DeviceRegistries {
		fmt.Fprintf(w, "\t%s\n", registry.Name)
	}

	return response.DeviceRegistries, nil
}

Java

For more information, see the Cloud IoT Core Java API reference documentation.

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

/** Lists all of the registries associated with the given project. */
protected static void listRegistries(String projectId, String cloudRegion)
    throws GeneralSecurityException, IOException {
  GoogleCredentials credential =
      GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all());
  JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
  HttpRequestInitializer init = new HttpCredentialsAdapter(credential);
  final CloudIot service =
      new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init)
          .setApplicationName(APP_NAME)
          .build();

  final String projectPath = "projects/" + projectId + "/locations/" + cloudRegion;

  List<DeviceRegistry> registries =
      service
          .projects()
          .locations()
          .registries()
          .list(projectPath)
          .execute()
          .getDeviceRegistries();

  if (registries != null) {
    System.out.println("Found " + registries.size() + " registries");
    for (DeviceRegistry r : registries) {
      System.out.println("Id: " + r.getId());
      System.out.println("Name: " + r.getName());
      if (r.getMqttConfig() != null) {
        System.out.println("Config: " + r.getMqttConfig().toPrettyString());
      }
      System.out.println();
    }
  } else {
    System.out.println("Project has no registries.");
  }
}

Node.js

For more information, see the Cloud IoT Core Node.js API reference documentation.

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

// const cloudRegion = 'us-central1';
// const projectId = 'adjective-noun-123';
const iot = require('@google-cloud/iot');

const newClient = new iot.v1.DeviceManagerClient({
  // optional auth parameters.
});

async function listDeviceRegistries() {
  // Construct request
  // Iterate over all elements.
  const formattedParent = newClient.locationPath(projectId, cloudRegion);

  const [response] = await newClient.listDeviceRegistries({
    parent: formattedParent,
  });
  const resources = response;
  console.log('Current registries in project:\n', resources);
}

listDeviceRegistries();

PHP

For more information, see the Cloud IoT Core PHP API reference documentation.

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

use Google\Cloud\Iot\V1\DeviceManagerClient;

/**
 * List all registries in the project.
 *
 * @param string $projectId Google Cloud project ID
 * @param string $location (Optional) Google Cloud region
 */
function list_registries(
    $projectId,
    $location = 'us-central1'
) {
    print('Listing Registries' . PHP_EOL);

    // The Google Cloud Client Library automatically checks the environment
    // variable GOOGLE_APPLICATION_CREDENTIALS for the Service Account
    // credentials, and defaults scopes to [
    //    'https://www.googleapis.com/auth/cloud-platform',
    //    'https://www.googleapis.com/auth/cloudiot'
    // ].
    $deviceManager = new DeviceManagerClient();
    $locationName = $deviceManager->locationName($projectId, $location);

    $response = $deviceManager->listDeviceRegistries($locationName);

    foreach ($response->iterateAllElements() as $registry) {
        printf(' - Id: %s, Name: %s' . PHP_EOL,
            $registry->getId(),
            $registry->getName());
    }
}

Python

For more information, see the Cloud IoT Core Python API reference documentation.

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

# project_id = 'YOUR_PROJECT_ID'
# cloud_region = 'us-central1'
print("Listing Registries")
client = iot_v1.DeviceManagerClient()
parent = f"projects/{project_id}/locations/{cloud_region}"

registries = list(client.list_device_registries(request={"parent": parent}))
for registry in registries:
    print("id: {}\n\tname: {}".format(registry.id, registry.name))

return registries

Ruby

For more information, see the Cloud IoT Core Ruby API reference documentation.

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

# project_id  = "Your Google Cloud project ID"
# location_id = "The Cloud region to list registries in"

require "google/apis/cloudiot_v1"

# Initialize the client and authenticate with the specified scope
Cloudiot   = Google::Apis::CloudiotV1
iot_client = Cloudiot::CloudIotService.new
iot_client.authorization = Google::Auth.get_application_default(
  "https://www.googleapis.com/auth/cloud-platform"
)

# The resource name of the location associated with the project
resource = "projects/#{project_id}/locations/#{location_id}"

# List the devices in the provided region
registries = iot_client.list_project_location_registries(
  resource
)

puts "Registries:"
if registries.device_registries && registries.device_registries.any?
  registries.device_registries.each { |registry| puts "\t#{registry.id}" }
else
  puts "\tNo device registries found in this region for your project."
end

What's next

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