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

Add resource provider registry #3347

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
5 changes: 5 additions & 0 deletions clusters/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,8 @@ func resourceClusterUpdate(ctx context.Context, d *schema.ResourceData, c *commo
}
return nil
}

func init() {
common.RegisterResourceProvider(compute.ClusterSpec{}, ClusterSpec{})
common.RegisterResourceProvider(compute.Library{}, LibraryResource{})
}
24 changes: 17 additions & 7 deletions clusters/resource_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func ResourceLibrary() common.Resource {
libraySdkSchema := common.StructToSchema(compute.Library{}, func(m map[string]*schema.Schema) map[string]*schema.Schema {
m["cluster_id"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
}
return m
type LibraryResource struct {
compute.Library
}

func (LibraryResource) Aliases() map[string]map[string]string {
return map[string]map[string]string{}
}

edwardfeng-db marked this conversation as resolved.
Show resolved Hide resolved
func (LibraryResource) CustomizeSchema(s map[string]*schema.Schema) map[string]*schema.Schema {
common.CustomizeSchemaPath(s).AddNewField("cluster_id", &schema.Schema{
Type: schema.TypeString,
Required: true,
})
return s
}

func ResourceLibrary() common.Resource {
libraySdkSchema := common.StructToSchema(LibraryResource{}, nil)
parseId := func(id string) (string, string) {
split := strings.SplitN(id, "/", 2)
if len(split) != 2 {
Expand Down
19 changes: 19 additions & 0 deletions common/reflect_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ var kindMap = map[reflect.Kind]string{
reflect.UnsafePointer: "UnsafePointer",
}

// Global registry for ResoureProvider, the goal is to make StructToSchema able to find pre-registered ResourceProvider for a
// given struct so that we don't have to specify aliases and customizations redundantly if one schema references the another
// schema.
var resourceProviderRegistry map[string]ResourceProvider

// Pre-registered ResourceProvider for a given struct into resourceProviderRegistry.
// This function should be called in the init() function in packages with ResourceProvider.
// Example:
//
// func init() {
// common.RegisterResourceProvider(jobs.JobSettings{}) = JobSettingsResource{}
// }
func RegisterResourceProvider(v any, r ResourceProvider) {
if resourceProviderRegistry == nil {
resourceProviderRegistry = make(map[string]ResourceProvider)
}
resourceProviderRegistry[getNameForType(reflect.ValueOf(v).Type())] = r
edwardfeng-db marked this conversation as resolved.
Show resolved Hide resolved
}

// Generic interface for ResourceProvider. Using CustomizeSchema function to keep track of additional information
// on top of the generated go-sdk struct.
type ResourceProvider interface {
Expand Down
Loading