Skip to content

Commit a2a60ef

Browse files
authored
Merge pull request #785 from cloudflare/skip-known-incomplete-resources
2 parents 94c1f5a + 39ea704 commit a2a60ef

File tree

5 files changed

+50
-19
lines changed

5 files changed

+50
-19
lines changed

internal/app/cf-terraforming/cmd/generate.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ func generateResources() func(cmd *cobra.Command, args []string) {
174174
}).Debug("no result found")
175175
continue
176176
}
177-
err = json.Unmarshal([]byte(value.String()), &jsonStructData)
177+
178+
modifiedJSON := modifyResponsePayload(resourceType, value)
179+
err = json.Unmarshal([]byte(modifiedJSON), &jsonStructData)
178180
if err != nil {
179181
log.Fatalf("failed to unmarshal result: %s", err)
180182
}
@@ -1418,7 +1420,7 @@ func generateResources() func(cmd *cobra.Command, args []string) {
14181420
log.WithFields(logrus.Fields{
14191421
"count": resourceCount,
14201422
"resource": resourceType,
1421-
}).Debug("found resources to generate output for")
1423+
}).Debug("generating resource output")
14221424

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

internal/app/cf-terraforming/cmd/import.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/hashicorp/hc-install/releases"
2121
"github.com/hashicorp/hcl/v2/hclwrite"
2222
"github.com/hashicorp/terraform-exec/tfexec"
23+
"github.com/sirupsen/logrus"
2324
"github.com/spf13/cobra"
2425
"github.com/spf13/viper"
2526
"github.com/tidwall/gjson"
@@ -121,7 +122,9 @@ func runImport() func(cmd *cobra.Command, args []string) {
121122

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

126129
var jsonStructData []interface{}
127130

@@ -152,14 +155,27 @@ func runImport() func(cmd *cobra.Command, args []string) {
152155
placeholderReplacer := strings.NewReplacer("{account_id}", accountID, "{zone_id}", zoneID)
153156
endpoint = placeholderReplacer.Replace(endpoint)
154157

158+
if strings.Contains(endpoint, "{") {
159+
log.WithFields(logrus.Fields{
160+
"resource": resourceType,
161+
"endpoint": endpoint,
162+
}).Debug("failed to substitute all path placeholders due to unknown parameters")
163+
164+
continue
165+
}
166+
155167
client := cloudflare.NewClient()
156168

157169
err := client.Get(context.Background(), endpoint, nil, &result)
158170
if err != nil {
159171
var apierr *cloudflare.Error
160172
if errors.As(err, &apierr) {
161173
if apierr.StatusCode == http.StatusNotFound {
162-
log.Debugf("no resources found at %s. skipping...", endpoint)
174+
log.WithFields(logrus.Fields{
175+
"resource": resourceType,
176+
"endpoint": endpoint,
177+
}).Debug("no resources found")
178+
163179
continue
164180
}
165181
}
@@ -173,7 +189,11 @@ func runImport() func(cmd *cobra.Command, args []string) {
173189

174190
value := gjson.Get(string(body), "result")
175191
if value.Type == gjson.Null {
176-
log.Debugf("no result found at %s. skipping...", endpoint)
192+
log.WithFields(logrus.Fields{
193+
"resource": resourceType,
194+
"endpoint": endpoint,
195+
}).Debug("no result found")
196+
177197
continue
178198
}
179199
err = json.Unmarshal([]byte(value.String()), &jsonStructData)

internal/app/cf-terraforming/cmd/resource_to_endpoint_mapping.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,6 @@ var resourceToEndpoint = map[string]map[string]string{
175175
"list": "/zones/{zone_id}/firewall/lockdowns",
176176
"get": "/zones/{zone_id}/firewall/lockdowns/{lock_downs_id}",
177177
},
178-
"cloudflare_firewall_rule": {
179-
"list": "/zones/{zone_id}/firewall/rules",
180-
"get": "/zones/{zone_id}/firewall/rules/{rule_id}",
181-
},
182178
"cloudflare_access_rule": {
183179
"list": "/{account_or_zone}/{account_or_zone_id}/firewall/access_rules/rules",
184180
"get": "/{account_or_zone}/{account_or_zone_id}/firewall/access_rules/rules/{rule_id}",
@@ -243,10 +239,6 @@ var resourceToEndpoint = map[string]map[string]string{
243239
"list": "/zones/{zone_id}/web3/hostnames",
244240
"get": "/zones/{zone_id}/web3/hostnames/{identifier}",
245241
},
246-
"cloudflare_workers_script": {
247-
"list": "/accounts/{account_id}/workers/scripts",
248-
"get": "/accounts/{account_id}/workers/scripts/{script_name}",
249-
},
250242
"cloudflare_workers_script_subdomain": {
251243
"list": "",
252244
"get": "/accounts/{account_id}/workers/scripts/{script_name}/subdomain",
@@ -691,10 +683,6 @@ var resourceToEndpoint = map[string]map[string]string{
691683
"list": "",
692684
"get": "/accounts/{account_identifier}/cloudforce-one/requests/{request_identifier}/asset/{asset_identifer}",
693685
},
694-
"cloudflare_cloud_connector_rules": {
695-
"list": "/zones/{zone_id}/cloud_connector/rules",
696-
"get": "",
697-
},
698686
"cloudflare_leaked_credential_check": {
699687
"list": "",
700688
"get": "/zones/{zone_id}/leaked-credential-checks",

internal/app/cf-terraforming/cmd/util.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/sirupsen/logrus"
1717
"github.com/spf13/cobra"
1818
"github.com/spf13/viper"
19+
"github.com/tidwall/gjson"
1920
"github.com/zclconf/go-cty/cty"
2021
)
2122

@@ -318,3 +319,10 @@ func boolToEnabledOrDisabled(value bool) string {
318319
}
319320
return "disabled"
320321
}
322+
323+
// modifyResponsePayload takes the current resource and the `gjson.Result`
324+
// to run arbitary modifications to the JSON before passing it to be overlayed
325+
// the provider schema.
326+
func modifyResponsePayload(resourceName string, value gjson.Result) string {
327+
return value.String()
328+
}

scripts/build-resource-to-endpoint-mapping

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ config = YAML.load_file(ARGV[0])
1111
mappingPath = "internal/app/cf-terraforming/cmd/resource_to_endpoint_mapping.go"
1212
output = ""
1313

14+
def skipped_resource?(terraform_name)
15+
[
16+
# deprecated and backend service support automatic migrations
17+
"firewall_rule",
18+
"firewall_filter",
19+
20+
# doesn't work with outer wrap parameters
21+
"cloud_connector_rules",
22+
23+
# terraform can't get the content (may be bundled, etc)
24+
"workers_script"
25+
].include?(terraform_name)
26+
end
27+
1428
def terraform_name(v)
1529
if v.is_a?(Hash)
1630
# don't generate if we've explicitly disabled the resource
@@ -80,13 +94,12 @@ def build_output(output, config)
8094
terraform_name = terraform_name(config)
8195
endpoint = list_endpoint(config)
8296

83-
if !terraform_name.nil? && !endpoint.nil?
97+
if !terraform_name.nil? && !endpoint.nil? && !skipped_resource?(terraform_name)
8498
output << "
8599
\"cloudflare_#{terraform_name}\": map[string]string{
86100
\"list\":\"#{endpoint[:list]}\",
87101
\"get\":\"#{endpoint[:get]}\",
88102
},"
89-
# output << ": \"\",\n"
90103
end
91104

92105
return if config["subresources"].nil?

0 commit comments

Comments
 (0)