2020-03-09 18:35:20 +01:00
|
|
|
//go:generate go run bundler.go
|
|
|
|
|
2020-02-27 19:04:34 +01:00
|
|
|
// Copyright 2016-2020, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Pulling out some of the repeated strings tokens into constants would harm readability, so we just ignore the
|
|
|
|
// goconst linter's warning.
|
|
|
|
//
|
|
|
|
// nolint: lll, goconst
|
|
|
|
package docs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-03-20 16:17:58 +01:00
|
|
|
"encoding/json"
|
2020-02-27 19:04:34 +01:00
|
|
|
"fmt"
|
2020-03-09 18:35:20 +01:00
|
|
|
"html"
|
|
|
|
"html/template"
|
2020-02-27 19:04:34 +01:00
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2020-04-03 20:26:52 +02:00
|
|
|
"github.com/golang/glog"
|
2020-03-09 18:35:20 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/pulumi/pulumi/pkg/codegen"
|
|
|
|
"github.com/pulumi/pulumi/pkg/codegen/dotnet"
|
|
|
|
go_gen "github.com/pulumi/pulumi/pkg/codegen/go"
|
|
|
|
"github.com/pulumi/pulumi/pkg/codegen/nodejs"
|
2020-02-27 19:04:34 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/codegen/python"
|
|
|
|
"github.com/pulumi/pulumi/pkg/codegen/schema"
|
2020-03-18 22:40:07 +01:00
|
|
|
"github.com/pulumi/pulumi/sdk/go/common/util/contract"
|
2020-02-27 19:04:34 +01:00
|
|
|
)
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
var (
|
|
|
|
supportedLanguages = []string{"csharp", "go", "nodejs", "python"}
|
|
|
|
templates *template.Template
|
|
|
|
packagedTemplates map[string][]byte
|
|
|
|
docHelpers map[string]codegen.DocLanguageHelper
|
2020-03-19 20:18:18 +01:00
|
|
|
|
|
|
|
// The following property case maps are for rendering property
|
|
|
|
// names of nested properties in Python language with the correct
|
|
|
|
// casing.
|
|
|
|
snakeCaseToCamelCase map[string]string
|
|
|
|
camelCaseToSnakeCase map[string]string
|
2020-04-03 20:26:52 +02:00
|
|
|
|
|
|
|
// The language-specific info objects for a certain package (provider).
|
|
|
|
goPkgInfo go_gen.GoInfo
|
|
|
|
csharpPkgInfo dotnet.CSharpPackageInfo
|
|
|
|
|
|
|
|
// langModuleNameLookup is a map of module name to its language-specific
|
|
|
|
// name.
|
|
|
|
langModuleNameLookup map[string]string
|
2020-03-12 01:58:12 +01:00
|
|
|
)
|
2020-03-09 18:35:20 +01:00
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
func init() {
|
|
|
|
docHelpers = make(map[string]codegen.DocLanguageHelper)
|
|
|
|
for _, lang := range supportedLanguages {
|
|
|
|
switch lang {
|
|
|
|
case "csharp":
|
2020-03-20 16:17:58 +01:00
|
|
|
docHelpers[lang] = &dotnet.DocLanguageHelper{}
|
2020-03-12 01:58:12 +01:00
|
|
|
case "go":
|
2020-03-20 16:17:58 +01:00
|
|
|
docHelpers[lang] = &go_gen.DocLanguageHelper{}
|
2020-03-12 01:58:12 +01:00
|
|
|
case "nodejs":
|
2020-03-20 16:17:58 +01:00
|
|
|
docHelpers[lang] = &nodejs.DocLanguageHelper{}
|
2020-03-12 01:58:12 +01:00
|
|
|
case "python":
|
2020-03-20 16:17:58 +01:00
|
|
|
docHelpers[lang] = &python.DocLanguageHelper{}
|
2020-03-12 01:58:12 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-19 20:18:18 +01:00
|
|
|
|
|
|
|
snakeCaseToCamelCase = map[string]string{}
|
|
|
|
camelCaseToSnakeCase = map[string]string{}
|
2020-04-03 20:26:52 +02:00
|
|
|
langModuleNameLookup = map[string]string{}
|
2020-03-12 01:58:12 +01:00
|
|
|
}
|
2020-03-09 18:35:20 +01:00
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
// header represents the header of each resource markdown file.
|
|
|
|
type header struct {
|
2020-03-09 18:35:20 +01:00
|
|
|
Title string
|
|
|
|
}
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
// property represents an input or an output property.
|
|
|
|
type property struct {
|
2020-03-20 16:17:58 +01:00
|
|
|
// DisplayName is the property name with word-breaks.
|
|
|
|
DisplayName string
|
2020-03-09 18:35:20 +01:00
|
|
|
Name string
|
|
|
|
Comment string
|
2020-03-12 01:58:12 +01:00
|
|
|
Type propertyType
|
2020-03-09 18:35:20 +01:00
|
|
|
DeprecationMessage string
|
|
|
|
|
|
|
|
IsRequired bool
|
2020-03-12 01:58:12 +01:00
|
|
|
IsInput bool
|
2020-03-09 18:35:20 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
// apiTypeDocLinks represents the links for a type's input and output API doc.
|
|
|
|
type apiTypeDocLinks struct {
|
|
|
|
InputType string
|
|
|
|
OutputType string
|
|
|
|
}
|
|
|
|
|
|
|
|
// docNestedType represents a complex type.
|
|
|
|
type docNestedType struct {
|
2020-03-09 18:35:20 +01:00
|
|
|
Name string
|
2020-03-12 01:58:12 +01:00
|
|
|
APIDocLinks map[string]apiTypeDocLinks
|
|
|
|
Properties map[string][]property
|
2020-03-09 18:35:20 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
// propertyType represents the type of a property.
|
|
|
|
type propertyType struct {
|
2020-03-23 22:17:14 +01:00
|
|
|
DisplayName string
|
|
|
|
Name string
|
2020-03-09 18:35:20 +01:00
|
|
|
// Link can be a link to an anchor tag on the same
|
|
|
|
// page, or to another page/site.
|
|
|
|
Link string
|
|
|
|
}
|
|
|
|
|
2020-03-17 21:37:40 +01:00
|
|
|
// formalParam represents the formal parameters of a constructor
|
|
|
|
// or a lookup function.
|
|
|
|
type formalParam struct {
|
2020-03-09 18:35:20 +01:00
|
|
|
Name string
|
2020-03-12 01:58:12 +01:00
|
|
|
Type propertyType
|
2020-03-09 18:35:20 +01:00
|
|
|
|
|
|
|
// This is the language specific optional type indicator.
|
|
|
|
// For example, in nodejs this is the character "?" and in Go
|
|
|
|
// it's "*".
|
|
|
|
OptionalFlag string
|
|
|
|
|
|
|
|
DefaultValue string
|
|
|
|
}
|
|
|
|
|
2020-04-02 02:22:23 +02:00
|
|
|
type packageDetails struct {
|
|
|
|
Repository string
|
|
|
|
License string
|
|
|
|
Notes string
|
|
|
|
}
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
type resourceDocArgs struct {
|
|
|
|
Header header
|
2020-03-09 18:35:20 +01:00
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
// Comment represents the introductory resource comment.
|
2020-04-07 02:01:33 +02:00
|
|
|
Comment string
|
|
|
|
DeprecationMessage string
|
2020-03-09 18:35:20 +01:00
|
|
|
|
|
|
|
ConstructorParams map[string]string
|
|
|
|
// ConstructorResource is the resource that is being constructed or
|
|
|
|
// is the result of a constructor-like function.
|
2020-03-12 01:58:12 +01:00
|
|
|
ConstructorResource map[string]propertyType
|
|
|
|
// ArgsRequired is a flag indicating if the args param is required
|
|
|
|
// when creating a new resource.
|
|
|
|
ArgsRequired bool
|
|
|
|
|
|
|
|
// InputProperties is a map per language and a corresponding slice of
|
|
|
|
// input properties accepted as args while creating a new resource.
|
|
|
|
InputProperties map[string][]property
|
|
|
|
// OutputProperties is a map per language and a corresponding slice of
|
|
|
|
// output properties returned when a new instance of the resource is
|
|
|
|
// created.
|
|
|
|
OutputProperties map[string][]property
|
|
|
|
|
2020-03-17 21:37:40 +01:00
|
|
|
// LookupParams is a map of the param string to be rendered per language
|
|
|
|
// for looking-up a resource.
|
|
|
|
LookupParams map[string]string
|
2020-03-12 01:58:12 +01:00
|
|
|
// StateInputs is a map per language and the corresponding slice of
|
|
|
|
// state input properties required while looking-up an existing resource.
|
|
|
|
StateInputs map[string][]property
|
|
|
|
// StateParam is the type name of the state param, if any.
|
|
|
|
StateParam string
|
|
|
|
|
|
|
|
// NestedTypes is a slice of the nested types used in the input and
|
|
|
|
// output properties.
|
|
|
|
NestedTypes []docNestedType
|
2020-04-02 02:22:23 +02:00
|
|
|
|
|
|
|
PackageDetails packageDetails
|
2020-03-12 01:58:12 +01:00
|
|
|
}
|
2020-03-09 18:35:20 +01:00
|
|
|
|
2020-04-03 20:26:52 +02:00
|
|
|
// typeUsage represents a nested type's usage.
|
|
|
|
type typeUsage struct {
|
2020-03-12 01:58:12 +01:00
|
|
|
Input bool
|
|
|
|
Output bool
|
2020-03-09 18:35:20 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
// nestedTypeUsageInfo is a type-alias for a map of Pulumi type-tokens
|
2020-04-03 20:26:52 +02:00
|
|
|
// and whether or not the type is used as an input and/or output
|
2020-03-12 01:58:12 +01:00
|
|
|
// properties.
|
2020-04-03 20:26:52 +02:00
|
|
|
type nestedTypeUsageInfo map[string]typeUsage
|
2020-03-12 01:58:12 +01:00
|
|
|
|
|
|
|
func (ss nestedTypeUsageInfo) add(s string, input bool) {
|
|
|
|
if v, ok := ss[s]; ok {
|
|
|
|
if input {
|
|
|
|
v.Input = true
|
|
|
|
} else {
|
|
|
|
v.Output = true
|
|
|
|
}
|
|
|
|
ss[s] = v
|
|
|
|
return
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-04-03 20:26:52 +02:00
|
|
|
ss[s] = typeUsage{
|
2020-03-12 01:58:12 +01:00
|
|
|
Input: input,
|
|
|
|
Output: !input,
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-03 20:26:52 +02:00
|
|
|
// contains returns true if the token already exists and matches the
|
|
|
|
// input or output flag of the token.
|
|
|
|
func (ss nestedTypeUsageInfo) contains(token string, input bool) bool {
|
|
|
|
a, ok := ss[token]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if input && a.Input {
|
|
|
|
return true
|
|
|
|
} else if !input && a.Output {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type modContext struct {
|
2020-04-03 20:26:52 +02:00
|
|
|
pkg *schema.Package
|
|
|
|
mod string
|
|
|
|
resources []*schema.Resource
|
|
|
|
functions []*schema.Function
|
|
|
|
children []*modContext
|
|
|
|
tool string
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
func resourceName(r *schema.Resource) string {
|
|
|
|
if r.IsProvider {
|
|
|
|
return "Provider"
|
|
|
|
}
|
|
|
|
return tokenToName(r.Token)
|
|
|
|
}
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
func getLanguageDocHelper(lang string) codegen.DocLanguageHelper {
|
|
|
|
if h, ok := docHelpers[lang]; ok {
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
panic(errors.Errorf("could not find a doc lang helper for %s", lang))
|
|
|
|
}
|
|
|
|
|
2020-03-17 21:37:40 +01:00
|
|
|
type propertyCharacteristics struct {
|
|
|
|
// input is a flag indicating if the property is an input type.
|
|
|
|
input bool
|
|
|
|
// optional is a flag indicating if the property is optional.
|
|
|
|
optional bool
|
|
|
|
}
|
|
|
|
|
2020-04-03 20:26:52 +02:00
|
|
|
// getLanguageModuleName returns the module name mapped to its language-specific
|
|
|
|
// equivalent if the schema for this provider has any overrides for that language.
|
|
|
|
// Otherwise, returns the module name as-is.
|
2020-04-07 02:01:33 +02:00
|
|
|
func getLanguageModuleName(pkg *schema.Package, mod, lang string) string {
|
2020-04-03 20:26:52 +02:00
|
|
|
modName := mod
|
|
|
|
lookupKey := lang + "_" + modName
|
|
|
|
if v, ok := langModuleNameLookup[lookupKey]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
switch lang {
|
|
|
|
case "go":
|
|
|
|
if override, ok := goPkgInfo.ModuleToPackage[modName]; ok {
|
|
|
|
modName = override
|
|
|
|
}
|
|
|
|
case "csharp":
|
|
|
|
if override, ok := csharpPkgInfo.Namespaces[modName]; ok {
|
|
|
|
modName = override
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
langModuleNameLookup[lookupKey] = modName
|
|
|
|
return modName
|
|
|
|
}
|
|
|
|
|
2020-04-07 02:01:33 +02:00
|
|
|
// cleanTypeString removes any namespaces from the generated type string for all languages.
|
|
|
|
// The result of this function should be used display purposes only.
|
|
|
|
func (mod *modContext) cleanTypeString(t schema.Type, langTypeString, lang, modName string, isInput bool) string {
|
|
|
|
if lang != "csharp" {
|
|
|
|
parts := strings.Split(langTypeString, ".")
|
|
|
|
return parts[len(parts)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// C# types can be wrapped in enumerable types such as List<> or Dictionary<>, so we have to
|
|
|
|
// only replace the namespace between the < and the > characters.
|
|
|
|
qualifier := "Inputs"
|
|
|
|
if !isInput {
|
|
|
|
qualifier = "Outputs"
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanCSharpName := func(pkgName, objModName string) string {
|
|
|
|
var csharpNS string
|
|
|
|
// This type could be at the package-level, so it won't have a module name.
|
|
|
|
if objModName != "" {
|
|
|
|
csharpNS = fmt.Sprintf("Pulumi.%s.%s.%s.", title(pkgName, lang), title(objModName, lang), qualifier)
|
|
|
|
} else {
|
|
|
|
csharpNS = fmt.Sprintf("Pulumi.%s.%s.", title(pkgName, lang), qualifier)
|
|
|
|
}
|
|
|
|
return strings.ReplaceAll(langTypeString, csharpNS, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
if isKubernetesPackage(mod.pkg) {
|
|
|
|
switch t := t.(type) {
|
|
|
|
case *schema.ArrayType:
|
|
|
|
if schema.IsPrimitiveType(t.ElementType) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
objType := t.ElementType.(*schema.ObjectType)
|
|
|
|
return mod.cleanTypeString(objType, langTypeString, lang, modName, isInput)
|
|
|
|
case *schema.UnionType:
|
|
|
|
for _, e := range t.ElementTypes {
|
|
|
|
if schema.IsPrimitiveType(e) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return mod.cleanTypeString(e.(*schema.ObjectType), langTypeString, lang, modName, isInput)
|
|
|
|
}
|
|
|
|
case *schema.ObjectType:
|
|
|
|
objTypeModName := mod.pkg.TokenToModule(t.Token)
|
|
|
|
if objTypeModName != mod.mod {
|
|
|
|
modName = getLanguageModuleName(mod.pkg, objTypeModName, lang)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cleanCSharpName(mod.pkg.Name, modName)
|
|
|
|
}
|
|
|
|
|
2020-03-17 21:37:40 +01:00
|
|
|
// typeString returns a property type suitable for docs with its display name and the anchor link to
|
|
|
|
// a type if the type of the property is an array or an object.
|
|
|
|
func (mod *modContext) typeString(t schema.Type, lang string, characteristics propertyCharacteristics, insertWordBreaks bool) propertyType {
|
2020-03-12 01:58:12 +01:00
|
|
|
docLanguageHelper := getLanguageDocHelper(lang)
|
2020-04-07 02:01:33 +02:00
|
|
|
modName := getLanguageModuleName(mod.pkg, mod.mod, lang)
|
2020-04-03 20:26:52 +02:00
|
|
|
langTypeString := docLanguageHelper.GetLanguageTypeString(mod.pkg, modName, t, characteristics.input, characteristics.optional)
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
// If the type is an object type, let's also wrap it with a link to the supporting type
|
|
|
|
// on the same page using an anchor tag.
|
|
|
|
var href string
|
2020-02-27 19:04:34 +01:00
|
|
|
switch t := t.(type) {
|
|
|
|
case *schema.ArrayType:
|
2020-03-17 21:37:40 +01:00
|
|
|
elementLangType := mod.typeString(t.ElementType, lang, characteristics, false)
|
2020-03-09 18:35:20 +01:00
|
|
|
href = elementLangType.Link
|
2020-02-27 19:04:34 +01:00
|
|
|
case *schema.ObjectType:
|
2020-03-09 18:35:20 +01:00
|
|
|
tokenName := tokenToName(t.Token)
|
2020-04-07 02:01:33 +02:00
|
|
|
// Links to anchor tags on the same page must be lower-cased.
|
|
|
|
href = "#" + strings.ToLower(tokenName)
|
2020-03-09 18:35:20 +01:00
|
|
|
}
|
|
|
|
|
2020-03-23 22:17:14 +01:00
|
|
|
// Strip the namespace/module prefix for the type's display name.
|
2020-04-07 02:01:33 +02:00
|
|
|
displayName := langTypeString
|
|
|
|
if !schema.IsPrimitiveType(t) {
|
|
|
|
displayName = mod.cleanTypeString(t, langTypeString, lang, modName, characteristics.input)
|
2020-03-23 22:17:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If word-breaks need to be inserted, then the type string
|
|
|
|
// should be html-encoded first if the language is C# in order
|
2020-03-30 23:37:30 +02:00
|
|
|
// to avoid confusing the Hugo rendering where the word-break
|
2020-03-23 22:17:14 +01:00
|
|
|
// tags are inserted.
|
2020-03-09 18:35:20 +01:00
|
|
|
if insertWordBreaks {
|
|
|
|
if lang == "csharp" {
|
2020-03-23 22:17:14 +01:00
|
|
|
displayName = html.EscapeString(displayName)
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-03-23 22:17:14 +01:00
|
|
|
displayName = wbr(displayName)
|
2020-03-09 18:35:20 +01:00
|
|
|
}
|
2020-03-12 01:58:12 +01:00
|
|
|
return propertyType{
|
2020-03-23 22:17:14 +01:00
|
|
|
Name: langTypeString,
|
|
|
|
DisplayName: displayName,
|
|
|
|
Link: href,
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 21:37:40 +01:00
|
|
|
func (mod *modContext) genConstructorTS(r *schema.Resource, argsOptional bool) []formalParam {
|
2020-02-27 19:04:34 +01:00
|
|
|
name := resourceName(r)
|
|
|
|
argsType := name + "Args"
|
2020-03-09 18:35:20 +01:00
|
|
|
argsFlag := ""
|
|
|
|
if argsOptional {
|
|
|
|
argsFlag = "?"
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
docLangHelper := getLanguageDocHelper("nodejs")
|
2020-03-17 21:37:40 +01:00
|
|
|
return []formalParam{
|
2020-03-09 18:35:20 +01:00
|
|
|
{
|
|
|
|
Name: "name",
|
2020-03-12 01:58:12 +01:00
|
|
|
Type: propertyType{
|
2020-03-09 18:35:20 +01:00
|
|
|
Name: "string",
|
|
|
|
Link: nodejs.GetDocLinkForBuiltInType("string"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "args",
|
|
|
|
OptionalFlag: argsFlag,
|
2020-03-12 01:58:12 +01:00
|
|
|
Type: propertyType{
|
2020-03-09 18:35:20 +01:00
|
|
|
Name: argsType,
|
|
|
|
Link: docLangHelper.GetDocLinkForResourceType(mod.pkg.Name, mod.mod, argsType),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "opts",
|
|
|
|
OptionalFlag: "?",
|
2020-03-12 01:58:12 +01:00
|
|
|
Type: propertyType{
|
2020-03-09 18:35:20 +01:00
|
|
|
Name: "pulumi.CustomResourceOptions",
|
2020-03-30 23:37:30 +02:00
|
|
|
Link: docLangHelper.GetDocLinkForResourceType("pulumi", "", "CustomResourceOptions"),
|
2020-03-09 18:35:20 +01:00
|
|
|
},
|
|
|
|
},
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 21:37:40 +01:00
|
|
|
func (mod *modContext) genConstructorGo(r *schema.Resource, argsOptional bool) []formalParam {
|
2020-02-27 19:04:34 +01:00
|
|
|
name := resourceName(r)
|
|
|
|
argsType := name + "Args"
|
2020-03-09 18:35:20 +01:00
|
|
|
argsFlag := ""
|
|
|
|
if argsOptional {
|
|
|
|
argsFlag = "*"
|
|
|
|
}
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
docLangHelper := getLanguageDocHelper("go")
|
2020-03-09 18:35:20 +01:00
|
|
|
// return fmt.Sprintf("func New%s(ctx *pulumi.Context, name string, args *%s, opts ...pulumi.ResourceOption) (*%s, error)\n", name, argsType, name)
|
2020-03-17 21:37:40 +01:00
|
|
|
return []formalParam{
|
2020-03-09 18:35:20 +01:00
|
|
|
{
|
|
|
|
Name: "ctx",
|
|
|
|
OptionalFlag: "*",
|
2020-03-12 01:58:12 +01:00
|
|
|
Type: propertyType{
|
2020-03-09 18:35:20 +01:00
|
|
|
Name: "pulumi.Context",
|
2020-03-17 21:37:40 +01:00
|
|
|
Link: docLangHelper.GetDocLinkForResourceType("", "pulumi", "Context"),
|
2020-03-09 18:35:20 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "name",
|
2020-03-12 01:58:12 +01:00
|
|
|
Type: propertyType{
|
2020-03-09 18:35:20 +01:00
|
|
|
Name: "string",
|
|
|
|
Link: go_gen.GetDocLinkForBuiltInType("string"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "args",
|
|
|
|
OptionalFlag: argsFlag,
|
2020-03-12 01:58:12 +01:00
|
|
|
Type: propertyType{
|
2020-03-09 18:35:20 +01:00
|
|
|
Name: argsType,
|
|
|
|
Link: docLangHelper.GetDocLinkForResourceType(mod.pkg.Name, mod.mod, argsType),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "opts",
|
|
|
|
OptionalFlag: "...",
|
2020-03-12 01:58:12 +01:00
|
|
|
Type: propertyType{
|
2020-03-09 18:35:20 +01:00
|
|
|
Name: "pulumi.ResourceOption",
|
2020-03-17 21:37:40 +01:00
|
|
|
Link: docLangHelper.GetDocLinkForResourceType("", "pulumi", "ResourceOption"),
|
2020-03-09 18:35:20 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 21:37:40 +01:00
|
|
|
func (mod *modContext) genConstructorCS(r *schema.Resource, argsOptional bool) []formalParam {
|
2020-02-27 19:04:34 +01:00
|
|
|
name := resourceName(r)
|
2020-03-09 18:35:20 +01:00
|
|
|
argsSchemaType := &schema.ObjectType{
|
|
|
|
Token: r.Token,
|
|
|
|
}
|
2020-04-03 20:26:52 +02:00
|
|
|
// Get the C#-specific name for the args type, which will be the fully-qualified name.
|
2020-03-17 21:37:40 +01:00
|
|
|
characteristics := propertyCharacteristics{
|
|
|
|
input: true,
|
|
|
|
optional: argsOptional,
|
|
|
|
}
|
|
|
|
argLangType := mod.typeString(argsSchemaType, "csharp", characteristics, false)
|
2020-04-03 20:26:52 +02:00
|
|
|
// The args type for a resource isn't part of "Inputs" namespace, so remove the "Inputs"
|
|
|
|
// namespace qualifier.
|
|
|
|
argLangTypeName := strings.ReplaceAll(argLangType.Name, "Inputs.", "")
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
var argsFlag string
|
2020-02-27 19:04:34 +01:00
|
|
|
var argsDefault string
|
2020-03-09 18:35:20 +01:00
|
|
|
if argsOptional {
|
2020-02-27 19:04:34 +01:00
|
|
|
// If the number of required input properties was zero, we can make the args object optional.
|
|
|
|
argsDefault = " = null"
|
2020-03-09 18:35:20 +01:00
|
|
|
argsFlag = "?"
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
docLangHelper := getLanguageDocHelper("csharp")
|
2020-03-17 21:37:40 +01:00
|
|
|
return []formalParam{
|
2020-03-09 18:35:20 +01:00
|
|
|
{
|
|
|
|
Name: "name",
|
2020-03-12 01:58:12 +01:00
|
|
|
Type: propertyType{
|
2020-03-09 18:35:20 +01:00
|
|
|
Name: "string",
|
|
|
|
Link: "https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/built-in-types",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "args",
|
|
|
|
OptionalFlag: argsFlag,
|
|
|
|
DefaultValue: argsDefault,
|
2020-03-12 01:58:12 +01:00
|
|
|
Type: propertyType{
|
2020-03-17 21:37:40 +01:00
|
|
|
Name: name + "Args",
|
2020-04-03 20:26:52 +02:00
|
|
|
Link: docLangHelper.GetDocLinkForResourceType(mod.pkg.Name, "", argLangTypeName),
|
2020-03-09 18:35:20 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "opts",
|
|
|
|
OptionalFlag: "?",
|
|
|
|
DefaultValue: " = null",
|
2020-03-12 01:58:12 +01:00
|
|
|
Type: propertyType{
|
2020-04-03 20:26:52 +02:00
|
|
|
Name: "CustomResourceOptions",
|
|
|
|
Link: docLangHelper.GetDocLinkForResourceType("", "", "Pulumi.CustomResourceOptions"),
|
2020-03-09 18:35:20 +01:00
|
|
|
},
|
|
|
|
},
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
func (mod *modContext) genNestedTypes(member interface{}, resourceType bool) []docNestedType {
|
|
|
|
tokens := nestedTypeUsageInfo{}
|
|
|
|
// Collect all of the types for this "member" as a map of resource names
|
|
|
|
// and if it appears in an input object and/or output object.
|
|
|
|
mod.getTypes(member, tokens)
|
2020-03-09 18:35:20 +01:00
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
var objs []docNestedType
|
2020-04-03 20:26:52 +02:00
|
|
|
for token, tyUsage := range tokens {
|
2020-02-27 19:04:34 +01:00
|
|
|
for _, t := range mod.pkg.Types {
|
2020-04-07 02:01:33 +02:00
|
|
|
obj, ok := t.(*schema.ObjectType)
|
|
|
|
if !ok || obj.Token != token {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(obj.Properties) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-09 18:35:20 +01:00
|
|
|
|
2020-04-07 02:01:33 +02:00
|
|
|
// Create maps to hold the per-language properties of this object and links to
|
|
|
|
// the API doc for each language.
|
|
|
|
props := make(map[string][]property)
|
|
|
|
apiDocLinks := make(map[string]apiTypeDocLinks)
|
|
|
|
for _, lang := range supportedLanguages {
|
|
|
|
// The nested type may be under a different package in a language.
|
|
|
|
// For example, in k8s, common types are in the core/v1 module and can appear in
|
|
|
|
// nested types elsewhere. So we use the appropriate name of that type,
|
|
|
|
// as well as its language-specific name. For example, module name for use as a C# namespace
|
|
|
|
// or as a Go package name.
|
|
|
|
modName := getLanguageModuleName(mod.pkg, mod.mod, lang)
|
|
|
|
nestedTypeModName := mod.pkg.TokenToModule(token)
|
|
|
|
if nestedTypeModName != mod.mod {
|
|
|
|
modName = getLanguageModuleName(mod.pkg, nestedTypeModName, lang)
|
|
|
|
}
|
2020-04-03 20:26:52 +02:00
|
|
|
|
2020-04-07 02:01:33 +02:00
|
|
|
docLangHelper := getLanguageDocHelper(lang)
|
|
|
|
inputCharacteristics := propertyCharacteristics{
|
|
|
|
input: true,
|
|
|
|
optional: true,
|
|
|
|
}
|
|
|
|
outputCharacteristics := propertyCharacteristics{
|
|
|
|
input: false,
|
|
|
|
optional: true,
|
|
|
|
}
|
|
|
|
inputObjLangType := mod.typeString(t, lang, inputCharacteristics, false /*insertWordBreaks*/)
|
|
|
|
outputObjLangType := mod.typeString(t, lang, outputCharacteristics, false /*insertWordBreaks*/)
|
|
|
|
|
|
|
|
// Get the doc link for this nested type based on whether the type is for a Function or a Resource.
|
|
|
|
var inputTypeDocLink string
|
|
|
|
var outputTypeDocLink string
|
|
|
|
if resourceType {
|
|
|
|
if tyUsage.Input {
|
|
|
|
inputTypeDocLink = docLangHelper.GetDocLinkForResourceInputOrOutputType(mod.pkg.Name, modName, inputObjLangType.Name, true)
|
2020-03-17 21:37:40 +01:00
|
|
|
}
|
2020-04-07 02:01:33 +02:00
|
|
|
if tyUsage.Output {
|
|
|
|
outputTypeDocLink = docLangHelper.GetDocLinkForResourceInputOrOutputType(mod.pkg.Name, modName, outputObjLangType.Name, false)
|
2020-03-17 21:37:40 +01:00
|
|
|
}
|
2020-04-07 02:01:33 +02:00
|
|
|
} else {
|
|
|
|
if tyUsage.Input {
|
|
|
|
inputTypeDocLink = docLangHelper.GetDocLinkForFunctionInputOrOutputType(mod.pkg.Name, modName, inputObjLangType.Name, true)
|
2020-03-12 01:58:12 +01:00
|
|
|
}
|
2020-04-07 02:01:33 +02:00
|
|
|
if tyUsage.Output {
|
|
|
|
outputTypeDocLink = docLangHelper.GetDocLinkForFunctionInputOrOutputType(mod.pkg.Name, modName, outputObjLangType.Name, false)
|
2020-03-09 18:35:20 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-07 02:01:33 +02:00
|
|
|
apiDocLinks[lang] = apiTypeDocLinks{
|
|
|
|
InputType: inputTypeDocLink,
|
|
|
|
OutputType: outputTypeDocLink,
|
|
|
|
}
|
|
|
|
props[lang] = mod.getProperties(obj.Properties, lang, true, true)
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-04-07 02:01:33 +02:00
|
|
|
|
|
|
|
objs = append(objs, docNestedType{
|
|
|
|
Name: wbr(tokenToName(obj.Token)),
|
|
|
|
APIDocLinks: apiDocLinks,
|
|
|
|
Properties: props,
|
|
|
|
})
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-09 18:35:20 +01:00
|
|
|
|
2020-02-27 19:04:34 +01:00
|
|
|
sort.Slice(objs, func(i, j int) bool {
|
2020-03-09 18:35:20 +01:00
|
|
|
return objs[i].Name < objs[j].Name
|
2020-02-27 19:04:34 +01:00
|
|
|
})
|
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
return objs
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
// getProperties returns a slice of properties that can be rendered for docs for
|
|
|
|
// the provided slice of properties in the schema.
|
2020-03-19 20:18:18 +01:00
|
|
|
func (mod *modContext) getProperties(properties []*schema.Property, lang string, input, nested bool) []property {
|
2020-03-09 18:35:20 +01:00
|
|
|
if len(properties) == 0 {
|
|
|
|
return nil
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-04-07 02:01:33 +02:00
|
|
|
isK8s := isKubernetesPackage(mod.pkg)
|
2020-03-12 01:58:12 +01:00
|
|
|
docProperties := make([]property, 0, len(properties))
|
2020-03-09 18:35:20 +01:00
|
|
|
for _, prop := range properties {
|
|
|
|
if prop == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-04-07 02:01:33 +02:00
|
|
|
// In k8s, apiVersion and kind are hard-coded in the SDK and not really
|
|
|
|
// user-provided input properties, so skip them.
|
|
|
|
if isK8s && (prop.Name == "apiVersion" || prop.Name == "kind") {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-17 21:37:40 +01:00
|
|
|
|
|
|
|
characteristics := propertyCharacteristics{
|
2020-03-19 20:18:18 +01:00
|
|
|
input: input,
|
2020-03-17 21:37:40 +01:00
|
|
|
optional: !prop.IsRequired,
|
|
|
|
}
|
2020-03-19 20:18:18 +01:00
|
|
|
|
2020-03-23 22:17:14 +01:00
|
|
|
langDocHelper := getLanguageDocHelper(lang)
|
|
|
|
var propLangName string
|
2020-03-19 20:18:18 +01:00
|
|
|
switch lang {
|
|
|
|
case "python":
|
|
|
|
pyName := python.PyName(prop.Name)
|
|
|
|
// The default casing for a Python property name is snake_case unless
|
|
|
|
// it is a property of a nested object, in which case, we should check the property
|
|
|
|
// case maps.
|
|
|
|
propLangName = pyName
|
|
|
|
|
|
|
|
if nested {
|
|
|
|
if snakeCase, ok := camelCaseToSnakeCase[prop.Name]; ok {
|
|
|
|
propLangName = snakeCase
|
|
|
|
} else if camelCase, ok := snakeCaseToCamelCase[pyName]; ok {
|
|
|
|
propLangName = camelCase
|
|
|
|
} else {
|
|
|
|
// If neither of the property case maps have the property
|
|
|
|
// then use the default name of the property.
|
|
|
|
propLangName = prop.Name
|
|
|
|
}
|
|
|
|
}
|
2020-03-23 22:17:14 +01:00
|
|
|
default:
|
|
|
|
name, err := langDocHelper.GetPropertyName(prop)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
propLangName = name
|
2020-03-19 20:18:18 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
docProperties = append(docProperties, property{
|
2020-03-20 16:17:58 +01:00
|
|
|
DisplayName: wbr(propLangName),
|
|
|
|
Name: propLangName,
|
2020-03-09 18:35:20 +01:00
|
|
|
Comment: prop.Comment,
|
|
|
|
DeprecationMessage: prop.DeprecationMessage,
|
|
|
|
IsRequired: prop.IsRequired,
|
2020-03-19 20:18:18 +01:00
|
|
|
IsInput: input,
|
2020-03-17 21:37:40 +01:00
|
|
|
Type: mod.typeString(prop.Type, lang, characteristics, true),
|
2020-03-09 18:35:20 +01:00
|
|
|
})
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
return docProperties
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
func (mod *modContext) genConstructors(r *schema.Resource, allOptionalInputs bool) map[string]string {
|
|
|
|
constructorParams := make(map[string]string)
|
|
|
|
for _, lang := range supportedLanguages {
|
|
|
|
var (
|
|
|
|
paramTemplate string
|
2020-03-17 21:37:40 +01:00
|
|
|
params []formalParam
|
2020-03-09 18:35:20 +01:00
|
|
|
)
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
|
|
|
|
switch lang {
|
|
|
|
case "nodejs":
|
|
|
|
params = mod.genConstructorTS(r, allOptionalInputs)
|
2020-03-17 21:37:40 +01:00
|
|
|
paramTemplate = "ts_formal_param"
|
2020-03-09 18:35:20 +01:00
|
|
|
case "go":
|
|
|
|
params = mod.genConstructorGo(r, allOptionalInputs)
|
2020-03-17 21:37:40 +01:00
|
|
|
paramTemplate = "go_formal_param"
|
2020-03-09 18:35:20 +01:00
|
|
|
case "csharp":
|
|
|
|
params = mod.genConstructorCS(r, allOptionalInputs)
|
2020-03-17 21:37:40 +01:00
|
|
|
paramTemplate = "csharp_formal_param"
|
|
|
|
case "python":
|
|
|
|
paramTemplate = "py_formal_param"
|
|
|
|
// The Pulumi Python SDK does not have types for constructor args.
|
|
|
|
// The input properties for a resource needs to be exploded as
|
|
|
|
// individual constructor params.
|
|
|
|
params = make([]formalParam, 0, len(r.InputProperties))
|
2020-04-07 02:01:33 +02:00
|
|
|
isK8s := isKubernetesPackage(mod.pkg)
|
2020-03-17 21:37:40 +01:00
|
|
|
for _, p := range r.InputProperties {
|
2020-04-07 02:01:33 +02:00
|
|
|
// In k8s, apiVersion and kind are hard-coded in the SDK and not really
|
|
|
|
// user-provided input properties, so skip them.
|
|
|
|
if isK8s && (p.Name == "apiVersion" || p.Name == "kind") {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-17 21:37:40 +01:00
|
|
|
params = append(params, formalParam{
|
|
|
|
Name: python.PyName(p.Name),
|
|
|
|
DefaultValue: "=None",
|
|
|
|
})
|
|
|
|
}
|
2020-03-09 18:35:20 +01:00
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
n := len(params)
|
|
|
|
for i, p := range params {
|
|
|
|
if err := templates.ExecuteTemplate(b, paramTemplate, p); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if i != n-1 {
|
|
|
|
if err := templates.ExecuteTemplate(b, "param_separator", nil); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-03-09 18:35:20 +01:00
|
|
|
constructorParams[lang] = b.String()
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-03-09 18:35:20 +01:00
|
|
|
return constructorParams
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
// getConstructorResourceInfo returns a map of per-language information about
|
|
|
|
// the resource being constructed.
|
2020-03-12 01:58:12 +01:00
|
|
|
func (mod *modContext) getConstructorResourceInfo(resourceTypeName string) map[string]propertyType {
|
|
|
|
resourceMap := make(map[string]propertyType)
|
2020-03-09 18:35:20 +01:00
|
|
|
resourceDisplayName := resourceTypeName
|
|
|
|
|
|
|
|
for _, lang := range supportedLanguages {
|
|
|
|
// Reset the type name back to the display name.
|
|
|
|
resourceTypeName = resourceDisplayName
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
docLangHelper := getLanguageDocHelper(lang)
|
2020-03-09 18:35:20 +01:00
|
|
|
switch lang {
|
2020-03-12 01:58:12 +01:00
|
|
|
case "nodejs", "go":
|
|
|
|
// Intentionally left blank.
|
2020-03-09 18:35:20 +01:00
|
|
|
case "csharp":
|
2020-04-07 02:01:33 +02:00
|
|
|
resourceTypeName = fmt.Sprintf("Pulumi.%s.%s.%s", title(mod.pkg.Name, "csharp"), title(mod.mod, "csharp"), resourceTypeName)
|
2020-03-09 18:35:20 +01:00
|
|
|
case "python":
|
|
|
|
// Pulumi's Python language SDK does not have "types" yet, so we will skip it for now.
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
panic(errors.Errorf("cannot generate constructor info for unhandled language %q", lang))
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-03-23 22:17:14 +01:00
|
|
|
parts := strings.Split(resourceTypeName, ".")
|
|
|
|
displayName := parts[len(parts)-1]
|
2020-03-12 01:58:12 +01:00
|
|
|
resourceMap[lang] = propertyType{
|
2020-03-23 22:17:14 +01:00
|
|
|
Name: resourceDisplayName,
|
|
|
|
DisplayName: displayName,
|
|
|
|
Link: docLangHelper.GetDocLinkForResourceType(mod.pkg.Name, mod.mod, resourceTypeName),
|
2020-03-09 18:35:20 +01:00
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-03-09 18:35:20 +01:00
|
|
|
|
|
|
|
return resourceMap
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 21:37:40 +01:00
|
|
|
func (mod *modContext) getTSLookupParams(r *schema.Resource, stateParam string) []formalParam {
|
|
|
|
docLangHelper := getLanguageDocHelper("nodejs")
|
|
|
|
return []formalParam{
|
|
|
|
{
|
|
|
|
Name: "name",
|
|
|
|
Type: propertyType{
|
|
|
|
Name: "string",
|
|
|
|
Link: nodejs.GetDocLinkForBuiltInType("string"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "id",
|
|
|
|
Type: propertyType{
|
2020-04-03 20:26:52 +02:00
|
|
|
Name: "Input<ID>",
|
2020-03-17 21:37:40 +01:00
|
|
|
Link: docLangHelper.GetDocLinkForResourceType("", "pulumi", "ID"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "state",
|
|
|
|
OptionalFlag: "?",
|
|
|
|
Type: propertyType{
|
|
|
|
Name: stateParam,
|
|
|
|
Link: docLangHelper.GetDocLinkForResourceType(mod.pkg.Name, mod.mod, stateParam),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "opts",
|
|
|
|
OptionalFlag: "?",
|
|
|
|
Type: propertyType{
|
2020-04-03 20:26:52 +02:00
|
|
|
Name: "CustomResourceOptions",
|
2020-03-17 21:37:40 +01:00
|
|
|
Link: docLangHelper.GetDocLinkForResourceType("", "pulumi", "CustomResourceOptions"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mod *modContext) getGoLookupParams(r *schema.Resource, stateParam string) []formalParam {
|
|
|
|
docLangHelper := getLanguageDocHelper("go")
|
|
|
|
// return fmt.Sprintf("func New%s(ctx *pulumi.Context, name string, args *%s, opts ...pulumi.ResourceOption) (*%s, error)\n", name, argsType, name)
|
|
|
|
return []formalParam{
|
|
|
|
{
|
|
|
|
Name: "ctx",
|
|
|
|
OptionalFlag: "*",
|
|
|
|
Type: propertyType{
|
2020-04-03 20:26:52 +02:00
|
|
|
Name: "Context",
|
2020-03-17 21:37:40 +01:00
|
|
|
Link: docLangHelper.GetDocLinkForResourceType("", "pulumi", "Context"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "name",
|
|
|
|
Type: propertyType{
|
|
|
|
Name: "string",
|
|
|
|
Link: go_gen.GetDocLinkForBuiltInType("string"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "id",
|
|
|
|
Type: propertyType{
|
2020-04-03 20:26:52 +02:00
|
|
|
Name: "IDInput",
|
2020-03-17 21:37:40 +01:00
|
|
|
Link: docLangHelper.GetDocLinkForResourceType("", "pulumi", "IDInput"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "state",
|
|
|
|
OptionalFlag: "*",
|
|
|
|
Type: propertyType{
|
|
|
|
Name: stateParam,
|
|
|
|
Link: docLangHelper.GetDocLinkForResourceType(mod.pkg.Name, mod.mod, stateParam),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "opts",
|
|
|
|
OptionalFlag: "...",
|
|
|
|
Type: propertyType{
|
2020-04-03 20:26:52 +02:00
|
|
|
Name: "ResourceOption",
|
2020-03-17 21:37:40 +01:00
|
|
|
Link: docLangHelper.GetDocLinkForResourceType("", "pulumi", "ResourceOption"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mod *modContext) getCSLookupParams(r *schema.Resource, stateParam string) []formalParam {
|
2020-04-07 02:01:33 +02:00
|
|
|
stateParamFQDN := fmt.Sprintf("Pulumi.%s.%s.%s", title(mod.pkg.Name, "csharp"), title(mod.mod, "csharp"), stateParam)
|
2020-03-17 21:37:40 +01:00
|
|
|
|
|
|
|
docLangHelper := getLanguageDocHelper("csharp")
|
|
|
|
return []formalParam{
|
|
|
|
{
|
|
|
|
Name: "name",
|
|
|
|
Type: propertyType{
|
|
|
|
Name: "string",
|
|
|
|
Link: "https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/built-in-types",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "id",
|
|
|
|
Type: propertyType{
|
2020-04-03 20:26:52 +02:00
|
|
|
Name: "Input<string>",
|
2020-03-17 21:37:40 +01:00
|
|
|
Link: docLangHelper.GetDocLinkForResourceType("", "", "Pulumi.Input"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "state",
|
|
|
|
OptionalFlag: "?",
|
|
|
|
Type: propertyType{
|
|
|
|
Name: stateParam,
|
|
|
|
Link: docLangHelper.GetDocLinkForResourceType(mod.pkg.Name, "", stateParamFQDN),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "opts",
|
|
|
|
OptionalFlag: "?",
|
|
|
|
DefaultValue: " = null",
|
|
|
|
Type: propertyType{
|
2020-04-03 20:26:52 +02:00
|
|
|
Name: "CustomResourceOptions",
|
|
|
|
Link: docLangHelper.GetDocLinkForResourceType("", "", "Pulumi.CustomResourceOptions"),
|
2020-03-17 21:37:40 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// genLookupParams generates a map of per-language way of rendering the formal parameters of the lookup function
|
|
|
|
// used to lookup an existing resource.
|
|
|
|
func (mod *modContext) genLookupParams(r *schema.Resource, stateParam string) map[string]string {
|
|
|
|
lookupParams := make(map[string]string)
|
|
|
|
if r.StateInputs == nil {
|
|
|
|
return lookupParams
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, lang := range supportedLanguages {
|
|
|
|
var (
|
|
|
|
paramTemplate string
|
|
|
|
params []formalParam
|
|
|
|
)
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
|
|
|
|
switch lang {
|
|
|
|
case "nodejs":
|
|
|
|
params = mod.getTSLookupParams(r, stateParam)
|
|
|
|
paramTemplate = "ts_formal_param"
|
|
|
|
case "go":
|
|
|
|
params = mod.getGoLookupParams(r, stateParam)
|
|
|
|
paramTemplate = "go_formal_param"
|
|
|
|
case "csharp":
|
|
|
|
params = mod.getCSLookupParams(r, stateParam)
|
|
|
|
paramTemplate = "csharp_formal_param"
|
|
|
|
case "python":
|
|
|
|
paramTemplate = "py_formal_param"
|
|
|
|
// The Pulumi Python SDK does not yet have types for formal parameters.
|
|
|
|
// The input properties for a resource needs to be exploded as
|
|
|
|
// individual constructor params.
|
|
|
|
params = make([]formalParam, 0, len(r.StateInputs.Properties))
|
|
|
|
for _, p := range r.StateInputs.Properties {
|
|
|
|
params = append(params, formalParam{
|
|
|
|
Name: python.PyName(p.Name),
|
|
|
|
DefaultValue: "=None",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n := len(params)
|
|
|
|
for i, p := range params {
|
|
|
|
if err := templates.ExecuteTemplate(b, paramTemplate, p); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if i != n-1 {
|
|
|
|
if err := templates.ExecuteTemplate(b, "param_separator", nil); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lookupParams[lang] = b.String()
|
|
|
|
}
|
|
|
|
return lookupParams
|
|
|
|
}
|
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
// genResource is the entrypoint for generating a doc for a resource
|
|
|
|
// from its Pulumi schema.
|
2020-03-12 01:58:12 +01:00
|
|
|
func (mod *modContext) genResource(r *schema.Resource) resourceDocArgs {
|
2020-02-27 19:04:34 +01:00
|
|
|
// Create a resource module file into which all of this resource's types will go.
|
|
|
|
name := resourceName(r)
|
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
// TODO: Unlike the other languages, Python does not have a separate Args object for inputs.
|
|
|
|
// The args are all just named parameters of the constructor. Consider injecting
|
|
|
|
// `resource_name` and `opts` as the first two items in the table of properties.
|
2020-03-12 01:58:12 +01:00
|
|
|
inputProps := make(map[string][]property)
|
|
|
|
outputProps := make(map[string][]property)
|
|
|
|
stateInputs := make(map[string][]property)
|
2020-03-09 18:35:20 +01:00
|
|
|
for _, lang := range supportedLanguages {
|
2020-03-19 20:18:18 +01:00
|
|
|
inputProps[lang] = mod.getProperties(r.InputProperties, lang, true, false)
|
2020-03-09 18:35:20 +01:00
|
|
|
if r.IsProvider {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-19 20:18:18 +01:00
|
|
|
outputProps[lang] = mod.getProperties(r.Properties, lang, false, false)
|
2020-03-09 18:35:20 +01:00
|
|
|
if r.StateInputs != nil {
|
2020-03-19 20:18:18 +01:00
|
|
|
stateInputs[lang] = mod.getProperties(r.StateInputs.Properties, lang, true, false)
|
2020-03-09 18:35:20 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
|
|
|
|
allOptionalInputs := true
|
|
|
|
for _, prop := range r.InputProperties {
|
2020-03-09 18:35:20 +01:00
|
|
|
// If at least one prop is required, then break.
|
|
|
|
if prop.IsRequired {
|
|
|
|
allOptionalInputs = false
|
|
|
|
break
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-02 02:22:23 +02:00
|
|
|
packageDetails := packageDetails{
|
|
|
|
Repository: mod.pkg.Repository,
|
|
|
|
License: mod.pkg.License,
|
|
|
|
Notes: mod.pkg.Attribution,
|
|
|
|
}
|
|
|
|
|
2020-03-17 21:37:40 +01:00
|
|
|
stateParam := name + "State"
|
2020-03-12 01:58:12 +01:00
|
|
|
data := resourceDocArgs{
|
|
|
|
Header: header{
|
2020-03-09 18:35:20 +01:00
|
|
|
Title: name,
|
|
|
|
},
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-04-07 02:01:33 +02:00
|
|
|
Comment: r.Comment,
|
|
|
|
DeprecationMessage: r.DeprecationMessage,
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
ConstructorParams: mod.genConstructors(r, allOptionalInputs),
|
|
|
|
ConstructorResource: mod.getConstructorResourceInfo(name),
|
|
|
|
ArgsRequired: !allOptionalInputs,
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
InputProperties: inputProps,
|
|
|
|
OutputProperties: outputProps,
|
2020-03-17 21:37:40 +01:00
|
|
|
LookupParams: mod.genLookupParams(r, stateParam),
|
2020-03-09 18:35:20 +01:00
|
|
|
StateInputs: stateInputs,
|
2020-03-17 21:37:40 +01:00
|
|
|
StateParam: stateParam,
|
2020-03-12 01:58:12 +01:00
|
|
|
NestedTypes: mod.genNestedTypes(r, true /*resourceType*/),
|
2020-04-02 02:22:23 +02:00
|
|
|
|
|
|
|
PackageDetails: packageDetails,
|
2020-03-09 18:35:20 +01:00
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
return data
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-03 20:26:52 +02:00
|
|
|
func (mod *modContext) getNestedTypes(t schema.Type, types nestedTypeUsageInfo, input bool) {
|
2020-02-27 19:04:34 +01:00
|
|
|
switch t := t.(type) {
|
|
|
|
case *schema.ArrayType:
|
2020-04-03 20:26:52 +02:00
|
|
|
glog.V(4).Infof("visiting array %s\n", t.ElementType.String())
|
|
|
|
skip := false
|
|
|
|
if o, ok := t.ElementType.(*schema.ObjectType); ok && types.contains(o.Token, input) {
|
|
|
|
glog.V(4).Infof("already added %s. skipping...\n", o.Token)
|
|
|
|
skip = true
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-03 20:26:52 +02:00
|
|
|
if !skip {
|
|
|
|
mod.getNestedTypes(t.ElementType, types, input)
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
case *schema.MapType:
|
2020-04-03 20:26:52 +02:00
|
|
|
glog.V(4).Infof("visiting map %s\n", t.ElementType.String())
|
|
|
|
skip := false
|
|
|
|
if o, ok := t.ElementType.(*schema.ObjectType); ok && types.contains(o.Token, input) {
|
|
|
|
glog.V(4).Infof("already added %s. skipping...\n", o.Token)
|
|
|
|
skip = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !skip {
|
|
|
|
mod.getNestedTypes(t.ElementType, types, input)
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
case *schema.ObjectType:
|
2020-04-03 20:26:52 +02:00
|
|
|
glog.V(4).Infof("visiting object %s\n", t.Token)
|
2020-03-12 01:58:12 +01:00
|
|
|
types.add(t.Token, input)
|
|
|
|
for _, p := range t.Properties {
|
2020-04-03 20:26:52 +02:00
|
|
|
if o, ok := p.Type.(*schema.ObjectType); ok && types.contains(o.Token, input) {
|
|
|
|
glog.V(4).Infof("already added %s. skipping...\n", o.Token)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("visiting object property %s\n", p.Type.String())
|
2020-03-12 01:58:12 +01:00
|
|
|
mod.getNestedTypes(p.Type, types, input)
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
case *schema.UnionType:
|
2020-04-03 20:26:52 +02:00
|
|
|
glog.V(4).Infof("visiting union type %s\n", t.String())
|
2020-02-27 19:04:34 +01:00
|
|
|
for _, e := range t.ElementTypes {
|
2020-04-03 20:26:52 +02:00
|
|
|
if o, ok := e.(*schema.ObjectType); ok && types.contains(o.Token, input) {
|
|
|
|
glog.V(4).Infof("already added %s. skipping...\n", o.Token)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
glog.V(4).Infof("visiting union element type %s\n", e.String())
|
2020-03-12 01:58:12 +01:00
|
|
|
mod.getNestedTypes(e, types, input)
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
func (mod *modContext) getTypes(member interface{}, types nestedTypeUsageInfo) {
|
2020-04-03 20:26:52 +02:00
|
|
|
glog.V(3).Infoln("getting nested types for module", mod.mod)
|
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
switch t := member.(type) {
|
2020-02-27 19:04:34 +01:00
|
|
|
case *schema.ObjectType:
|
2020-03-12 01:58:12 +01:00
|
|
|
for _, p := range t.Properties {
|
|
|
|
mod.getNestedTypes(p.Type, types, false)
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
case *schema.Resource:
|
2020-03-12 01:58:12 +01:00
|
|
|
for _, p := range t.Properties {
|
|
|
|
mod.getNestedTypes(p.Type, types, false)
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-03-12 01:58:12 +01:00
|
|
|
for _, p := range t.InputProperties {
|
|
|
|
mod.getNestedTypes(p.Type, types, true)
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
case *schema.Function:
|
2020-03-12 01:58:12 +01:00
|
|
|
if t.Inputs != nil {
|
|
|
|
mod.getNestedTypes(t.Inputs, types, true)
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-03-12 01:58:12 +01:00
|
|
|
if t.Outputs != nil {
|
|
|
|
mod.getNestedTypes(t.Outputs, types, false)
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fs map[string][]byte
|
|
|
|
|
|
|
|
func (fs fs) add(path string, contents []byte) {
|
|
|
|
_, has := fs[path]
|
|
|
|
contract.Assertf(!has, "duplicate file: %s", path)
|
|
|
|
fs[path] = contents
|
|
|
|
}
|
|
|
|
|
2020-04-07 02:01:33 +02:00
|
|
|
func (mod *modContext) getModuleFileName() string {
|
|
|
|
if !isKubernetesPackage(mod.pkg) {
|
|
|
|
return mod.mod
|
|
|
|
}
|
|
|
|
|
|
|
|
if override, ok := goPkgInfo.ModuleToPackage[mod.mod]; ok {
|
|
|
|
return override
|
|
|
|
}
|
|
|
|
return mod.mod
|
|
|
|
}
|
|
|
|
|
2020-02-27 19:04:34 +01:00
|
|
|
func (mod *modContext) gen(fs fs) error {
|
2020-04-07 02:01:33 +02:00
|
|
|
modName := mod.getModuleFileName()
|
2020-02-27 19:04:34 +01:00
|
|
|
var files []string
|
|
|
|
for p := range fs {
|
|
|
|
d := path.Dir(p)
|
|
|
|
if d == "." {
|
|
|
|
d = ""
|
|
|
|
}
|
2020-04-07 02:01:33 +02:00
|
|
|
if d == modName {
|
2020-02-27 19:04:34 +01:00
|
|
|
files = append(files, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addFile := func(name, contents string) {
|
2020-04-07 02:01:33 +02:00
|
|
|
p := path.Join(modName, name)
|
2020-02-27 19:04:34 +01:00
|
|
|
files = append(files, p)
|
|
|
|
fs.add(p, []byte(contents))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resources
|
|
|
|
for _, r := range mod.resources {
|
2020-03-09 18:35:20 +01:00
|
|
|
data := mod.genResource(r)
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-03-09 18:35:20 +01:00
|
|
|
title := resourceName(r)
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
err := templates.ExecuteTemplate(buffer, "resource.tmpl", data)
|
|
|
|
if err != nil {
|
2020-04-07 02:01:33 +02:00
|
|
|
return err
|
2020-03-09 18:35:20 +01:00
|
|
|
}
|
2020-04-07 02:01:33 +02:00
|
|
|
|
|
|
|
addFile(strings.ToLower(title)+".md", buffer.String())
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Functions
|
|
|
|
for _, f := range mod.functions {
|
2020-03-12 01:58:12 +01:00
|
|
|
data := mod.genFunction(f)
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-03-12 01:58:12 +01:00
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
err := templates.ExecuteTemplate(buffer, "function.tmpl", data)
|
|
|
|
if err != nil {
|
2020-04-07 02:01:33 +02:00
|
|
|
return err
|
2020-03-12 01:58:12 +01:00
|
|
|
}
|
2020-04-07 02:01:33 +02:00
|
|
|
|
|
|
|
addFile(strings.ToLower(tokenToName(f.Token))+".md", buffer.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the index files.
|
|
|
|
idxData := mod.genIndex()
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
err := templates.ExecuteTemplate(buffer, "index.tmpl", idxData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-07 02:01:33 +02:00
|
|
|
fs.add(path.Join(modName, "_index.md"), []byte(buffer.String()))
|
2020-02-27 19:04:34 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 02:01:33 +02:00
|
|
|
// indexEntry represents an individual entry on an index page.
|
|
|
|
type indexEntry struct {
|
|
|
|
Link string
|
|
|
|
DisplayName string
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-04-07 02:01:33 +02:00
|
|
|
// indexData represents the index file data to be rendered as _index.md.
|
|
|
|
type indexData struct {
|
|
|
|
Tool string
|
2020-03-27 15:56:51 +01:00
|
|
|
|
2020-04-07 02:01:33 +02:00
|
|
|
Title string
|
|
|
|
PackageDescription string
|
|
|
|
// Menu indicates if an index page should be part of the TOC menu.
|
|
|
|
Menu bool
|
|
|
|
|
|
|
|
Functions []indexEntry
|
|
|
|
Resources []indexEntry
|
|
|
|
Modules []indexEntry
|
|
|
|
PackageDetails packageDetails
|
|
|
|
}
|
|
|
|
|
|
|
|
// indexEntrySorter implements the sort.Interface for sorting
|
|
|
|
// a slice of indexEntry struct types.
|
|
|
|
type indexEntrySorter struct {
|
|
|
|
entries []indexEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len is part of sort.Interface. Returns the length of the
|
|
|
|
// entries slice.
|
|
|
|
func (s *indexEntrySorter) Len() int {
|
|
|
|
return len(s.entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap is part of sort.Interface.
|
|
|
|
func (s *indexEntrySorter) Swap(i, j int) {
|
|
|
|
s.entries[i], s.entries[j] = s.entries[j], s.entries[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less is part of sort.Interface. It sorts the entries by their
|
|
|
|
// display name in an ascending order.
|
|
|
|
func (s *indexEntrySorter) Less(i, j int) bool {
|
|
|
|
return s.entries[i].DisplayName < s.entries[j].DisplayName
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortIndexEntries(entries []indexEntry) {
|
|
|
|
if len(entries) == 0 {
|
|
|
|
return
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-07 02:01:33 +02:00
|
|
|
sorter := &indexEntrySorter{
|
|
|
|
entries: entries,
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-04-07 02:01:33 +02:00
|
|
|
sort.Sort(sorter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// genIndex emits an _index.md file for the module.
|
|
|
|
func (mod *modContext) genIndex() indexData {
|
|
|
|
glog.V(4).Infoln("genIndex for", mod.mod)
|
|
|
|
modules := make([]indexEntry, 0, len(mod.children))
|
|
|
|
resources := make([]indexEntry, 0, len(mod.resources))
|
|
|
|
functions := make([]indexEntry, 0, len(mod.functions))
|
|
|
|
|
|
|
|
modName := mod.getModuleFileName()
|
|
|
|
title := modName
|
|
|
|
menu := false
|
|
|
|
if title == "" {
|
|
|
|
title = mod.pkg.Name
|
|
|
|
// Flag top-level entries for inclusion in the table-of-contents menu.
|
|
|
|
menu = true
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there are submodules, list them.
|
|
|
|
for _, mod := range mod.children {
|
2020-04-07 02:01:33 +02:00
|
|
|
modName := mod.getModuleFileName()
|
|
|
|
parts := strings.Split(modName, "/")
|
|
|
|
modName = parts[len(parts)-1]
|
|
|
|
modules = append(modules, indexEntry{
|
|
|
|
Link: modName + "/",
|
|
|
|
DisplayName: modName,
|
|
|
|
})
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-04-07 02:01:33 +02:00
|
|
|
sortIndexEntries(modules)
|
2020-02-27 19:04:34 +01:00
|
|
|
|
|
|
|
// If there are resources in the root, list them.
|
|
|
|
for _, r := range mod.resources {
|
2020-04-07 02:01:33 +02:00
|
|
|
name := resourceName(r)
|
|
|
|
resources = append(resources, indexEntry{
|
|
|
|
Link: strings.ToLower(name),
|
|
|
|
DisplayName: name,
|
|
|
|
})
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-04-07 02:01:33 +02:00
|
|
|
sortIndexEntries(resources)
|
2020-02-27 19:04:34 +01:00
|
|
|
|
|
|
|
// If there are functions in the root, list them.
|
|
|
|
for _, f := range mod.functions {
|
2020-04-07 02:01:33 +02:00
|
|
|
name := tokenToName(f.Token)
|
|
|
|
functions = append(functions, indexEntry{
|
|
|
|
Link: strings.ToLower(name),
|
|
|
|
DisplayName: name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
sortIndexEntries(functions)
|
|
|
|
|
|
|
|
packageDetails := packageDetails{
|
|
|
|
Repository: mod.pkg.Repository,
|
|
|
|
License: mod.pkg.License,
|
|
|
|
Notes: mod.pkg.Attribution,
|
|
|
|
}
|
|
|
|
|
|
|
|
data := indexData{
|
|
|
|
Tool: mod.tool,
|
|
|
|
|
|
|
|
Title: title,
|
|
|
|
Menu: menu,
|
|
|
|
|
|
|
|
Resources: resources,
|
|
|
|
Functions: functions,
|
|
|
|
Modules: modules,
|
|
|
|
PackageDetails: packageDetails,
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-07 02:01:33 +02:00
|
|
|
// If this is the root module, write out the package description.
|
|
|
|
if mod.mod == "" {
|
|
|
|
data.PackageDescription = mod.pkg.Description
|
2020-04-02 20:26:11 +02:00
|
|
|
}
|
2020-04-02 02:22:23 +02:00
|
|
|
|
2020-04-07 02:01:33 +02:00
|
|
|
return data
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-03-30 23:37:30 +02:00
|
|
|
func decodeLangSpecificInfo(pkg *schema.Package, lang string, obj interface{}) error {
|
|
|
|
if csharp, ok := pkg.Language[lang]; ok {
|
|
|
|
if err := json.Unmarshal([]byte(csharp), &obj); err != nil {
|
|
|
|
return errors.Wrap(err, "decoding csharp package info")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 02:01:33 +02:00
|
|
|
func getMod(pkg *schema.Package, token string, modules map[string]*modContext, tool string) *modContext {
|
|
|
|
modName := pkg.TokenToModule(token)
|
|
|
|
mod, ok := modules[modName]
|
|
|
|
if !ok {
|
|
|
|
mod = &modContext{
|
|
|
|
pkg: pkg,
|
|
|
|
mod: modName,
|
|
|
|
tool: tool,
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-04-07 02:01:33 +02:00
|
|
|
if modName != "" {
|
|
|
|
parentName := path.Dir(modName)
|
|
|
|
// If the parent name is blank, it means this is the package-level.
|
|
|
|
if parentName == "." || parentName == "" {
|
|
|
|
parentName = ":index:"
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-04-07 02:01:33 +02:00
|
|
|
parent := getMod(pkg, parentName, modules, tool)
|
|
|
|
parent.children = append(parent.children, mod)
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-04-07 02:01:33 +02:00
|
|
|
|
|
|
|
modules[modName] = mod
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-04-07 02:01:33 +02:00
|
|
|
return mod
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateModulesFromSchemaPackage(tool string, pkg *schema.Package) map[string]*modContext {
|
|
|
|
// Group resources, types, and functions into modules.
|
|
|
|
modules := map[string]*modContext{}
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-03-30 23:37:30 +02:00
|
|
|
// Decode Go-specific language info.
|
2020-04-03 20:26:52 +02:00
|
|
|
if err := decodeLangSpecificInfo(pkg, "go", &goPkgInfo); err != nil {
|
2020-03-30 23:37:30 +02:00
|
|
|
panic(errors.Wrap(err, "error decoding go language info"))
|
2020-03-20 16:17:58 +01:00
|
|
|
}
|
2020-03-30 23:37:30 +02:00
|
|
|
goLangHelper := getLanguageDocHelper("go").(*go_gen.DocLanguageHelper)
|
2020-03-20 16:17:58 +01:00
|
|
|
// Generate the Go package map info now, so we can use that to get the type string
|
|
|
|
// names later.
|
2020-04-03 20:26:52 +02:00
|
|
|
goLangHelper.GeneratePackagesMap(pkg, tool, goPkgInfo)
|
2020-03-20 16:17:58 +01:00
|
|
|
|
2020-03-30 23:37:30 +02:00
|
|
|
// Decode C#-specific language info.
|
2020-04-03 20:26:52 +02:00
|
|
|
if err := decodeLangSpecificInfo(pkg, "csharp", &csharpPkgInfo); err != nil {
|
2020-03-30 23:37:30 +02:00
|
|
|
panic(errors.Wrap(err, "error decoding c# language info"))
|
|
|
|
}
|
|
|
|
csharpLangHelper := getLanguageDocHelper("csharp").(*dotnet.DocLanguageHelper)
|
2020-04-03 20:26:52 +02:00
|
|
|
csharpLangHelper.Namespaces = csharpPkgInfo.Namespaces
|
2020-03-30 23:37:30 +02:00
|
|
|
|
2020-03-20 16:17:58 +01:00
|
|
|
pyLangHelper := getLanguageDocHelper("python").(*python.DocLanguageHelper)
|
2020-02-27 19:04:34 +01:00
|
|
|
scanResource := func(r *schema.Resource) {
|
2020-04-07 02:01:33 +02:00
|
|
|
mod := getMod(pkg, r.Token, modules, tool)
|
2020-02-27 19:04:34 +01:00
|
|
|
mod.resources = append(mod.resources, r)
|
2020-03-19 20:18:18 +01:00
|
|
|
|
2020-02-27 19:04:34 +01:00
|
|
|
for _, p := range r.Properties {
|
2020-03-20 16:17:58 +01:00
|
|
|
pyLangHelper.GenPropertyCaseMap(mod.pkg, mod.mod, tool, p, snakeCaseToCamelCase, camelCaseToSnakeCase)
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-03-19 20:18:18 +01:00
|
|
|
|
2020-02-27 19:04:34 +01:00
|
|
|
for _, p := range r.InputProperties {
|
2020-03-20 16:17:58 +01:00
|
|
|
pyLangHelper.GenPropertyCaseMap(mod.pkg, mod.mod, tool, p, snakeCaseToCamelCase, camelCaseToSnakeCase)
|
2020-04-03 20:26:52 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-19 20:18:18 +01:00
|
|
|
|
2020-04-03 20:26:52 +02:00
|
|
|
scanK8SResource := func(r *schema.Resource) {
|
2020-04-07 02:01:33 +02:00
|
|
|
mod := getK8SMod(pkg, r.Token, modules, tool)
|
2020-04-03 20:26:52 +02:00
|
|
|
mod.resources = append(mod.resources, r)
|
|
|
|
|
|
|
|
// For k8s, all nested properties will use a snake_case,
|
|
|
|
// so we'll just add them all to the respective property
|
|
|
|
// case maps.
|
|
|
|
for _, p := range r.Properties {
|
|
|
|
n := p.Name
|
|
|
|
snakeCase := python.PyName(n)
|
|
|
|
snakeCaseToCamelCase[snakeCase] = n
|
|
|
|
camelCaseToSnakeCase[n] = snakeCase
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-03-19 20:18:18 +01:00
|
|
|
|
2020-04-03 20:26:52 +02:00
|
|
|
for _, p := range r.InputProperties {
|
|
|
|
n := p.Name
|
|
|
|
snakeCase := python.PyName(n)
|
|
|
|
snakeCaseToCamelCase[snakeCase] = n
|
|
|
|
camelCaseToSnakeCase[n] = snakeCase
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 20:26:52 +02:00
|
|
|
glog.V(3).Infoln("scanning resources")
|
2020-04-07 02:01:33 +02:00
|
|
|
if isKubernetesPackage(pkg) {
|
2020-04-03 20:26:52 +02:00
|
|
|
scanK8SResource(pkg.Provider)
|
|
|
|
for _, r := range pkg.Resources {
|
|
|
|
scanK8SResource(r)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
scanResource(pkg.Provider)
|
|
|
|
for _, r := range pkg.Resources {
|
|
|
|
scanResource(r)
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
}
|
2020-04-03 20:26:52 +02:00
|
|
|
glog.V(3).Infoln("done scanning resources")
|
2020-02-27 19:04:34 +01:00
|
|
|
|
|
|
|
for _, f := range pkg.Functions {
|
2020-04-07 02:01:33 +02:00
|
|
|
mod := getMod(pkg, f.Token, modules, tool)
|
2020-02-27 19:04:34 +01:00
|
|
|
mod.functions = append(mod.functions, f)
|
|
|
|
}
|
2020-03-20 16:17:58 +01:00
|
|
|
return modules
|
|
|
|
}
|
|
|
|
|
|
|
|
// GeneratePackage generates the docs package with docs for each resource given the Pulumi
|
|
|
|
// schema.
|
|
|
|
func GeneratePackage(tool string, pkg *schema.Package) (map[string][]byte, error) {
|
|
|
|
templates = template.New("").Funcs(template.FuncMap{
|
|
|
|
"htmlSafe": func(html string) template.HTML {
|
|
|
|
// Markdown fragments in the templates need to be rendered as-is,
|
|
|
|
// so that html/template package doesn't try to inject data into it,
|
|
|
|
// which will most certainly fail.
|
|
|
|
// nolint gosec
|
|
|
|
return template.HTML(html)
|
|
|
|
},
|
|
|
|
"pyName": func(str string) string {
|
|
|
|
return python.PyName(str)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
for name, b := range packagedTemplates {
|
|
|
|
template.Must(templates.New(name).Parse(string(b)))
|
|
|
|
}
|
2020-02-27 19:04:34 +01:00
|
|
|
|
2020-04-03 20:26:52 +02:00
|
|
|
defer glog.Flush()
|
|
|
|
|
2020-03-20 16:17:58 +01:00
|
|
|
// Generate the modules from the schema, and for every module
|
|
|
|
// run the generator functions to generate markdown files.
|
|
|
|
modules := generateModulesFromSchemaPackage(tool, pkg)
|
2020-04-03 20:26:52 +02:00
|
|
|
glog.V(3).Infoln("generating package now...")
|
2020-02-27 19:04:34 +01:00
|
|
|
files := fs{}
|
|
|
|
for _, mod := range modules {
|
|
|
|
if err := mod.gen(files); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return files, nil
|
|
|
|
}
|