Skip to content

Commit

Permalink
wip first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mgyucht committed Dec 4, 2024
1 parent 8e3117a commit e30e677
Show file tree
Hide file tree
Showing 23 changed files with 10,558 additions and 1,473 deletions.
35 changes: 29 additions & 6 deletions .codegen/model.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,35 @@ func (newState *{{.PascalName}}) SyncEffectiveFieldsDuringRead(existingState {{.
{{- end}}
}

func (a {{.PascalName}}) GetComplexFieldTypes() map[string]reflect.Type {
return map[string]reflect.Type{
{{- range .Fields -}}
{{- if or .Entity.IsObject .Entity.ArrayValue .Entity.MapValue}}
"{{.PascalName}}": reflect.TypeOf({{ template "complex-field-value" .Entity }}),
{{- end}}
{{- end}}
}
}

{{end}}
{{end}}

{{define "complex-field-value" -}}
{{- if .ArrayValue }}{{ template "complex-field-value" .ArrayValue }}
{{- else if .MapValue }}{{ template "complex-field-value" .MapValue }}
{{- else -}}
{{- if .IsExternal -}}{{.Package.Name}}.{{- end -}}
{{- if or .IsString .Enum -}}""
{{- else if .IsBool -}}false
{{- else if .IsInt64 -}}0
{{- else if .IsFloat64 -}}0.0
{{- else if .IsInt -}}0
{{- else if .IsAny -}}struct{}{}
{{- else if .IsObject -}}{{.PascalName}}{}
{{- end -}}
{{- end -}}
{{- end -}}

{{- define "field" -}}
{{if .effective}}Effective{{end}}{{.field.PascalName}} {{template "type" .field.Entity}} `{{template "field-tag" . }}`
{{- end -}}
Expand Down Expand Up @@ -91,12 +117,9 @@ func (newState *{{.PascalName}}) SyncEffectiveFieldsDuringRead(existingState {{.
{{- else if .IsFloat64}}types.Float64
{{- else if .IsInt}}types.Int64
{{- else if .IsByteStream}}io.ReadCloser
{{- else if .ArrayValue }}
{{- if .ArrayValue.IsObject }}{{template "type" .ArrayValue}}
{{- else }}[]{{template "type" .ArrayValue}}
{{- end }}
{{- else if .MapValue }}map[string]{{template "type" .MapValue}}
{{- else if .IsObject }}[]{{.PascalName}}
{{- else if .ArrayValue }}types.List
{{- else if .MapValue }}types.Map
{{- else if .IsObject }}types.Object
{{- else if .Enum }}types.String
{{- else}}any /* MISSING TYPE */
{{- end -}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"github.com/databricks/terraform-provider-databricks/internal/service/catalog_tf"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

const dataSourceName = "registered_model"
Expand Down Expand Up @@ -86,8 +88,10 @@ func (d *RegisteredModelDataSource) Read(ctx context.Context, req datasource.Rea
if resp.Diagnostics.HasError() {
return
}
if modelInfo.Aliases == nil {
modelInfo.Aliases = []catalog_tf.RegisteredModelAlias{}
if modelInfo.Aliases.IsNull() {
var d diag.Diagnostics
modelInfo.Aliases, d = basetypes.NewListValueFrom(ctx, modelInfo.Aliases.ElementType(ctx), []catalog_tf.RegisteredModelAlias{})
resp.Diagnostics.Append(d...)
}
registeredModel.ModelInfo = append(registeredModel.ModelInfo, modelInfo)
resp.Diagnostics.Append(resp.State.Set(ctx, registeredModel)...)
Expand Down
41 changes: 32 additions & 9 deletions internal/providers/pluginfw/products/sharing/resource_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import (
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/converters"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/tfschema"
"github.com/databricks/terraform-provider-databricks/internal/service/sharing_tf"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

const resourceName = "share"
Expand Down Expand Up @@ -145,8 +147,8 @@ func (r *ShareResource) Schema(ctx context.Context, req resource.SchemaRequest,
c.AddPlanModifier(stringplanmodifier.UseStateForUnknown(), "created_by")

c.SetRequired("object", "data_object_type")
c.SetRequired("object", "partitions", "values", "op")
c.SetRequired("object", "partitions", "values", "name")
c.SetRequired("object", "partition", "value", "op")
c.SetRequired("object", "partition", "value", "name")
return c
})
resp.Schema = schema.Schema{
Expand Down Expand Up @@ -214,9 +216,16 @@ func (r *ShareResource) Create(ctx context.Context, req resource.CreateRequest,
}

newState.SyncEffectiveFieldsDuringCreateOrUpdate(plan.ShareInfo)
for i := range newState.Objects {
newState.Objects[i].SyncEffectiveFieldsDuringCreateOrUpdate(plan.Objects[i])
planObjects := []sharing_tf.SharedDataObject{}
resp.Diagnostics.Append(plan.Objects.ElementsAs(ctx, &planObjects, true)...)
newStateObjects := []sharing_tf.SharedDataObject{}
resp.Diagnostics.Append(newState.Objects.ElementsAs(ctx, &newStateObjects, true)...)
for i := range newStateObjects {
newStateObjects[i].SyncEffectiveFieldsDuringCreateOrUpdate(planObjects[i])
}
var d diag.Diagnostics
newState.Objects, d = basetypes.NewListValueFrom(ctx, newState.Objects.ElementType(ctx), newStateObjects)
resp.Diagnostics.Append(d...)

resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -272,9 +281,16 @@ func (r *ShareResource) Read(ctx context.Context, req resource.ReadRequest, resp
}

newState.SyncEffectiveFieldsDuringRead(existingState.ShareInfo)
for i := range newState.Objects {
newState.Objects[i].SyncEffectiveFieldsDuringRead(existingState.Objects[i])
var existingObjects []sharing_tf.SharedDataObject
resp.Diagnostics.Append(existingState.Objects.ElementsAs(ctx, &existingObjects, true)...)
var newStateObjects []sharing_tf.SharedDataObject
resp.Diagnostics.Append(newState.Objects.ElementsAs(ctx, &newStateObjects, true)...)
for i := range newStateObjects {
newStateObjects[i].SyncEffectiveFieldsDuringRead(existingObjects[i])
}
var d diag.Diagnostics
newState.Objects, d = basetypes.NewListValueFrom(ctx, newState.Objects.ElementType(ctx), newStateObjects)
resp.Diagnostics.Append(d...)

resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
}
Expand Down Expand Up @@ -371,9 +387,16 @@ func (r *ShareResource) Update(ctx context.Context, req resource.UpdateRequest,
}

state.SyncEffectiveFieldsDuringCreateOrUpdate(plan.ShareInfo)
for i := range state.Objects {
state.Objects[i].SyncEffectiveFieldsDuringCreateOrUpdate(plan.Objects[i])
}
planObjects := []sharing_tf.SharedDataObject{}
resp.Diagnostics.Append(plan.Objects.ElementsAs(ctx, &planObjects, true)...)
stateObjects := []sharing_tf.SharedDataObject{}
resp.Diagnostics.Append(state.Objects.ElementsAs(ctx, &stateObjects, true)...)
for i := range stateObjects {
stateObjects[i].SyncEffectiveFieldsDuringCreateOrUpdate(planObjects[i])
}
var d diag.Diagnostics
state.Objects, d = basetypes.NewListValueFrom(ctx, state.Objects.ElementType(ctx), stateObjects)
resp.Diagnostics.Append(d...)

resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
Expand Down
33 changes: 29 additions & 4 deletions internal/providers/pluginfw/tfschema/struct_to_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type structTag struct {
singleObject bool
}

type complexFieldTypeProvider interface {
GetComplexFieldTypes() map[string]reflect.Type
}

func typeToSchema(v reflect.Value) NestedBlockObject {
scmAttr := map[string]AttributeBuilder{}
scmBlock := map[string]BlockBuilder{}
Expand All @@ -32,6 +36,13 @@ func typeToSchema(v reflect.Value) NestedBlockObject {
panic(fmt.Errorf("schema value of Struct is expected, but got %s: %#v. %s", rk.String(), v, common.TerraformBugErrorMessage))
}
fields := tfreflect.ListAllFields(v)

// Get metadata about complex fields
var complexFieldTypes map[string]reflect.Type
if provider, ok := v.Interface().(complexFieldTypeProvider); ok {
complexFieldTypes = provider.GetComplexFieldTypes()
}

for _, field := range fields {
typeField := field.StructField
fieldName := typeField.Tag.Get("tfsdk")
Expand Down Expand Up @@ -174,10 +185,24 @@ func typeToSchema(v reflect.Value) NestedBlockObject {
Required: !structTag.optional,
Computed: structTag.computed,
}
case types.List:
panic(fmt.Errorf("types.List should never be used in tfsdk structs. %s", common.TerraformBugErrorMessage))
case types.Map:
panic(fmt.Errorf("types.Map should never be used in tfsdk structs. %s", common.TerraformBugErrorMessage))
case types.Object, types.List:
// Look up nested struct type
if complexFieldTypes == nil {
panic(fmt.Errorf("complex field types not provided for type: %T. %s", v.Interface(), common.TerraformBugErrorMessage))
}
fieldType, ok := complexFieldTypes[fieldName]
if !ok {
panic(fmt.Errorf("complex field type not found for field %s on type %T. %s", typeField.Name, v.Interface(), common.TerraformBugErrorMessage))
}
fieldValue := reflect.New(fieldType).Elem()

// Generate the nested block schema
// Note: Objects are treated as lists for backward compatibility with the Terraform v5 protocol (i.e. SDKv2 resources).
scmBlock[fieldName] = ListNestedBlockBuilder{
NestedObject: typeToSchema(fieldValue),
}
case types.Set, types.Tuple, types.Map:
panic(fmt.Errorf("%T should never be used in tfsdk structs. %s", value.Interface(), common.TerraformBugErrorMessage))
default:
// If it is a real stuct instead of a tfsdk type, recursively resolve it.
elem := typeFieldType
Expand Down
Loading

0 comments on commit e30e677

Please sign in to comment.