Skip to content

Commit

Permalink
Merge pull request #784 from cloudflare/support-dynamic-import-output
Browse files Browse the repository at this point in the history
feat(import): support dynamic import statements
  • Loading branch information
jacobbednarz authored Jan 24, 2025
2 parents 387c96e + e5cb35f commit 94c1f5a
Show file tree
Hide file tree
Showing 4 changed files with 1,365 additions and 648 deletions.
38 changes: 30 additions & 8 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 @@ -108,14 +113,22 @@ func generateResources() func(cmd *cobra.Command, args []string) {
var jsonStructData []interface{}

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

var result *http.Response

endpoint := resourceToEndpoint[resourceType]
// by default, we want to use the `list` operation however, there are times
// when resources exist only as `get` operations but contain multiple
// resources.
endpoint := resourceToEndpoint[resourceType]["list"]
if endpoint == "" {
endpoint = resourceToEndpoint[resourceType]["get"]
}

// if we encounter a combined endpoint, we need to rewrite to use the correct
// endpoint depending on what parameters are being provided.
Expand All @@ -138,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 @@ -152,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 @@ -1396,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 94c1f5a

Please sign in to comment.