Skip to content

Commit 4c77b58

Browse files
committed
Extend noopenstackidref linter to flag plural *IDs fields
Fields like NetworkIDs, SubnetIDs should use NetworkRefs, SubnetRefs instead. This ensures consistency with the singular form (NetworkID -> NetworkRef).
1 parent cf0bdcb commit 4c77b58

2 files changed

Lines changed: 32 additions & 7 deletions

File tree

  • tools/orc-api-linter/pkg/analysis/noopenstackidref

tools/orc-api-linter/pkg/analysis/noopenstackidref/analyzer.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ const (
3737
ORC's API design philosophy states that spec fields should only reference
3838
ORC Kubernetes objects, not OpenStack resources directly by UUID.
3939
40-
Fields ending with 'ID' (like ProjectID, NetworkID) in spec structs should
41-
instead use KubernetesNameRef type with a 'Ref' suffix (like ProjectRef, NetworkRef).
40+
Fields ending with 'ID' or 'IDs' (like ProjectID, NetworkIDs) in spec structs should
41+
instead use KubernetesNameRef type with a 'Ref' or 'Refs' suffix (like ProjectRef, NetworkRefs).
4242
4343
See: https://k-orc.cloud/development/api-design/`
4444
)
4545

46-
// openstackIDPattern matches field names that end with "ID" and are likely
46+
// openstackIDPattern matches field names that end with "ID" or "IDs" and are likely
4747
// references to OpenStack resources by UUID. These should instead use
48-
// KubernetesNameRef with a "Ref" suffix to reference ORC objects.
49-
var openstackIDPattern = regexp.MustCompile(`ID$`)
48+
// KubernetesNameRef with a "Ref" or "Refs" suffix to reference ORC objects.
49+
var openstackIDPattern = regexp.MustCompile(`IDs?$`)
5050

51-
// excludedIDPatterns contains field name patterns that end in "ID" but are
51+
// excludedIDPatterns contains field name patterns that end in "ID" or "IDs" but are
5252
// not OpenStack resource references.
5353
var excludedIDPatterns = []string{
5454
"SegmentationID", // VLAN segmentation ID, not an OpenStack resource
@@ -120,7 +120,14 @@ func checkField(pass *analysis.Pass, field *ast.Field, qualifiedFieldName string
120120
return
121121
}
122122

123-
suggestedRef := strings.TrimSuffix(fieldName, "ID") + "Ref"
123+
// Generate the suggested Ref/Refs name based on singular/plural
124+
var suggestedRef string
125+
if strings.HasSuffix(fieldName, "IDs") {
126+
suggestedRef = strings.TrimSuffix(fieldName, "IDs") + "Refs"
127+
} else {
128+
suggestedRef = strings.TrimSuffix(fieldName, "ID") + "Ref"
129+
}
130+
124131
pass.Reportf(field.Pos(),
125132
"field %s references OpenStack resource by ID in spec; "+
126133
"use *KubernetesNameRef with %s instead; "+

tools/orc-api-linter/pkg/analysis/noopenstackidref/testdata/src/a/a.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,21 @@ type WrongNameCorrectTypeSpec struct {
129129
// ProjectID with *KubernetesNameRef type is allowed (type takes precedence).
130130
ProjectID *KubernetesNameRef `json:"projectID,omitempty"`
131131
}
132+
133+
// ---- Plural ID fields: should also be flagged ----
134+
135+
// PluralIDsSpec tests that plural IDs fields are flagged.
136+
type PluralIDsSpec struct {
137+
NetworkIDs []string `json:"networkIDs,omitempty"` // want `field PluralIDsSpec.NetworkIDs references OpenStack resource by ID in spec`
138+
139+
SubnetIDs []string `json:"subnetIDs,omitempty"` // want `field PluralIDsSpec.SubnetIDs references OpenStack resource by ID in spec`
140+
141+
// SecurityGroupRefs is correct - uses the Refs suffix.
142+
SecurityGroupRefs []KubernetesNameRef `json:"securityGroupRefs,omitempty"`
143+
}
144+
145+
// PluralIDsStatus tests that plural IDs in status are allowed.
146+
type PluralIDsStatus struct {
147+
// NetworkIDs is allowed in status.
148+
NetworkIDs []string `json:"networkIDs,omitempty"`
149+
}

0 commit comments

Comments
 (0)