# Setting Up a Google Drive to GCS Migration - Widget

## Setting Up Flows for the Widget

Each of these flows corresponds to an action button or event from your Google Drive widget. You can define steps inside the flow that should be triggered when the respective action occurs.

### Activation Flow

This flow will be triggered by the activation button in your widget for Google Drive. You can return a success message from flow components or add any additional steps in this flow that you want to be triggered when your connector is activated.

**Steps:**

* In this case, we simply return a static success message like:\
  `"Activation successful for {{input.source}}"`

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2Fe3HrbnM820KQYQsJlSbU%2Fimage.png?alt=media&#x26;token=e1744919-e352-4fec-bb35-f2742bb162da" alt=""><figcaption></figcaption></figure>

### Deactivation Flow

This flow handles deactivation logic, such as deleting registered webhooks.

**Steps:**

* **Initialize Flow Variables:**

The deactivation flow begins with initializing the variables `webhookIds` and `tenantId`. The `webhookIds` have a value of `{{input.source}}`, while `tenantId` is obtained from the request headers as `headers['x-fastn-space-tenantid']`.

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FtigEV7dypzG99hzosgiy%2Fimage.png?alt=media&#x26;token=74c74d86-eba2-4b5a-bb28-88f4007e6f4a" alt=""><figcaption></figcaption></figure>

2. **Loop over Webhook IDs:**

* Use a Loop step to iterate over `webhookIds`

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FllWVVpjYG65jWGGgtbl3%2Fimage.png?alt=media&#x26;token=ef6a9156-6972-487e-8699-ca2def699b98" alt=""><figcaption></figcaption></figure>

* Inside the loop, add a `Delete Webhook` step that deletes each of the webhooks fetched through there IDs as long as the flow runs.

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FLtnOWkPOJ9GNzvdUD1oM%2Fimage.png?alt=media&#x26;token=5357eade-e9ed-4162-ad98-91bd5ea7c87e" alt=""><figcaption></figcaption></figure>

3. **Return Success Message:**

* After the loop, return a success message like:\
  `"Deactivation completed for {{tenantId}}"`

### Configuration Flow (driveConfigFlow)

This flow is used to prepopulate options for user configuration in the widget, such as choosing files and sync frequency.

**Steps:**

* **Map Data:**

  * Maps values for:

  `files`: List of available files

  <figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FX13C9sO8RcvdHh8B3TTx%2Fimage.png?alt=media&#x26;token=c57ea4d3-3a1d-4c93-89ca-1c93122ee8ca" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
In the **mapping step**, users can click the three dots next to fields like **Files** and **Sync Frequency** to access **Configuration Field Options**.
{% endhint %}

For both **Files** and **Sync Frequency**, users can configure settings like setting a label, hiding the field in the config pop-up or based on the config Field.

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FSeN6liO0oupOexfQYpCn%2Fimage.png?alt=media&#x26;token=21d99ed3-6307-4849-953b-805253f04320" alt=""><figcaption></figcaption></figure>

In the **Selections** section for **Files**, you can:

* Enable or disable Selection
* Set the Selection Type as Custom, Static, or Dynamic
* Choose the Selector for your Widget

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FEsHrxK2m2pQnw6aQ0QUg%2Fimage.png?alt=media&#x26;token=2c27dc9f-2213-4f88-a908-8702bea4d3f7" alt=""><figcaption></figcaption></figure>

* **Map Schedule:**

`syncFrequency`: Frequency options (daily, hourly, etc.)

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2F0jsXNl4GUkFvTadr9nPo%2Fimage.png?alt=media&#x26;token=bb9a8fe1-4519-42b3-9d26-9ded1ca7d5db" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
`syncFrequency` is an object with dropdown values, left empty in the flow initially and populates dynamically.
{% endhint %}

In the **Selections** section for **Sync Frequency**, you can:

* Set the Selection Type as Custom, Static, or Dynamic
* Enable or disable Selection
* Option Items for your drop-down of Sync Frequency in the Widget with the value for each sync and its respective label

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FlYHUB9kcgOY1Sh0Yft1x%2Fimage.png?alt=media&#x26;token=9e203392-a4ba-490f-9adf-a8c916e50349" alt=""><figcaption></figcaption></figure>

### Sync Flow Breakdown (activate)

The sync flow fetches selected files from Google Drive and migrates them to a specified Google Cloud Storage (GCS) bucket. It handles file processing, formatting, and storage while respecting user-configured preferences.

* **Initialize flow variables**

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FiazQN8YB4ClpkwknqoUJ%2Fimage.png?alt=media&#x26;token=d3f034f6-1872-4af9-88e7-45311966169e" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FEhfLFiOTUIzqElE1K5Ug%2Fimage.png?alt=media&#x26;token=bc81d980-6aaa-42ed-86cc-a30486d314b7" alt=""><figcaption></figcaption></figure>

* **Switch**

In the switch statement, the Data Mapper:

* Takes values from the respective config flow
* Initializes a sync-specific variable based on flow output
* If the connector is Google Drive:
  * Calls `driveConfigFlow` for schedule info

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FwjMMh1vC3WTDZMrxDBI6%2Fimage.png?alt=media&#x26;token=3563a069-65a2-4e89-b3af-ed88aebe112a" alt=""><figcaption></figcaption></figure>

* If Redshift:
  * Calls `RedShiftconfigflow` for schedule info

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FUo87uWcjnvkR9BiLhQFR%2Fimage.png?alt=media&#x26;token=34641a6e-d22b-4bb5-99fb-b89ca1fe6b32" alt=""><figcaption></figcaption></figure>

* **Initializing Flow Variables**

In the next step, variables are initialized for both Drive and Redshift mapped outputs respectively.

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FZ6tSM1vnEUpimXHZX3LT%2Fimage.png?alt=media&#x26;token=8b7cd358-5bf9-40dd-86d9-293fe6c17d61" alt=""><figcaption></figcaption></figure>

* Else:
  * Ends the flow
* **Parse Sync Frequency**

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FCeqExqfsGtycqcckcVnq%2Fimage.png?alt=media&#x26;token=8ff08310-c74b-42d8-b2ed-18a701e9801f" alt=""><figcaption></figcaption></figure>

* A JavaScript step (`parseSyncFrequency`) extracts and formats frequency info

```
function handler(params) {
  const frequencyString = params.data.var.configs.syncFrequency.value;
  const [rate, ...unitParts] = frequencyString.split(" ");
  const unitTime = unitParts.join(" ");

  return {
    rate: rate,
    unitTime: unitTime.trim()
  };
}
```

* **Switch**
  * If tenant doesn’t exist → return error
  * Else → proceed to Loop Over Templates

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FybbIgZBBEs86QRSyulw4%2Fimage.png?alt=media&#x26;token=1d0e8067-2ed9-4005-86e9-6bd9422c0b31" alt=""><figcaption></figcaption></figure>

* **Loop Over Templates**
  * Fetches APIs for each project ID involved

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FSRbLnmO6JU5yJFw6JJx3%2Fimage.png?alt=media&#x26;token=d3e4ee91-c5b2-480b-9adf-67ac8cc0c0fb" alt=""><figcaption></figcaption></figure>

* **Loop Over Webhooks**
  * If webhooks exist:
    * Fetch scheduler values
  * If not:
    * Create webhooks using:
      * `projectID` in headers
      * `unitRate` and `unitTime` from `parseSyncFrequency`

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2F6KzgVTniT1MbU1efPmdz%2Fimage.png?alt=media&#x26;token=4f868f03-3d2c-4ba3-9d1e-ed30fee8b1a6" alt=""><figcaption></figcaption></figure>

* The flow returns **Success Message** after successful execution.

## Widget Setup Overview

You'll create a Fastn widget with **Google Drive** as the dependency connector.\
The widget will support the following actions:

* **Connect:** Activates and authenticates the widget
* **Disconnect:** Deactivates the connection
* **Configure:** Allows file selection from Drive
* **Sync:** Schedules and runs the migration

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FHBusjx0GA0VmWjczKMp6%2Fimage.png?alt=media&#x26;token=0a3e31ea-6955-43d6-a6ab-9ab561717575" alt=""><figcaption></figcaption></figure>

### 1. Connect Action

* **Visibility**: Pre-Auth
* **Flow Triggered**: [Activator](#activation-flow)
* **Behavior**:
  * This is the first action users will see.
  * The flow prepares the widget for authentication.
  * Once activated, users authenticate their Google account to connect.

### 2. Disconnect Action

* **Visibility**: Post-Auth
* **Flow Triggered**: [deactivate](#deactivation-flow)
* **Behavior**:
  * Lets users disconnect their Google Drive from the widget.
  * This action deactivates the widget and clears any saved data or context.

### 3. Configure Action

* **Visibility**: Post-Auth
* **Flow Triggered**: [driveConfigFlow](#configuration-flow-driveconfigflow)
* **Behavior**:
  * Appears once the user has connected their Google account.
  * Allows the user to select files or folders from their Drive.
  * Stores this configuration for the sync process.

{% hint style="info" %}
This button is not visible Pre-Auth.
{% endhint %}

### 4. Sync Action

* **Visibility**: Post-auth and post-config
* **Flow Triggered**: [activate](#sync-flow-breakdown-activate)

The Sync Action also takes the [drive\_to\_gcs](https://docs.fastn.ai/flows/tutorials/data-migrations/setting-up-a-google-drive-to-gcs-migration-flow) flow as a source input in Payload.

```
{
  "source": "drive_to_gcs"
}
```

* **Behavior**:
  * Starts the migration process based on the Drive config.
  * Triggers a structured flow with logic for both Google Drive and Redshift cases.

## User Experience in the Widget

* User sees the **Connect** button → clicks to authenticate with Google

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FkgYyfqAQb6vJXJgX9qru%2Fimage%20(2).png?alt=media&#x26;token=7638842d-1a85-4cd9-b9b1-8c60c2a2276c" alt=""><figcaption></figcaption></figure>

* On success, **Disconnect** and **Configure** buttons appear.

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2F5Re52yvhtOcPCO8oXHI6%2Fimage%20(3).png?alt=media&#x26;token=e54c0c9a-a133-45bb-ba98-0c9cb23893ac" alt=""><figcaption></figcaption></figure>

* User clicks **Configure** to:
  * Browse through Google Drive and select source files.

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2F1M53sjjwuXjNSDQ8nAPD%2Fimage.png?alt=media&#x26;token=6012639a-bae6-4f32-9b1e-229f46f9afd4" alt=""><figcaption></figcaption></figure>

* User selects sync frequency from the dropdown.

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2F4Tloxa5BKTZIsiud0vxX%2Fimage%20(5).png?alt=media&#x26;token=c38d304e-bc25-43c7-b4c0-c22d0448b97c" alt=""><figcaption></figcaption></figure>

* User will now be able to see the **Sync** button, which you can use to start a scheduled migration to GCS.

<figure><img src="https://1255842839-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F3iSr2Tx8FvvuoLPncziH%2Fuploads%2FovuRZB2QVvJJQPByrUCk%2Fimage%20(6).png?alt=media&#x26;token=25b47b71-718b-4e6e-ab62-88832074c38a" alt=""><figcaption></figcaption></figure>

* The **Disconnect** button allows you to disconnect your Widget.

## Final Outcome

Once set up, this Fastn widget gives your users a seamless experience to:

* Connect their Google Drive
* Configure what to sync
* Schedule automated migrations
* Push selected data to Google Cloud Storage reliably
