openapi.sh 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #!/bin/bash
  2. #
  3. # Copyright 2024 Apollo Authors
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # title openapi.sh
  18. # description functions to call openapi through http
  19. # author wxq
  20. # date 2021-09-12
  21. # Chinese reference website https://www.apolloconfig.com/#/zh/portal/apollo-open-api-platform
  22. # English reference website https://www.apolloconfig.com/#/en/portal/apollo-open-api-platform
  23. ####################################### Global variables #######################################
  24. # portal's address, just support 1 address without suffix '/'
  25. # Don't use http://ip:port/ with suffix '/' or multiple address http://ip1:port1,http://ip2:port2
  26. APOLLO_PORTAL_ADDRESS=${APOLLO_PORTAL_ADDRESS:-http://ip:port}
  27. APOLLO_OPENAPI_TOKEN=${APOLLO_OPENAPI_TOKEN:-please_change_me_by_environment_variable}
  28. CURL_OPTIONS=${CURL_OPTIONS:-}
  29. echo "apollo portal address: ${APOLLO_PORTAL_ADDRESS}"
  30. echo "curl options: ${CURL_OPTIONS}"
  31. ####################################### end of Global variables #######################################
  32. ####################################### basic http call #######################################
  33. #######################################
  34. # Http get by curl.
  35. # Globals:
  36. # APOLLO_PORTAL_ADDRESS: portal's address
  37. # APOLLO_OPENAPI_TOKEN: openapi's token
  38. # CURL_OPTIONS: options in curl
  39. # Arguments:
  40. # url_suffix
  41. #######################################
  42. function openapi_get() {
  43. local url_suffix=$1
  44. local url="${APOLLO_PORTAL_ADDRESS}/${url_suffix}"
  45. curl ${CURL_OPTIONS} --header "Authorization: ${APOLLO_OPENAPI_TOKEN}" --header "Content-Type: application/json;charset=UTF-8" "${url}"
  46. }
  47. #######################################
  48. # Http post by curl.
  49. # Globals:
  50. # APOLLO_PORTAL_ADDRESS: portal's address
  51. # APOLLO_OPENAPI_TOKEN: openapi's token
  52. # CURL_OPTIONS: options in curl
  53. # Arguments:
  54. # url_suffix
  55. # body
  56. #######################################
  57. function openapi_post() {
  58. local url_suffix=$1
  59. local body=$2
  60. local url="${APOLLO_PORTAL_ADDRESS}/${url_suffix}"
  61. curl ${CURL_OPTIONS} --header "Authorization: ${APOLLO_OPENAPI_TOKEN}" --header "Content-Type: application/json;charset=UTF-8" --data "${body}" "${url}"
  62. }
  63. #######################################
  64. # Http put by curl.
  65. # Globals:
  66. # APOLLO_PORTAL_ADDRESS: portal's address
  67. # APOLLO_OPENAPI_TOKEN: openapi's token
  68. # CURL_OPTIONS: options in curl
  69. # Arguments:
  70. # url_suffix
  71. # body
  72. #######################################
  73. function openapi_put() {
  74. local url_suffix=$1
  75. local body=$2
  76. local url="${APOLLO_PORTAL_ADDRESS}/${url_suffix}"
  77. curl ${CURL_OPTIONS} --header "Authorization: ${APOLLO_OPENAPI_TOKEN}" --header "Content-Type: application/json;charset=UTF-8" -X PUT --data "${body}" "${url}"
  78. }
  79. #######################################
  80. # Http delete by curl.
  81. # Globals:
  82. # APOLLO_PORTAL_ADDRESS: portal's address
  83. # APOLLO_OPENAPI_TOKEN: openapi's token
  84. # CURL_OPTIONS: options in curl
  85. # Arguments:
  86. # url_suffix
  87. # body
  88. #######################################
  89. function openapi_delete() {
  90. local url_suffix=$1
  91. local body=$2
  92. local url="${APOLLO_PORTAL_ADDRESS}/${url_suffix}"
  93. curl ${CURL_OPTIONS} --header "Authorization: ${APOLLO_OPENAPI_TOKEN}" --header "Content-Type: application/json;charset=UTF-8" -X DELETE --data "${body}" "${url}"
  94. }
  95. ####################################### end of basic http call #######################################
  96. ####################################### cluster #######################################
  97. #######################################
  98. # Get cluster.
  99. # 获取集群
  100. # Arguments:
  101. # env
  102. # appId
  103. # clusterName
  104. #######################################
  105. function cluster_get() {
  106. local env=$1
  107. local appId=$2
  108. local clusterName=$3
  109. openapi_get "openapi/v1/envs/${env}/apps/${appId}/clusters/${clusterName}"
  110. }
  111. #######################################
  112. # Create cluster in app's environment.
  113. # 创建集群
  114. # Arguments:
  115. # env
  116. # appId
  117. # clusterName
  118. # dataChangeCreatedBy
  119. #######################################
  120. function cluster_create() {
  121. local env=$1
  122. local appId=$2
  123. local clusterName=$3
  124. local dataChangeCreatedBy=$4
  125. openapi_post "openapi/v1/envs/${env}/apps/${appId}/clusters" "$(cat <<BODY
  126. {
  127. "name":"${clusterName}",
  128. "appId":"${appId}",
  129. "dataChangeCreatedBy":"${dataChangeCreatedBy}"
  130. }
  131. BODY
  132. )"
  133. }
  134. ####################################### end of cluster #######################################
  135. ####################################### namespace #######################################
  136. #######################################
  137. # Create a namespace of a app.
  138. # 创建namespace
  139. # Arguments:
  140. # appId
  141. # name
  142. # format
  143. # isPublic
  144. # comment
  145. # dataChangeCreatedBy
  146. #######################################
  147. function namespace_create() {
  148. local appId=$1
  149. local name=$2
  150. local format=$3
  151. local isPublic=$4
  152. local comment=$5
  153. local dataChangeCreatedBy=$6
  154. openapi_post "openapi/v1/apps/${appId}/appnamespaces" "$(cat <<BODY
  155. {
  156. "name": "${name}",
  157. "appId": "${appId}",
  158. "format": "${format}",
  159. "isPublic": ${isPublic},
  160. "comment": "${comment}",
  161. "dataChangeCreatedBy": "${dataChangeCreatedBy}"
  162. }
  163. BODY
  164. )"
  165. }
  166. #######################################
  167. # Release a namespace.
  168. # 发布配置
  169. # Arguments:
  170. # env
  171. # appId
  172. # clusterName
  173. # namespaceName
  174. # releaseTitle
  175. # releaseComment
  176. # releasedBy
  177. #######################################
  178. function namespace_release() {
  179. local env=$1
  180. local appId=$2
  181. local clusterName=$3
  182. local namespaceName=$4
  183. local releaseTitle=$5
  184. local releaseComment=$6
  185. local releasedBy=$7
  186. openapi_post "openapi/v1/envs/${env}/apps/${appId}/clusters/${clusterName}/namespaces/${namespaceName}/releases" "$(cat <<BODY
  187. {
  188. "releaseTitle":"${releaseTitle}",
  189. "releaseComment":"${releaseComment}",
  190. "releasedBy":"${releasedBy}"
  191. }
  192. BODY
  193. )"
  194. }
  195. ####################################### end of namespace #######################################
  196. ####################################### item #######################################
  197. #######################################
  198. # Create an item of a namespace.
  199. # 新增配置
  200. # Arguments:
  201. # env
  202. # appId
  203. # clusterName
  204. # namespaceName
  205. # key
  206. # value
  207. # comment
  208. # dataChangeCreatedBy
  209. #######################################
  210. function item_create() {
  211. local env=$1
  212. local appId=$2
  213. local clusterName=$3
  214. local namespaceName=$4
  215. local key=$5
  216. local value=$6
  217. local comment=$7
  218. local dataChangeCreatedBy=$8
  219. openapi_post "openapi/v1/envs/${env}/apps/${appId}/clusters/${clusterName}/namespaces/${namespaceName}/items" "$(cat <<BODY
  220. {
  221. "key":"${key}",
  222. "value":"${value}",
  223. "comment":"${comment}",
  224. "dataChangeCreatedBy":"${dataChangeCreatedBy}"
  225. }
  226. BODY
  227. )"
  228. }
  229. #######################################
  230. # Update an item of a namespace.
  231. # 修改配置
  232. # Arguments:
  233. # env
  234. # appId
  235. # clusterName
  236. # namespaceName
  237. # key
  238. # value
  239. # comment
  240. # dataChangeLastModifiedBy
  241. #######################################
  242. function item_update() {
  243. local env=$1
  244. local appId=$2
  245. local clusterName=$3
  246. local namespaceName=$4
  247. local key=$5
  248. local value=$6
  249. local comment=$7
  250. local dataChangeLastModifiedBy=$8
  251. openapi_put "openapi/v1/envs/${env}/apps/${appId}/clusters/${clusterName}/namespaces/${namespaceName}/items/${key}" "$(cat <<BODY
  252. {
  253. "key":"${key}",
  254. "value":"${value}",
  255. "comment":"${comment}",
  256. "dataChangeLastModifiedBy":"${dataChangeLastModifiedBy}"
  257. }
  258. BODY
  259. )"
  260. }
  261. #######################################
  262. # Update an item of a namespace, if item doesn't exist, create it.
  263. # 修改配置,当配置不存在时自动创建
  264. # Arguments:
  265. # env
  266. # appId
  267. # clusterName
  268. # namespaceName
  269. # key
  270. # value
  271. # comment
  272. # dataChangeLastModifiedBy
  273. #######################################
  274. function item_update_create_if_not_exists() {
  275. local env=$1
  276. local appId=$2
  277. local clusterName=$3
  278. local namespaceName=$4
  279. local key=$5
  280. local value=$6
  281. local comment=$7
  282. local dataChangeLastModifiedBy=$8
  283. local dataChangeCreatedBy=$9
  284. openapi_put "openapi/v1/envs/${env}/apps/${appId}/clusters/${clusterName}/namespaces/${namespaceName}/items/${key}?createIfNotExists=true" "$(cat <<BODY
  285. {
  286. "key":"${key}",
  287. "value":"${value}",
  288. "comment":"${comment}",
  289. "dataChangeLastModifiedBy":"${dataChangeLastModifiedBy}",
  290. "dataChangeCreatedBy":"${dataChangeCreatedBy}"
  291. }
  292. BODY
  293. )"
  294. }
  295. #######################################
  296. # Delete an item of a namespace.
  297. # 删除配置
  298. # Arguments:
  299. # env
  300. # appId
  301. # clusterName
  302. # namespaceName
  303. # key
  304. # operator
  305. #######################################
  306. function item_delete() {
  307. local env=$1
  308. local appId=$2
  309. local clusterName=$3
  310. local namespaceName=$4
  311. local key=$5
  312. local operator=$6
  313. openapi_delete "openapi/v1/envs/${env}/apps/${appId}/clusters/${clusterName}/namespaces/${namespaceName}/items/${key}?operator=${operator}"
  314. }
  315. ####################################### end of item #######################################