-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from conduktor/prepare_rest_client
Implement apply
- Loading branch information
Showing
12 changed files
with
263 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export CDK_TOKEN=a | ||
export CDK_BASE_URL=http://localhost:8080/api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"github.com/conduktor/ctl/resource" | ||
"github.com/go-resty/resty/v2" | ||
"os" | ||
) | ||
|
||
type Client struct { | ||
token string | ||
baseUrl string | ||
client *resty.Client | ||
} | ||
|
||
func Make(token string, baseUrl string) Client { | ||
return Client{ | ||
token: token, | ||
baseUrl: baseUrl, | ||
client: resty.New(), | ||
} | ||
} | ||
|
||
func MakeFromEnv() Client { | ||
token := os.Getenv("CDK_TOKEN") | ||
if token == "" { | ||
fmt.Fprintln(os.Stderr, "Please set CDK_TOKEN") | ||
os.Exit(1) | ||
} | ||
baseUrl := os.Getenv("CDK_BASE_URL") | ||
if baseUrl == "" { | ||
fmt.Fprintln(os.Stderr, "Please set CDK_BASE_URL") | ||
os.Exit(2) | ||
} | ||
|
||
return Make(token, baseUrl) | ||
} | ||
|
||
func (client *Client) Apply(resource *resource.Resource) error { | ||
url := client.baseUrl + "/" + resource.Kind | ||
resp, err := client.client.R().SetHeader("Authentication", "Bearer "+client.token).SetBody(resource.Json).Put(url) | ||
if resp.IsError() { | ||
return fmt.Errorf("Error applying resource %s/%s, got status code: %d:\n %s", resource.Kind, resource.Name, resp.StatusCode(), string(resp.Body())) | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/conduktor/ctl/resource" | ||
"github.com/jarcoal/httpmock" | ||
"testing" | ||
) | ||
|
||
func TestApplyShouldWork(t *testing.T) { | ||
baseUrl := "http://baseUrl/api" | ||
token := "aToken" | ||
client := Make(token, baseUrl) | ||
httpmock.ActivateNonDefault( | ||
client.client.GetClient(), | ||
) | ||
responder, err := httpmock.NewJsonResponder(200, "") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
topic := resource.Resource{ | ||
Json: []byte(`{"yolo": "data"}`), | ||
Kind: "topic", | ||
Name: "toto", | ||
ApiVersion: "v1", | ||
} | ||
|
||
httpmock.RegisterMatcherResponderWithQuery( | ||
"PUT", | ||
"http://baseUrl/api/topic", | ||
nil, | ||
httpmock.HeaderIs("Authentication", "Bearer "+token). | ||
And(httpmock.BodyContainsBytes(topic.Json)), | ||
responder, | ||
) | ||
|
||
err = client.Apply(&topic) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestApplyShouldFailIfNo2xx(t *testing.T) { | ||
baseUrl := "http://baseUrl/api" | ||
token := "aToken" | ||
client := Make(token, baseUrl) | ||
httpmock.ActivateNonDefault( | ||
client.client.GetClient(), | ||
) | ||
responder, err := httpmock.NewJsonResponder(400, "") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
topic := resource.Resource{ | ||
Json: []byte(`{"yolo": "data"}`), | ||
Kind: "topic", | ||
Name: "toto", | ||
ApiVersion: "v1", | ||
} | ||
|
||
httpmock.RegisterMatcherResponderWithQuery( | ||
"PUT", | ||
"http://baseUrl/api/topic", | ||
nil, | ||
httpmock.HeaderIs("Authentication", "Bearer "+token). | ||
And(httpmock.BodyContainsBytes(topic.Json)), | ||
responder, | ||
) | ||
|
||
err = client.Apply(&topic) | ||
if err == nil { | ||
t.Failed() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM golang:1.22 | ||
WORKDIR /app | ||
COPY go.mod go.sum ./ | ||
RUN go mod download | ||
COPY . ./ | ||
RUN CGO_ENABLED=0 GOOS=linux go build -o /conduktor . && rm -rf /app | ||
CMD ["/bin/conduktor"] | ||
|
||
FROM scratch | ||
COPY --from=0 /conduktor /bin/conduktor | ||
ENTRYPOINT ["/bin/conduktor"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
version: '3' | ||
services: | ||
conduktor: | ||
build: | ||
dockerfile: docker/Dockerfile | ||
context: .. | ||
environment: | ||
CDK_TOKEN: yo | ||
CDK_BASE_URL: http://mock:1080/api | ||
volumes: | ||
- ./test_resource.yml:/test_resource.yml | ||
mock: | ||
image: mockserver/mockserver:latest | ||
volumes: | ||
- ./initializer.json:/config/initializer.json | ||
environment: | ||
MOCKSERVER_INITIALIZATION_JSON_PATH: /config/initializer.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[ | ||
{ | ||
"httpRequest": { | ||
"method": "Put", | ||
"path": "/api/Topic", | ||
"headers": { | ||
"Authentication": "Bearer yo" | ||
} | ||
}, | ||
"httpResponse": { | ||
"statusCode": 200, | ||
"body": "{}", | ||
"headers": { | ||
"Content-Type": ["application/json"] | ||
} | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/bin/bash -eu | ||
|
||
echoerr() { echo "$@" 1>&2; } | ||
|
||
SCRIPTDIR=$(dirname "$0") | ||
|
||
function cleanup { | ||
docker compose -f "$SCRIPTDIR/docker/docker-compose.yml" down | ||
} | ||
|
||
|
||
trap cleanup EXIT | ||
main() { | ||
cd "$SCRIPTDIR" | ||
docker compose -f docker/docker-compose.yml up -d mock | ||
sleep 1 | ||
docker compose -f docker/docker-compose.yml run conduktor apply -f /test_resource.yml | ||
} | ||
|
||
main "$@" |