GCP IAM Binding using Temporal and GoLang(Gin Framework)
Gin is the web framework written in Go(GoLang). Gin is a high-performance micro-framework that can be used to build web applications. It allows you to write middleware that can be plugged into one or more request handlers or groups of request handlers. By the end of this tutorial, you will: Prerequisites For this tutorial, you will need GoLang, Temporal, docker, and postman installed on your machine. Note: If you don’t have postman, you can use any other tool that you would use to test API endpoints. List of Packages we are going to use: Goroutine Goroutine is a lightweight thread in Golang. All programs executed by Golang run on the Goroutine. That is, the main function is also executed on the Goroutine. In other words, every program in Golang must have a least one Goroutine. In Golang, you can use the Goroutine to execute the function with the go keyword like the below. Temporal A Temporal Application is a set of Temporal Workflow Executions. Each Temporal Workflow Execution has exclusive access to its local state, executes concurrently to all other Workflow Executions, and communicates with other Workflow Executions and the environment via message passing. A Temporal Application can consist of millions to billions of Workflow Executions. Workflow Executions are lightweight components. A Workflow Execution consumes few compute resources; in fact, if a Workflow Execution is suspended, such as when it is in a waiting state, the Workflow Execution consumes no compute resources at all. main.go we will be running the temporal worker as a thread to intialize the worker and starting our Gin server in parallel. Temporal Worker In day-to-day conversations, the term Worker is used to denote either a Worker Program, a Worker Process, or a Worker Entity. Temporal documentation aims to be explicit and differentiate between them. worker/worker.go The IamBindingGoogle workFlow and AddIAMBinding Activity is registered in the Worker. Workflow Definition refers to the source for the instance of a Workflow Execution, while a Workflow Function refers to the source for the instance of a Workflow Function Execution. The purpose of an Activity is to execute a single, well-defined action (either short or long running), such as calling another service, transcoding a media file, or sending an email. worker/iam_model.go This defines the schema of the Iam Inputs. worker/base.go LoadData function is used to Unmarshal the data that is recieved in the Api request. worker/workflowsvc.go here is the service layer of the WorkFlow where there is an interface which implements the methods which is defined on the interface. worker/workflow.go A Workflow Execution effectively executes once to completion, while a Workflow Function Execution occurs many times during the life of a Workflow Execution. The IamBindingGoogle WorkFlow has been using the context of workflow and the iamDetails which contains information of google_project_id, user_name and the role that should be given in gcp. Those details will be send to an activity function which executes IAM Binding. The ExecuteActivity function should have the Activity options such as StartToCloseTimeout, ScheduleToCloseTimeout, Retry policy and TaskQueue. Each Activity function can return the output that is defined the Activity. worker/activity.go Google Cloud Go SDK is used here for actual iamBinding. Finally we need temporal setup using docker, .local/quickstart.yml Export the environment variables in terminal : Run the docker-compose file to start the temporal : Perfect!! We are all set now. Let’s run this project: And I can see an Engine instance has been created and the APIs are running and the temporal is started as a thread: And Even the Temporal UI is on localhost:8088 Let’s hit our POST API: The Workflow is completed and IamBinding is Done is GCP also. If you are looking for an easy way to manage and automate your cloud infrastructure, Sailor Cloud is a good option to consider. To learn more about Sailor Cloud, please visit the Sailor Cloud website: https://www.sailorcloud.io/ git clone github.com/venkateshsuresh/temporal-iamBind.. I hope this article helped you. Thanks for reading and stay tuned!