This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
/
provider.go
76 lines (67 loc) · 1.52 KB
/
provider.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
// Copyright 2023 Volvo Car Corporation
// SPDX-License-Identifier: Apache-2.0
package terrajen
import (
"github.com/dave/jennifer/jen"
)
// ProviderFile generates a Go file for a Terraform provider configuration based on the given Schema
func ProviderFile(s *Schema) *jen.File {
f := jen.NewFile(s.ProviderName)
f.ImportName(pkgTerra, pkgTerraAlias)
f.HeaderComment(HeaderComment)
f.Add(providerNewFunc(s))
f.Add(providerStructCompileCheck(s))
f.Add(providerStruct(s))
f.Add(argsStruct(s))
return f
}
func providerNewFunc(s *Schema) *jen.Statement {
return jen.Func().Id(s.NewFuncName).Params(
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(idFieldArgs): jen.Id("args"),
},
),
),
)
}
func providerStructCompileCheck(s *Schema) *jen.Statement {
return jen.Var().Op("_").Qual(pkgTerra, "Provider").Op("=").
Params(
jen.Op("*").Id(s.StructName),
).
Params(jen.Nil()).
Line()
}
func providerStruct(s *Schema) *jen.Statement {
stmt := jen.Type().Id(s.StructName).Struct(
jen.Id(idFieldArgs).Id(s.ArgumentStructName),
jen.Line(),
)
stmt.Line()
stmt.Line()
// LocalName
stmt.Add(funcProviderLocalName(s))
stmt.Line()
stmt.Line()
// Source
stmt.Add(funcProviderSource(s))
stmt.Line()
stmt.Line()
// Version
stmt.Add(funcProviderVersion(s))
stmt.Line()
stmt.Line()
// Configuration
stmt.Add(funcConfiguration(s))
stmt.Line()
stmt.Line()
return stmt
}