Skip to content
Snippets Groups Projects
Commit 10969fa7 authored by Lyuben Penkovski's avatar Lyuben Penkovski
Browse files

Merge branch '7-import-trusted-data' into 'main'

HTTP endpoint to import trusted data

Closes #7

See merge request !6
parents 0d4c798b 508379a6
No related branches found
No related tags found
1 merge request!6HTTP endpoint to import trusted data
Pipeline #51827 passed with stages
in 1 minute and 48 seconds
Showing
with 200 additions and 22 deletions
......@@ -18,6 +18,7 @@ import (
"golang.org/x/sync/errgroup"
"code.vereign.com/gaiax/tsa/golib/cache"
"code.vereign.com/gaiax/tsa/golib/goadec"
"code.vereign.com/gaiax/tsa/golib/graceful"
goahealth "code.vereign.com/gaiax/tsa/infohub/gen/health"
goahealthsrv "code.vereign.com/gaiax/tsa/infohub/gen/http/health/server"
......@@ -41,7 +42,7 @@ func main() {
// load configuration from environment
var cfg config.Config
if err := envconfig.Process("", &cfg); err != nil {
log.Fatalf("cannot load configuration: %v", err)
log.Fatalf("cannot load configuration: %v\n", err)
}
// create logger
......@@ -134,6 +135,17 @@ func main() {
openapiServer = goaopenapisrv.New(openapiEndpoints, mux, dec, enc, nil, errFormatter, nil, nil)
}
// set custom request decoder, so that request body bytes are simply
// read and not decoded in some other way
infohubServer.Import = goainfohubsrv.NewImportHandler(
infohubEndpoints.Import,
mux,
goadec.BytesDecoder,
enc,
nil,
errFormatter,
)
// Configure the mux.
goainfohubsrv.Mount(mux, infohubServer)
goahealthsrv.Mount(mux, healthServer)
......
......@@ -27,6 +27,17 @@ var _ = Service("infohub", func() {
Response(StatusOK)
})
})
Method("Import", func() {
Description("Import the given data wrapped as Verifiable Presentation into the Cache.")
Payload(ImportRequest)
Result(ImportResult)
HTTP(func() {
POST("/v1/import")
Body("data")
Response(StatusOK)
})
})
})
var _ = Service("health", func() {
......
......@@ -5,7 +5,21 @@ import . "goa.design/goa/v3/dsl"
var ExportRequest = Type("ExportRequest", func() {
Field(1, "exportName", String, "Name of export to be performed.", func() {
Example("myexport")
Example("testexport")
})
Required("exportName")
})
var ImportRequest = Type("ImportRequest", func() {
Field(1, "data", Bytes, "Data wrapped in Verifiable Presentation that will be imported into Cache.", func() {
Example("data")
})
Required("data")
})
var ImportResult = Type("ImportResult", func() {
Field(1, "importIds", ArrayOf(String), "importIds is an array of unique identifiers used as Cache keys to retrieve the imported data entries later.", func() {
Example([]string{"585a999a-f36d-419d-bed3-8ebfa5bb79c9"})
})
Required("importIds")
})
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// health client
//
......
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// health endpoints
//
......
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// health service
//
......
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// infohub HTTP client CLI support package
//
......@@ -25,14 +25,14 @@ import (
//
func UsageCommands() string {
return `health (liveness|readiness)
infohub export
infohub (export|import)
`
}
// UsageExamples produces an example of a valid invocation of the CLI tool.
func UsageExamples() string {
return os.Args[0] + ` health liveness` + "\n" +
os.Args[0] + ` infohub export --export-name "myexport"` + "\n" +
os.Args[0] + ` infohub export --export-name "testexport"` + "\n" +
""
}
......@@ -56,6 +56,9 @@ func ParseEndpoint(
infohubExportFlags = flag.NewFlagSet("export", flag.ExitOnError)
infohubExportExportNameFlag = infohubExportFlags.String("export-name", "REQUIRED", "Name of export to be performed.")
infohubImportFlags = flag.NewFlagSet("import", flag.ExitOnError)
infohubImportBodyFlag = infohubImportFlags.String("body", "REQUIRED", "")
)
healthFlags.Usage = healthUsage
healthLivenessFlags.Usage = healthLivenessUsage
......@@ -63,6 +66,7 @@ func ParseEndpoint(
infohubFlags.Usage = infohubUsage
infohubExportFlags.Usage = infohubExportUsage
infohubImportFlags.Usage = infohubImportUsage
if err := flag.CommandLine.Parse(os.Args[1:]); err != nil {
return nil, nil, err
......@@ -113,6 +117,9 @@ func ParseEndpoint(
case "export":
epf = infohubExportFlags
case "import":
epf = infohubImportFlags
}
}
......@@ -151,6 +158,9 @@ func ParseEndpoint(
case "export":
endpoint = c.Export()
data, err = infohubc.BuildExportPayload(*infohubExportExportNameFlag)
case "import":
endpoint = c.Import()
data, err = infohubc.BuildImportPayload(*infohubImportBodyFlag)
}
}
}
......@@ -203,6 +213,7 @@ Usage:
COMMAND:
export: Export returns data signed as Verifiable Presentation.
import: Import the given data wrapped as Verifiable Presentation into the Cache.
Additional help:
%[1]s infohub COMMAND --help
......@@ -215,6 +226,17 @@ Export returns data signed as Verifiable Presentation.
-export-name STRING: Name of export to be performed.
Example:
%[1]s infohub export --export-name "myexport"
%[1]s infohub export --export-name "testexport"
`, os.Args[0])
}
func infohubImportUsage() {
fmt.Fprintf(os.Stderr, `%[1]s [flags] infohub import -body STRING
Import the given data wrapped as Verifiable Presentation into the Cache.
-body STRING:
Example:
%[1]s infohub import --body "data"
`, os.Args[0])
}
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// health HTTP client CLI support package
//
......
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// health client HTTP transport
//
......
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// health HTTP client encoders and decoders
//
......
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// HTTP request path constructors for the health service.
//
......
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// health HTTP client types
//
......
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// health HTTP server encoders and decoders
//
......
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// HTTP request path constructors for the health service.
//
......
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// health HTTP server
//
......
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// health HTTP server types
//
......
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// infohub HTTP client CLI support package
//
......@@ -23,3 +23,18 @@ func BuildExportPayload(infohubExportExportName string) (*infohub.ExportRequest,
return v, nil
}
// BuildImportPayload builds the payload for the infohub Import endpoint from
// CLI flags.
func BuildImportPayload(infohubImportBody string) (*infohub.ImportRequest, error) {
var body []byte
{
body = []byte(infohubImportBody)
}
v := body
res := &infohub.ImportRequest{
Data: v,
}
return res, nil
}
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// infohub client HTTP transport
//
......@@ -20,6 +20,9 @@ type Client struct {
// Export Doer is the HTTP client used to make requests to the Export endpoint.
ExportDoer goahttp.Doer
// Import Doer is the HTTP client used to make requests to the Import endpoint.
ImportDoer goahttp.Doer
// RestoreResponseBody controls whether the response bodies are reset after
// decoding so they can be read again.
RestoreResponseBody bool
......@@ -41,6 +44,7 @@ func NewClient(
) *Client {
return &Client{
ExportDoer: doer,
ImportDoer: doer,
RestoreResponseBody: restoreBody,
scheme: scheme,
host: host,
......@@ -67,3 +71,27 @@ func (c *Client) Export() goa.Endpoint {
return decodeResponse(resp)
}
}
// Import returns an endpoint that makes HTTP requests to the infohub service
// Import server.
func (c *Client) Import() goa.Endpoint {
var (
encodeRequest = EncodeImportRequest(c.encoder)
decodeResponse = DecodeImportResponse(c.decoder, c.RestoreResponseBody)
)
return func(ctx context.Context, v interface{}) (interface{}, error) {
req, err := c.BuildImportRequest(ctx, v)
if err != nil {
return nil, err
}
err = encodeRequest(req, v)
if err != nil {
return nil, err
}
resp, err := c.ImportDoer.Do(req)
if err != nil {
return nil, goahttp.ErrRequestError("infohub", "Import", err)
}
return decodeResponse(resp)
}
}
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// infohub HTTP client encoders and decoders
//
......@@ -77,3 +77,74 @@ func DecodeExportResponse(decoder func(*http.Response) goahttp.Decoder, restoreB
}
}
}
// BuildImportRequest instantiates a HTTP request object with method and path
// set to call the "infohub" service "Import" endpoint
func (c *Client) BuildImportRequest(ctx context.Context, v interface{}) (*http.Request, error) {
u := &url.URL{Scheme: c.scheme, Host: c.host, Path: ImportInfohubPath()}
req, err := http.NewRequest("POST", u.String(), nil)
if err != nil {
return nil, goahttp.ErrInvalidURL("infohub", "Import", u.String(), err)
}
if ctx != nil {
req = req.WithContext(ctx)
}
return req, nil
}
// EncodeImportRequest returns an encoder for requests sent to the infohub
// Import server.
func EncodeImportRequest(encoder func(*http.Request) goahttp.Encoder) func(*http.Request, interface{}) error {
return func(req *http.Request, v interface{}) error {
p, ok := v.(*infohub.ImportRequest)
if !ok {
return goahttp.ErrInvalidType("infohub", "Import", "*infohub.ImportRequest", v)
}
body := p.Data
if err := encoder(req).Encode(&body); err != nil {
return goahttp.ErrEncodingError("infohub", "Import", err)
}
return nil
}
}
// DecodeImportResponse returns a decoder for responses returned by the infohub
// Import endpoint. restoreBody controls whether the response body should be
// restored after having been read.
func DecodeImportResponse(decoder func(*http.Response) goahttp.Decoder, restoreBody bool) func(*http.Response) (interface{}, error) {
return func(resp *http.Response) (interface{}, error) {
if restoreBody {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
defer func() {
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))
}()
} else {
defer resp.Body.Close()
}
switch resp.StatusCode {
case http.StatusOK:
var (
body ImportResponseBody
err error
)
err = decoder(resp).Decode(&body)
if err != nil {
return nil, goahttp.ErrDecodingError("infohub", "Import", err)
}
err = ValidateImportResponseBody(&body)
if err != nil {
return nil, goahttp.ErrValidationError("infohub", "Import", err)
}
res := NewImportResultOK(&body)
return res, nil
default:
body, _ := ioutil.ReadAll(resp.Body)
return nil, goahttp.ErrInvalidResponse("infohub", "Import", resp.StatusCode, string(body))
}
}
}
// Code generated by goa v3.7.5, DO NOT EDIT.
// Code generated by goa v3.7.6, DO NOT EDIT.
//
// HTTP request path constructors for the infohub service.
//
......@@ -15,3 +15,8 @@ import (
func ExportInfohubPath(exportName string) string {
return fmt.Sprintf("/v1/export/%v", exportName)
}
// ImportInfohubPath returns the URL path to the infohub service Import HTTP endpoint.
func ImportInfohubPath() string {
return "/v1/import"
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment