Skip to content

Instantly share code, notes, and snippets.

@cassioeskelsen
Created November 16, 2022 02:58
Show Gist options
  • Save cassioeskelsen/bce24f51a9d02e9478c5daeabde2f114 to your computer and use it in GitHub Desktop.
Save cassioeskelsen/bce24f51a9d02e9478c5daeabde2f114 to your computer and use it in GitHub Desktop.
GCP Workflows commands
#initial setup
export PROJECT_ID=real-state-wf #altere para o nome desejado
export SERVICE_ACCOUNT=workflows-sa
export INCOMING_TOPIC_ID=rent-request
export SUCCES_TOPIC_ID=sucess-topic
export NOT_SUCESS_TOPIC_ID=not-sucess-topic
export REGION=us-east1 #você pode alterar pela sua preferência
export WORKFLOW_NAME=house-rent
gcloud config set project ${PROJECT_ID}
gcloud config set run/region ${REGION}
gcloud services enable cloudbuild.googleapis.com cloudfunctions.googleapis.com \
run.googleapis.com storage.googleapis.com containerregistry.googleapis.com \
workflows.googleapis.com eventarc.googleapis.com
gcloud config set workflows/location ${REGION}
gcloud config set eventarc/location ${REGION}
gcloud iam service-accounts create ${SERVICE_ACCOUNT}
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
--member "serviceAccount:${SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com" \
--role "roles/run.invoker"
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
--member="serviceAccount:${SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com" \
--role="roles/workflows.invoker"
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
--member="serviceAccount:service-270828717436@gcp-sa-pubsub.iam.gserviceaccount.com" \
--role="roles/iam.serviceAccountTokenCreato"
#create incoming topic for incominf rent requests
gcloud pubsub topics create ${INCOMING_TOPIC_ID} --project=${PROJECT_ID}
#result topics
gcloud pubsub topics create ${SUCCES_TOPIC_ID} --project=${PROJECT_ID}
gcloud pubsub topics create ${NOT_SUCESS_TOPIC_ID} --project=${PROJECT_ID}
#subscriptions
gcloud pubsub subscriptions create ${SUCCES_TOPIC_ID}"-subscription" --topic=${SUCCES_TOPIC_ID} --project=${PROJECT_ID}
gcloud pubsub subscriptions create ${NOT_SUCESS_TOPIC_ID}"-subscription" --topic=${NOT_SUCESS_TOPIC_ID} --project=${PROJECT_ID}
git clone git@github.com:cassioeskelsen/gcp_workflow.git
cd gcp_workflow/check_person_exists
gcloud functions deploy check_person_exists \
--runtime python38 \
--trigger-http \
--allow-unauthenticated
cd ../identify_person
gcloud functions deploy identify_person \
--runtime python38 \
--trigger-http \
--allow-unauthenticated
cd ../score_person
gcloud functions deploy score_person \
--runtime python38 \
--trigger-http \
--allow-unauthenticated
cd ../reserve_house
gcloud functions deploy reserve_house \
--runtime python38 \
--trigger-http \
--allow-unauthenticated
#testing
gcloud functions describe check_person_exists
curl $(gcloud functions describe check_person_exists --format='value(httpsTrigger.url)') \
-X POST \
-H "content-type: application/json" \
-d '{"person_id":"123.123.123-13"}'
curl $(gcloud functions describe identify_person --format='value(httpsTrigger.url)') \
-X POST \
-H "content-type: application/json" \
-d '{"person_id":"123.123.123-13","photo_id":"a8ec3803-d2c5-4e0a-8c38-629840ad212"}'
curl $(gcloud functions describe score_person --format='value(httpsTrigger.url)') \
-X POST \
-H "content-type: application/json" \
-d '{"person_id":"123.123.123-13","photo_id":"a8ec3803-d2c5-4e0a-8c38-629840ad212"}'
curl $(gcloud functions describe reserve_house --format='value(httpsTrigger.url)') \
-X POST \
-H "content-type: application/json" \
-d '{"person_id":"123.123.123-13","photo_id":"a8ec3803-d2c5-4e0a-8c38-629840ad212", "house_id": "1"}'
#create workflow
gcloud workflows deploy ${WORKFLOW_NAME} --source=workflow.yaml
#test workflow
#gcloud workflows run ${WORKFLOW_NAME}
#create trigger
gcloud eventarc triggers create new-house-rent-request \
--location=${REGION} \
--service-account="${SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com" \
--transport-topic=projects/real-state-wf/topics/${INCOMING_TOPIC_ID} \
--destination-workflow=${WORKFLOW_NAME} \
--destination-workflow-location=${REGION} \
--event-filters="type=google.cloud.pubsub.topic.v1.messagePublished"
#pesssoa não existe
gcloud pubsub topics publish ${INCOMING_TOPIC_ID} --message '{"person_id":"123.123.123-16", "person_email": "email@email.com", "photo_id":"a8ec3803-d2c5-4e0a-8c38-629840ad212", "house_id":"2"}'
#pesssoa não existe mas foi identificada por OCR/Biometria mas o score é baixo
gcloud pubsub topics publish ${INCOMING_TOPIC_ID} --message '{"person_id":"125.125.125-15", "person_email": "email@email.com", "photo_id":"a8ec3803-d2c5-4e0a-8c38-629840ad212", "house_id":"2"}'
#pesssoa existe mas o score exige fiador
gcloud pubsub topics publish ${INCOMING_TOPIC_ID} --message '{"person_id":"124.124.124-14", "person_email": "email@email.com", "photo_id":"a8ec3803-d2c5-4e0a-8c38-629840ad212", "house_id":"2"}'
#pesssoa existe, o score é bom mas a casa não está mais disponível
gcloud pubsub topics publish ${INCOMING_TOPIC_ID} --message '{"person_id":"123.123.123-13", "person_email": "email@email.com", "photo_id":"a8ec3803-d2c5-4e0a-8c38-629840ad212", "house_id":"1"}'
#Consultando resultado
gcloud pubsub subscriptions pull not-sucess-topic-subscription --auto-ack --limit 10
#caminho feliz: pessoa encontrada, score bom e casa disponível
gcloud pubsub topics publish ${INCOMING_TOPIC_ID} --message '{"person_id":"123.123.123-13", "person_email": "email@email.com", "photo_id":"a8ec3803-d2c5-4e0a-8c38-629840ad212", "house_id":"2"}'
#Consultando resultado
gcloud pubsub subscriptions pull sucess-topic-subscription --auto-ack --limit 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment