thomasfinstad / terraform-provider-vyos-rolling

Terraform provider for VyOS with a focus on automatic resource generation
6 stars 0 forks source link

set nil / null for terraform state on sub-objects #134

Closed github-actions[bot] closed 7 months ago

github-actions[bot] commented 8 months ago

tfval \= basetypes.NewObjectNull()

https://github.com/thomasfinstad/terraform-provider-vyos/blob/d558bb5559cc6f00e62c3a1c594287d292f52e9c/internal/terraform/helpers/unmarshal.go#L148


            tfValueRefection := reflect.ValueOf(tfValue)
            fValue.Set(tfValueRefection)
        case basetypes.NumberValue:
            var tfval basetypes.NumberValuable

            if !KeyInMap(flags["name"].(string), data) {
                fmt.Printf("\tNo data for field: %s, skipping\n", fName)
                tflog.Debug(ctx, "No data for field, skipping", map[string]interface{}{"field-name": fName})

                tfval = basetypes.NewNumberNull()
            } else {
                dataValue := data[flags["name"].(string)]

                fmt.Printf("\tUnmarshalling Number Field: %s\t%s=%s\n", fName, flags["name"].(string), dataValue)
                tflog.Debug(ctx, "Unmarshalling Number Field", map[string]interface{}{"field-name": fName, flags["name"].(string): dataValue})

                // Use reflection to convert anything (possible) to float, powered by https://stackoverflow.com/a/50008579
                var floatTypeReflection = reflect.TypeOf(float64(0))
                dataValueReflection := reflect.ValueOf(dataValue)
                dataValueReflection = reflect.Indirect(dataValueReflection)
                if !dataValueReflection.Type().ConvertibleTo(floatTypeReflection) {
                    return fmt.Errorf("cannot convert %v to float64", dataValueReflection.Type())
                }
                dataValueFloat := dataValueReflection.Convert(floatTypeReflection).Float()

                bf := big.NewFloat(dataValueFloat)
                tfval = basetypes.NewNumberValue(bf)
            }
            tfValueRefection := reflect.ValueOf(tfval)
            fValue.Set(tfValueRefection)

        case basetypes.ListValue:
            var tfval basetypes.ListValuable
            tfSchemaName := strings.Split(typeReflection.Field(i).Tag.Get("tfsdk"), ",")[0]
            schemaAttr := value.ResourceSchemaAttributes()[tfSchemaName]
            listAttr := schemaAttr.(schema.ListAttribute)
            elemType := listAttr.ElementType

            if !KeyInMap(flags["name"].(string), data) {
                fmt.Printf("\tNo data for field: %s, skipping\n", fName)
                tflog.Debug(ctx, "No data for field, skipping", map[string]interface{}{"field-name": fName})

                tfval = basetypes.NewListNull(elemType)
            } else {
                dataValue := data[flags["name"].(string)]

                fmt.Printf("\tUnmarshalling List Field: %s\t%s=%s\n", fName, flags["name"].(string), dataValue)
                tflog.Debug(ctx, "Unmarshalling List Field", map[string]interface{}{"field-name": fName, flags["name"].(string): dataValue})

                if reflect.TypeOf(dataValue).Kind() != reflect.Slice {
                    fmt.Printf("\tdataValue is not a slice, wrapping the value in list: %#v\n", dataValue)
                    tflog.Debug(ctx, "dataValue is not a slice, wrapping the value in list", map[string]interface{}{flags["name"].(string): dataValue})
                    dataValue = []interface{}{dataValue}
                }

                var diags diag.Diagnostics
                tfval, diags = basetypes.NewListValueFrom(ctx, elemType, dataValue)
                if diags != nil {
                    return fmt.Errorf("ERROR: %v", diags)
                }
            }
            tflog.Trace(ctx, "setting list value", map[string]interface{}{"tfval": tfval})
            tfValueRefection := reflect.ValueOf(tfval)
            fValue.Set(tfValueRefection)

        default:
            //var tfval VyosResourceDataModel

            if !KeyInMap(flags["name"].(string), data) {
                fmt.Printf("\tNo data for field: %s, skipping\n", fName)
                tflog.Debug(ctx, "No data for field, skipping", map[string]interface{}{"field-name": fName})

                // TODO set nil / null for terraform state on sub-objects
                //tfval = basetypes.NewObjectNull()
                continue
            }
            dataValue := data[flags["name"].(string)]
github-actions[bot] commented 7 months ago

Closed in 45125f1f2c456021699195c061b868957f3dc623