Skip to content

Commit

Permalink
Allow providers to pass a list of allowed plural resource names (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
praneetloke authored May 27, 2024
1 parent c1a3f65 commit 56b7ed3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
19 changes: 19 additions & 0 deletions pkg/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

var numbersRegexp = regexp.MustCompile("[0-9]+[_]*[a-zA-Z]+")
var defaultAllowedPluralResourceNames = []string{"Kubernetes"}

// ToSdkName converts a property or attribute name to the lowerCamelCase convention that
// is used in Pulumi schema's properties.
Expand Down Expand Up @@ -80,3 +81,21 @@ func addNameOverride(key, value string, m map[string]string) {

m[key] = value
}

// getSingularNameForResource returns a singular version of a resource name,
// as long as the name doesn't have one of the valid plural names as its
// suffix.
func getSingularNameForResource(resourceName string, allowedPluralNames []string) string {
allowPluralName := false
for _, n := range allowedPluralNames {
if strings.HasSuffix(resourceName, n) {
allowPluralName = true
}
}

if !allowPluralName {
return strings.TrimSuffix(resourceName, "s")
}

return resourceName
}
23 changes: 13 additions & 10 deletions pkg/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ type OpenAPIContext struct {
// TypeSpecNamespaceSeparator is the separator used in the operationId value.
TypeSpecNamespaceSeparator string

// AllowedPluralResources is a slice of resource names that should not
// be converted to their singular version.
AllowedPluralResources []string

// resourceCRUDMap is a map of the Pulumi resource type
// token to its CRUD endpoints.
resourceCRUDMap map[string]*CRUDOperationsMap
Expand All @@ -84,7 +88,8 @@ type OpenAPIContext struct {
// to the SDK name used in the Pulumi schema. This can
// be used by providers to look-up the value for a path
// param in the inputs map.
pathParamNameMap map[string]string
pathParamNameMap map[string]string
allowedPluralResources []string
}

type duplicateEnumError struct {
Expand Down Expand Up @@ -115,6 +120,8 @@ func (o *OpenAPIContext) GatherResourcesFromAPI(csharpNamespaces map[string]stri
o.apiToSDKNameMap = make(map[string]string)
o.pathParamNameMap = make(map[string]string)

o.allowedPluralResources = append(o.AllowedPluralResources, defaultAllowedPluralResourceNames...)

for _, path := range o.Doc.Paths.InMatchingOrder() {
pathItem := o.Doc.Paths.Find(path)
if pathItem == nil {
Expand Down Expand Up @@ -268,6 +275,7 @@ func (o *OpenAPIContext) GatherResourcesFromAPI(csharpNamespaces map[string]stri
}
} else {
resourceName := getResourceTitleFromOperationID(pathItem.Patch.OperationID, http.MethodPatch, o.OperationIdsHaveTypeSpecNamespace)
resourceName = getSingularNameForResource(resourceName, o.allowedPluralResources)
typeToken := fmt.Sprintf("%s:%s:%s", o.Pkg.Name, module, resourceName)
setUpdateOperationMapping(typeToken)
}
Expand Down Expand Up @@ -315,6 +323,7 @@ func (o *OpenAPIContext) GatherResourcesFromAPI(csharpNamespaces map[string]stri
// to create resources if the endpoint itself requires the ID of the resource.
if pathItem.Post == nil && !strings.HasSuffix(currentPath, "}") {
resourceName := getResourceTitleFromOperationID(pathItem.Put.OperationID, http.MethodPut, o.OperationIdsHaveTypeSpecNamespace)
resourceName = getSingularNameForResource(resourceName, o.allowedPluralResources)
parameters := pathItem.Parameters
parameters = append(parameters, pathItem.Put.Parameters...)
if err := o.gatherResource(currentPath, resourceName, *resourceType, nil /*response type*/, parameters, module); err != nil {
Expand Down Expand Up @@ -357,17 +366,13 @@ func (o *OpenAPIContext) GatherResourcesFromAPI(csharpNamespaces map[string]stri
}
} else {
resourceName := getResourceTitleFromOperationID(pathItem.Delete.OperationID, http.MethodDelete, o.OperationIdsHaveTypeSpecNamespace)
if !strings.HasSuffix(resourceName, "Kubernetes") {
resourceName = strings.TrimSuffix(resourceName, "s")
}
resourceName = getSingularNameForResource(resourceName, o.allowedPluralResources)
typeToken := fmt.Sprintf("%s:%s:%s", o.Pkg.Name, module, resourceName)
setDeleteOperationMapping(typeToken)
}
} else {
resourceName := getResourceTitleFromOperationID(pathItem.Delete.OperationID, http.MethodDelete, o.OperationIdsHaveTypeSpecNamespace)
if !strings.HasSuffix(resourceName, "Kubernetes") {
resourceName = strings.TrimSuffix(resourceName, "s")
}
resourceName = getSingularNameForResource(resourceName, o.allowedPluralResources)
typeToken := fmt.Sprintf("%s:%s:%s", o.Pkg.Name, module, resourceName)
setDeleteOperationMapping(typeToken)
}
Expand Down Expand Up @@ -425,9 +430,7 @@ func (o *OpenAPIContext) GatherResourcesFromAPI(csharpNamespaces map[string]stri
}

resourceName := getResourceTitleFromOperationID(pathItem.Post.OperationID, http.MethodPost, o.OperationIdsHaveTypeSpecNamespace)
if !strings.HasSuffix(resourceName, "Kubernetes") {
resourceName = strings.TrimSuffix(resourceName, "s")
}
resourceName = getSingularNameForResource(resourceName, o.allowedPluralResources)

resourceRequestType := jsonReq.Schema.Value
parameters := pathItem.Parameters
Expand Down

0 comments on commit 56b7ed3

Please sign in to comment.