generated from hashicorp/terraform-provider-scaffolding-framework
/
provider.go
160 lines (139 loc) · 4.35 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
153
154
155
156
157
158
159
160
package provider
import (
"context"
"github.com/hashicorp/go-retryablehttp"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"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/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/labd/apollostudio-go-sdk/apollostudio"
"os"
"regexp"
"time"
)
var (
defaultRetryMax = 10
defaultTimeout = 2 * time.Minute
retryTimeout = 5 * time.Second
_ provider.Provider = &ApolloStudioProvider{}
)
// ApolloStudioProvider defines the provider implementation.
type ApolloStudioProvider struct {
// version is set to the provider version on release, "dev" when the
// provider is built and ran locally, and "test" when running acceptance
// testing.
version string
// debug can be used to enable more logs
debug bool
}
// ApolloStudioProviderModel describes the provider data model.
type ApolloStudioProviderModel struct {
ApiKey types.String `tfsdk:"api_key"`
GraphRef types.String `tfsdk:"graph_ref"`
}
func New(version string, debug bool) func() provider.Provider {
return func() provider.Provider {
return &ApolloStudioProvider{
version: version,
debug: debug,
}
}
}
func (p *ApolloStudioProvider) Metadata(
_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse,
) {
resp.TypeName = "apollostudio"
resp.Version = p.version
}
func (p *ApolloStudioProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "The Apollo Studio provider allows you to manage your Apollo Studio Graphs and Subgraphs.",
Attributes: map[string]schema.Attribute{
"api_key": schema.StringAttribute{
MarkdownDescription: "Apollo studio graph API key",
Optional: true,
Sensitive: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
},
"graph_ref": schema.StringAttribute{
MarkdownDescription: "Apollo studio graph ref",
Optional: true,
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompilePOSIX(`^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+$`),
"should be in the format of <graph-name>@<variant-name>",
),
},
},
},
}
}
func (p *ApolloStudioProvider) Configure(
ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse,
) {
var data ApolloStudioProviderModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
var key string
if data.ApiKey.IsUnknown() || data.ApiKey.IsNull() {
key = os.Getenv("APOLLO_API_KEY")
} else {
key = data.ApiKey.ValueString()
}
var ref string
if data.GraphRef.IsUnknown() || data.GraphRef.IsNull() {
ref = os.Getenv("APOLLO_GRAPH_REF")
} else {
ref = data.GraphRef.ValueString()
}
if key == "" {
resp.Diagnostics.AddAttributeError(
path.Root("api_key"),
"Missing Apollo Studio API key",
"Please set the api_key attribute or the APOLLO_KEY environment variable",
)
return
}
if ref == "" {
resp.Diagnostics.AddAttributeError(
path.Root("graph_ref"),
"Missing Apollo Studio Graph ref",
"Please set the graph_ref attribute or the APOLLO_GRAPH_REF environment variable",
)
return
}
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = defaultRetryMax
var clientOpts = []apollostudio.ClientOpt{
apollostudio.WithHttpClient(retryClient.StandardClient()),
apollostudio.WithDebug(p.debug),
}
client, err := apollostudio.NewClient(key, ref, clientOpts...)
if err != nil {
resp.Diagnostics.AddError(
"Failed to create Apollo Studio client",
"Please check your API key and Graph ref",
)
return
}
resp.DataSourceData = client
resp.ResourceData = client
}
func (p *ApolloStudioProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewValidationDataSource,
}
}
func (p *ApolloStudioProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewSubGraphResource,
}
}