|
5 | 5 | "fmt" |
6 | 6 | "io/fs" |
7 | 7 | "maps" |
| 8 | + "os" |
8 | 9 | "path/filepath" |
9 | 10 | "regexp" |
10 | 11 | "runtime" |
@@ -1125,20 +1126,26 @@ func (spec *Normalization) ResourceXpathVariablesCount() int { |
1125 | 1126 | return len(spec.PanosXpath.Variables) |
1126 | 1127 | } |
1127 | 1128 |
|
1128 | | -const attributesFromXpathComponentsTmpl = ` |
1129 | | -{{- range .Components }} |
1130 | | - {{ if eq .Type "value" }} |
1131 | | - {{ $.Target }}.{{ .Name }} = types.StringValue(components[{{ .Idx }}]) |
1132 | | - {{- else if eq .Type "entry" }} |
1133 | | -{ |
1134 | | - component := components[{{ .Idx }}] |
1135 | | - component = strings.TrimPrefix(component, "entry[@name='") |
1136 | | - component = strings.TrimSuffix(component, "']") |
1137 | | - {{ $.Target }}.{{ .Name.CamelCase }} = types.StringValue(component) |
| 1129 | +// loadTemplate loads a template from either sdk or terraform-provider templates directory |
| 1130 | +func loadTemplate(templateType string, templatePath string) (string, error) { |
| 1131 | + fullPath := filepath.Join("templates", templateType, templatePath) |
| 1132 | + content, err := os.ReadFile(fullPath) |
| 1133 | + if err != nil { |
| 1134 | + // Try from parent directories (for when running from subdirectories) |
| 1135 | + for i := 1; i <= 3; i++ { |
| 1136 | + prefix := strings.Repeat("../", i) |
| 1137 | + altPath := filepath.Join(prefix, "templates", templateType, templatePath) |
| 1138 | + content, err = os.ReadFile(altPath) |
| 1139 | + if err == nil { |
| 1140 | + break |
| 1141 | + } |
| 1142 | + } |
| 1143 | + if err != nil { |
| 1144 | + return "", fmt.Errorf("failed to read template %s: %w", fullPath, err) |
| 1145 | + } |
| 1146 | + } |
| 1147 | + return string(content), nil |
1138 | 1148 | } |
1139 | | - {{- end }} |
1140 | | -{{- end }} |
1141 | | -` |
1142 | 1149 |
|
1143 | 1150 | func (spec *Normalization) AttributesFromXpathComponents(target string) (string, error) { |
1144 | 1151 | type componentCtx struct { |
@@ -1175,66 +1182,43 @@ func (spec *Normalization) AttributesFromXpathComponents(target string) (string, |
1175 | 1182 | Components: components, |
1176 | 1183 | } |
1177 | 1184 |
|
1178 | | - tmpl := template.Must(template.New("attributes-from-xpath-components").Parse(attributesFromXpathComponentsTmpl)) |
| 1185 | + tmplContent, err := loadTemplate("terraform-provider", "partials/attributes_from_xpath_components.tmpl") |
| 1186 | + if err != nil { |
| 1187 | + return "", err |
| 1188 | + } |
| 1189 | + |
| 1190 | + tmpl := template.Must(template.New("attributes-from-xpath-components").Parse(tmplContent)) |
1179 | 1191 |
|
1180 | 1192 | var buf bytes.Buffer |
1181 | | - err := tmpl.Execute(&buf, data) |
| 1193 | + err = tmpl.Execute(&buf, data) |
1182 | 1194 | if err != nil { |
1183 | 1195 | panic(err) |
1184 | 1196 | } |
1185 | 1197 |
|
1186 | 1198 | return buf.String(), nil |
1187 | 1199 | } |
1188 | 1200 |
|
1189 | | -const resourceXpathAssignmentsTmpl = ` |
1190 | | -{{- range .Variables }} |
1191 | | - {{- if or (eq .Type "entry") (eq .Type "value") }} |
1192 | | - ans = append(ans, components[{{ .Idx }}]) |
1193 | | - {{- else if eq .Type "static" }} |
1194 | | - ans = append(ans, "{{ .Name.Original }}") |
1195 | | - {{- end }} |
1196 | | -{{- end }} |
1197 | | -` |
1198 | | - |
1199 | 1201 | func (spec *Normalization) ResourceXpathAssignments() (string, error) { |
1200 | 1202 | data := xpathVariablesCtx{ |
1201 | 1203 | Variables: createXpathVariablesContext(spec), |
1202 | 1204 | } |
1203 | 1205 |
|
1204 | | - tmpl := template.Must(template.New("resource-xpath-assignments").Parse(resourceXpathAssignmentsTmpl)) |
| 1206 | + tmplContent, err := loadTemplate("sdk", "partials/resource_xpath_assignments.tmpl") |
| 1207 | + if err != nil { |
| 1208 | + return "", err |
| 1209 | + } |
| 1210 | + |
| 1211 | + tmpl := template.Must(template.New("resource-xpath-assignments").Parse(tmplContent)) |
1205 | 1212 |
|
1206 | 1213 | var buf bytes.Buffer |
1207 | | - err := tmpl.Execute(&buf, data) |
| 1214 | + err = tmpl.Execute(&buf, data) |
1208 | 1215 | if err != nil { |
1209 | 1216 | panic(err) |
1210 | 1217 | } |
1211 | 1218 |
|
1212 | 1219 | return buf.String(), nil |
1213 | 1220 | } |
1214 | 1221 |
|
1215 | | -const xpathVariableChecksTmpl = ` |
1216 | | -{{- range .Variables }} |
1217 | | - {{- if eq .Type "entry" }} |
1218 | | -{ |
1219 | | - component := components[{{ .Idx }}] |
1220 | | - {{- if .AllowEmpty }} |
1221 | | - if component != "entry" { |
1222 | | - {{- end }} |
1223 | | - if !strings.HasPrefix(component, "entry[@name=\"]") && !strings.HasPrefix(component, "entry[@name='") { |
1224 | | - return nil, errors.NewInvalidXpathComponentError(fmt.Sprintf("{{ .Name.CamelCase }} must be formatted as entry: %s", component)) |
1225 | | - } |
1226 | | -
|
1227 | | - if !strings.HasSuffix(component, "\"]") && !strings.HasSuffix(component, "']") { |
1228 | | - return nil, errors.NewInvalidXpathComponentError(fmt.Sprintf("{{ .Name.CamelCase }} must be formatted as entry: %s", component)) |
1229 | | - } |
1230 | | - {{- if .AllowEmpty }} |
1231 | | - } |
1232 | | - {{- end }} |
1233 | | -} |
1234 | | - {{- end }} |
1235 | | -{{- end }} |
1236 | | -` |
1237 | | - |
1238 | 1222 | type xpathVariableCtx struct { |
1239 | 1223 | Type object.PanosXpathVariableType |
1240 | 1224 | Name *NameVariant |
@@ -1299,10 +1283,15 @@ func (spec *Normalization) ResourceXpathVariableChecks() (string, error) { |
1299 | 1283 | Variables: createXpathVariablesContext(spec), |
1300 | 1284 | } |
1301 | 1285 |
|
| 1286 | + tmplContent, err := loadTemplate("sdk", "partials/xpath_variable_checks.tmpl") |
| 1287 | + if err != nil { |
| 1288 | + return "", err |
| 1289 | + } |
| 1290 | + |
1302 | 1291 | var buf bytes.Buffer |
1303 | | - tmpl := template.Must(template.New("xpath-variable-checks").Parse(xpathVariableChecksTmpl)) |
| 1292 | + tmpl := template.Must(template.New("xpath-variable-checks").Parse(tmplContent)) |
1304 | 1293 |
|
1305 | | - err := tmpl.Execute(&buf, data) |
| 1294 | + err = tmpl.Execute(&buf, data) |
1306 | 1295 | if err != nil { |
1307 | 1296 | panic(err) |
1308 | 1297 | } |
|
0 commit comments