Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/actions/generate-sdk/python/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Generate SDK
description: "Generates the Python SDK"
inputs:
python-version:
description: "Python version to install"
required: true
runs:
using: "composite"
steps:
- name: Download OAS
shell: bash
run: make download-oas
- name: Generate SDK
shell: bash
run: make generate-sdk LANGUAGE=python
46 changes: 43 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ env:
JAVA_VERSION: "11"

jobs:
main:
name: CI
main-go:
name: CI [Go]
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
Expand All @@ -32,7 +32,7 @@ jobs:
with:
go-version: ${{ env.GO_VERSION_BUILD }}
- name: Generate SDK
uses: ./.github/actions/generate-sdk
uses: ./.github/actions/generate-sdk/go
- name: Install Go ${{ matrix.go-version }}
uses: actions/setup-go@v5
with:
Expand All @@ -44,3 +44,43 @@ jobs:
- name: Test
working-directory: ./sdk-repo-updated
run: make test skip-non-generated-files=true
main-python:
name: CI [Python]
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
runs-on: ${{ matrix.os }}
steps:
- name: Install SSH Key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_PRIVATE_KEY }}
known_hosts: ${{ vars.SSH_KNOWN_HOSTS }}
- name: Install Java
uses: actions/setup-java@v4
with:
distribution: "temurin"
java-version: ${{ env.JAVA_VERSION }}
- name: Checkout
uses: actions/checkout@v4
- name: Build
uses: ./.github/actions/build
with:
go-version: ${{ env.GO_VERSION_BUILD }}
- name: Generate SDK
uses: ./.github/actions/generate-sdk/python
- name: Install Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install core
run: |
git clone https://github.com/stackitcloud/stackit-sdk-python-core.git core
cd core;make install-dev;
- name: Lint
working-directory: ./sdk-repo-updated
run: make lint
- name: Test
working-directory: ./sdk-repo-updated
run: make test
34 changes: 31 additions & 3 deletions .github/workflows/sdk-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ env:
JAVA_VERSION: '11'

jobs:
main:
main-go:
name: Update SDK Repo
runs-on: ubuntu-latest
steps:
Expand All @@ -32,10 +32,38 @@ jobs:
with:
go-version: ${{ env.GO_VERSION }}
- name: Generate SDK
uses: ./.github/actions/generate-sdk
uses: ./.github/actions/generate-sdk/go
- name: Push SDK
env:
GH_REPO: 'stackitcloud/stackit-sdk-go'
GH_TOKEN: ${{ secrets.SDK_PR_TOKEN }}
run: |
scripts/sdk-create-pr.sh "generator-bot-${{ github.run_id }}" "Generated from GitHub run [${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})"
scripts/sdk-create-pr.sh "generator-bot-${{ github.run_id }}" "Generated from GitHub run [${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})"
main-python:
name: Update SDK Repo
runs-on: ubuntu-latest
steps:
- name: Install SSH Key
uses: shimataro/ssh-key-action@v2
with:
key: ${{ secrets.SSH_PRIVATE_KEY }}
known_hosts: ${{ vars.SSH_KNOWN_HOSTS }}
- name: Install Java
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: ${{ env.JAVA_VERSION }}
- name: Checkout
uses: actions/checkout@v4
- name: Build
uses: ./.github/actions/build
with:
go-version: ${{ env.GO_VERSION }}
- name: Generate SDK
uses: ./.github/actions/generate-sdk/python
- name: Push SDK
env:
GH_REPO: 'stackitcloud/stackit-sdk-python'
GH_TOKEN: ${{ secrets.SDK_PR_TOKEN }}
run: |
scripts/sdk-create-pr.sh "generator-bot-${{ github.run_id }}" "Generated from GitHub run [${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})" "[email protected]:stackitcloud/stackit-sdk-python.git" "python"
9 changes: 8 additions & 1 deletion scripts/generate-sdk/.openapi-generator-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ git_push.sh
README.md
response.go
api/openapi.yaml
.openapi-generator/*
.openapi-generator/*
.gitlab-ci.yml
setup.cfg
setup.py
test-requirements.txt
requirements.txt
tox.ini
*/.github/*
24 changes: 22 additions & 2 deletions scripts/generate-sdk/generate-sdk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ ROOT_DIR=$(git rev-parse --show-toplevel)
GENERATOR_PATH="${ROOT_DIR}/scripts/bin"
LANGUAGE_GENERATORS_FOLDER_PATH="${ROOT_DIR}/scripts/generate-sdk/languages/"
# Renovate: datasource=github-tags depName=OpenAPITools/openapi-generator versioning=semver
GENERATOR_VERSION="v6.6.0"
GENERATOR_VERSION_NUMBER="${GENERATOR_VERSION:1}"

# Check parameters and set defaults if not provided
if [[ -z ${GIT_HOST} ]]; then
Expand Down Expand Up @@ -46,6 +44,21 @@ if [ ! -d ${ROOT_DIR}/oas ]; then
echo "\"oas\" folder not found in root. Please add it manually or run \"make download-oas\"."
exit 1
fi
# Choose generator version depending on the language
# Renovate: datasource=github-tags depName=OpenAPITools/openapi-generator versioning=semver
case "${LANGUAGE}" in
go)
GENERATOR_VERSION="v6.6.0" # There are issues with GO SDK generation in version v7
;;
python)
GENERATOR_VERSION="v7.7.0"
;;
*)
echo "SDK language not supported."
exit 1
;;
esac
GENERATOR_VERSION_NUMBER="${GENERATOR_VERSION:1}"

# Download OpenAPI generator if not already downloaded
jar_path="${GENERATOR_PATH}/openapi-generator-cli.jar"
Expand All @@ -67,6 +80,13 @@ go)
# Usage: generate_go_sdk GENERATOR_PATH GIT_HOST GIT_USER_ID [GIT_REPO_ID] [SDK_REPO_URL]
generate_go_sdk ${jar_path} ${GIT_HOST} ${GIT_USER_ID} ${GIT_REPO_ID} ${SDK_REPO_URL}
;;
python)
echo -e "\nGenerating the Python SDK...\n"

source ${LANGUAGE_GENERATORS_FOLDER_PATH}/${LANGUAGE}.sh
# Usage: generate_python_sdk GENERATOR_PATH GIT_HOST GIT_USER_ID [GIT_REPO_ID] [SDK_REPO_URL]
generate_python_sdk ${jar_path} ${GIT_HOST} ${GIT_USER_ID} ${GIT_REPO_ID} ${SDK_REPO_URL}
;;
*)
echo "SDK language not supported."
exit 1
Expand Down
149 changes: 149 additions & 0 deletions scripts/generate-sdk/languages/python.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#!/bin/bash
# This script clones the SDK repo and updates it with the generated API modules
# Pre-requisites: Java, goimports, Go
set -eo pipefail

ROOT_DIR=$(git rev-parse --show-toplevel)
SDK_REPO_LOCAL_PATH="${ROOT_DIR}/sdk-repo-updated"

OAS_REPO=https://github.com/stackitcloud/stackit-api-specifications

SERVICES_FOLDER="${SDK_REPO_LOCAL_PATH}/services"

GENERATOR_LOG_LEVEL="error" # Must be a Java log level (error, warn, info...)

generate_python_sdk() {
# Required parameters
local GENERATOR_JAR_PATH=$1
local GIT_HOST=$2
local GIT_USER_ID=$3

# Optional parameters
local GIT_REPO_ID=$4
local SDK_REPO_URL=$5

# Check required parameters
if [[ -z ${GIT_HOST} ]]; then
echo "GIT_HOST not specified."
exit 1
fi

if [[ -z ${GIT_USER_ID} ]]; then
echo "GIT_USER_ID id not specified."
exit 1
fi

# Check optional parameters and set defaults if not provided
if [[ -z ${GIT_REPO_ID} ]]; then
echo "GIT_REPO_ID not specified, default will be used."
GIT_REPO_ID="stackit-sdk-python"
fi

if [[ -z ${SDK_REPO_URL} ]]; then
echo "SDK_REPO_URL not specified, default will be used."
SDK_REPO_URL="https://github.com/stackitcloud/stackit-sdk-python.git"
fi

# Prepare folders
if [[ ! -d $SERVICES_FOLDER ]]; then
mkdir -p "$SERVICES_FOLDER"
fi

# Clone SDK repo
if [ -d ${SDK_REPO_LOCAL_PATH} ]; then
echo "Old SDK repo clone was found, it will be removed."
rm -rf ${SDK_REPO_LOCAL_PATH}
fi
git clone --quiet ${SDK_REPO_URL} ${SDK_REPO_LOCAL_PATH}

# Install SDK project tools
cd ${ROOT_DIR}
make project-tools

# Backup of the current state of the SDK services dir (services/)
sdk_services_backup_dir=$(mktemp -d)
if [[ ! ${sdk_services_backup_dir} || -d {sdk_services_backup_dir} ]]; then
echo "Unable to create temporary directory"
exit 1
fi
cleanup() {
rm -rf ${sdk_services_backup_dir}
}
cp -a "${SERVICES_FOLDER}/." ${sdk_services_backup_dir}

# Cleanup after we are done
trap cleanup EXIT

# Remove old contents of services dir (services/)
rm -rf ${SERVICES_FOLDER}

# Generate SDK for each service
for service_json in ${ROOT_DIR}/oas/*.json; do
service="${service_json##*/}"
service="${service%.json}"

# Remove invalid characters to ensure a valid Go pkg name
service="${service//-/}" # remove dashes
service="${service// /}" # remove empty spaces
service="${service//_/}" # remove underscores
service=$(echo "${service}" | tr '[:upper:]' '[:lower:]') # convert upper case letters to lower case
service=$(echo "${service}" | tr -d -c '[:alnum:]') # remove non-alphanumeric characters

echo "Generating \"${service}\" service..."
cd ${ROOT_DIR}

mkdir -p "${SERVICES_FOLDER}/${service}/"
cp "${ROOT_DIR}/scripts/generate-sdk/.openapi-generator-ignore" "${SERVICES_FOLDER}/${service}/"

# Run the generator
java -Dlog.level=${GENERATOR_LOG_LEVEL} -jar ${jar_path} generate \
--generator-name python \
--input-spec "${service_json}" \
--output "${SERVICES_FOLDER}/${service}" \
--package-name "stackit.${service}" \
--template-dir "${ROOT_DIR}/templates/python/" \
--git-host ${GIT_HOST} \
--git-user-id ${GIT_USER_ID} \
--git-repo-id ${GIT_REPO_ID} \
--global-property apis,models,modelTests=false,modelDocs=false,apiDocs=false,apiTests=false,supportingFiles \
--additional-properties=pythonPackageName="stackit-${service},removeEnumValuePrefix=false" >/dev/null

# Remove unnecessary files
rm "${SERVICES_FOLDER}/${service}/.openapi-generator-ignore"
rm -r "${SERVICES_FOLDER}/${service}/.openapi-generator/"
rm "${SERVICES_FOLDER}/${service}/stackit/__init__.py"
rm "${SERVICES_FOLDER}/${service}/.github/workflows/python.yml"


# If the service has a wait package files, move them inside the service folder
if [ -d ${sdk_services_backup_dir}/${service}/wait ]; then
echo "Found ${service} \"wait\" package"
cp -r ${sdk_services_backup_dir}/${service}/wait ${SERVICES_FOLDER}/${service}/wait
fi

# If the service has a CHANGELOG file, move it inside the service folder
if [ -f ${sdk_services_backup_dir}/${service}/CHANGELOG.md ]; then
echo "Found ${service} \"CHANGELOG\" file"
cp -r ${sdk_services_backup_dir}/${service}/CHANGELOG.md ${SERVICES_FOLDER}/${service}/CHANGELOG.md
fi

# If the service has a LICENSE file, move it inside the service folder
if [ -f ${sdk_services_backup_dir}/${service}/LICENSE.md ]; then
echo "Found ${service} \"LICENSE\" file"
cp -r ${sdk_services_backup_dir}/${service}/LICENSE.md ${SERVICES_FOLDER}/${service}/LICENSE.md
fi

# If the service has a NOTICE file, move it inside the service folder
if [ -f ${sdk_services_backup_dir}/${service}/NOTICE.txt ]; then
echo "Found ${service} \"NOTICE\" file"
cp -r ${sdk_services_backup_dir}/${service}/NOTICE.txt ${SERVICES_FOLDER}/${service}/NOTICE.txt
fi

cd ${SERVICES_FOLDER}/${service}
# Run formatter
isort .
autoimport --ignore-init-modules .
black .

done
}
1 change: 1 addition & 0 deletions scripts/project.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ elif [ "$action" = "tools" ]; then
cd ${ROOT_DIR}

go install golang.org/x/tools/cmd/goimports@latest
pip install black==24.8.0 isort~=5.13.2 autoimport~=1.6.1
else
echo "Invalid action: '$action', please use $0 help for help"
fi
9 changes: 8 additions & 1 deletion scripts/sdk-create-pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ else
REPO_URL_SSH=$3
fi

if [[ -z $4 ]]; then
echo "LANGUAGE not specified, default will be used."
LANGUAGE="go"
else
LANGUAGE=$4
fi

# Create temp directory to work on
work_dir=$(mktemp -d)
if [[ ! ${work_dir} || -d {work_dir} ]]; then
Expand Down Expand Up @@ -83,7 +90,7 @@ for service_path in ${work_dir}/sdk_to_push/services/*; do
fi

git add services/${service}/
if [ ! -d "${work_dir}/sdk_backup/services/${service}/" ]; then # Check if it is a newly added SDK module
if [ "${LANGUAGE}" == "go" ] && [ ! -d "${work_dir}/sdk_backup/services/${service}/" ]; then # Check if it is a newly added SDK module
# go work use -r adds a use directive to the go.work file for dir, if it exists, and removes the use directory if the argument directory doesn’t exist
# the -r flag examines subdirectories of dir recursively
# this prevents errors if there is more than one new module in the SDK generation
Expand Down
Loading
Loading