Skip to content

Commit

Permalink
feat(import): support dynamic import statements
Browse files Browse the repository at this point in the history
Updates the `import` command to support generating all resources dynamic for provider > v4

Signed-off-by: Jacob Bednarz <[email protected]>
  • Loading branch information
jacobbednarz committed Jan 24, 2025
1 parent 89c9812 commit e5cb35f
Show file tree
Hide file tree
Showing 2 changed files with 625 additions and 457 deletions.
28 changes: 22 additions & 6 deletions internal/app/cf-terraforming/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/hashicorp/hc-install/releases"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/hashicorp/terraform-exec/tfexec"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
Expand Down Expand Up @@ -83,7 +84,9 @@ func generateResources() func(cmd *cobra.Command, args []string) {

_, providerVersion, err := tf.Version(context.Background(), true)
providerVersionString := providerVersion[providerRegistryHostname+"/cloudflare/cloudflare"].String()
log.Debugf("detected provider version: %s", providerVersionString)
log.WithFields(logrus.Fields{
"version": providerVersionString,
}).Debug("detected provider")

log.Debug("reading Terraform schema for Cloudflare provider")
ps, err := tf.ProvidersSchema(context.Background())
Expand All @@ -99,7 +102,9 @@ func generateResources() func(cmd *cobra.Command, args []string) {
resources := strings.Split(resourceType, ",")
for _, resourceType := range resources {
r := s.ResourceSchemas[resourceType]
log.Debugf("beginning to read and build %q resources", resourceType)
log.WithFields(logrus.Fields{
"resource": resourceType,
}).Debug("reading and building resource")

// Initialise `resourceCount` outside of the switch for supported resources
// to allow it to be referenced further down in the loop that outputs the
Expand All @@ -109,7 +114,9 @@ func generateResources() func(cmd *cobra.Command, args []string) {

if strings.HasPrefix(providerVersionString, "5") {
if resourceToEndpoint[resourceType]["list"] == "" && resourceToEndpoint[resourceType]["get"] == "" {
log.Debugf("did not find API endpoint for %q. does it exist in the mapping?", resourceType)
log.WithFields(logrus.Fields{
"resource": resourceType,
}).Debug("did not find API endpoint. does it exist in the mapping?")
continue
}

Expand Down Expand Up @@ -144,7 +151,10 @@ func generateResources() func(cmd *cobra.Command, args []string) {
var apierr *cloudflare.Error
if errors.As(err, &apierr) {
if apierr.StatusCode == http.StatusNotFound {
log.Debugf("no resources found at %s. skipping...", endpoint)
log.WithFields(logrus.Fields{
"resource": resourceType,
"endpoint": endpoint,
}).Debug("no resources found")
continue
}
}
Expand All @@ -158,7 +168,10 @@ func generateResources() func(cmd *cobra.Command, args []string) {

value := gjson.Get(string(body), "result")
if value.Type == gjson.Null {
log.Debugf("no result found at %s. skipping...", endpoint)
log.WithFields(logrus.Fields{
"resource": resourceType,
"endpoint": endpoint,
}).Debug("no result found")
continue
}
err = json.Unmarshal([]byte(value.String()), &jsonStructData)
Expand Down Expand Up @@ -1402,7 +1415,10 @@ func generateResources() func(cmd *cobra.Command, args []string) {
}
}

log.Debugf("found %d resources to write out for %q", resourceCount, resourceType)
log.WithFields(logrus.Fields{
"count": resourceCount,
"resource": resourceType,
}).Debug("found resources to generate output for")

// If we don't have any resources to generate, just bail out early.
if resourceCount == 0 {
Expand Down
Loading

0 comments on commit e5cb35f

Please sign in to comment.