Skip to content

Commit

Permalink
feat(nms): generate swagger api for typescript (magma#12757)
Browse files Browse the repository at this point in the history
* feat(nms): generate swagger api for typescript

Signed-off-by: Fritz Lehnert <13189449+Neudrino@users.noreply.github.com>

* feat(nms): rename package in swagger api

Signed-off-by: Fritz Lehnert <13189449+Neudrino@users.noreply.github.com>

* feat(nms): auto-generated files from swagger

Signed-off-by: Fritz Lehnert <13189449+Neudrino@users.noreply.github.com>

* refactor(nms): Make TypeScript generation script executable and fix PackageType usage

Signed-off-by: Thomas Schmitt <thomas.schmitt@tngtech.com>

Co-authored-by: Thomas Schmitt <thomas.schmitt@tngtech.com>
  • Loading branch information
2 people authored and emakeev committed Aug 5, 2022
1 parent 775b362 commit baa7b7b
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 33 deletions.
4 changes: 2 additions & 2 deletions nms/app/views/equipment/GatewayDetailConfigEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import type {
apn_resources,
challenge_key,
distribution_package,
enodeb_serials,
gateway_device,
gateway_dns_configs,
Expand All @@ -25,7 +26,6 @@ import type {
gateway_ran_configs,
lte_gateway,
magmad_gateway_configs,
package_type,
} from '../../../generated/MagmaAPIBindings';

import Accordion from '@material-ui/core/Accordion';
Expand Down Expand Up @@ -344,7 +344,7 @@ type Props = {
onSave: lte_gateway => void,
};

type VersionType = $PropertyType<package_type, 'version'>;
type VersionType = $PropertyType<distribution_package, 'version'>;

export function ConfigEdit(props: Props) {
const enqueueSnackbar = useEnqueueSnackbar();
Expand Down
10 changes: 5 additions & 5 deletions nms/generated/MagmaAPIBindings.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions nms/scripts/generateAPIFromSwaggerForTypeScript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env bash
################################################################################
# Copyright 2022 The Magma Authors.

# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# 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.
################################################################################

set -euo pipefail

usage() {
cat <<EOF
generateAPIFromSwaggerForTypeScript.sh
— generates TypeScript NMS API bindings for swagger spec
Usage:
generateAPIFromSwaggerForTypeScript.sh [-f] <input> <output>
Options:
-i swagger.yml file
-o Output directory for ts bindings
-f Overwrite files without confirmation
<input> Input swagger.yml file to read.
<output> Output directory for js bindings.
EOF
exit 2
}

FORCE=false
INPUT=""
OUTPUT=""
while getopts 'hfi:o:' option; do
case "$option" in
f) FORCE=true ;;
i) INPUT="${OPTARG}" ;;
o) OUTPUT="${OPTARG}" ;;
h | *) usage ;;
esac
done

[[ -z "${INPUT}" ]] || [[ -z "${OUTPUT}" ]] && usage

if [ "$FORCE" = false ] && [ -d "${OUTPUT}" ] ; then
read -r -p "Are you sure you want to overwrite all files in ${OUTPUT} (y/N)? " REPLY
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
fi

(set -x;
yarn --silent openapi --client axios --useUnionTypes -i "${INPUT}" -o "${OUTPUT}")

addHeader() {
local file="$1"
TEMPORARY_FILE=$(mktemp)
cat <<EOF > "${TEMPORARY_FILE}"
/**
* Copyright 2022 The Magma Authors.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*
* 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.
*
* @generated
* This file is generated by "nms/scripts/generateAPIFromSwaggerForTypeScript.sh".
*/
$(cat "${file}")
EOF
mv "${TEMPORARY_FILE}" "${file}"
}

for file in "${OUTPUT}"/*.ts "${OUTPUT}"/**/*.ts; do
addHeader "${file}"
# TODO #12768 remove sed replacements
sed -i 's/?view=full(/_view_full(/g' "${file}"
sed -i 's/?verbose=false(/_verbose_false(/g' "${file}"
done
4 changes: 4 additions & 0 deletions orc8r/cloud/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ SWAGGER_V1_PARTIAL_SPECS_DIR := $(SWAGGER_V1_SPECS_DIR)/partial
SWAGGER_V1_COMMON_DIR := $(SWAGGER_V1_SPECS_DIR)/common
SWAGGER_V1_STANDALONE_DIR := $(SWAGGER_V1_SPECS_DIR)/standalone
SWAGGER_NMS_OUT := $(MAGMA_ROOT)/nms/generated/MagmaAPIBindings.js
SWAGGER_NMS_OUT_TS := $(MAGMA_ROOT)/nms/generated-ts/

OS_NAME := $(shell uname)

Expand Down Expand Up @@ -182,6 +183,7 @@ nms_prereqs_ubuntu: nms_node_ubuntu
# get codegen dependency
npm install --global yarn
yarn add swagger-js-codegen
yarn add openapi-typescript-codegen

nms_node_ubuntu:
# install pre-reqs
Expand All @@ -202,7 +204,9 @@ nms_prereqs_osx:
npm --version || brew install npm
yarn --version || npm install --global yarn
yarn add swagger-js-codegen
yarn add openapi-typescript-codegen

nms_gen:
# generate Swagger API bindings for NMS
$(MAGMA_ROOT)/nms/scripts/generateAPIFromSwagger.sh $(SWAGGER_V1_YML) $(SWAGGER_NMS_OUT)
$(MAGMA_ROOT)/nms/scripts/generateAPIFromSwaggerForTypeScript.sh -f -i $(SWAGGER_V1_YML) -o $(SWAGGER_NMS_OUT_TS)
20 changes: 10 additions & 10 deletions orc8r/cloud/go/obsidian/swagger/v1/swagger.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func NewDefaultGatewayStatus(hardwareID string) *GatewayStatus {
},
PlatformInfo: &PlatformInfo{
VpnIP: "facebook.com",
Packages: []*Package{
Packages: []*DistributionPackage{
{
Name: "magma",
Version: "0.0.0.0",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ magma-gen-meta:
filename: machine_info_swaggergen.go
- go-struct-name: NetworkInterface
filename: network_interface_swaggergen.go
- go-struct-name: Package
filename: package_swaggergen.go
- go-struct-name: DistributionPackage
filename: distribution_package_swaggergen.go
- go-struct-name: PlatformInfo
filename: platform_info_swaggergen.go
- go-struct-name: Route
Expand Down Expand Up @@ -1868,7 +1868,7 @@ definitions:
x-omitempty: true
items:
$ref: '#/definitions/disk_partition'
package:
distribution_package:
type: object
properties:
name:
Expand Down Expand Up @@ -1897,7 +1897,7 @@ definitions:
type: array
x-omitempty: true
items:
$ref: '#/definitions/package'
$ref: '#/definitions/distribution_package'
kernel_version:
type: string
example: 4.9.0-6-amd64
Expand Down

0 comments on commit baa7b7b

Please sign in to comment.