This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
/
data.go
99 lines (89 loc) · 1.9 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
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
// 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(dataNewFunc(s))
f.Add(dataStructCompileCheck(s))
f.Add(dataStruct(s))
f.Add(argsStruct(s))
f.Add(attributesStruct(s))
return f
}
func dataNewFunc(s *Schema) *jen.Statement {
return jen.Comment(
fmt.Sprintf(
"%s creates a new instance of [%s].",
s.NewFuncName,
s.StructName,
),
).
Line().
Func().Id(s.NewFuncName).Params(
jen.Id("name").String(),
jen.Id("args").Id(s.ArgumentStructName),
).
// Return
Op("*").Id(s.StructName).
// Block
Block(
jen.Return(
jen.Op("&").Id(s.StructName).Values(
jen.Dict{
jen.Id(idFieldName): jen.Id("name"),
jen.Id(idFieldArgs): jen.Id("args"),
},
),
),
)
}
func dataStructCompileCheck(s *Schema) *jen.Statement {
return jen.Var().Op("_").Qual(pkgTerra, "DataResource").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
}