|
1 |
| -{#-- retrieve the projects list --#} |
| 1 | +{#-- |
| 2 | + Retrieve the projects list from various input formats and always return an array. |
| 3 | + |
| 4 | + This macro supports multiple input formats and normalizes them to a consistent array output: |
| 5 | + |
| 6 | + SUPPORTED INPUT FORMATS: |
| 7 | + |
| 8 | + 1. dbt project variables (dbt_project.yml): |
| 9 | + vars: |
| 10 | + input_gcp_projects: "single-project" # → ["single-project"] |
| 11 | + input_gcp_projects: ["project1", "project2"] # → ["project1", "project2"] |
| 12 | + |
| 13 | + 2. CLI variables: |
| 14 | + --vars '{"input_gcp_projects": "test"}' # → ["test"] |
| 15 | + --vars '{"input_gcp_projects": ["test1", "test2"]}' # → ["test1", "test2"] |
| 16 | + |
| 17 | + 3. Environment variables: |
| 18 | + DBT_BQ_MONITORING_GCP_PROJECTS="single-project" # → ["single-project"] |
| 19 | + DBT_BQ_MONITORING_GCP_PROJECTS='["project1","project2"]' # → ["project1", "project2"] |
| 20 | + DBT_BQ_MONITORING_GCP_PROJECTS='[project1,project2]' # → ["project1", "project2"] (unquoted) |
| 21 | + DBT_BQ_MONITORING_GCP_PROJECTS='["project1"]' # → ["project1"] |
| 22 | + DBT_BQ_MONITORING_GCP_PROJECTS='[project1]' # → ["project1"] (unquoted) |
| 23 | + DBT_BQ_MONITORING_GCP_PROJECTS='[]' # → [] |
| 24 | + |
| 25 | + 4. Mixed quote styles: |
| 26 | + DBT_BQ_MONITORING_GCP_PROJECTS="['proj1',\"proj2\"]" # → ["proj1", "proj2"] |
| 27 | + |
| 28 | + EDGE CASES HANDLED: |
| 29 | + - Empty strings return empty arrays |
| 30 | + - Whitespace around project names is automatically trimmed |
| 31 | + - Surrounding quotes (single or double) are automatically removed |
| 32 | + - Invalid/malformed inputs fallback gracefully |
| 33 | + |
| 34 | + RETURN VALUE: |
| 35 | + Always returns an array of project strings |
| 36 | +--#} |
2 | 37 | {% macro project_list() %}
|
3 | 38 | {% set projects = dbt_bigquery_monitoring_variable_input_gcp_projects() %}
|
| 39 | + |
| 40 | + {#-- If it's already a list/array, return it directly --#} |
4 | 41 | {% if projects is iterable and projects is not string %}
|
5 |
| - {{ return(projects) }} |
6 |
| - {#-- check if it's the string and it contains a "," --#} |
| 42 | + {{ return(projects) }} |
| 43 | + |
| 44 | + {#-- Handle string inputs --#} |
7 | 45 | {% elif projects is string %}
|
8 |
| - {% if projects == '' %} |
9 |
| - {{ return([]) }} |
10 |
| - {% else %} |
11 |
| - {% set projects_replaced = projects | replace("'", '"') %} |
12 |
| - {% set json = fromjson('{"v":' ~ projects_replaced ~ '}') %} |
13 |
| - {{ return (json['v']) }} |
14 |
| - {% endif %} |
| 46 | + {#-- Empty string case --#} |
| 47 | + {% if projects == '' %} |
| 48 | + {{ return([]) }} |
| 49 | + {% endif %} |
| 50 | + |
| 51 | + {#-- Check if it looks like an array (starts with [ and ends with ]) --#} |
| 52 | + {% if projects.startswith('[') and projects.endswith(']') %} |
| 53 | + {#-- Extract content between brackets --#} |
| 54 | + {% set inner_content = projects[1:-1].strip() %} |
| 55 | + |
| 56 | + {#-- Handle empty array --#} |
| 57 | + {% if inner_content == '' %} |
| 58 | + {{ return([]) }} |
| 59 | + {% endif %} |
| 60 | + |
| 61 | + {#-- Split by comma and process each project --#} |
| 62 | + {% set project_list = [] %} |
| 63 | + {% set raw_projects = inner_content.split(',') %} |
| 64 | + |
| 65 | + {% for raw_project in raw_projects %} |
| 66 | + {% set cleaned_project = raw_project.strip() %} |
| 67 | + |
| 68 | + {#-- Remove surrounding quotes if present --#} |
| 69 | + {% if (cleaned_project.startswith('"') and cleaned_project.endswith('"')) or |
| 70 | + (cleaned_project.startswith("'") and cleaned_project.endswith("'")) %} |
| 71 | + {% set cleaned_project = cleaned_project[1:-1] %} |
| 72 | + {% endif %} |
| 73 | + |
| 74 | + {#-- Add non-empty projects to the list --#} |
| 75 | + {% if cleaned_project %} |
| 76 | + {% set _ = project_list.append(cleaned_project) %} |
| 77 | + {% endif %} |
| 78 | + {% endfor %} |
| 79 | + |
| 80 | + {{ return(project_list) }} |
| 81 | + {% else %} |
| 82 | + {#-- Single project string - wrap in array --#} |
| 83 | + {{ return([projects]) }} |
| 84 | + {% endif %} |
| 85 | + |
| 86 | + {#-- Fallback: return empty array for any other type --#} |
15 | 87 | {% else %}
|
16 |
| - {{ exceptions.raise_compiler_error('Invalid `input_gcp_projects` variables. Got: ' ~ input_gcp_projects ~ ' but expected form like input_gcp_projects = ["project_1", "project_2"] ') }} |
| 88 | + {{ return([]) }} |
17 | 89 | {% endif %}
|
18 | 90 | {% endmacro %}
|
0 commit comments