To change the target Google Cloud Platform (GCP) project for publishing messages from a registry to a different GCP project, follow these steps:

  1. Identify your original project (e.g., skysharma-gcp), your target project you want to publish (e.g., skysharma-gcp-2), your Pub/Sub topic (e.g., root), and subscription (e.g., root-sub).

  2. Give your original service account permissions in the target project as per Cloud Pub/Sub Documentation.

    1. Update the subscription policy:

      • Retrieve the current policy (change yours accordingly):

        gcloud pubsub subscriptions get-iam-policy projects/skysharma-gcp-2/subscriptions/root-sub --format json > ~/Temp/subscription_policy.json
      • Edit subscription_policy.json to add your service account with the role roles/pubsub.editor. This gives the original service account access to this subscription in the new project. The updated subscription_policy.json would look like this:

        •    {
               "etag": "BwUjMhCsNvY=",
               "bindings": [
                 {
                   "role": "roles/pubsub.admin",
                   "members": [
                     "user:user-1@gmail.com"
                   ]
                 },
                 {
                   "role": "roles/pubsub.editor",
                   "members": [
                     "serviceAccount:skysharma-gcp-svc-acct@skysharma-gcp.iam.gserviceaccount.com"
                 }
               ]
             }
      • Apply the updated policy (change yours accordingly):

        gcloud pubsub subscriptions set-iam-policy projects/skysharma-gcp-2/subscriptions/root-sub ~/Temp/subscription_policy.json
    2. Update the topic policy:

      • Retrieve the current policy (change yours accordingly):

        gcloud pubsub topics get-iam-policy projects/skysharma-gcp-2/topics/root --format json > ~/Temp/topic_policy.json

      • Edit topic_policy.json similar to the subscription policy step to add the bindings item to allow the current service account access to this topic. The updated topic_policy.json would look like this:

        •    {
               "etag": "BwUjMhCsNvY=",
               "bindings": [
                 {
                   "role": "roles/pubsub.editor",
                   "members": [
                      "user:user-1@gmail.com",
                      "serviceAccount:skysharma-gcp-svc-acct@skysharma-gcp.iam.gserviceaccount.com"
                   ]
                 }
               ]
             }
      • Apply the updated policy (change yours accordingly):

        gcloud pubsub topics set-iam-policy projects/skysharma-gcp-2/topics/root ~/Temp/topic_policy.json
  3. Have the API keys for your registry ready.

  4. Use the curl command to send a POST request to the changePubSubProject endpoint. This API changes the target project for your Pub/Sub topics.

    curl 'https://us-central1.clearblade.com/api/v/1/code/{SystemKey}/changePubSubProject' \ -X POST \ -d '{"project": "skysharma-gcp-2"}' \ -H 'ClearBlade-UserToken: {Token}'

  5. Send a test MQTT message from your device into the registry. Verify the message appears in the target project’s Pub/Sub subscription.

Additional notes