use Google\Cloud\Iot\V1\DeviceManagerClient;
use Google\Cloud\Iot\V1\DeviceRegistry;
use Google\Cloud\Iot\V1\EventNotificationConfig;
/**
* Creates a registry if it doesn't exist and prints the result.
*
* @param string $registryId IOT Device Registry ID
* @param string $pubsubTopic PubSub topic name for the new registry's event change notification.
* @param string $projectId Google Cloud project ID
* @param string $location (Optional) Google Cloud region
*/
function create_registry(
$registryId,
$pubsubTopic,
$projectId,
$location = 'us-central1'
) {
print('Creating Registry' . 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);
$pubsubTopicPath = sprintf('projects/%s/topics/%s', $projectId, $pubsubTopic);
$eventNotificationConfig = (new EventNotificationConfig)
->setPubsubTopicName($pubsubTopicPath);
$registry = (new DeviceRegistry)
->setId($registryId)
->setEventNotificationConfigs([$eventNotificationConfig]);
$registry = $deviceManager->createDeviceRegistry($locationName, $registry);
printf('Id: %s, Name: %s' . PHP_EOL,
$registry->getId(),
$registry->getName());
}
# project_id = "Your Google Cloud project ID"
# location_id = "The Cloud region that you created the registry in"
# registry_id = "The Google Cloud IoT Core device registry identifier"
# pubsub_topic = "The Google Cloud PubSub topic to use for this registry"
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 project / location where the registry is created.
parent = "projects/#{project_id}/locations/#{location_id}"
registry = Cloudiot::DeviceRegistry.new
registry.id = registry_id
registry.event_notification_configs = [Cloudiot::EventNotificationConfig.new]
registry.event_notification_configs[0].pubsub_topic_name = pubsub_topic
registry = iot_client.create_project_location_registry(
parent, registry
)
puts "Created registry: #{registry.name}"