|
| 1 | +package postgresql |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +var schemaQueries = map[string]string{ |
| 12 | + "query_include_system_schemas": ` |
| 13 | + SELECT schema_name |
| 14 | + FROM information_schema.schemata |
| 15 | + `, |
| 16 | + "query_exclude_system_schemas": ` |
| 17 | + SELECT schema_name |
| 18 | + FROM information_schema.schemata |
| 19 | + WHERE schema_name NOT LIKE 'pg_%' |
| 20 | + AND schema_name <> 'information_schema' |
| 21 | + `, |
| 22 | +} |
| 23 | + |
| 24 | +const schemaPatternMatchingTarget = "schema_name" |
| 25 | + |
| 26 | +func dataSourcePostgreSQLDatabaseSchemas() *schema.Resource { |
| 27 | + return &schema.Resource{ |
| 28 | + Read: PGResourceFunc(dataSourcePostgreSQLSchemasRead), |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "database": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + ForceNew: true, |
| 34 | + Description: "The PostgreSQL database which will be queried for schema names", |
| 35 | + }, |
| 36 | + "include_system_schemas": { |
| 37 | + Type: schema.TypeBool, |
| 38 | + Default: false, |
| 39 | + Optional: true, |
| 40 | + Description: "Determines whether to include system schemas (pg_ prefix and information_schema). 'public' will always be included.", |
| 41 | + }, |
| 42 | + "like_any_patterns": { |
| 43 | + Type: schema.TypeList, |
| 44 | + Optional: true, |
| 45 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 46 | + MinItems: 0, |
| 47 | + Description: "Expression(s) which will be pattern matched in the query using the PostgreSQL LIKE ANY operator", |
| 48 | + }, |
| 49 | + "like_all_patterns": { |
| 50 | + Type: schema.TypeList, |
| 51 | + Optional: true, |
| 52 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 53 | + MinItems: 0, |
| 54 | + Description: "Expression(s) which will be pattern matched in the query using the PostgreSQL LIKE ALL operator", |
| 55 | + }, |
| 56 | + "not_like_all_patterns": { |
| 57 | + Type: schema.TypeList, |
| 58 | + Optional: true, |
| 59 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 60 | + MinItems: 0, |
| 61 | + Description: "Expression(s) which will be pattern matched in the query using the PostgreSQL NOT LIKE ALL operator", |
| 62 | + }, |
| 63 | + "regex_pattern": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Optional: true, |
| 66 | + Description: "Expression which will be pattern matched in the query using the PostgreSQL ~ (regular expression match) operator", |
| 67 | + }, |
| 68 | + "schemas": { |
| 69 | + Type: schema.TypeSet, |
| 70 | + Computed: true, |
| 71 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 72 | + Set: schema.HashString, |
| 73 | + Description: "The list of PostgreSQL schemas retrieved by this data source", |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func dataSourcePostgreSQLSchemasRead(db *DBConnection, d *schema.ResourceData) error { |
| 80 | + database := d.Get("database").(string) |
| 81 | + |
| 82 | + txn, err := startTransaction(db.client, database) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + defer deferredRollback(txn) |
| 87 | + |
| 88 | + includeSystemSchemas := d.Get("include_system_schemas").(bool) |
| 89 | + |
| 90 | + var query string |
| 91 | + var queryConcatKeyword string |
| 92 | + if includeSystemSchemas { |
| 93 | + query = schemaQueries["query_include_system_schemas"] |
| 94 | + queryConcatKeyword = queryConcatKeywordWhere |
| 95 | + } else { |
| 96 | + query = schemaQueries["query_exclude_system_schemas"] |
| 97 | + queryConcatKeyword = queryConcatKeywordAnd |
| 98 | + } |
| 99 | + |
| 100 | + query = applySchemaDataSourceQueryFilters(query, queryConcatKeyword, d) |
| 101 | + |
| 102 | + rows, err := txn.Query(query) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + defer rows.Close() |
| 107 | + |
| 108 | + schemas := []string{} |
| 109 | + for rows.Next() { |
| 110 | + var schema string |
| 111 | + |
| 112 | + if err = rows.Scan(&schema); err != nil { |
| 113 | + return fmt.Errorf("could not scan schema name for database: %w", err) |
| 114 | + } |
| 115 | + schemas = append(schemas, schema) |
| 116 | + } |
| 117 | + |
| 118 | + d.Set("schemas", stringSliceToSet(schemas)) |
| 119 | + d.SetId(generateDataSourceSchemasID(d, database)) |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func generateDataSourceSchemasID(d *schema.ResourceData, databaseName string) string { |
| 125 | + return strings.Join([]string{ |
| 126 | + databaseName, strconv.FormatBool(d.Get("include_system_schemas").(bool)), |
| 127 | + generatePatternArrayString(d.Get("like_any_patterns").([]interface{}), queryArrayKeywordAny), |
| 128 | + generatePatternArrayString(d.Get("like_all_patterns").([]interface{}), queryArrayKeywordAll), |
| 129 | + generatePatternArrayString(d.Get("not_like_all_patterns").([]interface{}), queryArrayKeywordAll), |
| 130 | + d.Get("regex_pattern").(string), |
| 131 | + }, "_") |
| 132 | +} |
| 133 | + |
| 134 | +func applySchemaDataSourceQueryFilters(query string, queryConcatKeyword string, d *schema.ResourceData) string { |
| 135 | + filters := []string{} |
| 136 | + filters = append(filters, applyPatternMatchingToQuery(schemaPatternMatchingTarget, d)...) |
| 137 | + |
| 138 | + return finalizeQueryWithFilters(query, queryConcatKeyword, filters) |
| 139 | +} |
0 commit comments