Skip to content

Commit

Permalink
Change order for dynamic resource names (take 3)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Sep 9, 2024
1 parent 2535bf6 commit 69fe479
Show file tree
Hide file tree
Showing 4 changed files with 832 additions and 804 deletions.
12 changes: 9 additions & 3 deletions dynamic/internal/fixup/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,16 @@ func fixID(providerName string, tokenPrefix string) fixupProperty {
}

// We have an ordered list of names to attempt when we alias ID.
//
// 1. "<resource_name>_id"
// 2. "<provider_name>_<resource_name>_id"
// 4. "resource_id"
// 3. "<provider_name>_id"
candidateNames := []string{
"resource_id", // "resource_id"
getResourceName(r.TFName) + "_id", // "<resource_name>_id"
strings.ReplaceAll(providerName, "-", "_") + "_id", // "<provider_name>_id"
getResourceName(r.TFName) + "_id",
strings.ReplaceAll(providerName, "-", "_") + "_" + getResourceName(r.TFName) + "_id",
"resource_id",
strings.ReplaceAll(providerName, "-", "_") + "_id",
}

for _, proposedIDFieldName := range candidateNames {
Expand Down
40 changes: 31 additions & 9 deletions dynamic/internal/fixup/properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestFixPropertyConflicts(t *testing.T) {
}).Shim(),
},
expected: map[string]*info.Schema{
"id": {Name: "resourceId"},
"id": {Name: "resId"},
},
expectComputeIDSet: true,
},
Expand All @@ -118,7 +118,7 @@ func TestFixPropertyConflicts(t *testing.T) {
}).Shim(),
},
expected: map[string]*info.Schema{
"id": {Name: "resourceId"},
"id": {Name: "resId"},
},
expectComputeIDSet: true,
},
Expand Down Expand Up @@ -149,19 +149,41 @@ func TestFixPropertyConflicts(t *testing.T) {
"id": {Name: "overridden"},
},
},

// Test ID fallbacks

{
name: "fallback to resource ID for property name",
name: "fallback to resource and provider ID for property name",
schema: schema.SchemaMap{
"id": (&schema.Schema{
Type: shim.TypeString,
Required: true,
}).Shim(),
"resource_id": (&schema.Schema{
"res_id": (&schema.Schema{
Type: shim.TypeString,
}).Shim(),
},
expected: map[string]*info.Schema{
"id": {Name: "resId"},
"id": {Name: "testResId"},
},
expectComputeIDSet: true,
},
{
name: "fallback to literal ID for property name",
schema: schema.SchemaMap{
"id": (&schema.Schema{
Type: shim.TypeString,
Required: true,
}).Shim(),
"res_id": (&schema.Schema{
Type: shim.TypeString,
}).Shim(),
"test_res_id": (&schema.Schema{
Type: shim.TypeString,
}).Shim(),
},
expected: map[string]*info.Schema{
"id": {Name: "resourceId"},
},
expectComputeIDSet: true,
},
Expand All @@ -175,6 +197,9 @@ func TestFixPropertyConflicts(t *testing.T) {
"res_id": (&schema.Schema{
Type: shim.TypeString,
}).Shim(),
"test_res_id": (&schema.Schema{
Type: shim.TypeString,
}).Shim(),
"resource_id": (&schema.Schema{
Type: shim.TypeString,
}).Shim(),
Expand Down Expand Up @@ -243,9 +268,6 @@ func TestFixIDKebabCaseProvider(t *testing.T) {
// ("<resource_name>_id"), so these fields
// must be present to test the provider
// behavior.
"resource_id": (&schema.Schema{
Type: shim.TypeString,
}).Shim(),
"res_id": (&schema.Schema{
Type: shim.TypeString,
}).Shim(),
Expand All @@ -259,7 +281,7 @@ func TestFixIDKebabCaseProvider(t *testing.T) {

r := p.Resources["test-provider_res"]
assert.Equal(t, map[string]*info.Schema{
"id": {Name: "testProviderId"},
"id": {Name: "testProviderResId"},
}, r.Fields)
assert.NotNil(t, r.ComputeID)
}
Expand Down
60 changes: 30 additions & 30 deletions dynamic/testdata/TestSchemaGeneration/Backblaze/b2-0.8.9.golden
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,9 @@
"type": "string",
"description": "The ID of the newly created key.\n"
},
"b2ApplicationKeyId": {
"type": "string"
},
"bucketId": {
"type": "string",
"description": "When present, restricts access to one bucket.\n"
Expand All @@ -645,20 +648,20 @@
"type": "string"
},
"description": "List of application key options.\n"
},
"resourceId": {
"type": "string"
}
},
"required": [
"applicationKey",
"applicationKeyId",
"capabilities",
"resourceId",
"b2ApplicationKeyId",
"keyName",
"options"
],
"inputProperties": {
"b2ApplicationKeyId": {
"type": "string"
},
"bucketId": {
"type": "string",
"description": "When present, restricts access to one bucket.\n"
Expand All @@ -677,9 +680,6 @@
"namePrefix": {
"type": "string",
"description": "When present, restricts access to files whose names start with the prefix.\n"
},
"resourceId": {
"type": "string"
}
},
"requiredInputs": [
Expand All @@ -698,6 +698,9 @@
"type": "string",
"description": "The ID of the newly created key.\n"
},
"b2ApplicationKeyId": {
"type": "string"
},
"bucketId": {
"type": "string",
"description": "When present, restricts access to one bucket.\n"
Expand All @@ -723,9 +726,6 @@
"type": "string"
},
"description": "List of application key options.\n"
},
"resourceId": {
"type": "string"
}
},
"type": "object"
Expand All @@ -737,6 +737,9 @@
"type": "string",
"description": "Account ID that the bucket belongs to.\n"
},
"b2BucketId": {
"type": "string"
},
"bucketId": {
"type": "string",
"description": "The ID of the bucket.\n"
Expand Down Expand Up @@ -788,9 +791,6 @@
},
"description": "List of bucket options.\n"
},
"resourceId": {
"type": "string"
},
"revision": {
"type": "number",
"description": "Bucket revision.\n"
Expand All @@ -801,11 +801,14 @@
"bucketId",
"bucketName",
"bucketType",
"resourceId",
"b2BucketId",
"options",
"revision"
],
"inputProperties": {
"b2BucketId": {
"type": "string"
},
"bucketInfo": {
"type": "object",
"additionalProperties": {
Expand Down Expand Up @@ -845,9 +848,6 @@
"$ref": "#/types/b2:index/BucketLifecycleRule:BucketLifecycleRule"
},
"description": "The initial list of lifecycle rules for this bucket.\n"
},
"resourceId": {
"type": "string"
}
},
"requiredInputs": [
Expand All @@ -861,6 +861,9 @@
"type": "string",
"description": "Account ID that the bucket belongs to.\n"
},
"b2BucketId": {
"type": "string"
},
"bucketId": {
"type": "string",
"description": "The ID of the bucket.\n"
Expand Down Expand Up @@ -912,9 +915,6 @@
},
"description": "List of bucket options.\n"
},
"resourceId": {
"type": "string"
},
"revision": {
"type": "number",
"description": "Bucket revision.\n"
Expand All @@ -929,6 +929,9 @@
"type": "string",
"description": "One of 'start', 'upload', 'hide', 'folder', or other values added in the future.\n"
},
"bucketFileVersionId": {
"type": "string"
},
"bucketId": {
"type": "string",
"description": "The ID of the bucket.\n"
Expand Down Expand Up @@ -960,9 +963,6 @@
"type": "string",
"description": "The name of the B2 file.\n"
},
"resourceId": {
"type": "string"
},
"serverSideEncryption": {
"$ref": "#/types/b2:index/BucketFileVersionServerSideEncryption:BucketFileVersionServerSideEncryption",
"description": "Server-side encryption settings.\n"
Expand All @@ -988,12 +988,15 @@
"fileId",
"fileInfo",
"fileName",
"resourceId",
"bucketFileVersionId",
"size",
"source",
"uploadTimestamp"
],
"inputProperties": {
"bucketFileVersionId": {
"type": "string"
},
"bucketId": {
"type": "string",
"description": "The ID of the bucket.\n"
Expand All @@ -1013,9 +1016,6 @@
"type": "string",
"description": "The name of the B2 file.\n"
},
"resourceId": {
"type": "string"
},
"serverSideEncryption": {
"$ref": "#/types/b2:index/BucketFileVersionServerSideEncryption:BucketFileVersionServerSideEncryption",
"description": "Server-side encryption settings.\n"
Expand All @@ -1037,6 +1037,9 @@
"type": "string",
"description": "One of 'start', 'upload', 'hide', 'folder', or other values added in the future.\n"
},
"bucketFileVersionId": {
"type": "string"
},
"bucketId": {
"type": "string",
"description": "The ID of the bucket.\n"
Expand Down Expand Up @@ -1068,9 +1071,6 @@
"type": "string",
"description": "The name of the B2 file.\n"
},
"resourceId": {
"type": "string"
},
"serverSideEncryption": {
"$ref": "#/types/b2:index/BucketFileVersionServerSideEncryption:BucketFileVersionServerSideEncryption",
"description": "Server-side encryption settings.\n"
Expand Down
Loading

0 comments on commit 69fe479

Please sign in to comment.