xref: /aosp_15_r20/external/google-cloud-java/.cloud/helpers/generate-config.sh (revision 55e87721aa1bc457b326496a7ca40f3ea1a63287)
1#
2# Copyright 2022 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      https://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16set -eo pipefail
17
18function initializeGeneratedFiles() {
19  cp ./helpers/generated-main.template.tf generated-main.tf
20  cp ./helpers/generated-outputs.template.tf generated-outputs.tf
21  cp ./helpers/generated-variables.template.tf generated-variables.tf
22}
23
24function appendModule() {
25  friendlyName=$(getFriendlyOutputName "$1")
26
27  # Append the given module to the generated-main.tf configuration to be
28  # included in the project's resources during 'terraform apply'.
29  echo "module \"$friendlyName\" {
30    source = \"./../$1/.cloud\"
31    inputs = local.data
32    depends_on = [time_sleep.for_1m_allowBaseCloudApisToFullyEnable]
33  }" >>generated-main.tf
34
35  # Append the given module to the generated-output.tf file to provide
36  # all of this module's outputs as an object.
37  # See https://www.terraform.io/cli/commands/output
38  echo "output \"$friendlyName\" {
39    value = module.$friendlyName
40    sensitive = true
41  }" >>generated-outputs.tf
42}
43
44function appendAllModules() {
45  # Either use given module list, or get a list of all modules in the parent directory.
46  if [ -n "$1" ]; then
47    modules=$1
48  else
49    modules=$(listAllModules)
50  fi
51  IFS=','
52  for module in $modules; do
53    # Only include modules with a .cloud subdirectory in the generated config.
54    if [ -d "../$module/.cloud" ]; then
55      appendModule "${module%/}" # Remove possible trailing '/'
56    fi
57  done
58}
59
60# Ensure current directory is <root>/.cloud
61generateDir="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
62pushd "$generateDir/.." >/dev/null
63
64source ./helpers/common.sh
65initializeGeneratedFiles
66appendAllModules "$1"
67
68popd >/dev/null
69