-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgenerate.sh
More file actions
executable file
·119 lines (101 loc) · 4.81 KB
/
Copy pathgenerate.sh
File metadata and controls
executable file
·119 lines (101 loc) · 4.81 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
# Copyright (c) 2024 Cisco Systems, Inc. and its affiliates
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0
set -ex
export PATH=$PATH:$GOPATH/bin
OUTDIR=${1:-"./internal/provider"}
REPLACE=${2}
PROVIDER="terraform-provider-nd"
mkdir -p $OUTDIR
if [[ ! -f $GOPATH/bin/tfplugingen-framework ]]
then
go install github.com/hashicorp/terraform-plugin-codegen-framework/cmd/tfplugingen-framework@latest
fi
if [[ ! -f $GOPATH/bin/generator ]]
then
echo "Compile and install generator"
exit 1
fi
if [[ ! -f $GOPATH/bin/addlicense ]]
then
echo "Compile and install addlicense tool"
exit 1
fi
mkdir -p ./out
rm -rf ./out/*
# Stage 1: Generate spec Json used for TF plugin generator framework
echo "Stage 1 - Generating provider spec JSON"
$GOPATH/bin/generator spec -in ./generator/defs -out ./out -split
# Stage 2: Use TF plugin generator framrework to generate Resource/Provider/Datasource definitions
# Use Stage 1 output as input here
echo "Stage 2 - Generating TF provider definitions from spec JSON"
CATEGORY_FILE="./out/category.json"
FILES=$(ls ./out)
for filename in $FILES; do
# Skip category.json and non-json files
if [[ $filename == "category.json" ]]; then
continue
fi
if [[ $filename == *"provider.json" ]]
then
echo "Generate provider code from $filename"
tfplugingen-framework generate provider --input ./out/$filename --output internal/provider --package provider
elif [[ $filename == *"resource.json" ]]
then
# Extract resource name: provider_spec_<name>_resource.json -> <name>
RSC_NAME="${filename#provider_spec_}"
RSC_NAME="${RSC_NAME%_resource.json}"
RSC_OUTDIR="$OUTDIR"
if [[ -f "$CATEGORY_FILE" ]]; then
CATEGORY=$(jq -r --arg name "$RSC_NAME" '.[$name] // empty' "$CATEGORY_FILE")
if [[ -n "$CATEGORY" ]]; then
RSC_OUTDIR="$OUTDIR/${CATEGORY}"
fi
fi
echo "Generate Resource code from $filename (resource=$RSC_NAME, output=$RSC_OUTDIR)"
tfplugingen-framework generate resources --input ./out/$filename --output $RSC_OUTDIR
elif [[ $filename == *"datasource.json" ]]
then
# Extract datasource name: provider_spec_<name>_datasource.json -> <name>
DS_NAME="${filename#provider_spec_}"
DS_NAME="${DS_NAME%_datasource.json}"
DS_OUTDIR="$OUTDIR"
if [[ -f "$CATEGORY_FILE" ]]; then
CATEGORY=$(jq -r --arg name "$DS_NAME" '.[$name] // empty' "$CATEGORY_FILE")
if [[ -n "$CATEGORY" ]]; then
DS_OUTDIR="$OUTDIR/${CATEGORY}"
fi
fi
echo "Generate data-source code from $filename (datasource=$DS_NAME, output=$DS_OUTDIR)"
tfplugingen-framework generate data-sources --input ./out/$filename --output $DS_OUTDIR
fi
done
#Stage 3: Generate additional encoder/decoder code for the generated definitions
#Generated in the same output folder of Stage 2
echo "Stage 3 - Generating additional codec code"
$GOPATH/bin/generator code -in generator/defs -template generator/templates -out $OUTDIR -provider ${PROVIDER} --types internal/common $REPLACE
# Add license headers to all generated files
$GOPATH/bin/addlicense -c "Cisco Systems, Inc. and its affiliates" -l "mpl" -s internal/provider/resources/**/*_gen.go
$GOPATH/bin/addlicense -c "Cisco Systems, Inc. and its affiliates" -l "mpl" -s internal/manage/**/*_gen.go
$GOPATH/bin/addlicense -c "Cisco Systems, Inc. and its affiliates" -l "mpl" -s internal/provider/resources/**/*_test.go
$GOPATH/bin/addlicense -c "Cisco Systems, Inc. and its affiliates" -l "mpl" -s internal/provider/provider/**/*_gen.go
$GOPATH/bin/addlicense -c "Cisco Systems, Inc. and its affiliates" -l "mpl" -s internal/provider/datasources/**/*_gen.go
$GOPATH/bin/addlicense -c "Cisco Systems, Inc. and its affiliates" -l "mpl" -s internal/provider/datasources/**/*_test.go
$GOPATH/bin/addlicense -c "Cisco Systems, Inc. and its affiliates" -l "mpl" -s internal/provider/types/
$GOPATH/bin/addlicense -c "Cisco Systems, Inc. and its affiliates" -l "mpl" -s internal/provider/*_test.go
$GOPATH/bin/addlicense -c "Cisco Systems, Inc. and its affiliates" -l "mpl" -s internal/manage/**/*_gen.go
$GOPATH/bin/addlicense -c "Cisco Systems, Inc. and its affiliates" -l "mpl" -s internal/provider/resources/**/*_test.go
$GOPATH/bin/addlicense -c "Cisco Systems, Inc. and its affiliates" -l "mpl" -s internal/provider/datasources/**/*_test.go
terraform fmt -recursive examples/
if [[ -f $GOPATH/bin/tfplugindocs ]]
then
$GOPATH/bin/tfplugindocs generate
else
go install github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs@latest
fi
rm -rf ./out