forked from chriskuchin/terraform-provider-bowtie
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bowtie.go
195 lines (164 loc) · 6.27 KB
/
bowtie.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package provider
import (
"context"
"os"
"github.com/bowtieworks/terraform-provider-bowtie/internal/bowtie/client"
"github.com/bowtieworks/terraform-provider-bowtie/internal/bowtie/data_sources"
"github.com/bowtieworks/terraform-provider-bowtie/internal/bowtie/resources"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
)
type BowtieProvider struct{}
type bowtieProviderModel struct {
Host types.String `tfsdk:"host"`
Username types.String `tfsdk:"username"`
Password types.String `tfsdk:"password"`
LazyAuthentication types.Bool `tfsdk:"lazy_authentication"`
TaggedLocations types.Bool `tfsdk:"tagged_locations"`
}
func New() provider.Provider {
return &BowtieProvider{}
}
func (b *BowtieProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: `The Bowtie provider for Terraform configures your Bowtie installation via native Terraform resources instead of the Controller web interface. Use the provider to declaratively manage API resources such as resource groups, DNS resources, user groups, and more.
Note that you must configure appropriate credentials to authenticate with the Bowtie API before you can use this provider.
For more documentation about installing and configuring Bowtie, refer to the official [Bowtie documentation](https://docs.bowtie.works/).
`,
Attributes: map[string]schema.Attribute{
"host": schema.StringAttribute{
Description: "The Bowtie HTTP Controller endpoint. Honors the `BOWTIE_HOST` environment variable if set. Example: `https://bowtie.example.com`",
Optional: true,
},
"username": schema.StringAttribute{
Description: "Administrator username/email login credentials. Honors the `BOWTIE_USERNAME` environment variable if set",
Optional: true,
},
"password": schema.StringAttribute{
Description: "Administrator password login credentials. Honors the `BOWTIE_PASSWORD` environment variable if set",
Sensitive: true,
Optional: true,
},
"lazy_authentication": schema.BoolAttribute{
Description: "By default, the provider will authenticate to the Bowtie API just in time (or lazily) which permits use cases like creating Controllers in Terraform before using their API endpoints. Set this variable to `false` if you instead want to authenticate at the time the provider is configured - for example, to catch authentication errors up-front before starting an `apply` or `plan`.",
Optional: true,
},
"tagged_locations": schema.BoolAttribute{
Description: "Control whether the provider will send policy resource locations using the new tagged type format or legacy format.",
Optional: true,
},
},
}
}
func (b *BowtieProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "bowtie"
}
func (b *BowtieProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var config bowtieProviderModel
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
lazy_auth := true
tagged_locations := true
if config.Host.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("host"),
"Unknown Bowtie API Host",
"The provider cannot create the Bowtie API Client as the host value is unknown",
)
}
if config.Username.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("username"),
"Unknown Bowtie API Username",
"The provider cannot create the Bowtie API Client as the username value is unknown",
)
}
if config.Password.IsUnknown() {
resp.Diagnostics.AddAttributeError(
path.Root("password"),
"Unknown Bowtie API Password",
"The provider cannot create the Bowtie API Client as the password value is unknown",
)
}
if resp.Diagnostics.HasError() {
return
}
host := os.Getenv("BOWTIE_HOST")
username := os.Getenv("BOWTIE_USERNAME")
password := os.Getenv("BOWTIE_PASSWORD")
if !config.Host.IsNull() {
host = config.Host.ValueString()
}
if !config.Username.IsNull() {
username = config.Username.ValueString()
}
if !config.Password.IsNull() {
password = config.Password.ValueString()
}
if host == "" {
resp.Diagnostics.AddAttributeError(
path.Root("host"),
"Missing Bowtie API Host",
"The provider cannot create the Bowtie API client without a host being set",
)
}
if username == "" {
resp.Diagnostics.AddAttributeError(
path.Root("username"),
"Missing Bowtie API Username",
"The provider cannot create the Bowtie API Client without a username",
)
}
if password == "" {
resp.Diagnostics.AddAttributeError(
path.Root("password"),
"Missing Bowtie API Password",
"The provider cannot create the Bowtie API Client without a password",
)
}
if !config.LazyAuthentication.IsNull() {
lazy_auth = config.LazyAuthentication.ValueBool()
}
if !config.TaggedLocations.IsNull() {
tagged_locations = config.TaggedLocations.ValueBool()
}
if resp.Diagnostics.HasError() {
return
}
client, err := client.NewClient(host, username, password, lazy_auth, tagged_locations)
if err != nil {
resp.Diagnostics.AddError(
"Failed to create Bowtie API Client",
"An unexpected error creating the Bowtie API Client: "+err.Error(),
)
return
}
resp.DataSourceData = client
resp.ResourceData = client
}
func (b *BowtieProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
resources.NewDNSBlockListResource,
resources.NewDNSResource,
resources.NewGroupResource,
resources.NewOrganizationResource,
resources.NewSiteRangeResource,
resources.NewSiteResource,
resources.NewResourceResource,
resources.NewResourceGroupResource,
resources.NewGroupMembershipResource,
resources.NewUserResource,
}
}
func (b *BowtieProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
data_sources.NewUserDataSource,
}
}