Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FLPROD-1370: Add client for CloudConnectorAPI #2698

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2698.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
Add CloudConnectorAPI Client
```
71 changes: 71 additions & 0 deletions cloud_connector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cloudflare

import (
"context"
"fmt"
"net/http"

"github.com/goccy/go-json"
)

type CloudConnectorRulesResponse struct {
Response
Result []CloudConnectorRule `json:"result"`
}

type CloudConnectorRuleParameters struct {
Host string `json:"host"`
}

type CloudConnectorRule struct {
ID string `json:"id"`
Enabled bool `json:"enabled"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Enabled bool `json:"enabled"`
Enabled *bool `json:"enabled"`

https://github.com/cloudflare/cloudflare-go/blob/master/docs/conventions.md#booleans

Expression string `json:"expression"`
Provider string `json:"provider"`
Parameters CloudConnectorRuleParameters `json:"parameters"`
Description string `json:"description"`
}

func (api *API) ListZoneCloudConnectorRules(ctx context.Context, rc *ResourceContainer) ([]CloudConnectorRule, error) {
if rc.Identifier == "" {
return nil, ErrMissingZoneID
}

uri := buildURI(fmt.Sprintf("/zones/%s/cloud_connector/rules", rc.Identifier), nil)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}

result := CloudConnectorRulesResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return nil, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return result.Result, nil
}

func (api *API) UpdateZoneCloudConnectorRules(ctx context.Context, rc *ResourceContainer, params []CloudConnectorRule) ([]CloudConnectorRule, error) {
if rc.Identifier == "" {
return nil, ErrMissingZoneID
}

uri := fmt.Sprintf("/zones/%s/cloud_connector/rules", rc.Identifier)

payload, err := json.Marshal(params)
if err != nil {
return nil, err
}

res, err := api.makeRequestContext(ctx, http.MethodPut, uri, payload)
if err != nil {
return nil, err
}

result := CloudConnectorRulesResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return nil, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return result.Result, nil
}
161 changes: 161 additions & 0 deletions cloud_connector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package cloudflare

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestCloudConnectorRules(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"result": [
{
"id": "some_id_1",
"provider": "aws_s3",
"expression": "true",
"enabled": true,
"description": "some description",
"parameters": {
"host": "aws.s3.bucket"
}
},
{
"id": "some_id_2",
"provider": "r2",
"expression": "true",
"enabled": true,
"description": "some description",
"parameters": {
"host": "r2.hostname"
}
}
],
"success": true,
"errors": [],
"messages": []
}`)
}
mux.HandleFunc("/zones/"+testZoneID+"/cloud_connector/rules", handler)

want := []CloudConnectorRule{
{
ID: "some_id_1",
Provider: "aws_s3",
Expression: "true",
Enabled: true,
Description: "some description",
Parameters: CloudConnectorRuleParameters{
Host: "aws.s3.bucket",
},
},
{
ID: "some_id_2",
Provider: "r2",
Expression: "true",
Enabled: true,
Description: "some description",
Parameters: CloudConnectorRuleParameters{
Host: "r2.hostname",
},
},
}

zoneActual, err := client.ListZoneCloudConnectorRules(context.Background(), ZoneIdentifier(testZoneID))
if assert.NoError(t, err) {
assert.Equal(t, want, zoneActual)
}
}

func TestUpdateCloudConnectorRules(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PATCH', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"result": [
{
"id": "some_id_1",
"provider": "aws_s3",
"expression": "true",
"enabled": true,
"description": "some description",
"parameters": {
"host": "aws.s3.bucket"
}
},
{
"id": "some_id_2",
"provider": "r2",
"expression": "true",
"enabled": true,
"description": "some description",
"parameters": {
"host": "r2.hostname"
}
}
],
"success": true,
"errors": [],
"messages": []
}`)
}

mux.HandleFunc("/zones/"+testZoneID+"/cloud_connector/rules", handler)
toUpdate := []CloudConnectorRule{
{
Provider: "aws_s3",
Expression: "true",
Enabled: true,
Description: "some description",
Parameters: CloudConnectorRuleParameters{
Host: "aws.s3.bucket",
},
},
{
Provider: "r2",
Expression: "true",
Enabled: true,
Description: "some description",
Parameters: CloudConnectorRuleParameters{
Host: "r2.hostname",
},
},
}
want := []CloudConnectorRule{
{
ID: "some_id_1",
Provider: "aws_s3",
Expression: "true",
Enabled: true,
Description: "some description",
Parameters: CloudConnectorRuleParameters{
Host: "aws.s3.bucket",
},
},
{
ID: "some_id_2",
Provider: "r2",
Expression: "true",
Enabled: true,
Description: "some description",
Parameters: CloudConnectorRuleParameters{
Host: "r2.hostname",
},
},
}
zoneActual, err := client.UpdateZoneCloudConnectorRules(context.Background(), ZoneIdentifier(testZoneID), toUpdate)
if assert.NoError(t, err) {
assert.Equal(t, want, zoneActual)
}
}
Loading