forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-gradually-deprecated.sh
executable file
·163 lines (151 loc) · 6.54 KB
/
run-gradually-deprecated.sh
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
#!/usr/bin/env bash
function runGraduallyDeprecatedFunctions {
echo "==> Checking for use of gradually deprecated functions..."
IFS=$'\n' read -r -d '' -a flist < <(git diff --diff-filter=AMRC origin/main --name-only)
for f in "${flist[@]}"; do
# require resources to be imported is now hard-coded on - but only checking for additions
grep -H -n "features\.ShouldResourcesBeImported" "$f" && {
echo "The Feature Flag for 'ShouldResourcesBeImported' will be deprecated in the future"
echo "and shouldn't be used in new resources - please remove new usages of the"
echo "'ShouldResourcesBeImported' function from these changes - since this is now enabled"
echo "by default."
echo ""
echo "In the future this function will be marked as Deprecated - however it's not for"
echo "the moment to not conflict with open Pull Requests."
exit 1
}
# using Resource ID Formatters/Parsers
grep -H -n "d\.SetId(\\*" "$f" && {
echo "Due to the Azure API returning the Resource ID's inconsistently - Terraform"
echo "now manages it's own Resource ID's, all new resources should use a generated"
echo "Resource ID Formatter and Parser."
echo ""
echo "A Resource ID Formatter and Parser can be generated by adding a 'resourceids.go'"
echo "file to the service package (for example"
echo "./internal/services/myservice/resourceids.go) - with the line:"
echo
echo "//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Server"
echo "-id={the value of the Resource ID}"
echo ""
echo "At which point running 'make generate' will generate a Resource ID Formatter, Parser"
echo "and Validator."
echo ""
echo "This allows a Resource ID to be defined in-line via:"
echo " > subscriptionId := meta.(*clients.Client).Account.SubscriptionId"
echo " > id := parse.NewMyResourceId(subscriptionId, resourceGroup, name)"
echo ""
echo "This means that the 'SetID' function can change from:"
echo " > d.SetId(\"*read.ID\")"
echo "to:"
echo " > d.SetId(id.ID())"
echo ""
echo "In addition when parsing the Resource ID during a Read, Update or Delete method"
echo "the generated Resource ID Parser can be used via:"
echo " > id, err := parse.MyResourceID(d.Id())"
echo " > if err != nil {"
echo " > return err"
echo " > }"
echo ""
echo "New Resources should be using Resource ID Formatters/Parsers by default"
echo "however existing (unmodified) resources can continue to use the Azure ID"
echo "for the moment - but over time these will be switched across."
exit 1
}
# check for new combined CreateUpdate methods
line=$(grep -H -n "Create:.*CreateUpdate," "$f" -m1)
if [ "$line" != "" ];
then
git diff --diff-filter=AMC origin/main -U0 "$f" | grep -q "+.*Create:.*CreateUpdate," && {
echo "$line"
echo "New Resources should no longer use combined CreateUpdate methods, please"
echo "split these into two separate Create and Update methods."
echo ""
echo "Existing resources can continue to use combined CreateUpdate methods"
echo "for the moment - but over time these will be split into separate Create and"
echo "Update methods."
exit 1
}
fi
# avoid false positives
isThisScript=$(echo "$f" | grep "run-gradually-deprecated")
if [ "$isThisScript" == "" ];
then
# check for d.Get inside Delete
deleteFuncName=$(grep -o "Delete: .*," "$f" -m1 | grep -o " .*Delete"| tr -d " ")
if [ "$deleteFuncName" != "" ];
then
deleteMethod=$(cat -n $f | sed -n -e "/func $deleteFuncName.*$/,/[[:digit:]]*\treturn nil/{ /func $deleteFuncName$/d; /[[:digit:]]*\treturn nil/d; p; }")
foundGet=$(echo "$deleteMethod" | grep "d\.Get(.*)" -m1)
if [ "$foundGet" != "" ];
then
echo "$f $foundGet"
echo "Please do not use 'd.Get' within the Delete function as this does not work as expected in Delete"
exit 1
fi
else
# check for Get in typed resource
deleteFuncName=" Delete() sdk.ResourceFunc "
deleteMethod=$(cat -n $f | sed -n -e "/$deleteFuncName.*$/,/[[:digit:]]*\t\t\treturn nil/{ /$deleteFuncName.*$/d; /[[:digit:]]*\t\t\treturn nil/d; p; }")
foundGet=$(echo "$deleteMethod" | grep "metadata.ResourceData.Get" -m1)
if [ "$foundGet" != "" ];
then
echo "$f $foundGet"
echo "Please do not use 'metadata.ResourceData.Get' within the Delete function as this does not work as expected in Delete"
exit 1
fi
fi
fi
done
}
function checkForUnclearErrorMessages {
result=$(grep -R "invalid format of " ./internal)
if [ "$result" != "" ];
then
echo "The error messages in these files aren't descriptive, please add more"
echo "context for how users can fix these. For example, changing \"invalid"
echo "format of 'foo'\" can clearer as \"'foo' must start with letter, can"
echo "contain both letters and numbers and must end with a letter\"."
echo ""
echo "$result"
exit 1
fi
}
function runDeprecatedFunctions {
echo "==> Checking for use of deprecated functions..."
result=$(grep -Ril "d.setid(\"\")" ./internal/services/**/*data_source*.go)
if [ "$result" != "" ];
then
echo "Data Sources should return an error when a resource cannot be found rather than"
echo "setting an empty ID (by calling 'd.SetId("")'."
echo ""
echo "Please remove the references to 'd.SetId("") from the Data Sources listed below"
echo "and raise an error instead:"
echo ""
echo "$result"
exit 1
fi
result=$(grep -Ril "markasgone" ./internal/services/**/*data_source*.go)
if [ "$result" != "" ];
then
echo "Data Sources should return an error when a resource cannot be found rather than"
echo "marking the resource as gone."
echo ""
echo "Please remove the references to 'metadata.MarkAsGone' from the Data Sources listed below"
echo "and raise an error instead:"
echo ""
echo "$result"
exit 1
fi
}
function main {
if [ "$GITHUB_ACTIONS_STAGE" == "UNIT_TESTS" ];
then
echo "Skipping - the Gradually Deprecated check is separate in Github Actions"
echo "so this can be skipped as a part of the build process."
exit 0
fi
runGraduallyDeprecatedFunctions
runDeprecatedFunctions
checkForUnclearErrorMessages
}
main