/
data.go
71 lines (62 loc) · 1.37 KB
/
data.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
// Copyright 2023 Volvo Car Corporation
// SPDX-License-Identifier: Apache-2.0
package terrajen
import (
"fmt"
"github.com/dave/jennifer/jen"
)
// DataSourceFile generates a Go file for a Terraform data source configuration
// based on the given
// Schema
func DataSourceFile(s *Schema) *jen.File {
f := jen.NewFile(s.PackageName)
f.ImportAlias(pkgHCL, "hcl")
f.ImportName(pkgTerra, pkgTerraAlias)
f.HeaderComment(HeaderComment)
f.Add(dataStructCompileCheck(s))
f.Add(dataStruct(s))
f.Add(argsStruct(s))
f.Add(attributesStruct(s))
return f
}
func dataStructCompileCheck(s *Schema) *jen.Statement {
return jen.Var().Op("_").Qual(pkgTerra, "DataSource").Op("=").
Params(
jen.Op("*").Id(s.StructName),
).
Params(jen.Nil()).
Line()
}
func dataStruct(s *Schema) *jen.Statement {
stmt := jen.Comment(
fmt.Sprintf(
"%s represents the Terraform data resource %s.",
s.StructName,
s.Type,
),
).
Line().
Type().Id(s.StructName).Struct(
jen.Id(idFieldName).String(),
jen.Id(idFieldArgs).Id(s.ArgumentStructName),
)
stmt.Line()
stmt.Line()
// DataSource
stmt.Add(funcSchemaType(s, "DataSource"))
stmt.Line()
stmt.Line()
// LocalName
stmt.Add(funcLocalName(s))
stmt.Line()
stmt.Line()
// Configuration
stmt.Add(funcConfiguration(s))
stmt.Line()
stmt.Line()
// Attributes
stmt.Add(funcAttributes(s))
stmt.Line()
stmt.Line()
return stmt
}