[codegen/nodejs] Fix remaining issues with k8s codegen (#4325)
This commit is contained in:
parent
e6fb3ef0f8
commit
875c4dab89
|
@ -26,6 +26,7 @@ import (
|
|||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -37,6 +38,9 @@ import (
|
|||
"github.com/pulumi/pulumi/sdk/v2/go/common/util/contract"
|
||||
)
|
||||
|
||||
// Match k8s version suffix. Examples include "/v1beta1" and "/v1alpha2".
|
||||
var k8sVersionSuffix = regexp.MustCompile(`/v\d+((alpha|beta)\d+)?$`)
|
||||
|
||||
type typeDetails struct {
|
||||
outputType bool
|
||||
inputType bool
|
||||
|
@ -185,15 +189,15 @@ func (mod *modContext) typeString(t schema.Type, input, wrapInput, optional bool
|
|||
}
|
||||
}
|
||||
|
||||
if constValue != nil && typ == "string" {
|
||||
typ = fmt.Sprintf("%q", constValue.(string))
|
||||
}
|
||||
if wrapInput && typ != "any" {
|
||||
typ = fmt.Sprintf("pulumi.Input<%s>", typ)
|
||||
}
|
||||
if optional {
|
||||
return typ + " | undefined"
|
||||
}
|
||||
if constValue != nil && typ == "string" {
|
||||
typ = constValue.(string)
|
||||
}
|
||||
return typ
|
||||
}
|
||||
|
||||
|
@ -254,7 +258,7 @@ func (mod *modContext) genPlainType(w io.Writer, name, comment string, propertie
|
|||
sigil = "?"
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "%s %s%s%s: %s;\n", indent, prefix, p.Name, sigil, mod.typeString(p.Type, input, wrapInput, false, nil))
|
||||
fmt.Fprintf(w, "%s %s%s%s: %s;\n", indent, prefix, p.Name, sigil, mod.typeString(p.Type, input, wrapInput, false, p.ConstValue))
|
||||
}
|
||||
fmt.Fprintf(w, "%s}\n", indent)
|
||||
}
|
||||
|
@ -380,7 +384,11 @@ func (mod *modContext) genResource(w io.Writer, r *schema.Resource) error {
|
|||
fmt.Fprintf(w, " *\n")
|
||||
fmt.Fprintf(w, " * @param name The _unique_ name of the resulting resource.\n")
|
||||
fmt.Fprintf(w, " * @param id The _unique_ provider ID of the resource to lookup.\n")
|
||||
fmt.Fprintf(w, " * @param state Any extra arguments used during the lookup.\n")
|
||||
// TODO: Document id format: https://github.com/pulumi/pulumi/issues/4754
|
||||
if r.StateInputs != nil {
|
||||
fmt.Fprintf(w, " * @param state Any extra arguments used during the lookup.\n")
|
||||
}
|
||||
fmt.Fprintf(w, " * @param opts Optional settings to control the behavior of the CustomResource.\n")
|
||||
fmt.Fprintf(w, " */\n")
|
||||
|
||||
stateParam, stateRef := "", "undefined, "
|
||||
|
@ -743,6 +751,9 @@ func (mod *modContext) getTypeImports(t schema.Type, recurse bool, imports map[s
|
|||
return true
|
||||
case *schema.TokenType:
|
||||
modName, name, modPath := mod.pkg.TokenToModule(t.Token), tokenToName(t.Token), "./index"
|
||||
if override, ok := mod.modToPkg[modName]; ok {
|
||||
modName = override
|
||||
}
|
||||
if modName != mod.mod {
|
||||
mp, err := filepath.Rel(mod.mod, modName)
|
||||
contract.Assert(err == nil)
|
||||
|
@ -875,6 +886,7 @@ func (mod *modContext) genConfig(w io.Writer, variables []*schema.Property) erro
|
|||
printComment(w, p.Comment, "", "")
|
||||
|
||||
configFetch := fmt.Sprintf("%s__config.%s(\"%s\")", cast, getfunc, p.Name)
|
||||
// TODO: handle ConstValues https://github.com/pulumi/pulumi/issues/4755
|
||||
if p.DefaultValue != nil {
|
||||
v, err := mod.getDefaultValue(p.DefaultValue, p.Type)
|
||||
if err != nil {
|
||||
|
@ -952,7 +964,11 @@ func (mod *modContext) genTypes() (string, string) {
|
|||
}
|
||||
|
||||
for _, t := range mod.types {
|
||||
ns := getNamespace(mod.pkg.TokenToModule(t.Token))
|
||||
modName := mod.pkg.TokenToModule(t.Token)
|
||||
if override, ok := mod.modToPkg[modName]; ok {
|
||||
modName = override
|
||||
}
|
||||
ns := getNamespace(modName)
|
||||
ns.types = append(ns.types, t)
|
||||
}
|
||||
|
||||
|
@ -1031,8 +1047,15 @@ func (mod *modContext) gen(fs fs) error {
|
|||
fs.add(p, []byte(contents))
|
||||
}
|
||||
|
||||
// Ensure that the top-level (provider) module directory contains a README.md file.
|
||||
if mod.mod == "" {
|
||||
// Utilities, config, readme
|
||||
switch mod.mod {
|
||||
case "":
|
||||
buffer := &bytes.Buffer{}
|
||||
mod.genHeader(buffer, nil, nil)
|
||||
fmt.Fprintf(buffer, "%s", utilitiesFile)
|
||||
fs.add(path.Join(mod.mod, "utilities.ts"), buffer.Bytes())
|
||||
|
||||
// Ensure that the top-level (provider) module directory contains a README.md file.
|
||||
readme := mod.pkg.Language["nodejs"].(NodePackageInfo).Readme
|
||||
if readme == "" {
|
||||
readme = mod.pkg.Description
|
||||
|
@ -1050,15 +1073,6 @@ func (mod *modContext) gen(fs fs) error {
|
|||
readme += "\n"
|
||||
}
|
||||
fs.add(path.Join(mod.mod, "README.md"), []byte(readme))
|
||||
}
|
||||
|
||||
// Utilities, config
|
||||
switch mod.mod {
|
||||
case "":
|
||||
buffer := &bytes.Buffer{}
|
||||
mod.genHeader(buffer, nil, nil)
|
||||
fmt.Fprintf(buffer, "%s", utilitiesFile)
|
||||
fs.add(path.Join(mod.mod, "utilities.ts"), buffer.Bytes())
|
||||
case "config":
|
||||
if len(mod.pkg.Config) > 0 {
|
||||
buffer := &bytes.Buffer{}
|
||||
|
@ -1136,12 +1150,22 @@ func (mod *modContext) genIndex(exports []string) string {
|
|||
}
|
||||
}
|
||||
|
||||
var children []string
|
||||
children := codegen.NewStringSet()
|
||||
|
||||
for _, mod := range mod.children {
|
||||
children = append(children, mod.mod)
|
||||
child := mod.mod
|
||||
if mod.compatibility == kubernetes20 {
|
||||
// Trim version suffix from child modules. Nested versions will have their own index.ts file.
|
||||
if match := k8sVersionSuffix.FindStringIndex(child); len(match) != 0 {
|
||||
child = child[:match[0]]
|
||||
}
|
||||
}
|
||||
children.Add(child)
|
||||
}
|
||||
|
||||
if len(mod.types) > 0 {
|
||||
children = append(children, "input", "output")
|
||||
children.Add("input")
|
||||
children.Add("output")
|
||||
}
|
||||
|
||||
// Finally, if there are submodules, export them.
|
||||
|
@ -1151,14 +1175,13 @@ func (mod *modContext) genIndex(exports []string) string {
|
|||
}
|
||||
fmt.Fprintf(w, "// Export sub-modules:\n")
|
||||
|
||||
sort.Strings(children)
|
||||
|
||||
for _, mod := range children {
|
||||
sorted := children.SortedValues()
|
||||
for _, mod := range sorted {
|
||||
fmt.Fprintf(w, "import * as %[1]s from \"./%[1]s\";\n", mod)
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "export {")
|
||||
for i, mod := range children {
|
||||
for i, mod := range sorted {
|
||||
if i > 0 {
|
||||
fmt.Fprint(w, ", ")
|
||||
}
|
||||
|
@ -1333,6 +1356,7 @@ func generateModuleContextMap(tool string, pkg *schema.Package, info NodePackage
|
|||
mod: modName,
|
||||
tool: tool,
|
||||
compatibility: info.Compatibility,
|
||||
modToPkg: info.ModuleToPackage,
|
||||
disableUnionOutputTypes: info.DisableUnionOutputTypes,
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue