-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprovider.go
122 lines (107 loc) · 3.44 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
package provider
import (
"context"
"net/http"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/labd/contentstack-go-sdk/management"
)
func New(version string) func() tfsdk.Provider {
return func() tfsdk.Provider {
return &provider{version: version}
}
}
type provider struct {
stack *management.StackInstance
client *management.Client
version string
}
// GetSchema
func (p *provider) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"base_url": {
Type: types.StringType,
Optional: true,
Description: "The BaseURL, e.g. https://eu-api.contentstack.com/. See https://www.contentstack.com/docs/developers/apis/content-management-api/#base-url",
},
"api_key": {
Type: types.StringType,
Optional: true,
Description: "The API key is a unique key assigned to each stack.",
},
"management_token": {
Type: types.StringType,
Optional: true,
Sensitive: true,
Description: "Management Tokens are stack-level tokens, with no users attached to them.",
},
"auth_token": {
Type: types.StringType,
Optional: true,
Sensitive: true,
Description: "The Authtoken is a read-write token used to make authorized CMA requests, and it is a user-specific token.",
},
},
}, nil
}
// Provider schema struct
type providerData struct {
BaseURL types.String `tfsdk:"base_url"`
AuthToken types.String `tfsdk:"auth_token"`
ApiKey types.String `tfsdk:"api_key"`
ManagementToken types.String `tfsdk:"management_token"`
}
func (p *provider) Configure(ctx context.Context, req tfsdk.ConfigureProviderRequest, resp *tfsdk.ConfigureProviderResponse) {
// Retrieve provider data from configuration
var config providerData
diags := req.Config.Get(ctx, &config)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
cfg := management.ClientConfig{
BaseURL: config.BaseURL.Value,
AuthToken: config.AuthToken.Value,
HTTPClient: &http.Client{
Transport: management.DebugTransport,
},
}
c, err := management.NewClient(cfg)
if err != nil {
resp.Diagnostics.AddError(
"Unable to create client",
"Unable to create contentstack client:\n\n"+err.Error(),
)
return
}
stackAuth := management.StackAuth{
ApiKey: config.ApiKey.Value,
ManagementToken: config.ManagementToken.Value,
}
instance, err := c.Stack(&stackAuth)
if err != nil {
resp.Diagnostics.AddError(
"Unable to create stack client",
"Unable to create contentstack stack client:\n\n"+err.Error(),
)
return
}
p.client = c
p.stack = instance
}
// GetResources - Defines provider resources
func (p *provider) GetResources(_ context.Context) (map[string]tfsdk.ResourceType, diag.Diagnostics) {
return map[string]tfsdk.ResourceType{
"contentstack_content_type": resourceContentTypeType{},
"contentstack_environment": resourceEnvironmentType{},
"contentstack_global_field": resourceGlobalFieldType{},
"contentstack_locale": resourceLocaleType{},
"contentstack_webhook": resourceWebhookType{},
}, nil
}
// GetDataSources - Defines provider data sources
func (p *provider) GetDataSources(_ context.Context) (map[string]tfsdk.DataSourceType, diag.Diagnostics) {
return map[string]tfsdk.DataSourceType{}, nil
}