-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathdoc_edits.go
More file actions
158 lines (140 loc) · 5.54 KB
/
Copy pathdoc_edits.go
File metadata and controls
158 lines (140 loc) · 5.54 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package gcp
import (
"bytes"
"fmt"
"math/rand"
"regexp"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
)
func editRules(defaults []tfbridge.DocsEdit) []tfbridge.DocsEdit {
return append(defaults,
fixUpKmsCryptoKey,
fixupEffectiveLabels,
removeSecretsInPlainTextNote,
removeBetaFromDescriptionField,
substituteRandomSuffix,
rewritemembersField,
apiConfigAPIID,
)
}
const effectiveLabels = `including the labels configured through Terraform`
var effectiveLabelsRegexp = regexp.MustCompile(effectiveLabels)
var fixupEffectiveLabels = tfbridge.DocsEdit{
Path: "*",
Edit: func(_ string, content []byte) ([]byte, error) {
content = effectiveLabelsRegexp.ReplaceAllLiteral(content, []byte("including the labels configured through Pulumi"))
return content, nil
},
}
//nolint:lll
var secretsInPlainTextNoteRegexps = []*regexp.Regexp{
regexp.MustCompile(`~?> \*\*Warning:\*\* All arguments including the following potentially sensitive\nvalues will be stored in the raw state as plain text: .*\nRead more about sensitive data in state\.`),
regexp.MustCompile(`(?s)~?> \*\*((Warning)|(Note)):\*\* All arguments including .*as plain-text(\.)?( Read more about sensitive data in state\.)?`),
}
var removeSecretsInPlainTextNote = tfbridge.DocsEdit{
Path: "*",
Edit: func(_ string, content []byte) ([]byte, error) {
for _, r := range secretsInPlainTextNoteRegexps {
content = r.ReplaceAllLiteral(content, nil)
}
return content, nil
},
}
// Some descriptions had a link to google-beta removed as part of link scrubbing by the bridge.
// This leaves us with the following patterns unique to this provider to remove from the "Description" fields.
// We have to be somewhat verbose here to avoid removing legitimate use of the term "Beta", e.g "Kubernetes Beta API".
// "(Optional, Beta, Deprecated)
// "(Optional, Beta)
// "(Beta, Deprecated)
// "(Beta only)
// "(Optional) Beta
var betaRegexps = []*regexp.Regexp{
regexp.MustCompile(`\(Beta\)`),
regexp.MustCompile(`\(Optional, Beta\)`),
regexp.MustCompile(`\(Optional, Beta, Deprecated\)`),
regexp.MustCompile(`\(Beta, Deprecated\)`),
regexp.MustCompile(`\(Beta only\)`),
regexp.MustCompile(`\(Optional\) Beta `),
}
var removeBetaFromDescriptionField = tfbridge.DocsEdit{
Path: "*",
Edit: func(_ string, content []byte) ([]byte, error) {
for _, betaRegex := range betaRegexps {
content = betaRegex.ReplaceAllLiteral(content, nil)
}
return content, nil
},
}
// Example HCL seems to employ `%{random_suffix}` which trips up the example conversion. Substitute
// it out to an actual random suffix.
var substituteRandomSuffix = (func() tfbridge.DocsEdit {
pattern := regexp.MustCompile(regexp.QuoteMeta("%{random_suffix}"))
//nolint:gosec
randGen := rand.New(rand.NewSource(123456789))
return tfbridge.DocsEdit{
Path: "*",
Edit: func(_ string, content []byte) ([]byte, error) {
return pattern.ReplaceAllFunc(content, func([]byte) []byte {
//nolint:gosec
return []byte(fmt.Sprintf("_%d", randGen.Intn(100000)))
}), nil
},
}
})()
// Docs discovery gets tripped up on `member/members` fields for IAM-type properties and doesn't align the content
// correctly.
var memberRegexp = regexp.MustCompile("`member/members`")
func trimFrontMatter(text []byte) []byte {
body, ok := bytes.CutPrefix(text, []byte("---"))
if !ok {
return text
}
idx := bytes.Index(body, []byte("\n---"))
// Unable to find closing, so just return.
if idx == -1 {
return text
}
return body[idx+3:]
}
var rewritemembersField = tfbridge.DocsEdit{
Path: "*iam.html.markdown",
Edit: func(_ string, content []byte) ([]byte, error) {
membersByte := []byte("`members`")
memberByte := []byte("`member`")
var returnContent []byte
membersContent := memberRegexp.ReplaceAllLiteral(content, membersByte)
memberContent := memberRegexp.ReplaceAllLiteral(trimFrontMatter(content), memberByte)
// Because the IamBinding property matches to a `members` field, while the `IAMMember` property matches to a
//`member` field, we need to create content for both `members` and `member` so the bridge can match each.
//The easiest way to do this is to duplicate the content in its entirety, once for `members` and once for
//`member`, and let the bridge figure it out.
// See https://github.com/pulumi/pulumi-gcp/issues/1920 for context.
returnContent = append(returnContent, membersContent...)
returnContent = append(returnContent, memberContent...)
return returnContent, nil
},
}
func targetedSimpleReplace(filePath, from, to string) tfbridge.DocsEdit {
fromB, toB := []byte(from), []byte(to)
return tfbridge.DocsEdit{
Path: filePath,
Edit: func(_ string, content []byte) ([]byte, error) {
return bytes.ReplaceAll(content, fromB, toB), nil
},
}
}
var fixUpKmsCryptoKey = targetedSimpleReplace(
"kms_crypto_key.html.markdown",
"For this reason, it is strongly recommended that you add\nlifecycle\nhooks "+
"to the resource to prevent accidental destruction.",
"For this reason, it is strongly recommended that you use "+
"Pulumi's [protect resource option](https://www.pulumi.com/docs/concepts/options/protect/).")
var apiConfigAPIIDStr = "Identifier to assign to the API Config. Must be unique within scope of the parent resource(api)."
var apiConfigAPIIDRegexp = regexp.MustCompile(regexp.QuoteMeta(apiConfigAPIIDStr))
var apiConfigAPIID = tfbridge.DocsEdit{
Path: "*api_gateway_api_config.html.markdown",
Edit: func(_ string, content []byte) ([]byte, error) {
content = apiConfigAPIIDRegexp.ReplaceAllLiteral(content, []byte(apiConfigAPIIDStr+" **Note: use apiId if calling a resource**"))
return content, nil
},
}