cloudflare / cf-terraforming

A command line utility to facilitate terraforming your existing Cloudflare resources.
Mozilla Public License 2.0
960 stars 136 forks source link

Panic while running cf-terraforming generate for access application #707

Open hhkumar opened 4 months ago

hhkumar commented 4 months ago

Confirmation

cf-terraforming version

cf-terraforming 0.20.0

Expected outcome

cf-terraforming generate --zone <zone_id> --resource-type cloudflare_access_application generates resource blocks for the applications in the zone

Actual outcome

cf-terraforming % cf-terraforming generate --zone <zone_id> --resource-type cloudflare_access_application
panic: interface conversion: interface {} is float64, not string

goroutine 1 [running]:
github.com/cloudflare/cf-terraforming/internal/app/cf-terraforming/cmd.writeAttrLine({0x1400016e2e8, 0x8}, {0x1033d2ca0, 0x140003393b0}, {0x0?, 0x1033dc6e0?}, 0x140003390e0)
    internal/app/cf-terraforming/cmd/util.go:228 +0xbb0
github.com/cloudflare/cf-terraforming/internal/app/cf-terraforming/cmd.writeAttrLine({0x1400016e2e8, 0x8}, {0x1033d5c80, 0x1400025e828}, {0x0, 0x0}, 0x140003390e0)
    internal/app/cf-terraforming/cmd/util.go:275 +0x1200
github.com/cloudflare/cf-terraforming/internal/app/cf-terraforming/cmd.init.generateResources.func2(0x1038f2fe0, {0x103268c6b?, 0x4?, 0x103268beb?})
    internal/app/cf-terraforming/cmd/generate.go:1325 +0x5494
github.com/spf13/cobra.(*Command).execute(0x1038f2fe0, {0x14000089b40, 0x4, 0x4})
    /Users/hariharakumar-hana/Library/Caches/Homebrew/go_mod_cache/pkg/mod/github.com/spf13/cobra@v1.8.0/command.go:987 +0x828
github.com/spf13/cobra.(*Command).ExecuteC(0x1038f35a0)
    /Users/hariharakumar-hana/Library/Caches/Homebrew/go_mod_cache/pkg/mod/github.com/spf13/cobra@v1.8.0/command.go:1115 +0x344
github.com/spf13/cobra.(*Command).Execute(...)
    /Users/hariharakumar-hana/Library/Caches/Homebrew/go_mod_cache/pkg/mod/github.com/spf13/cobra@v1.8.0/command.go:1039
github.com/cloudflare/cf-terraforming/internal/app/cf-terraforming/cmd.Execute()
    internal/app/cf-terraforming/cmd/root.go:30 +0x24
main.main()
    cmd/cf-terraforming/main.go:8 +0x1c

Steps to reproduce

  1. brew install cloudflare/cloudflare/cf-terraforming should upgrade to v0.20.0
  2. Running cf-terraforming generate command panics for access application. Not sure if limited to only that
  3. Generate command worked on previous version v0.19.0

References

No response

sebdanielsson commented 4 months ago

Getting the same error when trying to import cloudflare_access_application.

panic: interface conversion: interface {} is float64, not string

goroutine 1 [running]:
github.com/cloudflare/cf-terraforming/internal/app/cf-terraforming/cmd.writeAttrLine({0x140006153b8, 0x8}, {0x102ecaca0, 0x1400000f9b0}, {0x0?, 0x102ed46e0?}, 0x1400000f6e0)
    internal/app/cf-terraforming/cmd/util.go:228 +0xbb0
github.com/cloudflare/cf-terraforming/internal/app/cf-terraforming/cmd.writeAttrLine({0x140006153b8, 0x8}, {0x102ecdc80, 0x1400000f338}, {0x0, 0x0}, 0x1400000f6e0)
    internal/app/cf-terraforming/cmd/util.go:275 +0x1200
github.com/cloudflare/cf-terraforming/internal/app/cf-terraforming/cmd.init.generateResources.func2(0x1033eafe0, {0x102d60c6b?, 0x4?, 0x102d60beb?})
    internal/app/cf-terraforming/cmd/generate.go:1325 +0x5494
github.com/spf13/cobra.(*Command).execute(0x1033eafe0, {0x1400003bb00, 0x4, 0x4})
    /Users/sebastian/Library/Caches/Homebrew/go_mod_cache/pkg/mod/github.com/spf13/cobra@v1.8.0/command.go:987 +0x828
github.com/spf13/cobra.(*Command).ExecuteC(0x1033eb5a0)
    /Users/sebastian/Library/Caches/Homebrew/go_mod_cache/pkg/mod/github.com/spf13/cobra@v1.8.0/command.go:1115 +0x344
github.com/spf13/cobra.(*Command).Execute(...)
    /Users/sebastian/Library/Caches/Homebrew/go_mod_cache/pkg/mod/github.com/spf13/cobra@v1.8.0/command.go:1039
github.com/cloudflare/cf-terraforming/internal/app/cf-terraforming/cmd.Execute()
    internal/app/cf-terraforming/cmd/root.go:30 +0x24
main.main()
    cmd/cf-terraforming/main.go:8 +0x1c
sebdanielsson commented 3 months ago

@jacobbednarz could you please take a look?😇

andywancg commented 3 months ago

I'm also seeing the same problem, looks like I can import other resource types ok but I get the same error when trying to extract out an existing application I've got configured in Cloudflare with the same panic error - interface {} is float64, not string.

I'm no golang expert but I've taken a quick look at the code and it looks like it's using a generic map to write the values out to keep the code simple, but I'm assuming it's the JSON unmarshalling that's flipping a value to the float64.

Not sure if I can easily hack a fix for it but looking at internal/app/cf-terraforming/cmd/util.go on line 228 I think: mapCty[k] = cty.StringVal(v.(string)) needs to cater for non-string values.

A proper fix might involve more changes to the response handler when it receives data from the API to populate structs or something similar depending on the type of object being queried.

I'll let you know if I manage to find a quick hack for this if the repo owner can't find a proper fix before me, but as mentioned, I'm no golang expert!

andywancg commented 3 months ago

I've been able to get it a little bit further, but I'm hitting further issues unfortunately. I've got the code to be able to handle different variable types now however when querying for the "cloudflare_access_application" resource type, I start seeing nested arrays in the response which means an additional layer of handling is required. Not sure what to suggest at this stage, as I'd need to understand the API a little more and the code itself to work out how best to handle the different responses, and I've not got very much spare time on my hands right now, sorry!