diff --git a/.gitattributes b/.gitattributes index af19312d146..c2a655ad66c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ -text=auto \ No newline at end of file +text=auto +*.sh text eol=lf diff --git a/CHANGES.md b/CHANGES.md index af7d1d6d2a5..4f05a4b88ee 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,7 @@ Apollo 1.10.0 * [Remove spring dependencies from internal code](https://github.com/apolloconfig/apollo/pull/3937) * [Fix issue: ingress syntax](https://github.com/apolloconfig/apollo/pull/3933) * [refactor: let open api more easier to use and development](https://github.com/apolloconfig/apollo/pull/3943) +* [feat(scripts): use bash to call openapi](https://github.com/apolloconfig/apollo/pull/3980) * [Support search by item](https://github.com/apolloconfig/apollo/pull/3977) ------------------ diff --git a/docs/zh/usage/apollo-open-api-platform.md b/docs/zh/usage/apollo-open-api-platform.md index 14733554c8f..f8a22a2c182 100644 --- a/docs/zh/usage/apollo-open-api-platform.md +++ b/docs/zh/usage/apollo-open-api-platform.md @@ -26,7 +26,7 @@ Apollo管理员在 http://{portal_address}/open/manage.html 创建第三方应 任何语言的第三方应用都可以调用Apollo的Open API,在调用接口时,需要设置注意以下两点: * Http Header中增加一个Authorization字段,字段值为申请的token * Http Header的Content-Type字段需要设置成application/json;charset=UTF-8 - + ##### 2.3.2 Java应用通过apollo-openapi调用Apollo Open API 从1.1.0版本开始,Apollo提供了[apollo-openapi](https://github.com/ctripcorp/apollo/tree/master/apollo-openapi)客户端,所以Java语言的第三方应用可以更方便地调用Apollo Open API。 @@ -55,17 +55,26 @@ ApolloOpenApiClient client = ApolloOpenApiClient.newBuilder() .Net core也提供了open api的客户端,详见https://github.com/ctripcorp/apollo.net/pull/77 +##### 2.3.4 Shell Scripts调用Apollo Open API + +封装了bash的function,底层使用curl来发送HTTP请求 + +* bash函数:[openapi.sh](https://github.com/apolloconfig/apollo/blob/master/scripts/openapi/bash/openapi.sh) + +* 使用示例:[openapi-usage-example.sh](https://github.com/apolloconfig/apollo/blob/master/scripts/openapi/bash/openapi-usage-example.sh) +* 全部和openapi有关的shell脚本在文件夹 https://github.com/apolloconfig/apollo/tree/master/scripts/sql 下 + ### 三、 接口文档 #### 3.1 URL路径参数说明 - + 参数名 | 参数说明 --- | --- env | 所管理的配置环境 appId | 所管理的配置AppId clusterName | 所管理的配置集群名, 一般情况下传入 default 即可。如果是特殊集群,传入相应集群的名称即可 namespaceName | 所管理的Namespace的名称,如果是非properties格式,需要加上后缀名,如`sample.yml` - + #### 3.2 API接口列表 ##### 3.2.1 获取App的环境,集群信息 @@ -549,4 +558,3 @@ operator | true | String | 删除配置的操作者,域账号 #### 4.6 500 - Internal Server Error 其它类型的错误默认都会返回500,对这类错误如果应用无法根据提示信息找到原因的话,可以找Apollo研发团队一起排查问题。 - diff --git a/scripts/openapi/bash/openapi-usage-example.sh b/scripts/openapi/bash/openapi-usage-example.sh new file mode 100644 index 00000000000..96bdbc569fc --- /dev/null +++ b/scripts/openapi/bash/openapi-usage-example.sh @@ -0,0 +1,111 @@ +#!/bin/bash +# +# Copyright 2021 Apollo Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# title openapi-usage-example.sh +# description show how to use openapi.sh +# author wxq +# date 2021-09-12 +# Chinese reference website https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform +# English reference website https://www.apolloconfig.com/#/en/usage/apollo-open-api-platform + +# export global varialbes +export APOLLO_PORTAL_ADDRESS=http://106.54.227.205 +export APOLLO_OPENAPI_TOKEN=284fe833cbaeecf2764801aa73965080b184fc88 +export CURL_OPTIONS="" +# load functions +source openapi.sh + +# set up global environment variable +APOLLO_APP_ID=openapi +APOLLO_ENV=DEV +APOLLO_CLUSTER=default +APOLLO_USER=apollo + +####################################### cluster ####################################### +# get cluster +printf "get cluster: env = '%s', app id = '%s', cluster = '%s'\n" ${APOLLO_ENV} ${APOLLO_APP_ID} ${APOLLO_CLUSTER} +cluster_get ${APOLLO_ENV} ${APOLLO_APP_ID} ${APOLLO_CLUSTER} +printf "\n\n" + +# create cluster. To forbid cluster xxx already exists, add timestamp to suffix +temp_apollo_cluster="cluster-$(date +%s)" +printf "create cluster: env = '%s', app id = '%s', cluster = '%s'\n" ${APOLLO_ENV} ${APOLLO_APP_ID} ${temp_apollo_cluster} +cluster_create ${APOLLO_ENV} ${APOLLO_APP_ID} ${temp_apollo_cluster} ${APOLLO_USER} +printf "\n\n" +####################################### end of cluster ####################################### + +####################################### namespace ####################################### +# create namespace +temp_namespace_name="application-123" +temp_namespace_format=yaml +echo "create namespace: namespace name = '${temp_namespace_name}', app id = '${APOLLO_APP_ID}', format = '${temp_namespace_format}'" +namespace_create ${APOLLO_APP_ID} ${temp_namespace_name} ${temp_format} false 'create by openapi, bash scripts' ${APOLLO_USER} +printf "\n\n" +####################################### end of namespace ####################################### + +####################################### item ####################################### +# create an item, i.e a key value pair +temp_item_key="openapi-usage-create-item-key-$(date +%s)" +temp_item_value="openapi-usage-create-item-value-$(date +%s)" +echo -e "create item: app id = ${APOLLO_APP_ID} env = ${APOLLO_ENV} key = ${temp_item_key} value = ${temp_item_value}" +item_create ${APOLLO_ENV} ${APOLLO_APP_ID} default application ${temp_item_key} ${temp_item_value} "openapi-create-item" ${APOLLO_USER} +printf "\n\n" + +# update an item +echo "show update failed when item key not exists" +sleep 1 +temp_item_key="openapi-usage-update-item-key-$(date +%s)" +temp_item_value="openapi-usage-update-item-value-$(date +%s)" +item_update ${APOLLO_ENV} ${APOLLO_APP_ID} default application ${temp_item_key} ${temp_item_value} "openapi-update-item" ${APOLLO_USER} +printf "\n\n" + +echo "show after created, update success" +item_create ${APOLLO_ENV} ${APOLLO_APP_ID} default application ${temp_item_key} ${temp_item_value} "openapi-create-item" ${APOLLO_USER} +temp_item_value="item-update-success" +printf "\n" +item_update ${APOLLO_ENV} ${APOLLO_APP_ID} default application ${temp_item_key} ${temp_item_value} "openapi-update-item" ${APOLLO_USER} +printf "\n\n" + +echo "show Update an item of a namespace, if item doesn's exist, create it" +sleep 1 +temp_item_key="openapi-usage-item_update_create_if_not_exists-key-$(date +%s)" +temp_item_value="openapi-usage-item_update_create_if_not_exists-value-$(date +%s)" +echo "create it, key = '${temp_item_key}' value = '${temp_item_value}'" +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} +temp_item_value="openapi-value-of-item_update_create_if_not_exists" +echo "update it, key = '${temp_item_key}' value = '${temp_item_value}'" +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} +printf "\n\n" + +echo "show delete item failed" +item_delete ${APOLLO_ENV} ${APOLLO_APP_ID} default application "key-be-deleted" ${APOLLO_USER} +printf "\nshow delete item success\n" +item_delete ${APOLLO_ENV} ${APOLLO_APP_ID} default application ${temp_item_key} ${APOLLO_USER} +printf "\n\n" +####################################### end of item ####################################### + +####################################### namespace release ####################################### +temp_namespace_name="application-$(date +%s)" +temp_namespace_format=properties +echo -e "create namespace: namespace name = '${temp_namespace_name}', app id = '${APOLLO_APP_ID}', format = '${temp_namespace_format}'" +namespace_create ${APOLLO_APP_ID} ${temp_namespace_name} ${temp_namespace_format} false 'create by openapi, bash scripts for release' ${APOLLO_USER} +echo -e "\ncreate or update an item '${temp_item_key}'='${temp_item_value}'" +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} +echo -e "\nrelease namespace: '${temp_namespace_name}'" +namespace_release ${APOLLO_ENV} ${APOLLO_APP_ID} ${APOLLO_CLUSTER} ${temp_namespace_name} 'releaseTitle-openapi-2021-01-01' 'releaseComment-openapi' ${APOLLO_USER} +printf "\n\n" +####################################### end of namespace release ####################################### \ No newline at end of file diff --git a/scripts/openapi/bash/openapi.sh b/scripts/openapi/bash/openapi.sh new file mode 100644 index 00000000000..6a14d75fe47 --- /dev/null +++ b/scripts/openapi/bash/openapi.sh @@ -0,0 +1,343 @@ +#!/bin/bash +# +# Copyright 2021 Apollo Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# title openapi.sh +# description functions to call openapi through http +# author wxq +# date 2021-09-12 +# Chinese reference website https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform +# English reference website https://www.apolloconfig.com/#/en/usage/apollo-open-api-platform + +####################################### Global variables ####################################### +# portal's address, just support 1 address without suffix '/' +# Don't use http://ip:port/ with suffix '/' or multiple address http://ip1:port1,http://ip2:port2 +APOLLO_PORTAL_ADDRESS=${APOLLO_PORTAL_ADDRESS:-http://ip:port} +APOLLO_OPENAPI_TOKEN=${APOLLO_OPENAPI_TOKEN:-please_change_me_by_environment_variable} +CURL_OPTIONS=${CURL_OPTIONS:-} + +echo "apollo portal address: ${APOLLO_PORTAL_ADDRESS}" +echo "curl options: ${CURL_OPTIONS}" +####################################### end of Global variables ####################################### + +####################################### basic http call ####################################### +####################################### +# Http get by curl. +# Globals: +# APOLLO_PORTAL_ADDRESS: portal's address +# APOLLO_OPENAPI_TOKEN: openapi's token +# CURL_OPTIONS: options in curl +# Arguments: +# url_suffix +####################################### +function openapi_get() { + local url_suffix=$1 + + local url="${APOLLO_PORTAL_ADDRESS}/${url_suffix}" + curl ${CURL_OPTIONS} --header "Authorization: ${APOLLO_OPENAPI_TOKEN}" --header "Content-Type: application/json;charset=UTF-8" "${url}" +} + +####################################### +# Http post by curl. +# Globals: +# APOLLO_PORTAL_ADDRESS: portal's address +# APOLLO_OPENAPI_TOKEN: openapi's token +# CURL_OPTIONS: options in curl +# Arguments: +# url_suffix +# body +####################################### +function openapi_post() { + local url_suffix=$1 + local body=$2 + + local url="${APOLLO_PORTAL_ADDRESS}/${url_suffix}" + curl ${CURL_OPTIONS} --header "Authorization: ${APOLLO_OPENAPI_TOKEN}" --header "Content-Type: application/json;charset=UTF-8" --data "${body}" "${url}" +} + +####################################### +# Http put by curl. +# Globals: +# APOLLO_PORTAL_ADDRESS: portal's address +# APOLLO_OPENAPI_TOKEN: openapi's token +# CURL_OPTIONS: options in curl +# Arguments: +# url_suffix +# body +####################################### +function openapi_put() { + local url_suffix=$1 + local body=$2 + + local url="${APOLLO_PORTAL_ADDRESS}/${url_suffix}" + curl ${CURL_OPTIONS} --header "Authorization: ${APOLLO_OPENAPI_TOKEN}" --header "Content-Type: application/json;charset=UTF-8" -X PUT --data "${body}" "${url}" +} + +####################################### +# Http delete by curl. +# Globals: +# APOLLO_PORTAL_ADDRESS: portal's address +# APOLLO_OPENAPI_TOKEN: openapi's token +# CURL_OPTIONS: options in curl +# Arguments: +# url_suffix +# body +####################################### +function openapi_delete() { + local url_suffix=$1 + local body=$2 + + local url="${APOLLO_PORTAL_ADDRESS}/${url_suffix}" + curl ${CURL_OPTIONS} --header "Authorization: ${APOLLO_OPENAPI_TOKEN}" --header "Content-Type: application/json;charset=UTF-8" -X DELETE --data "${body}" "${url}" +} +####################################### end of basic http call ####################################### + + +####################################### cluster ####################################### +####################################### +# Get cluster. +# 获取集群 +# Arguments: +# env +# appId +# clusterName +####################################### +function cluster_get() { + local env=$1 + local appId=$2 + local clusterName=$3 + openapi_get "openapi/v1/envs/${env}/apps/${appId}/clusters/${clusterName}" +} + +####################################### +# Create cluster in app's environment. +# 创建集群 +# Arguments: +# env +# appId +# clusterName +# dataChangeCreatedBy +####################################### +function cluster_create() { + local env=$1 + local appId=$2 + local clusterName=$3 + local dataChangeCreatedBy=$4 + openapi_post "openapi/v1/envs/${env}/apps/${appId}/clusters" "$(cat <