-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessTf.nix
More file actions
80 lines (72 loc) · 2.22 KB
/
processTf.nix
File metadata and controls
80 lines (72 loc) · 2.22 KB
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
lib: pkgs:
let
inherit (lib.attrsets)
filterAttrs
optionalAttrs
mapAttrs
mapAttrsToList
;
inherit (lib.trivial) isString;
inherit (lib.strings) concatStringsSep concatMapStringsSep;
inherit (lib.lists) join groupBy' foldl';
notRefTo = v: to: !(v ? _type) || v._type != "tfRef" || v.ref != to;
strAttrInner =
v:
if v._type == "tfRef" then
concatStringsSep "." v.ref
else if
v._type == "tfCall"
# TODO: Some params might not be strings?..
then
"${v.fn}(${concatMapStringsSep ", " strAttrInner v.params})"
else
throw "idk: ${v._type}";
strAttr = attr: if attr ? _type then "\${${strAttrInner attr}}" else attr;
processResource =
sectName: groupName: resName: resource:
mapAttrs (param: val: strAttr val) (
filterAttrs (
attrName: v:
notRefTo v [
sectName
groupName
resName
attrName
]
) resource
);
processGroup =
sectName: groupName: group:
mapAttrs (processResource sectName groupName) group;
processSection =
sectName: section: mapAttrs (processGroup sectName) (filterAttrs (_: v: v != { }) section);
filterEmptyListGroup = filterAttrs (_: v: v != [ ]);
processTf =
conf:
let
tfUnwrapped = conf.package pkgs;
tfPlugins = tfUnwrapped.plugins;
data = processSection "data" conf.data;
resource = processSection "resource" conf.resource;
provider = filterEmptyListGroup (
mapAttrs (
_: p:
let
aliases' = removeAttrs p [ "package" ];
in
mapAttrsToList (
alias: conf: conf // (optionalAttrs (alias != "default") { inherit alias; })
) aliases'
) conf.providers
);
required_providers = mapAttrs (_: p: {
source = (p.package tfPlugins).provider-source-address;
}) conf.providers;
terraform = optionalAttrs (required_providers != { }) { inherit required_providers; };
in
optionalAttrs (data != { }) { inherit data; }
// optionalAttrs (resource != { }) { inherit resource; }
// optionalAttrs (provider != { }) { inherit provider; }
// optionalAttrs (terraform != { }) { inherit terraform; };
in
processTf