diff --git a/README.md b/README.md index 9ba3319..84ad04a 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,17 @@ This project demonstrates an automated workflow for pushing a gRPC API definition into WireMock Cloud so that it can be mocked. -Currently, the only example uses Maven, and does the following: +## Building with Maven + +The example in the `maven` directory does the following: * Build the set of .proto files under `src/main/proto` into a descriptor file. -* Push this into a specific WireMock Cloud mock API via the WireMock Cloud's API. +* Push this into a specific WireMock Cloud mock API via the WireMock Cloud's API. The API ID is determined from the `MOCK_API_ID` env var. + +A [GitHub Actions workflow](.github/workflows/maven-build-and-push.yml) does this on each push to `main`. + + +## Building directly with protoc -A [GitHub Actions workflow](.github/workflows/maven-build-and-push.yml) does this on each push to `main`. \ No newline at end of file +The example in the `protoc` directory does the following: +* Build the set of .proto files under `proto` into the descriptor file `build/services.dsc`. +* Push this into a specific WireMock Cloud mock API via the WireMock Cloud's API. The API ID is determined from the `MOCK_API_ID` env var. diff --git a/protoc/.gitignore b/protoc/.gitignore new file mode 100644 index 0000000..378eac2 --- /dev/null +++ b/protoc/.gitignore @@ -0,0 +1 @@ +build diff --git a/protoc/proto/ExampleServices.proto b/protoc/proto/ExampleServices.proto new file mode 100644 index 0000000..12aa17f --- /dev/null +++ b/protoc/proto/ExampleServices.proto @@ -0,0 +1,40 @@ +/* + * Copyright 2016 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +// 1. syntax, package, option +syntax = "proto3"; + +package com.example.grpc; + +option java_multiple_files = true; +import "request/helloRequest.proto"; +import "response/helloResponse.proto"; +import "google/protobuf/empty.proto"; +import "google/protobuf/any.proto"; + +// 4. service, unary request/response +service GreetingService { + rpc greeting(com.example.grpc.request.HelloRequest) returns (com.example.grpc.response.HelloResponse); + rpc manyGreetingsOneReply(stream com.example.grpc.request.HelloRequest) returns (com.example.grpc.response.HelloResponse); + rpc oneGreetingManyReplies(com.example.grpc.request.HelloRequest) returns (stream com.example.grpc.response.HelloResponse); + rpc oneGreetingEmptyReply(com.example.grpc.request.HelloRequest) returns (google.protobuf.Empty); + rpc greetingAnyRequest(google.protobuf.Any) returns ( com.example.grpc.response.HelloResponse) ; + rpc greetingAnyResponse(com.example.grpc.request.HelloRequest) returns (google.protobuf.Any) ; +} + +service AnotherGreetingService { + rpc differentGreeting(com.example.grpc.request.HelloRequest) returns (com.example.grpc.response.HelloResponse); +} \ No newline at end of file diff --git a/protoc/proto/request/helloRequest.proto b/protoc/proto/request/helloRequest.proto new file mode 100644 index 0000000..60f960b --- /dev/null +++ b/protoc/proto/request/helloRequest.proto @@ -0,0 +1,15 @@ +syntax = "proto3"; + +package com.example.grpc.request; + +option java_multiple_files = true; +import "request/models/sentiment.proto"; + +message HelloRequest { + string name = 1; + int32 age = 2; + repeated string hobbies = 3; + map bagOfTricks = 4; + com.example.grpc.request.models.Sentiment sentiment = 5; +} + diff --git a/protoc/proto/request/models/sentiment.proto b/protoc/proto/request/models/sentiment.proto new file mode 100644 index 0000000..e47d40a --- /dev/null +++ b/protoc/proto/request/models/sentiment.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; + +package com.example.grpc.request.models; + +option java_multiple_files = true; + +enum Sentiment { + HAPPY = 0; + SLEEPY = 1; + ANGRY = 2; +} diff --git a/protoc/proto/response/helloResponse.proto b/protoc/proto/response/helloResponse.proto new file mode 100644 index 0000000..f64fcaf --- /dev/null +++ b/protoc/proto/response/helloResponse.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; + +package com.example.grpc.response; + +option java_multiple_files = true; + +message HelloResponse { + string greeting = 1; +} \ No newline at end of file diff --git a/protoc/push-to-wmc.sh b/protoc/push-to-wmc.sh new file mode 100755 index 0000000..7421dc8 --- /dev/null +++ b/protoc/push-to-wmc.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +set -eo pipefail + +if [ -z "$WIREMOCK_API_TOKEN" ]; then + echo "Environment variable WIREMOCK_API_TOKEN must be set with a valid API token" + exit 1 +fi + +if [ -z "$MOCK_API_ID" ]; then + echo "Environment variable MOCK_API_ID must be set with the target API's ID string e.g. ek2z9" + exit 1 +fi + +mkdir -p build +protoc --descriptor_set_out=build/services.dsc --include_imports --include_source_info --proto_path=proto proto/*.proto + +curl "https://api.wiremock.cloud/v1/mock-apis/$MOCK_API_ID/__admin/ext/grpc/descriptor" \ + -X PUT \ + --data-binary @build/services.dsc \ + -H 'content-type:application/octet-stream' \ + -H "authorization:Token $WIREMOCK_API_TOKEN" +