goFunction-Cli

command module
v0.0.0-...-1a1e447 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2024 License: MIT Imports: 1 Imported by: 0

README

goFunction-Cli

A CLI tool for creating GCP Cloud Functions in Gen2 using the "functions-framework-go"

Often I'm required to build out a quick serverless function to give a website access to a server side function in Genesys Cloud or another service and to front end a Client Credentials server side request with allowing CORS etc. To do this with Google Cloud Platform you can leverage the Functions framework to run and test the serverless function on your localhost machine before deploying it to the cloud. To do this though you need to follow a specific folder structure as well as leverage the SDK.

This CLI tool is designed to make it SUPER easy to build and deploy these with the Genesys Cloud GO SDK with your own Basic Auth to make demo development easy.

Installing the CLI

You first of all need to ensure you have GO installed based on your operating system that may differ in process but its well documented in that link.

Now you can simply run:

go install github.com/mcphee11/goFunction-Cli@latest

This will then install the CLI tool in your GOPATH. if this is setup in your system paths then you can now simply use the CLI directly. To test this type:

goFunction-Cli --version

And you should get an output like: goFunction-Cli version version 0.4 NOTE: as I update the versions this number may be higher.

Using the CLI

To create a new project go to a location in your terminal eg: ~/Documents and then type in the below command ensuring to change the --flags with your own information

goFunction-Cli buildGenesys --region=YOUR_REGION --clientId=YOUR_CLIENT_ID --secret=YOUR_SECRET --username=YOUR_USERNAME --password=YOUR_PASSWORD --name=YOUR_FUNCTION_NAME

I have put some more details on what each of these flags mean. This information can also be found by going

goFunction-Cli buildGenesys --help

Here is the output about these flags

--clientId string    OAuth 2.0 ClientId
--name string        A name for the cloud function
--password string   A Basic password for external auth
--region string      The Genesys Cloud Region eg: mypurecloud.com.au
--secret string      OAuth 2.0 secret
--username string    A Basic username for external auth

Running the project locally

Now there will be a few files and folders created to run this project locally simply cd into the folder created that will be the --name you gave the project and run the run.sh file

./run.sh

By default this runs the server on port 8081

This default config is reading a POST request with a body of {id: xyz} where xyz = conversationId. As the default is also using a Basic Auth you will need to pass in the -u if using curl and example is below:

curl -u username:password http://localhost:8081 -H "Content-Type: application/json" -d '{"Id":"YOUR_CONVERSATION_ID"}'

Deploying the project to GCP

To deploy this to GCP as a serverless function, in the same root folder of the project where you ran the run.sh file you will find a deploy.sh file so like above run this file.

./deploy.sh
NOTE:

While I am passing in the --flags when building this file you will require to have gcloud installed which is the Google Cloud CLI tool. As well as a default Project set in gcloud, I have set the GCP region to deploy as --region=australia-southeast1 you may want to change that as well to your own local region.

Right now I am also only storing the clientId and secret as env-vars. This should really be using GCP secret storage which I plan to move this to in the future. Also im using the --allow-unauthenticated and setting my own 'Basic Auth' in the function. In a prod env you may want to use the GCP function Auth that they provide.

This method is designed for me to quickly spin up a test then destroy it later.

Changing the request

In the genesys.go file is where the request to the server side endpoint happens so when your changing it to call endpoint X this is where you can edit the code to suit your use case.

Final thoughts

I may add more into this in the future but for now I hope this enabled you to spin up demo test functions much faster as it does for me.

Documentation

Overview

Copyright © 2024 https://github.com/mcphee11

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Directories

Path Synopsis
cmd
_templates/cmd command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL