forked from Snowflake-Labs/terraform-provider-snowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (99 loc) · 3.14 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"flag"
"fmt"
"log"
"os"
"sort"
"strings"
"github.com/chanzuckerberg/terraform-provider-snowflake/pkg/provider"
"github.com/chanzuckerberg/terraform-provider-snowflake/pkg/version"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/plugin"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/olekukonko/tablewriter"
)
func main() {
doc := flag.Bool("doc", false, "spit out docs for resources here")
ver := flag.Bool("version", false, "spit out version for resources here")
flag.Parse()
if *doc {
generateDocs()
return
}
if *ver {
verString, err := version.VersionString()
if err != nil {
log.Fatal(err)
}
fmt.Println(verString)
return
}
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() terraform.ResourceProvider {
return provider.Provider()
},
})
}
func generateDocs() {
// schema := provider.Provider().Schema
resources := provider.Provider().ResourcesMap
names := make([]string, 0)
for k := range resources {
names = append(names, k)
}
sort.Strings(names)
for _, name := range names {
resource := resources[name]
fmt.Printf("\n### %s\n\n", name)
if strings.HasSuffix(name, "_grant") {
grant_resource_name := strings.Replace(name, "_grant", "", -1)
granted_to_name := strings.Replace(grant_resource_name, "snowflake_", "", -1)
fmt.Printf(
`**Note**: The %s resource creates exclusive attachments of grants.
Across the entire Snowflake account, all of the %ss to which a single grant is attached must be declared
by a single %s resource. This means that even any %s that have the attached
grant via any other mechanism (including other Terraform resources) will have that attached grant revoked by this resource.
These resources do not enforce exclusive attachment of a grant, it is the user's responsibility to enforce this.
`, name, granted_to_name, name, grant_resource_name)
fmt.Println("")
}
fmt.Printf("#### properties\n\n")
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetHeader([]string{"name", "type", "description", "optional", " required", "computed", "default"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
properties := make([]string, 0)
for k := range resource.Schema {
properties = append(properties, k)
}
sort.Strings(properties)
for _, property := range properties {
s := resource.Schema[property]
table.Append([]string{property, typeString(s.Type), s.Description, boolString(s.Optional), boolString(s.Required), boolString(s.Computed), interfaceString(s.Default)})
}
table.Render()
}
}
func typeString(t schema.ValueType) string {
switch t {
case schema.TypeBool:
return "bool"
case schema.TypeInt:
return "int"
case schema.TypeFloat:
return "float"
case schema.TypeString:
return "string"
case schema.TypeList:
return "list"
case schema.TypeMap:
return "map"
case schema.TypeSet:
return "set"
}
return "?"
}
func boolString(t bool) string { return fmt.Sprintf("%t", t) }
func interfaceString(t interface{}) string { return fmt.Sprintf("%#v", t) }