Skip to content

Commit d8a52d8

Browse files
shanewxythxCode
authored andcommitted
refactor: generate schema
1 parent c2889fc commit d8a52d8

File tree

5 files changed

+154
-23
lines changed

5 files changed

+154
-23
lines changed

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ help:
1818
# Usage:
1919
#
2020
# * [dev] `make generate`, generate README file.
21-
# - `make generate examples/standalone` only generate README file under examples/standalone directory.
21+
# - `make generate examples/standalone` only generate docs and schema under examples/standalone directory.
22+
# - `make generate docs examples/standalone` only generate README file under examples/standalone directory.
23+
# - `make generate schema examples/standalone` only generate schema.yaml under examples/standalone directory.
2224
#
2325
# * [dev] `make lint`, check style and security.
2426
# - `LINT_DIRTY=true make lint` verify whether the code tree is dirty.
@@ -33,4 +35,4 @@ help:
3335

3436

3537
.DEFAULT_GOAL := ci
36-
.PHONY: $(targets) examples $(examples) modules $(modules) tests
38+
.PHONY: $(targets) examples $(examples) modules $(modules) tests docs schema

hack/generate.sh

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)"
88
source "${ROOT_DIR}/hack/lib/init.sh"
99

1010
function generate() {
11+
local mode="${1:-all}"
12+
13+
case $mode in
14+
docs)
15+
shift 1
16+
generate_doc "${ROOT_DIR}" "$@"
17+
;;
18+
schema)
19+
shift 1
20+
generate_schema "${ROOT_DIR}" "$@"
21+
;;
22+
*)
23+
generate_doc "${ROOT_DIR}" "$@"
24+
generate_schema "${ROOT_DIR}" "$@"
25+
;;
26+
esac
27+
}
28+
29+
function generate_doc() {
1130
local target="$1"
1231
shift 1
1332

@@ -23,7 +42,7 @@ function generate() {
2342
seal::log::warn "There is no Terraform files under ${path}"
2443
fi
2544
done
26-
45+
2746
return 0
2847
fi
2948

@@ -36,12 +55,41 @@ function generate() {
3655
done
3756
}
3857

58+
function generate_schema() {
59+
local target="$1"
60+
shift 1
61+
62+
if [[ $# > 0 ]]; then
63+
for subdir in "$@"; do
64+
local path="${target}/${subdir}"
65+
local tfs
66+
tfs=$(seal::util::find_files "${path}" "*.tf")
67+
68+
if [[ -n "${tfs}" ]]; then
69+
seal::walrus_cli::schema "${target}"
70+
else
71+
seal::log::warn "There is no Terraform files under ${path}"
72+
fi
73+
done
74+
75+
return 0
76+
fi
77+
78+
seal::walrus_cli::schema "${target}"
79+
local sub_modules=()
80+
# shellcheck disable=SC2086
81+
IFS=" " read -r -a sub_modules <<<"$(seal::util::find_subdirs ${target}/modules)"
82+
for module in "${sub_modules[@]}"; do
83+
seal::walrus_cli::schema "${target}/modules/${module}"
84+
done
85+
}
86+
3987
#
4088
# main
4189
#
4290

4391
seal::log::info "+++ GENERATE +++"
4492

45-
generate "${ROOT_DIR}" "$@"
93+
generate "$@"
4694

4795
seal::log::info "--- GENERATE ---"

hack/lib/terraform.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function seal::terraform::validate() {
8585
fi
8686

8787
seal::log::info "validating ${target} ..."
88-
$(seal::terraform::terraform::bin) -chdir="${target}" init "${extra_args[@]}" 1>/dev/null
88+
$(seal::terraform::terraform::bin) -chdir="${target}" init "${extra_args[@]}" -upgrade 1>/dev/null
8989
$(seal::terraform::terraform::bin) -chdir="${target}" validate "${extra_args[@]}" "$@"
9090
}
9191

@@ -104,7 +104,7 @@ function seal::terraform::test() {
104104
fi
105105

106106
seal::log::info "testing ${target} ..."
107-
$(seal::terraform::terraform::bin) -chdir="${target}" init "${extra_args[@]}" 1>/dev/null
107+
$(seal::terraform::terraform::bin) -chdir="${target}" init "${extra_args[@]}" -upgrade 1>/dev/null
108108
$(seal::terraform::terraform::bin) -chdir="${target}" test "${extra_args[@]}" "$@"
109109
}
110110

hack/lib/walrus-cli.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env bash
2+
3+
# -----------------------------------------------------------------------------
4+
# Schema helpers. These functions need the
5+
# following variables:
6+
#
7+
# WARUS_CLI_VERSION - The walrus cli version, default is latest.
8+
9+
walrus_cli_version=${WARUS_CLI_VERSION:-"latest"}
10+
11+
function seal::walrus_cli::install() {
12+
local os
13+
os=$(seal::util::get_os)
14+
local arch
15+
arch=$(seal::util::get_arch)
16+
17+
curl --retry 3 --retry-all-errors --retry-delay 3 \
18+
-o "${ROOT_DIR}/.sbin/walrus-cli" \
19+
-sSfL "https://walrus-cli-1303613262.cos.ap-guangzhou.myqcloud.com/releases/latest/walrus-cli-${os}-${arch}"
20+
21+
chmod a+x "${ROOT_DIR}/.sbin/walrus-cli"
22+
}
23+
24+
function seal::walrus_cli::validate() {
25+
local os
26+
os=$(seal::util::get_os)
27+
local arch
28+
arch=$(seal::util::get_arch)
29+
30+
# shellcheck disable=SC2046
31+
if [[ -n "$(command -v $(seal::walrus_cli::bin))" ]]; then
32+
if [[ "${walrus_cli_version}" == "latest" ]]; then
33+
local expected_md5sum
34+
expected_md5sum=$(curl --retry 3 --retry-all-errors --retry-delay 3 -IsSfL "https://walrus-cli-1303613262.cos.ap-guangzhou.myqcloud.com/releases/latest/walrus-cli-${os}-${arch}" | grep ETag | cut -d " " -f 2 | sed -e 's/"//g')
35+
local actual_md5sum
36+
actual_md5sum=$(md5sum "$(seal::walrus_cli::bin)" | cut -d " " -f 1)
37+
if [[ "${expected_md5sum}" == "${actual_md5sum}" ]]; then
38+
return 0
39+
fi
40+
return 0
41+
elif [[ $($(seal::walrus_cli::bin) --version 2>/dev/null | head -n 1 | cut -d " " -f 3 2>&1) == "${walrus_cli_version}" ]]; then
42+
return 0
43+
fi
44+
fi
45+
46+
seal::log::info "installing walrus-cli ${walrus_cli_version}"
47+
if seal::walrus_cli::install; then
48+
seal::log::info "walrus-cli $($(seal::walrus_cli::bin) --version 2>/dev/null | head -n 1)"
49+
return 0
50+
fi
51+
seal::log::error "no walrus-cli"
52+
return 1
53+
}
54+
55+
function seal::walrus_cli::bin() {
56+
local bin="walrus-cli"
57+
if [[ -f "${ROOT_DIR}/.sbin/walrus-cli" ]]; then
58+
bin="${ROOT_DIR}/.sbin/walrus-cli"
59+
fi
60+
echo -n "${bin}"
61+
}
62+
63+
function seal::walrus_cli::schema() {
64+
if ! seal::walrus_cli::validate; then
65+
seal::log::error "cannot execute walrus-cli as it hasn't installed"
66+
return 1
67+
fi
68+
69+
local target="$1"
70+
shift 1
71+
72+
seal::log::info "schema generating ${target} ..."
73+
$(seal::walrus_cli::bin) schema generate --dir="${target}" "$@" 1>/dev/null
74+
}

schema.yaml

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ info:
44
components:
55
schemas:
66
variables:
7+
type: object
78
required:
89
- infrastructure
910
- architecture
1011
- engine_version
11-
type: object
1212
properties:
1313
context:
1414
title: Context
@@ -35,26 +35,25 @@ components:
3535
- vpc_id
3636
properties:
3737
domain_suffix:
38-
title: Domain Suffix
39-
type: string
4038
description: |
4139
Specify the domain suffix of the CloudMap. The domain suffix must be a valid DNS name.
4240
nullable: true
41+
title: Domain Suffix
42+
type: string
4343
x-walrus-ui:
4444
order: 3
4545
kms_key_id:
46-
title: Kms Key Id
47-
type: string
4846
description: |
4947
Specify the KMS key ID which to encrypt the Redis data. If not specified, the Redis data will not be encrypted.
5048
nullable: true
49+
title: Kms Key Id
50+
type: string
5151
x-walrus-ui:
5252
order: 2
5353
vpc_id:
54+
description: "Specify the VPC ID where the Redis service applies. \n"
5455
title: Vpc Id
5556
type: string
56-
description: |
57-
Specify the VPC ID where the Redis service applies.
5857
x-walrus-ui:
5958
order: 1
6059
x-walrus-ui:
@@ -70,6 +69,7 @@ components:
7069
enum:
7170
- standalone
7271
- replication
72+
nullable: true
7373
x-walrus-ui:
7474
group: Basic
7575
order: 3
@@ -79,11 +79,11 @@ components:
7979
description: |
8080
Specify the number of read-only replicas under the replication deployment.
8181
default: 1
82-
nullable: true
8382
enum:
8483
- 1
8584
- 3
8685
- 5
86+
nullable: true
8787
x-walrus-ui:
8888
group: Basic
8989
order: 4
@@ -97,6 +97,7 @@ components:
9797
enum:
9898
- "7.0"
9999
- "6.0"
100+
nullable: true
100101
x-walrus-ui:
101102
group: Basic
102103
order: 5
@@ -106,37 +107,41 @@ components:
106107
description: |
107108
Specify the deployment parameters, see https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/ParameterGroups.Redis.html.
108109
default: []
109-
nullable: true
110110
items:
111111
properties:
112112
name:
113113
title: Name
114114
type: string
115+
x-walrus-ui:
116+
order: 1
115117
value:
116118
title: Value
117119
type: string
120+
x-walrus-ui:
121+
order: 2
118122
required:
119123
- name
120124
- value
121125
type: object
122126
x-walrus-ui:
123127
colSpan: 12
128+
nullable: true
124129
x-walrus-ui:
125130
colSpan: 12
126131
group: Basic
127-
order: 6
128132
hidden: true
133+
order: 6
129134
password:
130135
title: Password
131-
maxLength: 32
132-
minLength: 16
133136
type: string
134137
description: |
135138
Specify the account password. If not specified, it will generate a random password. The password must be 16-32 characters long and start with any letter, number, or the following symbols: ! # $ % ^ & * ( ) _ + - =.
136139
If not specified, it will generate a random password.
137-
nullable: true
138140
format: password
141+
nullable: true
139142
writeOnly: true
143+
minLength: 16
144+
maxLength: 32
140145
x-walrus-ui:
141146
group: Basic
142147
order: 7
@@ -150,17 +155,19 @@ components:
150155
nullable: true
151156
properties:
152157
class:
153-
title: Class
154-
type: string
158+
default: cache.t3.micro
155159
description: |
156160
Specify the computing resources. See https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/CacheNodes.SupportedTypes.html.
157-
default: cache.t3.micro
158161
nullable: true
162+
title: Class
163+
type: string
164+
x-walrus-ui:
165+
order: 1
159166
x-walrus-ui:
160167
colSpan: 12
161168
group: Basic
162169
order: 8
163170
x-walrus-ui:
164-
groupOrder:
171+
groupOrder:
165172
- Basic
166173
- Advanced

0 commit comments

Comments
 (0)