Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC: Super simple Plugin Framework data source self-registration #26101

Merged
merged 41 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
9b83499
'providerConfigure' -> 'configure'.
ewbankkit Aug 2, 2022
a19fda3
Determine Terraform version in 'configure'.
ewbankkit Aug 2, 2022
f71adec
Start to tidy up 'expandAssumeRole'.
ewbankkit Aug 2, 2022
dcb2a50
Merge branch 'main' into f-simple-service-self-registration
ewbankkit Aug 2, 2022
a926fb2
Use 'flex' in 'expandAssumeRole'.
ewbankkit Aug 2, 2022
f3d52b9
Tidy up 'expandAssumeRoleWithWebIdentity'.
ewbankkit Aug 2, 2022
d385ae0
Use 'flex' in 'configure'.
ewbankkit Aug 2, 2022
b008bf0
'expandProviderDefaultTags' -> 'expandDefaultTags'.
ewbankkit Aug 2, 2022
041d4f5
'expandProviderIgnoreTags' -> 'expandIgnoreTags'.
ewbankkit Aug 2, 2022
f0cec01
Modify the signature of 'expandEndpoints'.
ewbankkit Aug 2, 2022
4b268b0
Merge branch 'main' into f-simple-service-self-registration
ewbankkit Aug 2, 2022
7cb89aa
Extract schema, resources map and data sources map to local variables.
ewbankkit Aug 2, 2022
63644ed
Tweak AWSClient generators.
ewbankkit Aug 2, 2022
588d242
Create AWSClient in the provider's ConfigureContextFunc.
ewbankkit Aug 2, 2022
6f15f4a
Rename aws_arn data source implementation source file.
ewbankkit Aug 2, 2022
215bc37
Simple Plugin Framework data source self-registration.
ewbankkit Aug 2, 2022
b1a19eb
Add TODOs.
ewbankkit Aug 2, 2022
18792b2
sweep: 'conf.Client' -> 'conf.ConfigureProvider'.
ewbankkit Aug 2, 2022
150389f
CodePipeline: 'conf.Client' -> 'conf.ConfigureProvider'.
ewbankkit Aug 2, 2022
d33013f
Minimize diffs.
ewbankkit Aug 2, 2022
d7902a9
Merge branch 'main' into f-simple-service-self-registration
ewbankkit Aug 19, 2022
61c7d25
Merge branch 'main' into f-simple-service-self-registration
ewbankkit Aug 26, 2022
0a99c6f
'ServiceData' -> 'Services'.
ewbankkit Aug 26, 2022
5121aea
'DataSourceTypes' -> 'DataSources'.
ewbankkit Aug 26, 2022
17294ae
Add 'ServiceData.Configure'.
ewbankkit Aug 26, 2022
ade7ccf
Configure each Service.
ewbankkit Aug 26, 2022
c7ef7d9
'ServiceDataMap' -> 'ServiceMap'.
ewbankkit Aug 26, 2022
556c929
Merge branch 'main' into f-simple-service-self-registration
ewbankkit Sep 2, 2022
dc4c1b4
tools/tfsdk2fw: Add 'emitImpliedType' to emit Framework code for a Co…
ewbankkit Sep 2, 2022
ef2d01a
tools/tfsdk2fw: Correct generated code template.
ewbankkit Sep 2, 2022
93c8cbd
tools/tfsdk2fw: Generate Plugin Framework schema for Plugin SDK v2 Co…
ewbankkit Sep 2, 2022
d9005aa
tools/tfsdk2fw: Additional comments.
ewbankkit Sep 2, 2022
d4fbbc7
Package for Terraform Plugin Framework provider-defined types.
ewbankkit Sep 2, 2022
f7e060b
Move Terraform Plugin Framework provider-defined types to 'internal/f…
ewbankkit Sep 2, 2022
f1f48e8
Move Terraform Plugin Framework provider-defined types to 'internal/f…
ewbankkit Sep 2, 2022
0370f02
tools/tfsdk2fw: Start to add Go struct generation.
ewbankkit Sep 2, 2022
b8aa99a
tools/tfsdk2fw: Add 'ToCamelCase'.
ewbankkit Sep 2, 2022
b459525
tools/tfsdk2fw: Start to generate Go structures.
ewbankkit Sep 2, 2022
2b217fc
Merge branch 'main' into f-simple-service-self-registration
ewbankkit Sep 9, 2022
bf76bf8
Restore missing data sources and resources after merge.
ewbankkit Sep 9, 2022
5762d39
Bump CI.
ewbankkit Sep 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions internal/conns/awsclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package conns

import (
"context"
"fmt"

"github.com/hashicorp/terraform-provider-aws/internal/intf"
)

// PartitionHostname returns a hostname with the provider domain suffix for the partition
// e.g. PREFIX.amazonaws.com
// The prefix should not contain a trailing period.
func (client *AWSClient) PartitionHostname(prefix string) string {
return fmt.Sprintf("%s.%s", prefix, client.DNSSuffix)
}

// RegionalHostname returns a hostname with the provider domain suffix for the region and partition
// e.g. PREFIX.us-west-2.amazonaws.com
// The prefix should not contain a trailing period.
func (client *AWSClient) RegionalHostname(prefix string) string {
return fmt.Sprintf("%s.%s.%s", prefix, client.Region, client.DNSSuffix)
}

func (client *AWSClient) Services(_ context.Context) map[string]intf.ServiceData {
return client.ServiceMap
}
18 changes: 2 additions & 16 deletions internal/conns/awsclient_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions internal/conns/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ type Config struct {
UseFIPSEndpoint bool
}

// Client configures and returns a fully initialized AWSClient
func (c *Config) Client(ctx context.Context) (interface{}, diag.Diagnostics) {
// ConfigureProvider configures the provided provider Meta (instance data).
func (c *Config) ConfigureProvider(ctx context.Context, client *AWSClient) (*AWSClient, diag.Diagnostics) {
awsbaseConfig := awsbase.Config{
AccessKey: c.AccessKey,
APNInfo: StdUserAgentProducts(c.TerraformVersion),
Expand Down Expand Up @@ -185,7 +185,7 @@ func (c *Config) Client(ctx context.Context) (interface{}, diag.Diagnostics) {
DNSSuffix = p.DNSSuffix()
}

client := c.clientConns(sess)
c.clientConns(client, sess)

client.AccountID = accountID
client.DefaultTagsConfig = c.DefaultTagsConfig
Expand Down
578 changes: 288 additions & 290 deletions internal/conns/config_gen.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions internal/fwtypes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Terraform Plugin Framework Provider-Defined Types

This package contains Terraform Plugin Framework [provider-defined types](https://www.terraform.io/plugin/framework/types#create-provider-defined-types-and-values) (and values).
3 changes: 1 addition & 2 deletions internal/service/meta/arn.go → internal/fwtypes/arn.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// TODO: Move this to a shared 'types' package.
package meta
package fwtypes

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// TODO: Move this to a shared 'types' package.
package meta_test
package fwtypes_test

import (
"context"
Expand All @@ -10,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-provider-aws/internal/service/meta"
"github.com/hashicorp/terraform-provider-aws/internal/fwtypes"
)

func TestARNTypeValueFromTerraform(t *testing.T) {
Expand All @@ -23,15 +22,15 @@ func TestARNTypeValueFromTerraform(t *testing.T) {
}{
"null value": {
val: tftypes.NewValue(tftypes.String, nil),
expected: meta.ARN{Null: true},
expected: fwtypes.ARN{Null: true},
},
"unknown value": {
val: tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
expected: meta.ARN{Unknown: true},
expected: fwtypes.ARN{Unknown: true},
},
"valid ARN": {
val: tftypes.NewValue(tftypes.String, "arn:aws:rds:us-east-1:123456789012:db:test"), // lintignore:AWSAT003,AWSAT005
expected: meta.ARN{Value: arn.ARN{
expected: fwtypes.ARN{Value: arn.ARN{
Partition: "aws",
Service: "rds",
Region: "us-east-1", // lintignore:AWSAT003
Expand All @@ -49,7 +48,7 @@ func TestARNTypeValueFromTerraform(t *testing.T) {
name, test := name, test
t.Run(name, func(t *testing.T) {
ctx := context.Background()
val, err := meta.ARNType.ValueFromTerraform(ctx, test.val)
val, err := fwtypes.ARNType.ValueFromTerraform(ctx, test.val)

if err == nil && test.expectError {
t.Fatal("expected error, got no error")
Expand Down Expand Up @@ -97,7 +96,7 @@ func TestARNTypeValidate(t *testing.T) {
t.Run(name, func(t *testing.T) {
ctx := context.Background()

diags := meta.ARNType.Validate(ctx, test.val, path.Root("test"))
diags := fwtypes.ARNType.Validate(ctx, test.val, path.Root("test"))

if !diags.HasError() && test.expectError {
t.Fatal("expected error, got no error")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// TODO: Move this to a shared 'types' package.
package fwprovider
package fwtypes

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// TODO: Move this to a shared 'types' package.
package fwprovider_test
package fwtypes_test

import (
"context"
Expand All @@ -10,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-provider-aws/internal/provider/fwprovider"
"github.com/hashicorp/terraform-provider-aws/internal/fwtypes"
)

func TestDurationTypeValueFromTerraform(t *testing.T) {
Expand All @@ -23,15 +22,15 @@ func TestDurationTypeValueFromTerraform(t *testing.T) {
}{
"null value": {
val: tftypes.NewValue(tftypes.String, nil),
expected: fwprovider.Duration{Null: true},
expected: fwtypes.Duration{Null: true},
},
"unknown value": {
val: tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
expected: fwprovider.Duration{Unknown: true},
expected: fwtypes.Duration{Unknown: true},
},
"valid duration": {
val: tftypes.NewValue(tftypes.String, "2h"),
expected: fwprovider.Duration{Value: 2 * time.Hour},
expected: fwtypes.Duration{Value: 2 * time.Hour},
},
"invalid duration": {
val: tftypes.NewValue(tftypes.String, "not ok"),
Expand All @@ -43,7 +42,7 @@ func TestDurationTypeValueFromTerraform(t *testing.T) {
name, test := name, test
t.Run(name, func(t *testing.T) {
ctx := context.Background()
val, err := fwprovider.DurationType.ValueFromTerraform(ctx, test.val)
val, err := fwtypes.DurationType.ValueFromTerraform(ctx, test.val)

if err == nil && test.expectError {
t.Fatal("expected error, got no error")
Expand Down Expand Up @@ -91,7 +90,7 @@ func TestDurationTypeValidate(t *testing.T) {
t.Run(name, func(t *testing.T) {
ctx := context.Background()

diags := fwprovider.DurationType.Validate(ctx, test.val, path.Root("test"))
diags := fwtypes.DurationType.Validate(ctx, test.val, path.Root("test"))

if !diags.HasError() && test.expectError {
t.Fatal("expected error, got no error")
Expand Down
31 changes: 31 additions & 0 deletions internal/generate/awsclient/file.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Code generated by internal/generate/awsclient/main.go; DO NOT EDIT.
package conns

import (
{{ range .Services }}
"github.com/aws/aws-sdk-go{{ if eq .SDKVersion "2" }}-v2{{ end }}/service/{{ .GoPackage }}"
{{- end }}
"github.com/aws/aws-sdk-go/aws/session"
"github.com/hashicorp/terraform-provider-aws/internal/intf"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

type AWSClient struct {
AccountID string
DefaultTagsConfig *tftags.DefaultConfig
DNSSuffix string
IgnoreTagsConfig *tftags.IgnoreConfig
MediaConvertAccountConn *mediaconvert.MediaConvert
Partition string
Region string
ReverseDNSPrefix string
S3ConnURICleaningDisabled *s3.S3
ServiceMap map[string]intf.ServiceData
Session *session.Session
SupportedPlatforms []string
TerraformVersion string

{{ range .Services }}
{{ .ProviderNameUpper }}Conn *{{ .GoPackage }}.{{ .ClientTypeName }}
{{- end }}
}
51 changes: 3 additions & 48 deletions internal/generate/awsclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"bytes"
_ "embed"
"encoding/csv"
"fmt"
"go/format"
Expand Down Expand Up @@ -120,51 +121,5 @@ func writeTemplate(body string, templateName string, td TemplateData) {
}
}

var tmpl = `
// Code generated by internal/generate/awsclient/main.go; DO NOT EDIT.
package conns

import (
"fmt"


{{ range .Services }}
"github.com/aws/aws-sdk-go{{ if eq .SDKVersion "2" }}-v2{{ end }}/service/{{ .GoPackage }}"
{{- end }}
"github.com/aws/aws-sdk-go/aws/session"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

type AWSClient struct {
AccountID string
DefaultTagsConfig *tftags.DefaultConfig
DNSSuffix string
IgnoreTagsConfig *tftags.IgnoreConfig
MediaConvertAccountConn *mediaconvert.MediaConvert
Partition string
Region string
ReverseDNSPrefix string
S3ConnURICleaningDisabled *s3.S3
Session *session.Session
SupportedPlatforms []string
TerraformVersion string

{{ range .Services }}
{{ .ProviderNameUpper }}Conn *{{ .GoPackage }}.{{ .ClientTypeName }}
{{- end }}
}

// PartitionHostname returns a hostname with the provider domain suffix for the partition
// e.g. PREFIX.amazonaws.com
// The prefix should not contain a trailing period.
func (client *AWSClient) PartitionHostname(prefix string) string {
return fmt.Sprintf("%s.%s", prefix, client.DNSSuffix)
}

// RegionalHostname returns a hostname with the provider domain suffix for the region and partition
// e.g. PREFIX.us-west-2.amazonaws.com
// The prefix should not contain a trailing period.
func (client *AWSClient) RegionalHostname(prefix string) string {
return fmt.Sprintf("%s.%s.%s", prefix, client.Region, client.DNSSuffix)
}
`
//go:embed file.tmpl
var tmpl string
17 changes: 17 additions & 0 deletions internal/generate/clientconfig/file.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Code generated by internal/generate/clientconfig/main.go; DO NOT EDIT.
package conns

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
{{- range .Services }}
"github.com/aws/aws-sdk-go{{ if eq .SDKVersion "2" }}-v2{{ end }}/service/{{ .GoPackage }}"
{{- end }}
"github.com/hashicorp/terraform-provider-aws/names"
)

func (c *Config) clientConns(client *AWSClient, sess *session.Session) {
{{- range .Services }}
client.{{ .ProviderNameUpper }}Conn = {{ .GoPackage }}.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints[names.{{ .ProviderNameUpper }}])}))
{{- end }}
}
24 changes: 3 additions & 21 deletions internal/generate/clientconfig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main

import (
"bytes"
_ "embed"
"encoding/csv"
"fmt"
"go/format"
Expand Down Expand Up @@ -117,24 +118,5 @@ func writeTemplate(body string, templateName string, td TemplateData) {
}
}

var tmpl = `
// Code generated by internal/generate/clientconfig/main.go; DO NOT EDIT.
package conns

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
{{- range .Services }}
"github.com/aws/aws-sdk-go{{ if eq .SDKVersion "2" }}-v2{{ end }}/service/{{ .GoPackage }}"
{{- end }}
"github.com/hashicorp/terraform-provider-aws/names"
)

func (c *Config) clientConns(sess *session.Session) *AWSClient {
return &AWSClient{
{{- range .Services }}
{{ .ProviderNameUpper }}Conn: {{ .GoPackage }}.New(sess.Copy(&aws.Config{Endpoint: aws.String(c.Endpoints[names.{{ .ProviderNameUpper }}])})),
{{- end }}
}
}
`
//go:embed file.tmpl
var tmpl string
9 changes: 9 additions & 0 deletions internal/intf/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package intf

import (
"context"
)

type ProviderData interface {
Services(context.Context) map[string]ServiceData
}
13 changes: 13 additions & 0 deletions internal/intf/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package intf

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/provider"
)

// ServiceData is data about a service.
type ServiceData interface {
Configure(context.Context, ProviderData) error
DataSources(context.Context) (map[string]provider.DataSourceType, error)
}
Loading