|
| 1 | +package redshift |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + datashareProducerNameAttr = "datashare_producer_name" |
| 13 | + datashareProducerAccountAttr = "datashare_producer_account" |
| 14 | + datashareProducerNamespaceAttr = "datashare_producer_namespace" |
| 15 | +) |
| 16 | + |
| 17 | +func dataSourceRedshiftDatabase() *schema.Resource { |
| 18 | + return &schema.Resource{ |
| 19 | + Description: `Fetches information about a Redshift database.`, |
| 20 | + Read: RedshiftResourceFunc(dataSourceRedshiftDatabaseRead), |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + databaseNameAttr: { |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + Description: "Name of the database", |
| 26 | + StateFunc: func(val interface{}) string { |
| 27 | + return strings.ToLower(val.(string)) |
| 28 | + }, |
| 29 | + }, |
| 30 | + databaseOwnerAttr: { |
| 31 | + Type: schema.TypeString, |
| 32 | + Computed: true, |
| 33 | + Description: "Owner of the database, usually the user who created it", |
| 34 | + }, |
| 35 | + databaseConnLimitAttr: { |
| 36 | + Type: schema.TypeInt, |
| 37 | + Computed: true, |
| 38 | + Description: "The maximum number of concurrent connections that can be made to this database. A value of -1 means no limit.", |
| 39 | + }, |
| 40 | + datashareProducerNameAttr: { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + Optional: true, |
| 44 | + Description: "For databases created from datashares, this is the producer's datashare name.", |
| 45 | + }, |
| 46 | + datashareProducerAccountAttr: { |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + Optional: true, |
| 50 | + Description: "For databases created from datashares, this is the producer's account number.", |
| 51 | + }, |
| 52 | + datashareProducerNamespaceAttr: { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + Optional: true, |
| 56 | + Description: "For databases created from datashares, this is the producer's namespace.", |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func dataSourceRedshiftDatabaseRead(db *DBConnection, d *schema.ResourceData) error { |
| 63 | + var id, owner, connLimit string |
| 64 | + var shareName, producerAccount, producerNamespace sql.NullString |
| 65 | + |
| 66 | + err := db.QueryRow(`SELECT |
| 67 | + pg_database_info.datid, |
| 68 | + trim(pg_user_info.usename), |
| 69 | + COALESCE(pg_database_info.datconnlimit::text, 'UNLIMITED'), |
| 70 | + trim(svv_datashares.share_name), |
| 71 | + trim(svv_datashares.producer_account), |
| 72 | + trim(svv_datashares.producer_namespace) |
| 73 | +FROM |
| 74 | + svv_redshift_databases |
| 75 | +LEFT JOIN pg_database_info |
| 76 | + ON svv_redshift_databases.database_name=pg_database_info.datname |
| 77 | +LEFT JOIN pg_user_info |
| 78 | + ON pg_user_info.usesysid = svv_redshift_databases.database_owner |
| 79 | +LEFT JOIN svv_datashares |
| 80 | + on (svv_datashares.share_type='INBOUND' AND svv_datashares.consumer_database=svv_redshift_databases.database_name) |
| 81 | +WHERE svv_redshift_databases.database_name = $1 |
| 82 | + `, d.Get(databaseNameAttr).(string)).Scan(&id, &owner, &connLimit, &shareName, &producerAccount, &producerNamespace) |
| 83 | + |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + connLimitNumber := -1 |
| 89 | + if connLimit != "UNLIMITED" { |
| 90 | + if connLimitNumber, err = strconv.Atoi(connLimit); err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + d.SetId(id) |
| 96 | + d.Set(databaseOwnerAttr, owner) |
| 97 | + d.Set(databaseConnLimitAttr, connLimitNumber) |
| 98 | + |
| 99 | + if shareName.Valid { |
| 100 | + d.Set(datashareProducerNameAttr, shareName.String) |
| 101 | + } else { |
| 102 | + d.Set(datashareProducerNameAttr, nil) |
| 103 | + } |
| 104 | + |
| 105 | + if producerAccount.Valid { |
| 106 | + d.Set(datashareProducerAccountAttr, producerAccount.String) |
| 107 | + } else { |
| 108 | + d.Set(datashareProducerAccountAttr, nil) |
| 109 | + } |
| 110 | + |
| 111 | + if producerNamespace.Valid { |
| 112 | + d.Set(datashareProducerNamespaceAttr, producerNamespace.String) |
| 113 | + } else { |
| 114 | + d.Set(datashareProducerNamespaceAttr, nil) |
| 115 | + } |
| 116 | + |
| 117 | + return nil |
| 118 | +} |
0 commit comments