Skip to content

Commit

Permalink
Moved makefiles and scripts from quake-client
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitra-mylarappachar committed May 10, 2022
1 parent 306eead commit 4dee71b
Show file tree
Hide file tree
Showing 17 changed files with 590 additions and 11 deletions.
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Mac Filesystem files
*.DS_Store

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
vendor/

# build artifacts
v1/bin
v1/pkg/client/generic-error.go

#misc
scratch
.idea/
16 changes: 16 additions & 0 deletions build/verify_generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#! /bin/bash
# (C) Copyright 2021-2022 Hewlett Packard Enterprise Development LP

if [ $# -ne 1 ]; then
echo "usage: $0 <directory>" 1>&2
exit 1
fi

GENERATED_DIR=$1

diff="$(git status -s ${GENERATED_DIR})";
if [[ ! -z $diff ]]; then
echo "Error - Generated files not checked in. Run \"make gen\" and then check-in generated files.";
echo "$diff";
exit 1;
fi
17 changes: 17 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/HewlettPackard/hpegl-metal-client

go 1.17

require (
github.com/antihax/optional v1.0.0
github.com/golang/mock v1.4.4
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
)

require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.7 // indirect
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
)
372 changes: 372 additions & 0 deletions go.sum

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions v1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# (C) Copyright 2020-2022 Hewlett Packard Enterprise Development LP

PWD := $(shell pwd)
GOPATH := $(shell go env GOPATH)

export GOPRIVATE=github.com/HewlettPackard

OUTPUT_DIR="pkg/client"

BIN_DIR := ${PWD}/bin
export IFACEMAKER := ${BIN_DIR}/ifacemaker

# the location of the go mock generator binary
export GO_MOCKGEN := ${BIN_DIR}/mockgen

# The package containing the mocks
export MOCK_CLIENT_PACKAGE=mockquakeclient

# The location of any generated golang mocks
export GO_MOCK_DIR := ${PWD}/pkg/${MOCK_CLIENT_PACKAGE}

default: build

vendor: ../go.mod ../go.sum
go mod download
go mod vendor

.PHONY: validate
validate:
@echo "Running $@"
openapi-generator validate -i ./api/swagger/swagger.yaml

.PHONY: gen
gen: api_files fixup

.PHONY: verify_gen
verify_gen: gen
@echo "Checking the status of the generated client directory"
@bash ../build/verify_generate.sh ${OUTPUT_DIR}

.PHONY: api_files
api_files:
@echo "Running $@"
rm -rf pkg/client
@echo "Generating client from swagger..."
openapi-generator generate --remove-operation-id-prefix -g go -o ${OUTPUT_DIR} -i ./api/swagger/swagger.yaml -p enumClassPrefix=true -p structPrefix=true -p packageName=client
# The generator produces go.mod and go.sum, but we don't want them.
rm -f pkg/client/go.mod pkg/client/go.sum
# additional files that we don't need
rm -f pkg/client/git_push.sh pkg/client/.travis.yml pkg/client/.openapi-generator-ignore

${IFACEMAKER}:
GOBIN="$(@D)" go get github.com/vburenin/ifacemaker@v1.1.0

${GO_MOCKGEN}:
GOBIN="$(@D)" go get github.com/golang/mock/mockgen@v1.5.0

# fixup invokes a script to generate interfaces for all of the
# generated client types to allow consumers to use mocks to test their code that
# interacts with Quake via the client and adds copyrights to all the generated
# files.
.PHONY: fixup
fixup: api_files ${IFACEMAKER} ${GO_MOCKGEN} vendor
@bash fixup_generated_files.bash ${OUTPUT_DIR}

83 changes: 83 additions & 0 deletions v1/fixup_generated_files.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#! /bin/bash
COPYRIGHT="// (C) Copyright 2021-2022 Hewlett Packard Enterprise Development LP"
set -e

if [ $# -ne 1 ]; then
echo "usage: $0 <directory>" 1>&2
exit 1
fi

OUTPUT_DIR=$1
shift

mkdir -p ${GO_MOCK_DIR}

cd ${OUTPUT_DIR}

#
# Generate interfaces for the generated api files
#
echo "Generating interfaces for the client..."
for file in $(ls api_*.go); do
INTERFACE_FILE=$(echo ${file} | sed -e 's/^api_/interface_/g')
MOCK_FILE=$(echo ${file} | sed -e 's/^api_/mock_/g')
TYPE=$(grep "type \([^ ]*\) service" ${file} | sed -e 's/type \([^ ]*\)ApiService service/\1/g')
SERVICE="${TYPE}ApiService"
INTERFACE="${TYPE}API"
# the generator gets the capitalization of IPPools wrong.
if [ "${INTERFACE}" == "IppoolsAPI" ]; then
INTERFACE="IPPoolsAPI"
fi
echo "Generating interface and mocks for ${SERVICE}"
${IFACEMAKER} -p client -y "${INTERFACE} defines the client functions provided for ${TYPE}." --struct=${SERVICE} --file=${file} --output=${INTERFACE_FILE} --iface=${INTERFACE}
sed -i "s/\(${TYPE}Api\) \*${SERVICE}/\1 ${INTERFACE}/g" client.go
${GO_MOCKGEN} --source ${INTERFACE_FILE} --destination ${GO_MOCK_DIR}/${MOCK_FILE} --package=${MOCK_CLIENT_PACKAGE} ${INTERFACE}
done

#
# Create error.go
#
echo "Generating generic-error.go..."

cat - <<*EOF* > generic-error.go
package client
// NewGenericOpenAPIError creates a GenericOpenAPIError from the provided
// parameters.
func NewGenericOpenAPIError(body []byte, error string, model interface{}) GenericOpenAPIError {
return GenericOpenAPIError{
body: body,
error: error,
model: model,
}
}
// Message returns the short error message for display purpose
// If e.model is set and is of type ErrorResponse, then returns the value in ErrorResponse.Message,
// otherwise, returns the entire response body in string format.
func (e GenericOpenAPIError) Message() string {
msg := string(e.Body())
model := e.Model()
if model != nil {
res, ok := model.(ErrorResponse)
if ok {
msg = res.Message
}
}
return msg
}
*EOF*

#
# Add copyrights to all the generated files
#
echo "Adding copyright to generated files..."
for file in $(ls *go) $(ls ${GO_MOCK_DIR}/*.go); do
TMP_FILE="$(mktemp /tmp/copyright.XXXXXX)"
cat - ${file} << *EOF* > ${TMP_FILE}
${COPYRIGHT}
*EOF*
mv ${TMP_FILE} ${file}
done
2 changes: 1 addition & 1 deletion v1/pkg/mockquakeclient/mock_available_resources.go

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

2 changes: 1 addition & 1 deletion v1/pkg/mockquakeclient/mock_hosts.go

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

2 changes: 1 addition & 1 deletion v1/pkg/mockquakeclient/mock_ippools.go

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

2 changes: 1 addition & 1 deletion v1/pkg/mockquakeclient/mock_networks.go

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

2 changes: 1 addition & 1 deletion v1/pkg/mockquakeclient/mock_projects.go

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

2 changes: 1 addition & 1 deletion v1/pkg/mockquakeclient/mock_projects_info.go

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

2 changes: 1 addition & 1 deletion v1/pkg/mockquakeclient/mock_sshkeys.go

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

2 changes: 1 addition & 1 deletion v1/pkg/mockquakeclient/mock_usage_reports.go

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

2 changes: 1 addition & 1 deletion v1/pkg/mockquakeclient/mock_version.go

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

2 changes: 1 addition & 1 deletion v1/pkg/mockquakeclient/mock_volume_attachments.go

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

2 changes: 1 addition & 1 deletion v1/pkg/mockquakeclient/mock_volumes.go

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

0 comments on commit 4dee71b

Please sign in to comment.