forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
documentation.go
128 lines (113 loc) · 3.52 KB
/
documentation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2021 Google LLC. All Rights Reserved.
//
// 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.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"path"
"sort"
"text/template"
"github.com/golang/glog"
)
// Merges beta and GA resources for doc generation for a particular resource.
func mergeResource(res *Resource, resources map[Version][]*Resource, version *Version) *Resource {
resourceAcrossVersions := make(map[Version]*Resource)
for v, resList := range resources {
for _, r := range resList {
// Name is not unique, TerraformName must be
if r.TerraformName() == res.TerraformName() {
resourceAcrossVersions[v] = r
}
}
}
ga, gaExists := resourceAcrossVersions[GA_VERSION]
beta, betaExists := resourceAcrossVersions[BETA_VERSION]
alpha, alphaExists := resourceAcrossVersions[ALPHA_VERSION]
if alphaExists {
return alpha
}
if gaExists {
if betaExists {
return mergeResources(ga, beta)
}
return ga
}
beta.Description = fmt.Sprintf("Beta only: %s", beta.Description)
return beta
}
func mergeResources(ga, beta *Resource) *Resource {
beta.Properties = mergeProperties(ga.Properties, beta.Properties)
return beta
}
// Marks any sub properties as beta only
func mergeProperties(ga, beta []Property) []Property {
gaProps := make(map[string]Property)
for _, p := range ga {
gaProps[p.title] = p
}
betaProps := make(map[string]Property)
for _, p := range beta {
betaProps[p.title] = p
}
inOrder := make([]string, 0)
for k, _ := range betaProps {
inOrder = append(inOrder, k)
}
sort.Strings(inOrder)
modifiedProps := make([]Property, 0)
for _, name := range inOrder {
v := betaProps[name]
if gaProp, ok := gaProps[name]; !ok {
v.Description = fmt.Sprintf("(Beta only) %s", v.Description)
} else if len(v.Properties) != 0 {
// Look for sub-properties that might be beta only.
// If the top-level property is beta only, sub-properties don't need to be marked
v.Properties = mergeProperties(gaProp.Properties, v.Properties)
}
modifiedProps = append(modifiedProps, v)
}
return modifiedProps
}
func generateResourceWebsiteFile(res *Resource, resources map[Version][]*Resource, version *Version) {
res = mergeResource(res, resources, version)
if len(res.DocSamples()) <= 0 {
fmt.Printf(" %-40s no samples, skipping doc generation\n", res.TerraformName())
return
}
// Generate resource website file
tmplInput := ResourceInput{
Resource: *res,
}
tmpl, err := template.New("resource.html.markdown.tmpl").Funcs(TemplateFunctions).ParseFiles(
"templates/resource.html.markdown.tmpl",
)
if err != nil {
glog.Exit(err)
}
contents := bytes.Buffer{}
if err = tmpl.ExecuteTemplate(&contents, "resource.html.markdown.tmpl", tmplInput); err != nil {
glog.Exit(err)
}
source := contents.Bytes()
if oPath == nil || *oPath == "" {
fmt.Printf("%v\n", string(source))
} else {
outname := fmt.Sprintf("%s_%s.html.markdown", res.ProductName(), res.Name())
err := ioutil.WriteFile(path.Join(*oPath, "website/docs/r", outname), source, 0644)
if err != nil {
glog.Exit(err)
}
}
}