From 1b4db333d3dc147a09081bbe4abe368498777cf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Ferr=C3=A3o?= Date: Wed, 10 Apr 2024 17:28:24 +0100 Subject: [PATCH 1/5] add example for middlewares --- examples/middleware/go.mod | 13 +++++++ examples/middleware/go.sum | 9 +++++ examples/middleware/middleware.go | 62 +++++++++++++++++++++++++++++++ go.work | 1 + 4 files changed, 85 insertions(+) create mode 100644 examples/middleware/go.mod create mode 100644 examples/middleware/go.sum create mode 100644 examples/middleware/middleware.go diff --git a/examples/middleware/go.mod b/examples/middleware/go.mod new file mode 100644 index 00000000..2c483dad --- /dev/null +++ b/examples/middleware/go.mod @@ -0,0 +1,13 @@ +module github.com/stackitcloud/stackit-sdk-go/examples/middleware + +go 1.18 + +require ( + github.com/stackitcloud/stackit-sdk-go/core v0.10.1 + github.com/stackitcloud/stackit-sdk-go/services/argus v0.9.5 +) + +require ( + github.com/golang-jwt/jwt/v5 v5.2.1 // indirect + github.com/google/uuid v1.6.0 // indirect +) diff --git a/examples/middleware/go.sum b/examples/middleware/go.sum new file mode 100644 index 00000000..09424e49 --- /dev/null +++ b/examples/middleware/go.sum @@ -0,0 +1,9 @@ +github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= +github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/stackitcloud/stackit-sdk-go/core v0.10.1 h1:lzyualywD/2xIsYUHwlqCurG1OwlqCJVtJbOcPO6OzE= +github.com/stackitcloud/stackit-sdk-go/core v0.10.1/go.mod h1:mDX1mSTsB3mP+tNBGcFNx6gH1mGBN4T+dVt+lcw7nlw= +github.com/stackitcloud/stackit-sdk-go/services/argus v0.9.5 h1:RLsA2jO9sMNpn7NYdtFyumY5Vj4n4WtBm2J2NBKlsnw= +github.com/stackitcloud/stackit-sdk-go/services/argus v0.9.5/go.mod h1:lzGbqwV0hqeX/kUvaaFTgjOJRxUlsZ911TX1YAcKwqc= diff --git a/examples/middleware/middleware.go b/examples/middleware/middleware.go new file mode 100644 index 00000000..e603bb29 --- /dev/null +++ b/examples/middleware/middleware.go @@ -0,0 +1,62 @@ +package main + +import ( + "context" + "fmt" + "net/http" + "os" + + "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/services/argus" +) + +// RequestCapturer is a middleware that captures the request and appends it to a variable. +func RequestCapturer(status string) config.Middleware { + return func(rt http.RoundTripper) http.RoundTripper { + return &roundTripperWithCapture{rt, status} + } +} + +// roundTripperWithCapture wraps an existing RoundTripper and captures requests. +type roundTripperWithCapture struct { + transport http.RoundTripper + status string +} + +func (rt roundTripperWithCapture) RoundTrip(req *http.Request) (*http.Response, error) { + returnStr := fmt.Sprintf("%s %s", req.Method, req.URL.String()) + + fmt.Println("Captured request:", returnStr) + fmt.Println("Status:", rt.status) + + // Proceed with the original round trip + return rt.transport.RoundTrip(req) +} + +func main() { + // Specify the project ID + projectId := "PROJECT_ID" + + // Create a new API client, that uses default authentication + // Use the `WithMiddleware` option to add the middleware to the client + // The middleware will be executed in the reverse order they are added + // In this case, the middleware with status "1" will be executed first + argusClient, err := argus.NewAPIClient( + config.WithRegion("eu01"), + config.WithMiddleware(RequestCapturer("2")), + config.WithMiddleware(RequestCapturer("1")), + ) + if err != nil { + fmt.Fprintf(os.Stderr, "[Argus API] Creating API client: %v\n", err) + os.Exit(1) + } + + // Get the argus instances for your project + getInstanceResp, err := argusClient.ListInstances(context.Background(), projectId).Execute() + + if err != nil { + fmt.Fprintf(os.Stderr, "[Argus API] Error when calling `GetInstances`: %v\n", err) + } else { + fmt.Printf("[Argus API] Number of instances: %v\n", len(*getInstanceResp.Instances)) + } +} diff --git a/go.work b/go.work index 2ba5ee7a..64c77f8f 100644 --- a/go.work +++ b/go.work @@ -13,6 +13,7 @@ use ( ./examples/loadbalancer ./examples/logme ./examples/mariadb + ./examples/middleware ./examples/mongodbflex ./examples/objectstorage ./examples/opensearch From 3c9cb36ff431e48a9b06e7144db268665e17f2fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Ferr=C3=A3o?= Date: Wed, 10 Apr 2024 17:32:56 +0100 Subject: [PATCH 2/5] improve documentation of example --- examples/middleware/middleware.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/middleware/middleware.go b/examples/middleware/middleware.go index e603bb29..5c2ea642 100644 --- a/examples/middleware/middleware.go +++ b/examples/middleware/middleware.go @@ -10,14 +10,14 @@ import ( "github.com/stackitcloud/stackit-sdk-go/services/argus" ) -// RequestCapturer is a middleware that captures the request and appends it to a variable. +// RequestCapturer is a middleware that prints the request and a status it receives. func RequestCapturer(status string) config.Middleware { return func(rt http.RoundTripper) http.RoundTripper { return &roundTripperWithCapture{rt, status} } } -// roundTripperWithCapture wraps an existing RoundTripper and captures requests. +// roundTripperWithCapture is a custom round tripper that prints the request and a status it receives. type roundTripperWithCapture struct { transport http.RoundTripper status string @@ -30,6 +30,7 @@ func (rt roundTripperWithCapture) RoundTrip(req *http.Request) (*http.Response, fmt.Println("Status:", rt.status) // Proceed with the original round trip + // This step is important to preserve the original behavior of the client return rt.transport.RoundTrip(req) } From c0a2d6e7afaecda07edbc59e5c8b14f898d2afac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Ferr=C3=A3o?= Date: Thu, 11 Apr 2024 15:30:59 +0100 Subject: [PATCH 3/5] update go mod, improve examples --- examples/middleware/go.mod | 4 ++-- examples/middleware/middleware.go | 18 +++++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/examples/middleware/go.mod b/examples/middleware/go.mod index 2c483dad..a425b01e 100644 --- a/examples/middleware/go.mod +++ b/examples/middleware/go.mod @@ -3,8 +3,8 @@ module github.com/stackitcloud/stackit-sdk-go/examples/middleware go 1.18 require ( - github.com/stackitcloud/stackit-sdk-go/core v0.10.1 - github.com/stackitcloud/stackit-sdk-go/services/argus v0.9.5 + github.com/stackitcloud/stackit-sdk-go/core v0.12.0 + github.com/stackitcloud/stackit-sdk-go/services/argus v0.10.0 ) require ( diff --git a/examples/middleware/middleware.go b/examples/middleware/middleware.go index 5c2ea642..5e75ee1a 100644 --- a/examples/middleware/middleware.go +++ b/examples/middleware/middleware.go @@ -17,21 +17,25 @@ func RequestCapturer(status string) config.Middleware { } } -// roundTripperWithCapture is a custom round tripper that prints the request and a status it receives. +// roundTripperWithCapture is a custom round tripper that prints the request and an input it receives. type roundTripperWithCapture struct { - transport http.RoundTripper - status string + transport http.RoundTripper + inputString string } func (rt roundTripperWithCapture) RoundTrip(req *http.Request) (*http.Response, error) { returnStr := fmt.Sprintf("%s %s", req.Method, req.URL.String()) fmt.Println("Captured request:", returnStr) - fmt.Println("Status:", rt.status) + fmt.Println("Input:", rt.inputString) // Proceed with the original round trip // This step is important to preserve the original behavior of the client - return rt.transport.RoundTrip(req) + resp, err := rt.transport.RoundTrip(req) + + fmt.Println("Captured response status:", resp.Status) + + return resp, err } func main() { @@ -44,8 +48,8 @@ func main() { // In this case, the middleware with status "1" will be executed first argusClient, err := argus.NewAPIClient( config.WithRegion("eu01"), - config.WithMiddleware(RequestCapturer("2")), - config.WithMiddleware(RequestCapturer("1")), + config.WithMiddleware(RequestCapturer("Middleware 2")), + config.WithMiddleware(RequestCapturer("Middleware 1")), ) if err != nil { fmt.Fprintf(os.Stderr, "[Argus API] Creating API client: %v\n", err) From 150db2ad2a5926723830af21ca7ae7352930162e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Ferr=C3=A3o?= Date: Thu, 11 Apr 2024 15:35:02 +0100 Subject: [PATCH 4/5] update go sum --- examples/middleware/go.sum | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/middleware/go.sum b/examples/middleware/go.sum index 09424e49..1edca131 100644 --- a/examples/middleware/go.sum +++ b/examples/middleware/go.sum @@ -3,7 +3,7 @@ github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVI github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/stackitcloud/stackit-sdk-go/core v0.10.1 h1:lzyualywD/2xIsYUHwlqCurG1OwlqCJVtJbOcPO6OzE= -github.com/stackitcloud/stackit-sdk-go/core v0.10.1/go.mod h1:mDX1mSTsB3mP+tNBGcFNx6gH1mGBN4T+dVt+lcw7nlw= -github.com/stackitcloud/stackit-sdk-go/services/argus v0.9.5 h1:RLsA2jO9sMNpn7NYdtFyumY5Vj4n4WtBm2J2NBKlsnw= -github.com/stackitcloud/stackit-sdk-go/services/argus v0.9.5/go.mod h1:lzGbqwV0hqeX/kUvaaFTgjOJRxUlsZ911TX1YAcKwqc= +github.com/stackitcloud/stackit-sdk-go/core v0.12.0 h1:auIzUUNRuydKOScvpICP4MifGgvOajiDQd+ncGmBL0U= +github.com/stackitcloud/stackit-sdk-go/core v0.12.0/go.mod h1:mDX1mSTsB3mP+tNBGcFNx6gH1mGBN4T+dVt+lcw7nlw= +github.com/stackitcloud/stackit-sdk-go/services/argus v0.10.0 h1:FAYOt6UBy/F2jPH2C/NnZnbjLZryJBjtM3afLVgGc4w= +github.com/stackitcloud/stackit-sdk-go/services/argus v0.10.0/go.mod h1:nVllQfYODhX1q3bgwVTLO7wHOp+8NMLiKbn3u/Dg5nU= From c48ab53de2185cad5bc0b2243925d0ab72ead9e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diogo=20Ferr=C3=A3o?= Date: Thu, 11 Apr 2024 16:09:46 +0100 Subject: [PATCH 5/5] move httpresponse example to runtime.go, added example for WithCaptureHTTPRequest --- examples/{httpresponse => runtime}/go.mod | 6 +++--- examples/{httpresponse => runtime}/go.sum | 8 ++++---- .../httpresponse.go => runtime/runtime.go} | 13 ++++++++++++- go.work | 2 +- 4 files changed, 20 insertions(+), 9 deletions(-) rename examples/{httpresponse => runtime}/go.mod (60%) rename examples/{httpresponse => runtime}/go.sum (68%) rename examples/{httpresponse/httpresponse.go => runtime/runtime.go} (64%) diff --git a/examples/httpresponse/go.mod b/examples/runtime/go.mod similarity index 60% rename from examples/httpresponse/go.mod rename to examples/runtime/go.mod index d3ff87af..2298c611 100644 --- a/examples/httpresponse/go.mod +++ b/examples/runtime/go.mod @@ -1,10 +1,10 @@ -module github.com/stackitcloud/stackit-sdk-go/examples/httpresponse +module github.com/stackitcloud/stackit-sdk-go/examples/runtime go 1.18 require ( - github.com/stackitcloud/stackit-sdk-go/core v0.10.1 - github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.12.0 + github.com/stackitcloud/stackit-sdk-go/core v0.12.0 + github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.13.0 ) require ( diff --git a/examples/httpresponse/go.sum b/examples/runtime/go.sum similarity index 68% rename from examples/httpresponse/go.sum rename to examples/runtime/go.sum index 4c1e5a64..0a610e6c 100644 --- a/examples/httpresponse/go.sum +++ b/examples/runtime/go.sum @@ -3,7 +3,7 @@ github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVI github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/stackitcloud/stackit-sdk-go/core v0.10.1 h1:lzyualywD/2xIsYUHwlqCurG1OwlqCJVtJbOcPO6OzE= -github.com/stackitcloud/stackit-sdk-go/core v0.10.1/go.mod h1:mDX1mSTsB3mP+tNBGcFNx6gH1mGBN4T+dVt+lcw7nlw= -github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.12.0 h1:W2WSYUyhKaHQ+BZfmyRw9PKv5q7ihGRyNhNgIlyM+Y8= -github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.12.0/go.mod h1:P0YyvgwIsVKJijdWGVJVOp/ac7PVX99Oj+dr4v1zECc= +github.com/stackitcloud/stackit-sdk-go/core v0.12.0 h1:auIzUUNRuydKOScvpICP4MifGgvOajiDQd+ncGmBL0U= +github.com/stackitcloud/stackit-sdk-go/core v0.12.0/go.mod h1:mDX1mSTsB3mP+tNBGcFNx6gH1mGBN4T+dVt+lcw7nlw= +github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.13.0 h1:PGLjBZxWM7NIrH1+W1+f+/4kZEgwv9DGnXcUzOqM0M8= +github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.13.0/go.mod h1:SdrqGLCkilL6wl1+jcxmLtks2IocgIg+bsyeyYUIzR4= diff --git a/examples/httpresponse/httpresponse.go b/examples/runtime/runtime.go similarity index 64% rename from examples/httpresponse/httpresponse.go rename to examples/runtime/runtime.go index d0fedbf5..dc18500d 100644 --- a/examples/httpresponse/httpresponse.go +++ b/examples/runtime/runtime.go @@ -33,5 +33,16 @@ func main() { os.Exit(1) } fmt.Printf("Number of instances: %v\n\n", len(*getInstancesResp.Items)) - fmt.Printf("HTTP response: %+v\n", *httpResp) + fmt.Printf("HTTP response: %+v\n\n", *httpResp) + + // Get the MongoDB Flex instances for your project and capture the HTTP request using the context + var httpReq *http.Request + ctxWithHTTPReq := runtime.WithCaptureHTTPRequest(context.Background(), &httpReq) + getInstancesResp, err = postgresflexClient.ListInstances(ctxWithHTTPReq, projectId).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "Error when calling `ListInstances`: %v\n", err) + os.Exit(1) + } + fmt.Printf("Number of instances: %v\n\n", len(*getInstancesResp.Items)) + fmt.Printf("HTTP request: %+v\n\n", *httpReq) } diff --git a/go.work b/go.work index 64c77f8f..9b55cbe2 100644 --- a/go.work +++ b/go.work @@ -9,7 +9,6 @@ use ( ./examples/configuration ./examples/dns ./examples/errorhandling - ./examples/httpresponse ./examples/loadbalancer ./examples/logme ./examples/mariadb @@ -22,6 +21,7 @@ use ( ./examples/rabbitmq ./examples/redis ./examples/resourcemanager + ./examples/runtime ./examples/secretsmanager ./examples/serviceaccount ./examples/ske