openapi-usage-example.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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-usage-example.sh
  18. # description show how to use openapi.sh
  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. # export global variables
  24. export APOLLO_PORTAL_ADDRESS=http://106.54.227.205
  25. export APOLLO_OPENAPI_TOKEN=284fe833cbaeecf2764801aa73965080b184fc88
  26. export CURL_OPTIONS=""
  27. # load functions
  28. source openapi.sh
  29. # set up global environment variable
  30. APOLLO_APP_ID=openapi
  31. APOLLO_ENV=DEV
  32. APOLLO_CLUSTER=default
  33. APOLLO_USER=apollo
  34. ####################################### cluster #######################################
  35. # get cluster
  36. printf "get cluster: env = '%s', app id = '%s', cluster = '%s'\n" ${APOLLO_ENV} ${APOLLO_APP_ID} ${APOLLO_CLUSTER}
  37. cluster_get ${APOLLO_ENV} ${APOLLO_APP_ID} ${APOLLO_CLUSTER}
  38. printf "\n\n"
  39. # create cluster. To forbid cluster xxx already exists, add timestamp to suffix
  40. temp_apollo_cluster="cluster-$(date +%s)"
  41. printf "create cluster: env = '%s', app id = '%s', cluster = '%s'\n" ${APOLLO_ENV} ${APOLLO_APP_ID} ${temp_apollo_cluster}
  42. cluster_create ${APOLLO_ENV} ${APOLLO_APP_ID} ${temp_apollo_cluster} ${APOLLO_USER}
  43. printf "\n\n"
  44. ####################################### end of cluster #######################################
  45. ####################################### namespace #######################################
  46. # create namespace
  47. temp_namespace_name="application-123"
  48. temp_namespace_format=yaml
  49. echo "create namespace: namespace name = '${temp_namespace_name}', app id = '${APOLLO_APP_ID}', format = '${temp_namespace_format}'"
  50. namespace_create ${APOLLO_APP_ID} ${temp_namespace_name} ${temp_format} false 'create by openapi, bash scripts' ${APOLLO_USER}
  51. printf "\n\n"
  52. ####################################### end of namespace #######################################
  53. ####################################### item #######################################
  54. # create an item, i.e a key value pair
  55. temp_item_key="openapi-usage-create-item-key-$(date +%s)"
  56. temp_item_value="openapi-usage-create-item-value-$(date +%s)"
  57. echo -e "create item: app id = ${APOLLO_APP_ID} env = ${APOLLO_ENV} key = ${temp_item_key} value = ${temp_item_value}"
  58. item_create ${APOLLO_ENV} ${APOLLO_APP_ID} default application ${temp_item_key} ${temp_item_value} "openapi-create-item" ${APOLLO_USER}
  59. printf "\n\n"
  60. # update an item
  61. echo "show update failed when item key not exists"
  62. sleep 1
  63. temp_item_key="openapi-usage-update-item-key-$(date +%s)"
  64. temp_item_value="openapi-usage-update-item-value-$(date +%s)"
  65. item_update ${APOLLO_ENV} ${APOLLO_APP_ID} default application ${temp_item_key} ${temp_item_value} "openapi-update-item" ${APOLLO_USER}
  66. printf "\n\n"
  67. echo "show after created, update success"
  68. item_create ${APOLLO_ENV} ${APOLLO_APP_ID} default application ${temp_item_key} ${temp_item_value} "openapi-create-item" ${APOLLO_USER}
  69. temp_item_value="item-update-success"
  70. printf "\n"
  71. item_update ${APOLLO_ENV} ${APOLLO_APP_ID} default application ${temp_item_key} ${temp_item_value} "openapi-update-item" ${APOLLO_USER}
  72. printf "\n\n"
  73. echo "show Update an item of a namespace, if item doesn't exist, create it"
  74. sleep 1
  75. temp_item_key="openapi-usage-item_update_create_if_not_exists-key-$(date +%s)"
  76. temp_item_value="openapi-usage-item_update_create_if_not_exists-value-$(date +%s)"
  77. echo "create it, key = '${temp_item_key}' value = '${temp_item_value}'"
  78. item_update_create_if_not_exists ${APOLLO_ENV} ${APOLLO_APP_ID} default application ${temp_item_key} ${temp_item_value} "openapi-update-item" ${APOLLO_USER} ${APOLLO_USER}
  79. temp_item_value="openapi-value-of-item_update_create_if_not_exists"
  80. echo "update it, key = '${temp_item_key}' value = '${temp_item_value}'"
  81. item_update_create_if_not_exists ${APOLLO_ENV} ${APOLLO_APP_ID} default application ${temp_item_key} ${temp_item_value} "openapi-update-item" ${APOLLO_USER} ${APOLLO_USER}
  82. printf "\n\n"
  83. echo "show delete item failed"
  84. item_delete ${APOLLO_ENV} ${APOLLO_APP_ID} default application "key-be-deleted" ${APOLLO_USER}
  85. printf "\nshow delete item success\n"
  86. item_delete ${APOLLO_ENV} ${APOLLO_APP_ID} default application ${temp_item_key} ${APOLLO_USER}
  87. printf "\n\n"
  88. ####################################### end of item #######################################
  89. ####################################### namespace release #######################################
  90. temp_namespace_name="application-$(date +%s)"
  91. temp_namespace_format=properties
  92. echo -e "create namespace: namespace name = '${temp_namespace_name}', app id = '${APOLLO_APP_ID}', format = '${temp_namespace_format}'"
  93. namespace_create ${APOLLO_APP_ID} ${temp_namespace_name} ${temp_namespace_format} false 'create by openapi, bash scripts for release' ${APOLLO_USER}
  94. echo -e "\ncreate or update an item '${temp_item_key}'='${temp_item_value}'"
  95. item_update_create_if_not_exists ${APOLLO_ENV} ${APOLLO_APP_ID} default ${temp_namespace_name} ${temp_item_key} ${temp_item_value} "openapi-update-item" ${APOLLO_USER} ${APOLLO_USER}
  96. echo -e "\nrelease namespace: '${temp_namespace_name}'"
  97. namespace_release ${APOLLO_ENV} ${APOLLO_APP_ID} ${APOLLO_CLUSTER} ${temp_namespace_name} 'releaseTitle-openapi-2021-01-01' 'releaseComment-openapi' ${APOLLO_USER}
  98. printf "\n\n"
  99. ####################################### end of namespace release #######################################