Skip to content

Commit

Permalink
Support data source
Browse files Browse the repository at this point in the history
  • Loading branch information
magodo committed Dec 4, 2024
1 parent 203ca5a commit fde21fe
Show file tree
Hide file tree
Showing 17 changed files with 99,650 additions and 34 deletions.
10 changes: 7 additions & 3 deletions .tools/schema-generator/aws/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ import (
)

func main() {
schemas := map[string]*tfschema.Schema{}
provider, err := provider.New(context.TODO())
if err != nil {
log.Fatal(err)
}
rschs := map[string]*tfschema.Schema{}
for name, rs := range provider.ResourcesMap {
schemas[name] = &tfschema.Schema{Block: tfpluginschema.FromSDKv2SchemaMap(rs.Schema)}
rschs[name] = &tfschema.Schema{Block: tfpluginschema.FromSDKv2SchemaMap(rs.Schema)}
}
b, err := json.MarshalIndent(tfschema.ProviderSchema{ResourceSchemas: schemas}, "", " ")
dschs := map[string]*tfschema.Schema{}
for name, ds := range provider.DataSourcesMap {
dschs[name] = &tfschema.Schema{Block: tfpluginschema.FromSDKv2SchemaMap(ds.Schema)}
}
b, err := json.MarshalIndent(tfschema.ProviderSchema{ResourceSchemas: rschs, DatasourceSchemas: dschs}, "", " ")
if err != nil {
log.Fatal(err)
}
Expand Down
12 changes: 9 additions & 3 deletions .tools/schema-generator/azapi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ func main() {
if err != nil {
log.Fatal(err)
}
schemas := map[string]*tfschema.Schema{}
rschs := map[string]*tfschema.Schema{}
for name, rs := range sch.ResourceSchemas {
schemas[name] = &tfschema.Schema{Block: rs.Block}
rschs[name] = &tfschema.Schema{Block: rs.Block}
}
b, err := json.MarshalIndent(tfschema.ProviderSchema{ResourceSchemas: schemas}, "", " ")

dschs := map[string]*tfschema.Schema{}
for name, ds := range sch.DataSourceSchemas {
dschs[name] = &tfschema.Schema{Block: ds.Block}
}

b, err := json.MarshalIndent(tfschema.ProviderSchema{ResourceSchemas: rschs, DatasourceSchemas: dschs}, "", " ")
if err != nil {
log.Fatal(err)
}
Expand Down
29 changes: 24 additions & 5 deletions .tools/schema-generator/azurerm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func main() {

func run() error {
resources := map[string]*schema.Resource{}

for _, service := range provider.SupportedTypedServices() {
for _, rs := range service.Resources() {
wrapper := sdk.NewResourceWrapper(rs)
Expand All @@ -37,13 +36,33 @@ func run() error {
resources[name] = rs
}
}
rschs := map[string]*tfschema.Schema{}
for name, rs := range resources {
rschs[name] = &tfschema.Schema{Block: tfpluginschema.FromSDKv2SchemaMap(rs.Schema)}
}

schemas := map[string]*tfschema.Schema{}
for name, res := range resources {
schemas[name] = &tfschema.Schema{Block: tfpluginschema.FromSDKv2SchemaMap(res.Schema)}
datasources := map[string]*schema.Resource{}
for _, service := range provider.SupportedTypedServices() {
for _, ds := range service.DataSources() {
wrapper := sdk.NewDataSourceWrapper(ds)
dsWrapper, err := wrapper.DataSource()
if err != nil {
return fmt.Errorf("wrapping DataSource %q: %+v", ds.ResourceType(), err)
}
datasources[ds.ResourceType()] = dsWrapper
}
}
for _, service := range provider.SupportedUntypedServices() {
for name, ds := range service.SupportedDataSources() {
datasources[name] = ds
}
}
dschs := map[string]*tfschema.Schema{}
for name, ds := range datasources {
dschs[name] = &tfschema.Schema{Block: tfpluginschema.FromSDKv2SchemaMap(ds.Schema)}
}

b, err := json.MarshalIndent(tfschema.ProviderSchema{ResourceSchemas: schemas}, "", " ")
b, err := json.MarshalIndent(tfschema.ProviderSchema{ResourceSchemas: rschs, DatasourceSchemas: dschs}, "", " ")
if err != nil {
return err
}
Expand Down
13 changes: 9 additions & 4 deletions .tools/schema-generator/google/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ import (
)

func main() {
schemas := map[string]*tfschema.Schema{}
for name, rs := range provider.Provider().ResourcesMap {
schemas[name] = &tfschema.Schema{Block: tfpluginschema.FromSDKv2SchemaMap(rs.Schema)}
provider := provider.Provider()
rschs := map[string]*tfschema.Schema{}
for name, rs := range provider.ResourcesMap {
rschs[name] = &tfschema.Schema{Block: tfpluginschema.FromSDKv2SchemaMap(rs.Schema)}
}
b, err := json.MarshalIndent(tfschema.ProviderSchema{ResourceSchemas: schemas}, "", " ")
dschs := map[string]*tfschema.Schema{}
for name, ds := range provider.DataSourcesMap {
dschs[name] = &tfschema.Schema{Block: tfpluginschema.FromSDKv2SchemaMap(ds.Schema)}
}
b, err := json.MarshalIndent(tfschema.ProviderSchema{ResourceSchemas: rschs, DatasourceSchemas: dschs}, "", " ")
if err != nil {
log.Fatal(err)
}
Expand Down
28 changes: 24 additions & 4 deletions addr/addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"regexp"
"strconv"
"strings"

tfjson "github.com/hashicorp/terraform-json"
)

type ModuleStep struct {
Expand Down Expand Up @@ -86,12 +88,16 @@ func ParseModuleAddr(addr string) (ModuleAddr, error) {

type ResourceAddr struct {
ModuleAddr ModuleAddr
Mode tfjson.ResourceMode
Type string
Name string
}

func (addr ResourceAddr) String() string {
raddr := addr.Type + "." + addr.Name
if addr.Mode == tfjson.DataResourceMode {
raddr = "data." + raddr
}
if moduleAddr := addr.ModuleAddr.String(); moduleAddr != "" {
raddr = moduleAddr + "." + raddr
}
Expand All @@ -101,27 +107,41 @@ func (addr ResourceAddr) String() string {
func ParseResourceAddr(addr string) (*ResourceAddr, error) {
segs := strings.Split(addr, ".")

mode := tfjson.ManagedResourceMode
if len(segs)%2 != 0 {
return nil, fmt.Errorf("invalid resource address")
// Data source's address starts with modules (e.g. "module.mod1.module.mod2"), if any,
// then ends with "data.rt.rn".
if len(segs) < 3 {
return nil, fmt.Errorf("invalid resource address (expect the length > 3)")
}
if segs[len(segs)-3] != "data" {
return nil, fmt.Errorf("invalid resource address (expect the last 3rd segment is \"data\")")
}
mode = tfjson.DataResourceMode
}

raddr := &ResourceAddr{
Mode: mode,
Type: segs[len(segs)-2],
Name: segs[len(segs)-1],
}

if len(segs) == 2 {
minLen := 2
if mode == tfjson.DataResourceMode {
minLen = 3
}

if len(segs) == minLen {
return raddr, nil
}

maddr, err := ParseModuleAddr(strings.Join(segs[:len(segs)-2], "."))
maddr, err := ParseModuleAddr(strings.Join(segs[:len(segs)-minLen], "."))
if err != nil {
return nil, err
}

raddr.ModuleAddr = maddr
return raddr, nil

}

func MustParseResourceAddr(addr string) *ResourceAddr {
Expand Down
29 changes: 29 additions & 0 deletions addr/addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package addr
import (
"testing"

tfjson "github.com/hashicorp/terraform-json"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -104,6 +105,16 @@ func TestParseResourceAddr(t *testing.T) {
name: "resource only",
input: "null_resource.test",
addr: ResourceAddr{
Mode: tfjson.ManagedResourceMode,
Type: "null_resource",
Name: "test",
},
},
{
name: "data source only",
input: "data.null_resource.test",
addr: ResourceAddr{
Mode: tfjson.DataResourceMode,
Type: "null_resource",
Name: "test",
},
Expand All @@ -117,6 +128,21 @@ func TestParseResourceAddr(t *testing.T) {
Name: "mod1",
},
},
Mode: tfjson.ManagedResourceMode,
Type: "null_resource",
Name: "test",
},
},
{
name: "data source with module",
input: "module.mod1.data.null_resource.test",
addr: ResourceAddr{
ModuleAddr: []ModuleStep{
{
Name: "mod1",
},
},
Mode: tfjson.DataResourceMode,
Type: "null_resource",
Name: "test",
},
Expand All @@ -131,6 +157,7 @@ func TestParseResourceAddr(t *testing.T) {
Key: ptr("foo"),
},
},
Mode: tfjson.ManagedResourceMode,
Type: "null_resource",
Name: "test",
},
Expand All @@ -145,6 +172,7 @@ func TestParseResourceAddr(t *testing.T) {
Index: ptr(0),
},
},
Mode: tfjson.ManagedResourceMode,
Type: "null_resource",
Name: "test",
},
Expand All @@ -166,6 +194,7 @@ func TestParseResourceAddr(t *testing.T) {
Name: "mod3",
},
},
Mode: tfjson.ManagedResourceMode,
Type: "null_resource",
Name: "test",
},
Expand Down
Loading

0 comments on commit fde21fe

Please sign in to comment.