This repository has been archived by the owner on Dec 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeprecated.go
218 lines (179 loc) · 8.11 KB
/
deprecated.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package superschema
import "fmt"
const (
warningAttributeDeprecated = "\n\n ~> **Attribute deprecated** "
warningResourceDeprecated = "\n\n !> **Resource deprecated** "
)
// Deprecated is a struct to describe a deprecated attribute.
type Deprecated struct {
// DeprecationMessage is the message to display in the CLI when the user
// attempts to use the deprecated attribute.
// This field is required.
DeprecationMessage string
// MarkdownDeprecationMessage is the message to display in the Documentation portal
// when the user attempts to use the deprecated attribute.
// This field is optional if ComputeMarkdownDeprecationMessage is false.
MarkdownDeprecationMessage string
// ComputeMarkdownDeprecationMessage is a flag to indicate whether the MarkdownDeprecationMessage
// should be computed from the parameters of the Deprecated struct.
ComputeMarkdownDeprecationMessage bool
// Renamed is a flag to indicate whether the attribute has been renamed.
// Removed is a flag to indicate whether the attribute has been removed.
// One of these fields must be true.
Renamed, Removed bool
// FromAttributeName is the name of the attribute that has been deprecated.
// This field is required if ComputeMarkdownDeprecationMessage is true.
FromAttributeName string
// TargetAttributeName is the name of the attribute that replaces the deprecated attribute.
// TargetResourceName is the name of the resource that replaces the deprecated attribute.
// These fields are optional if the attribute has been removed.
TargetAttributeName, TargetResourceName string
// TargetRelease is the release version in which the attribute was deprecated. (e.g. v1.0.0).
// This field is Required.
TargetRelease string
// LinkToIssue is the link to the GitHub issue that describes the deprecation.
// This field is optional.
LinkToIssue string
// LinkToResourceDoc is the link to the terraform documentation for the resource that replaces the deprecated attribute.
// This field is optional.
LinkToResourceDoc string
// LinkToMilestone is the link to the GitHub milestone that describes the release in which the attribute was deprecated.
// This field is optional.
LinkToMilestone string
// OnlyResource and OnlyDataSource are flags to indicate whether the deprecation message should be displayed
// only for the resource or only for the data source.
// If not set, the deprecation message will be displayed for both.
OnlyResource, OnlyDataSource *bool
}
func (d *Deprecated) computeDeprecatedDocumentation() string {
if (!d.ComputeMarkdownDeprecationMessage && d.MarkdownDeprecationMessage == "") || (d.FromAttributeName == "" && d.ComputeMarkdownDeprecationMessage) || d.TargetRelease == "" {
return ""
}
if d.MarkdownDeprecationMessage != "" {
return d.MarkdownDeprecationMessage
}
message := warningAttributeDeprecated
switch {
case d.Renamed:
if d.TargetAttributeName == "" {
return ""
}
message += fmt.Sprintf("Rename the `%s` attribute to `%s`", d.FromAttributeName, d.TargetAttributeName)
case d.Removed:
message += fmt.Sprintf("Remove the `%s` attribute configuration", d.FromAttributeName)
if d.TargetResourceName != "" {
if d.LinkToResourceDoc != "" {
message += fmt.Sprintf("as it replaced by the resource [`%s`](%s)", d.TargetResourceName, d.LinkToResourceDoc)
} else {
message += fmt.Sprintf("as it replaced by the resource `%s`", d.TargetResourceName)
}
}
default:
return ""
}
if d.LinkToMilestone != "" {
message += fmt.Sprintf(", it will be removed in the version [`%s`](%s) of the provider", d.TargetRelease, d.LinkToMilestone)
} else {
message += fmt.Sprintf(", it will be removed in the version `%s` of the provider", d.TargetRelease)
}
if d.LinkToIssue != "" {
message += fmt.Sprintf(". See the [GitHub issue](%s) for more information.", d.LinkToIssue)
}
return addEndDot(message)
}
// GetDeprecationMessage returns the deprecation message for the attribute.
func (d *Deprecated) GetDeprecationMessage() string {
return d.DeprecationMessage
}
// GetMarkdownDeprecationMessage returns the markdown deprecation message for the attribute.
func (d *Deprecated) GetMarkdownDeprecationMessage() string {
return d.computeDeprecatedDocumentation()
}
// DeprecatedResource is a struct to describe a deprecated resource or data source.
type DeprecatedResource struct {
// DeprecationMessage is the message to display in the CLI when the user
// attempts to use the deprecated resource.
// This field is required.
DeprecationMessage string
// MarkdownDeprecationMessage is the message to display in the Documentation portal
// when the user attempts to use the deprecated attribute.
// This field is required if ComputeMarkdownDeprecationMessage is false.
MarkdownDeprecationMessage string
// ComputeMarkdownDeprecationMessage is a flag to indicate whether the MarkdownDeprecationMessage
// should be computed from the parameters of the Deprecated struct.
ComputeMarkdownDeprecationMessage bool
// Renamed is a flag to indicate whether the resource or datasource has been renamed.
// Removed is a flag to indicate whether the resource or datasource has been removed.
// One of these fields must be true.
Renamed, Removed bool
// TargetResourceName is the name of the resource that replaces the deprecated resource or data source.
// These fields are required if the resource or data source has been renamed and computeMarkdownDeprecationMessage is true.
TargetResourceName string
// TargetRelease is the release version in which the resource or datasource was deprecated. (e.g. v1.0.0).
// This field is Required.
TargetRelease string
// LinkToIssue is the link to the GitHub issue that describes the deprecation.
// This field is optional.
LinkToIssue string
// LinkToMigrationGuide is the link to the actual resource documentation.
// This field is optional.
LinkToMigrationGuide string
// LinkToNewResourceDoc is the link to the terraform documentation for the resource that replaces the deprecated attribute.
// This field is optional.
LinkToNewResourceDoc string
// LinkToMilestone is the link to the GitHub milestone that describes the release in which the attribute was deprecated.
// This field is optional.
LinkToMilestone string
}
func (d *DeprecatedResource) computeDeprecatedDocumentation(isResource bool) string {
if (!d.ComputeMarkdownDeprecationMessage && d.MarkdownDeprecationMessage == "") || (d.Renamed && d.TargetResourceName == "" && d.ComputeMarkdownDeprecationMessage) || d.TargetRelease == "" {
return ""
}
if d.Removed && d.Renamed {
return ""
}
ressOrData := func() string {
if isResource {
return "resource"
}
return "data source"
}()
message := warningResourceDeprecated + d.MarkdownDeprecationMessage
if d.ComputeMarkdownDeprecationMessage {
switch {
case d.Renamed:
if d.TargetResourceName == "" {
return ""
}
if d.LinkToNewResourceDoc != "" {
message += fmt.Sprintf("The %s has renamed to [`%s`](%s)", ressOrData, d.TargetResourceName, d.LinkToNewResourceDoc)
} else {
message += fmt.Sprintf("The %s has renamed to `%s`", ressOrData, d.TargetResourceName)
}
case d.Removed:
message += fmt.Sprintf("The %s has been removed", ressOrData)
default:
return ""
}
}
if d.LinkToMilestone != "" {
message += fmt.Sprintf(", it will be removed in the version [`%s`](%s) of the provider", d.TargetRelease, d.LinkToMilestone)
} else {
message += fmt.Sprintf(", it will be removed in the version `%s` of the provider", d.TargetRelease)
}
if d.LinkToIssue != "" {
message += fmt.Sprintf(". See the [GitHub issue](%s) for more information.", d.LinkToIssue)
}
return addEndDot(message)
}
// GetDeprecationMessage returns the deprecation message for the attribute.
func (d *DeprecatedResource) GetDeprecationMessage() string {
if d.LinkToMigrationGuide != "" {
return fmt.Sprintf("%s. See the migration guide(%s) for more information.", d.DeprecationMessage, d.LinkToMigrationGuide)
}
return d.DeprecationMessage
}
// GetMarkdownDeprecationMessage returns the markdown deprecation message for the attribute.
func (d *DeprecatedResource) GetMarkdownDeprecationMessage(isResource bool) string {
return d.computeDeprecatedDocumentation(isResource)
}