Search code examples
gcloudgoogle-cloud-pubsubgcloud-cli

Ignore pubsub topic if it already created


I have a simple script to deploy a pubsub application.

This script will run on every deploy of my Cloud Run service and I have a line with:

gcloud pubsub topics create some-topic

I want to improve my script if the topic already exist, currently if I run my script, the output will be:

ERROR: Failed to create topic [projects/project-id/topics/some-topic]: Resource already exists in the project (resource=some-topic).

ERROR: (gcloud.pubsub.topics.create) Failed to create the following: [some-topic].

I tried the flag --no-user-output-enabled but no success.

Is there a way to ignore if the resource already exists, or a way to check before create?


Solution

  • Yes.

    You can repeat the operation knowing that, if the topic didn't exist beforehand, it will if the command succeeds.

    You can swallow stderr (with 2>/dev/null) and then check whether the previous command ($?) succeeded (0):

    gcloud pubsub topic create do-something 2>/dev/null
    if [ $? -eq 0 ]
    then
      # Command succeeded, topic did not exist
      echo "Topic ${TOPIC} did not exist, created."
    else
      # Command did not succeed, topic may (!) not have existed
      echo "Failure"
    fi
    

    NOTE This approach misses the fact that, the command may fail and the topic didn't exist (i.e. some other issue).

    Alternatively (more accurately and more expensively!) you can enumerate the topics first and then try (!) to create it if it doesn't exist:

    TOPIC="some-topic"
    RESULT=$(\
      gcloud pubsub topics list \
      --filter="name.scope(topics)=${TOPIC}" \
      --format="value(name)" 2>/dev/null)
    
    if [ "${RESULT}" == "" ]
    then
      echo "Topic ${TOPIC} does not exist, creating..."
      gcloud pubsub topics create ${TOPIC}
      if [ $? -eq 0 ]
      then
        # Command succeeded, topic created
      else
        # Command did not succeed, topic was not created
      fi
    fi
    

    Depending on the complexity of your needs, you can automate using:

    • any of Google's (Pub/Sub) libraries which provide better error-handling and retry capabilities.
    • Terraform e.g. google_pubsub_topic