-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection_config.go
72 lines (60 loc) · 1.93 KB
/
connection_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package postgres
import (
"fmt"
"os"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/schema"
)
type PostgresConfig struct {
ConnectionString *string `cty:"connection_string"`
Schema *string `cty:"schema"`
TablesToExpose []string `cty:"tables_to_expose"`
}
var ConfigSchema = map[string]*schema.Attribute{
"connection_string": {Type: schema.TypeString},
"schema": {Type: schema.TypeString},
"tables_to_expose": {Type: schema.TypeList, Elem: &schema.Attribute{Type: schema.TypeString}},
}
func ConfigInstance() interface{} {
return &PostgresConfig{}
}
// GetConfig :: retrieve and cast connection config from query data
func GetConfig(connection *plugin.Connection) PostgresConfig {
if connection == nil || connection.Config == nil {
return PostgresConfig{}
}
config, _ := connection.Config.(PostgresConfig)
return config
}
func (c PostgresConfig) String() string {
return fmt.Sprintf(
"PostgresConfig{schema=%s}",
c.GetSchema()) // can't print connection_string, since it has credentials embedded
}
/*
GetSchema returns the schema that was configured in the .spc file, if available, and "public" otherwise
*/
func (c PostgresConfig) GetSchema() string {
if c.Schema != nil && *c.Schema != "" {
return *c.Schema
}
return "public"
}
func (c PostgresConfig) GetConnectionString() (string, error) {
if c.ConnectionString != nil && *c.ConnectionString != "" {
return *c.ConnectionString, nil
}
if v := os.Getenv("DATABASE_URL"); v != "" {
return v, nil
}
return "", fmt.Errorf("please provide either the connection_string param or the DATABASE_URL envvar")
}
/*
GetTablesToExpose returns the slice of table blobs that was configured in the .spc file, if set, and ["*"] otherwise (which will expose every table)
*/
func (c PostgresConfig) GetTablesToExpose() []string {
if len(c.TablesToExpose) > 0 {
return c.TablesToExpose
}
return []string{"*"}
}