Skip to content

Commit 9a6e8c2

Browse files
feat: update to terraform plugin framework
1 parent 910289c commit 9a6e8c2

File tree

3,375 files changed

+578
-1075153
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

3,375 files changed

+578
-1075153
lines changed

githubfile/provider.go

Lines changed: 119 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,20 @@
1515
package githubfile
1616

1717
import (
18+
"context"
1819
"encoding/base64"
19-
"fmt"
20+
"os"
21+
2022
"github.com/google/go-github/v54/github"
21-
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
22-
tpg "github.com/integrations/terraform-provider-github/v5/github"
23-
"strings"
23+
"github.com/hashicorp/terraform-plugin-framework/datasource"
24+
"github.com/hashicorp/terraform-plugin-framework/provider"
25+
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
26+
"github.com/hashicorp/terraform-plugin-framework/resource"
27+
"github.com/hashicorp/terraform-plugin-framework/types"
28+
"golang.org/x/oauth2"
2429
)
2530

26-
const (
27-
resourceFileName = "githubfile_file"
28-
)
29-
30-
const (
31-
commitMessagePrefixKey = "commit_message_prefix"
32-
githubEmailKey = "github_email"
33-
githubTokenKey = "github_token"
34-
githubUsernameKey = "github_username"
35-
gpgPassphraseKey = "gpg_passphrase"
36-
gpgSecretKeyKey = "gpg_secret_key"
37-
)
31+
var _ provider.Provider = &githubfileProvider{}
3832

3933
type providerConfiguration struct {
4034
commitMessagePrefix string
@@ -45,83 +39,131 @@ type providerConfiguration struct {
4539
gpgSecretKey string
4640
}
4741

48-
func Provider() *schema.Provider {
49-
return &schema.Provider{
50-
ConfigureFunc: func(d *schema.ResourceData) (interface{}, error) {
51-
c := tpg.Config{
52-
Token: d.Get(githubTokenKey).(string),
53-
BaseURL: "https://api.github.com/",
54-
}
55-
56-
gc, err := c.NewRESTClient(c.AuthenticatedHTTPClient())
57-
if err != nil {
58-
return nil, fmt.Errorf("failed to create GitHub Client: %v", err)
59-
}
60-
61-
// Support reading a base64-encoded GPG secret key.
62-
sk := d.Get(gpgSecretKeyKey).(string)
63-
if v, err := base64.StdEncoding.DecodeString(sk); err == nil {
64-
sk = string(v)
65-
}
66-
return &providerConfiguration{
67-
commitMessagePrefix: d.Get(commitMessagePrefixKey).(string),
68-
githubClient: gc,
69-
githubEmail: d.Get(githubEmailKey).(string),
70-
githubUsername: d.Get(githubUsernameKey).(string),
71-
gpgSecretKey: sk,
72-
gpgPassphrase: d.Get(gpgPassphraseKey).(string),
73-
}, nil
74-
},
75-
ResourcesMap: map[string]*schema.Resource{
76-
resourceFileName: resourceFile(),
77-
},
78-
Schema: map[string]*schema.Schema{
79-
commitMessagePrefixKey: {
80-
Type: schema.TypeString,
81-
DefaultFunc: defaultFuncForKey(commitMessagePrefixKey),
42+
type githubfileProvider struct{}
43+
44+
type githubfileProviderModel struct {
45+
CommitMessagePrefix types.String `tfsdk:"commit_message_prefix"`
46+
GithubEmail types.String `tfsdk:"github_email"`
47+
GithubToken types.String `tfsdk:"github_token"`
48+
GithubUsername types.String `tfsdk:"github_username"`
49+
GpgPassphrase types.String `tfsdk:"gpg_passphrase"`
50+
GpgSecretKey types.String `tfsdk:"gpg_secret_key"`
51+
}
52+
53+
// New returns a new instance of the githubfile provider.
54+
func New() provider.Provider {
55+
return &githubfileProvider{}
56+
}
57+
58+
func (p *githubfileProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
59+
resp.TypeName = "githubfile"
60+
}
61+
62+
func (p *githubfileProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
63+
resp.Schema = schema.Schema{
64+
Attributes: map[string]schema.Attribute{
65+
"commit_message_prefix": schema.StringAttribute{
8266
Optional: true,
83-
Sensitive: false,
84-
Description: "An optional prefix to be added to all commits created as a result of manipulating files.",
67+
Description: "An optional prefix to be added to all commits created as a result of manipulating files. Can also be set via the COMMIT_MESSAGE_PREFIX environment variable.",
8568
},
86-
githubEmailKey: {
87-
Type: schema.TypeString,
88-
DefaultFunc: defaultFuncForKey(githubEmailKey),
89-
Required: true,
69+
"github_email": schema.StringAttribute{
70+
Optional: true,
9071
Sensitive: true,
91-
Description: "The email address to use for commit messages. If a GPG key is provided, this must match the one which the key corresponds to.",
72+
Description: "The email address to use for commit messages. If a GPG key is provided, this must match the one which the key corresponds to. Can also be set via the GITHUB_EMAIL environment variable.",
9273
},
93-
githubTokenKey: {
94-
Type: schema.TypeString,
95-
DefaultFunc: defaultFuncForKey(githubTokenKey),
96-
Required: true,
74+
"github_token": schema.StringAttribute{
75+
Optional: true,
9776
Sensitive: true,
98-
Description: "A GitHub authorisation token with permissions to manage CRUD files in the target repositories.",
77+
Description: "A GitHub authorisation token with permissions to manage CRUD files in the target repositories. Can also be set via the GITHUB_TOKEN environment variable.",
9978
},
100-
githubUsernameKey: {
101-
Type: schema.TypeString,
102-
DefaultFunc: defaultFuncForKey(githubUsernameKey),
103-
Required: true,
79+
"github_username": schema.StringAttribute{
80+
Optional: true,
10481
Sensitive: true,
105-
Description: "The username to use for commit messages.",
82+
Description: "The username to use for commit messages. Can also be set via the GITHUB_USERNAME environment variable.",
10683
},
107-
gpgPassphraseKey: {
108-
Type: schema.TypeString,
109-
DefaultFunc: defaultFuncForKey(gpgPassphraseKey),
84+
"gpg_passphrase": schema.StringAttribute{
11085
Optional: true,
11186
Sensitive: true,
112-
Description: fmt.Sprintf("The passphrase associated with the provided %q.", gpgSecretKeyKey),
87+
Description: "The passphrase associated with the provided \"gpg_secret_key\". Can also be set via the GPG_PASSPHRASE environment variable.",
11388
},
114-
gpgSecretKeyKey: {
115-
Type: schema.TypeString,
116-
DefaultFunc: defaultFuncForKey(gpgSecretKeyKey),
89+
"gpg_secret_key": schema.StringAttribute{
11790
Optional: true,
11891
Sensitive: true,
119-
Description: "The GPG secret key to be use for commit signing.",
92+
Description: "The GPG secret key to be use for commit signing. Can also be set via the GPG_SECRET_KEY environment variable.",
12093
},
12194
},
12295
}
12396
}
12497

125-
func defaultFuncForKey(v string) schema.SchemaDefaultFunc {
126-
return schema.EnvDefaultFunc(strings.ToUpper(v), "")
98+
func (p *githubfileProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
99+
var config githubfileProviderModel
100+
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
101+
if resp.Diagnostics.HasError() {
102+
return
103+
}
104+
105+
token := stringValueOrEnv(config.GithubToken, "GITHUB_TOKEN")
106+
if token == "" {
107+
resp.Diagnostics.AddError(
108+
"Missing GitHub Token",
109+
"github_token must be configured or the GITHUB_TOKEN environment variable must be set.",
110+
)
111+
return
112+
}
113+
114+
email := stringValueOrEnv(config.GithubEmail, "GITHUB_EMAIL")
115+
if email == "" {
116+
resp.Diagnostics.AddError(
117+
"Missing GitHub Email",
118+
"github_email must be configured or the GITHUB_EMAIL environment variable must be set.",
119+
)
120+
return
121+
}
122+
123+
username := stringValueOrEnv(config.GithubUsername, "GITHUB_USERNAME")
124+
if username == "" {
125+
resp.Diagnostics.AddError(
126+
"Missing GitHub Username",
127+
"github_username must be configured or the GITHUB_USERNAME environment variable must be set.",
128+
)
129+
return
130+
}
131+
132+
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
133+
tc := oauth2.NewClient(ctx, ts)
134+
gc := github.NewClient(tc)
135+
136+
sk := stringValueOrEnv(config.GpgSecretKey, "GPG_SECRET_KEY")
137+
if v, err := base64.StdEncoding.DecodeString(sk); err == nil {
138+
sk = string(v)
139+
}
140+
141+
providerConfig := &providerConfiguration{
142+
commitMessagePrefix: stringValueOrEnv(config.CommitMessagePrefix, "COMMIT_MESSAGE_PREFIX"),
143+
githubClient: gc,
144+
githubEmail: email,
145+
githubUsername: username,
146+
gpgSecretKey: sk,
147+
gpgPassphrase: stringValueOrEnv(config.GpgPassphrase, "GPG_PASSPHRASE"),
148+
}
149+
150+
resp.DataSourceData = providerConfig
151+
resp.ResourceData = providerConfig
152+
}
153+
154+
func (p *githubfileProvider) Resources(_ context.Context) []func() resource.Resource {
155+
return []func() resource.Resource{
156+
NewFileResource,
157+
}
158+
}
159+
160+
func (p *githubfileProvider) DataSources(_ context.Context) []func() datasource.DataSource {
161+
return nil
162+
}
163+
164+
func stringValueOrEnv(v types.String, envKey string) string {
165+
if !v.IsNull() && !v.IsUnknown() {
166+
return v.ValueString()
167+
}
168+
return os.Getenv(envKey)
127169
}

githubfile/provider_test.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,12 @@ import (
1818
"os"
1919
"testing"
2020

21-
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
22-
"github.com/hashicorp/terraform-plugin-sdk/terraform"
21+
"github.com/hashicorp/terraform-plugin-framework/providerserver"
22+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
2323
)
2424

25-
var (
26-
testAccProviders map[string]terraform.ResourceProvider
27-
testAccProvider *schema.Provider
28-
)
29-
30-
func init() {
31-
testAccProvider = Provider()
32-
testAccProviders = map[string]terraform.ResourceProvider{
33-
"githubfile": testAccProvider,
34-
}
25+
var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
26+
"githubfile": providerserver.NewProtocol6WithError(New()),
3527
}
3628

3729
func testAccPreCheck(t *testing.T) {
@@ -48,7 +40,8 @@ func testAccPreCheck(t *testing.T) {
4840
}
4941

5042
func TestProvider(t *testing.T) {
51-
if err := Provider().InternalValidate(); err != nil {
52-
t.Fatalf("failed to test provider: %v", err)
43+
p := New()
44+
if p == nil {
45+
t.Fatal("provider should not be nil")
5346
}
5447
}

0 commit comments

Comments
 (0)