crossplane / upjet

A code generation framework and runtime for Crossplane providers
Apache License 2.0
271 stars 78 forks source link

Can't create cross resource reference if the terraform field type is Number #91

Open ytsarev opened 1 year ago

ytsarev commented 1 year ago

What happened?

See https://github.com/crossplane/terrajet/issues/277

Referencing it for proper tracking within upjet

joakimhew commented 1 year ago

I just wanted to say that I've started working on implementing support for cross-resource references for other primitives than string. So far I've added support for float64 but will add support for all basic types.

From my understanding, the best way to solve this is by updating the crossplane-runtime and add methods that support converting resolved references to other primitives than strings and also to update the crossplane-tools code generation to make sure the correct methods are used to convert the resolved values.

The initial commit for supporting this can be found in my fork of crossplane-runtime and crossplane-tools

E.g. in the case where the the source is of type []*float64, crossplane-tools will now generate the following code instead:

    for i3 := 0; i3 < len(mg.Spec.ForProvider.Reviewers); i3++ {
    mrsp, err = r.ResolveMultiple(ctx, reference.MultiResolutionRequest{
--       CurrentValues: reference.FromPtrValues(mg.Spec.ForProvider.Reviewers[i3].Teams),
++       CurrentValues: reference.FromFloatPtrValues(mg.Spec.ForProvider.Reviewers[i3].Teams),
        Extract:       reference.ExternalName(),
        References:    mg.Spec.ForProvider.Reviewers[i3].TeamsRefs,
        Selector:      mg.Spec.ForProvider.Reviewers[i3].TeamsSelector,
        To: reference.To{
            List:    &v1alpha1.TeamList{},
            Managed: &v1alpha1.Team{},
        },
    })
    if err != nil {
        return errors.Wrap(err, "mg.Spec.ForProvider.Reviewers[i3].Teams")
    }
--  mg.Spec.ForProvider.Reviewers[i3].Teams = reference.ToPtrValues(mrsp.ResolvedValues)
++  mg.Spec.ForProvider.Reviewers[i3].Teams = reference.ToFloatPtrValues(mrsp.ResolvedValues)
     mg.Spec.ForProvider.Reviewers[i3].TeamsRefs = mrsp.ResolvedReferences
    }

Feel free to chime in if you have any ideas on how to solve this in a better way! 😄

luebken commented 1 year ago

Setting to severity:2 (low) as I'm assuming this doesn't happen very often also you still get a working API just reference functionality is missing.

ytsarev commented 1 year ago

it hit me again in the new provider configuration, so maybe it is not super low :)

apis/appsec/v1alpha1/zz_generated.resolvers.go:23:40: cannot use mg.Spec.ForProvider.ConfigID (variable of type *float64) as *string value in argument to reference.FromPtrValue (typecheck)
        CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.ConfigID),
                                             ^
apis/appsec/v1alpha1/zz_generated.resolvers.go:35:33: cannot use reference.ToPtrValue(rsp.ResolvedValue) (value of type *string) as *float64 value in assignment (typecheck)
    mg.Spec.ForProvider.ConfigID = reference.ToPtrValue(rsp.ResolvedValue)