-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
152 lines (134 loc) · 4.84 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package provider
import (
"context"
"fmt"
"github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/nullstone-io/terraform-provider-ns/internal/server"
"github.com/nullstone-io/terraform-provider-ns/ns"
"gopkg.in/nullstone-io/go-api-client.v0"
"log"
)
func Mock(version string, getNsConfig func() api.Config, getTfeConfig func() *tfe.Config, alterPlanConfig func(config *PlanConfig)) tfprotov5.ProviderServer {
return newProviderServer(version, func() (api.Config, *tfe.Config, PlanConfig) {
apiConfig := getNsConfig()
tfeConfig := getTfeConfig()
planConfig, _ := LoadPlanConfig()
if alterPlanConfig != nil {
alterPlanConfig(&planConfig)
}
return apiConfig, tfeConfig, planConfig
})
}
func New(version string) tfprotov5.ProviderServer {
return newProviderServer(version, func() (api.Config, *tfe.Config, PlanConfig) {
apiConfig := api.DefaultConfig()
if profile, ac, _ := ns.LoadProfile(); profile != nil {
apiConfig = ac
}
tfeConfig := ns.NewTfeConfig(apiConfig)
planConfig, _ := LoadPlanConfig()
return apiConfig, tfeConfig, planConfig
})
}
func newProviderServer(version string, fn func() (api.Config, *tfe.Config, PlanConfig)) tfprotov5.ProviderServer {
s := server.MustNew(func() server.Provider {
apiConfig, tfeConfig, planConfig := fn()
return &provider{
Version: version,
NsConfig: apiConfig,
TfeConfig: tfeConfig,
PlanConfig: &planConfig,
}
})
// data sources
s.MustRegisterDataSource("ns_workspace", newDataWorkspace)
s.MustRegisterDataSource("ns_connection", newDataConnection)
s.MustRegisterDataSource("ns_app_connection", newDataAppConnection)
s.MustRegisterDataSource("ns_subdomain", newDataSubdomain)
s.MustRegisterDataSource("ns_domain", newDataDomain)
s.MustRegisterDataSource("ns_app_env", newDataAppEnv)
s.MustRegisterDataSource("ns_env_variables", newDataEnvVariables)
s.MustRegisterDataSource("ns_secret_keys", newDataSecretKeys)
s.MustRegisterDataSource("ns_env", newDataEnv)
// resources
s.MustRegisterResource("ns_autogen_subdomain", newResourceAutogenSubdomain)
s.MustRegisterResource("ns_autogen_subdomain_delegation", newResourceAutogenSubdomainDelegation)
return s
}
var _ server.Provider = (*provider)(nil)
type provider struct {
Version string
TfeConfig *tfe.Config
TfeClient *tfe.Client
NsConfig api.Config
PlanConfig *PlanConfig
}
func (p *provider) Schema(ctx context.Context) *tfprotov5.Schema {
return &tfprotov5.Schema{
Version: 1,
Block: &tfprotov5.SchemaBlock{
Attributes: []*tfprotov5.SchemaAttribute{
{
Name: "organization",
Type: tftypes.String,
Optional: true,
Description: "Configure provider with this organization.",
DescriptionKind: tfprotov5.StringKindMarkdown,
},
{
Name: "capability_id",
Type: tftypes.Number,
Optional: true,
Description: "Configure provider with the context of the capability's id",
DescriptionKind: tfprotov5.StringKindMarkdown,
},
},
},
}
}
func (p *provider) Validate(ctx context.Context, config map[string]tftypes.Value) (diags []*tfprotov5.Diagnostic, err error) {
if !config["organization"].IsNull() {
var orgName string
if err := config["organization"].As(&orgName); err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "organization must be a string",
})
}
}
if p.NsConfig.ApiKey == "" {
diags = append(diags, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: fmt.Sprintf("Nullstone API Key is required (Set %q environment variable)", api.ApiKeyEnvVar),
})
}
if p.TfeConfig.Token == "" {
diags = append(diags, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: fmt.Sprintf("TFE Token is required (Set %q environment variable)", api.ApiKeyEnvVar),
})
}
if len(diags) > 0 {
return diags, nil
}
return nil, nil
}
func (p *provider) Configure(ctx context.Context, config map[string]tftypes.Value) (diags []*tfprotov5.Diagnostic, err error) {
log.Printf("[DEBUG] Configuring Nullstone provider %s", p.Version)
if !config["organization"].IsNull() {
// This is already checked in Validate, just cast it
config["organization"].As(&p.PlanConfig.OrgName)
}
p.NsConfig.OrgName = p.PlanConfig.OrgName
log.Printf("[DEBUG] Configured Nullstone API client (Address=%s)\n", p.NsConfig.BaseAddress)
p.PlanConfig.CapabilityId = extractInt64FromConfig(config, "capability_id")
log.Printf("[DEBUG] capability_id set to %d\n", p.PlanConfig.CapabilityId)
p.TfeClient, err = tfe.NewClient(p.TfeConfig)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] Configured TFE client (Address=%s, BasePath=%s)\n", p.TfeConfig.Address, p.TfeConfig.BasePath)
return nil, nil
}