Skip to content

Commit

Permalink
feat(scripts): use bash to call openapi (#3980)
Browse files Browse the repository at this point in the history
* feat(scripts): use bash to call openapi

* add Copyright to header of .sh file

* Update CHANGES.md

* Update CHANGES.md

* feat(scripts): openapi create namespace

* refactor: change name to hump naming

* 添加中文说明

* change argument's order

* feat: create item

* feat: item update

* feat: delete item

* feat: release namespace

* docs: shell scripts call open-api
  • Loading branch information
Anilople authored Sep 18, 2021
1 parent 2455c0f commit debf749
Show file tree
Hide file tree
Showing 5 changed files with 469 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
text=auto
text=auto
*.sh text eol=lf
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

------------------
All issues and pull requests are [here](https://github.com/ctripcorp/apollo/milestone/8?closed=1)
16 changes: 12 additions & 4 deletions docs/zh/usage/apollo-open-api-platform.md
Original file line number Diff line number Diff line change
Expand Up @@ -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。

Expand Down Expand Up @@ -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的环境,集群信息
Expand Down Expand Up @@ -549,4 +558,3 @@ operator | true | String | 删除配置的操作者,域账号
#### 4.6 500 - Internal Server Error
其它类型的错误默认都会返回500,对这类错误如果应用无法根据提示信息找到原因的话,可以找Apollo研发团队一起排查问题。


111 changes: 111 additions & 0 deletions scripts/openapi/bash/openapi-usage-example.sh
Original file line number Diff line number Diff line change
@@ -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 #######################################
Loading

0 comments on commit debf749

Please sign in to comment.