Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nms): generate swagger api for typescript #12757

Merged
merged 4 commits into from
May 23, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
maxhbr marked this conversation as resolved.
Show resolved Hide resolved
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