-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
76 lines (64 loc) · 1.9 KB
/
provider.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
package main
import (
"log"
"github.com/jfcantu/threatstack-golang/threatstack"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
// Provider - as required
func Provider() *schema.Provider {
p := &schema.Provider{
Schema: map[string]*schema.Schema{
"api_key": {
Type: schema.TypeString,
Required: true,
Description: "Threat Stack API key.",
DefaultFunc: schema.EnvDefaultFunc("THREATSTACK_API_KEY", nil),
},
"organization_id": {
Type: schema.TypeString,
Required: true,
Description: "Threat Stack organization ID.",
DefaultFunc: schema.EnvDefaultFunc("THREATSTACK_ORG_ID", nil),
},
"user_id": {
Type: schema.TypeString,
Required: true,
Description: "Threat Stack user ID.",
DefaultFunc: schema.EnvDefaultFunc("THREATSTACK_USER_ID", nil),
},
},
ResourcesMap: map[string]*schema.Resource{
"threatstack_ruleset": resourceRuleset(),
"threatstack_host_rule": resourceHostRule(),
"threatstack_file_rule": resourceFileRule(),
},
}
p.ConfigureFunc = func(d *schema.ResourceData) (interface{}, error) {
return providerConfigure(d)
}
return p
}
func providerConfigure(data *schema.ResourceData) (interface{}, error) {
config := Config{
APIKey: data.Get("api_key").(string),
OrganizationID: data.Get("organization_id").(string),
UserID: data.Get("user_id").(string),
}
log.Println("[INFO] Initializing Threat Stack client")
return config.Client()
}
// Client creates a new client.
func (cfg *Config) Client() (*threatstack.Client, error) {
tsconfig := &threatstack.Config{
BaseURL: "https://api.threatstack.com",
APIKey: cfg.APIKey,
OrganizationID: cfg.OrganizationID,
UserID: cfg.UserID,
}
log.Printf("[INFO] Creating Threat Stack client")
client, err := threatstack.NewClient(tsconfig)
if err != nil {
return nil, err
}
return client, err
}