Skip to content

Commit d9d8032

Browse files
authored
Merge branch 'main' into setup-codeowners
2 parents 971465f + 7e348ba commit d9d8032

15 files changed

+607
-199
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Check API version lock
2+
3+
on:
4+
# run after PR merge
5+
push:
6+
branches: ["main"]
7+
# run every night at 01:00
8+
schedule:
9+
- cron: "0 1 * * *"
10+
11+
env:
12+
THRESHOLD: "10 days ago"
13+
14+
jobs:
15+
main-go:
16+
name: "[Go] Update SDK Repo"
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
- name: Check commit date
22+
run: |
23+
set -eo pipefail
24+
25+
THRESHOLD="10 minutes ago"
26+
27+
if jq -e 'map_values(select(. != "main")) != {}' api-versions-lock.json; then
28+
thresholdDate=$(date -Iminutes -d "${THRESHOLD}")
29+
commitDate=$(git -P log -n 1 --format="%aI" api-versions-lock.json)
30+
if [[ "$commitDate" < "$thresholdDate" ]]; then
31+
echo "latest commit of api-versions-lock.json is older than ${THRESHOLD}"
32+
exit 1
33+
fi
34+
echo "api-versions-lock.json contains locked versions but is recent enough"
35+
else
36+
echo "no locked versions in api-versions-lock.json"
37+
fi

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ROOT_DIR ?= $(shell git rev-parse --show-toplevel)
22
SCRIPTS_BASE ?= $(ROOT_DIR)/scripts
3-
API_VERSION ?= $(shell cat api_version|grep -v '^\#'|head -n 1)
3+
API_VERSIONS ?= $(ROOT_DIR)/api-versions-lock.json
44
SDK_BRANCH ?= main
55

66
# SETUP AND TOOL INITIALIZATION TASKS
@@ -12,7 +12,7 @@ project-tools:
1212

1313
# GENERATE
1414
download-oas:
15-
@$(SCRIPTS_BASE)/download-oas.sh "$(OAS_REPO_NAME)" "$(OAS_REPO)" "$(ALLOW_ALPHA)" "$(API_VERSION)"
15+
@$(SCRIPTS_BASE)/download-oas.sh "$(OAS_REPO_NAME)" "$(OAS_REPO)" "$(ALLOW_ALPHA)" "$(API_VERSIONS)"
1616

1717
generate-sdk:
1818
@$(SCRIPTS_BASE)/generate-sdk/generate-sdk.sh "$(GIT_HOST)" "$(GIT_USER_ID)" "$(GIT_REPO_ID)" "$(SDK_REPO_URL)" "$(LANGUAGE)" "$(SDK_BRANCH)"

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![Check API version lock](https://github.com/stackitcloud/stackit-sdk-generator/actions/workflows/check-api-versions.yaml/badge.svg)](https://github.com/stackitcloud/stackit-sdk-generator/actions/workflows/check-api-versions.yaml)
2+
13
# Overview
24

35
This repository implements the automatic generation of client libraries to access STACKIT APIs. It is based on the [OpenAPI Generator](https://openapi-generator.tech/). The process' input are the REST API specs in the [OpenAPI Specification](https://github.com/OAI/OpenAPI-Specification) format (OAS), which are stored in [STACKIT API specifications](https://github.com/stackitcloud/stackit-api-specifications).

api-versions-lock.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

api_version

Lines changed: 0 additions & 3 deletions
This file was deleted.

blacklist.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
# Transitional file to disable generation for selected services
2-
cdn
1+
# Transitional file to disable generation for selected services

regional-whitelist.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
objectstorage
44
sqlserverflex
55
postgresflex
6-
serviceenablement
6+
serviceenablement
7+
loadbalancer

scripts/download-oas.sh

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ROOT_DIR=$(git rev-parse --show-toplevel)
66
OAS_REPO_NAME=$1
77
OAS_REPO=$2
88
ALLOW_ALPHA=$3
9-
OAS_API_VERSION=$4
9+
OAS_API_VERSIONS=$4
1010

1111
if [[ -z ${OAS_REPO_NAME} ]]; then
1212
echo "Repo name is empty, default public OAS repo name will be used."
@@ -18,9 +18,9 @@ if [[ ! ${OAS_REPO} || -d ${OAS_REPO} ]]; then
1818
OAS_REPO="https://github.com/stackitcloud/${OAS_REPO_NAME}.git"
1919
fi
2020

21-
if [[ -z ${OAS_API_VERSION} ]]; then
22-
echo "No API version passed, main branch will be used"
23-
OAS_API_VERSION="main"
21+
if [[ -z ${OAS_API_VERSIONS} ]]; then
22+
echo "No API version passed, using ${ROOTDIR}/api-versions-lock.json"
23+
OAS_API_VERSIONS="${ROOT_DIR}/api-versions-lock.json"
2424
fi
2525

2626
# Create temp directory to clone OAS repo
@@ -41,16 +41,23 @@ mkdir ${ROOT_DIR}/oas
4141
cd ${work_dir}
4242
git clone ${OAS_REPO} --quiet
4343

44-
echo "Using api version ${OAS_API_VERSION}"
45-
cd ${OAS_REPO_NAME}
46-
git checkout --quiet ${OAS_API_VERSION}
47-
cd -
48-
4944
for service_dir in ${work_dir}/${OAS_REPO_NAME}/services/*; do
45+
5046
max_version_dir=""
5147
max_version=-1
5248
service=$(basename "$service_dir")
5349

50+
apiVersion=$(jq -r -f <(cat <<EOF
51+
if has("${service}") then ."${service}" else "main" end
52+
EOF
53+
) ${OAS_API_VERSIONS})
54+
if [ "${apiVersion}" != "main" ]; then
55+
echo "Using ${apiVersion} for ${service}"
56+
fi
57+
cd ${work_dir}/${OAS_REPO_NAME} > /dev/null
58+
git checkout -q $apiVersion || (echo "version ${apiVersion} does not exist, using main instead" && git checkout -q main)
59+
cd - > /dev/null
60+
5461
# Prioritize GA over Beta over Alpha versions
5562
# GA priority = 3, Beta priority = 2, Alpha priority = 1
5663
max_version_priority=1

templates/go/api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"io"
99
"net/http"
1010
"net/url"
11-
"{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/core/config"
12-
"{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/core/oapierror"
11+
"github.com/stackitcloud/stackit-sdk-go/core/config"
12+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1313
{{#imports}} "{{import}}"
1414
{{/imports}}
1515
)

templates/go/api_test.mustache

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ import (
1919
"net/url"
2020
"strings"
2121
"testing"
22-
"{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/core/config"
22+
"github.com/stackitcloud/stackit-sdk-go/core/config"
2323
)
2424

2525
func Test_{{packageName}}_{{classname}}Service(t *testing.T) {
2626
2727
{{#operations}}
2828
{{#operation}}
2929
t.Run("Test {{classname}}Service {{{nickname}}}", func(t *testing.T) {
30-
path := "{{{path}}}"{{#pathParams}}
31-
{{paramName}}Value := {{#isString}}"{{paramName}}"{{/isString}}{{#isNumber}}123{{/isNumber}}{{#isFloat}}float32(123){{/isFloat}}{{#isDouble}}float64(123){{/isDouble}}{{#isInteger}}int32(123){{/isInteger}}{{#isLong}}int64(123){{/isLong}}{{^isString}}{{^isInteger}}{{defaultValue}}{{/isInteger}}{{/isString}}
32-
path = strings.Replace(path, "{"+"{{baseName}}"+"}", url.PathEscape(ParameterValueToString({{paramName}}Value, "{{paramName}}")), -1){{/pathParams}}
30+
_apiUrlPath := "{{{path}}}"{{#pathParams}}
31+
{{paramName}}Value := {{#isAnyType}}"unspecified type"{{/isAnyType}}{{#isString}}{{#isUuid}}uuid.NewString(){{/isUuid}}{{^isUuid}}"{{paramName}}"{{/isUuid}}{{/isString}}{{#isNumber}}123{{/isNumber}}{{#isFloat}}float32(123){{/isFloat}}{{#isDouble}}float64(123){{/isDouble}}{{#isInteger}}int32(123){{/isInteger}}{{#isLong}}int64(123){{/isLong}}{{^isString}}{{^isInteger}}{{defaultValue}}{{/isInteger}}{{/isString}}
32+
_apiUrlPath = strings.Replace(_apiUrlPath, "{"+"{{baseName}}"+"}", url.PathEscape(ParameterValueToString({{paramName}}Value, "{{paramName}}")), -1){{/pathParams}}
3333

3434
test{{classname}}ServeMux := http.NewServeMux()
35-
test{{classname}}ServeMux.HandleFunc(path, func(w http.ResponseWriter, req *http.Request) {
35+
test{{classname}}ServeMux.HandleFunc(_apiUrlPath, func(w http.ResponseWriter, req *http.Request) {
3636
{{! Only binary data sets a ReturnFormat}}
3737
{{#returnFormat}}
3838
w.Header().Add("Content-Type", "application/octet-stream")
@@ -41,7 +41,18 @@ func Test_{{packageName}}_{{classname}}Service(t *testing.T) {
4141
{{/returnFormat}}
4242
{{^returnFormat}}
4343
{{#returnType}}
44+
{{#returnProperty.isPrimitiveType}}
45+
{{! according to the model, freeform objects might be primitive...}}
46+
{{#returnProperty.isFreeFormObject}}
4447
data := {{{.}}}{}
48+
{{/returnProperty.isFreeFormObject}}
49+
{{^returnProperty.isFreeFormObject}}
50+
var data {{{.}}}
51+
{{/returnProperty.isFreeFormObject}}
52+
{{/returnProperty.isPrimitiveType}}
53+
{{^returnProperty.isPrimitiveType}}
54+
data := {{{.}}}{}
55+
{{/returnProperty.isPrimitiveType}}
4556
w.Header().Add("Content-Type", "application/json")
4657
json.NewEncoder(w).Encode(data)
4758
{{/returnType}}
@@ -79,7 +90,7 @@ func Test_{{packageName}}_{{classname}}Service(t *testing.T) {
7990
{{#allParams}}
8091
{{#required}}
8192
{{#isPathParam}}
82-
{{paramName}} := {{#isString}}"{{paramName}}"{{/isString}}{{#isNumber}}123{{/isNumber}}{{#isFloat}}float32(123){{/isFloat}}{{#isDouble}}float64(123){{/isDouble}}{{#isInteger}}int32(123){{/isInteger}}{{#isLong}}int64(123){{/isLong}}{{^isString}}{{^isInteger}}{{defaultValue}}{{/isInteger}}{{/isString}}
93+
{{paramName}} := {{paramName}}Value
8394
{{/isPathParam}}
8495
{{^isPathParam}}
8596
{{#isPrimitiveType}}
@@ -98,7 +109,7 @@ func Test_{{packageName}}_{{classname}}Service(t *testing.T) {
98109
t.Fatalf("error in call: %v", reqErr)
99110
}
100111
{{#returnType}}
101-
if resp == nil {
112+
if IsNil(resp) {
102113
t.Fatalf("response not present")
103114
}
104115
{{/returnType}}

0 commit comments

Comments
 (0)