Skip to content

Commit

Permalink
Merge pull request #2698 from WowVeryLogin/denis/FLPROD-1370
Browse files Browse the repository at this point in the history
FLPROD-1370: Add client for CloudConnectorAPI
  • Loading branch information
jacobbednarz authored Jul 12, 2024
2 parents 45e334a + cf068c5 commit d5733c2
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 0 deletions.
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,omitempty"`
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: BoolPtr(true),
Description: "some description",
Parameters: CloudConnectorRuleParameters{
Host: "aws.s3.bucket",
},
},
{
ID: "some_id_2",
Provider: "r2",
Expression: "true",
Enabled: BoolPtr(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: BoolPtr(true),
Description: "some description",
Parameters: CloudConnectorRuleParameters{
Host: "aws.s3.bucket",
},
},
{
Provider: "r2",
Expression: "true",
Enabled: BoolPtr(true),
Description: "some description",
Parameters: CloudConnectorRuleParameters{
Host: "r2.hostname",
},
},
}
want := []CloudConnectorRule{
{
ID: "some_id_1",
Provider: "aws_s3",
Expression: "true",
Enabled: BoolPtr(true),
Description: "some description",
Parameters: CloudConnectorRuleParameters{
Host: "aws.s3.bucket",
},
},
{
ID: "some_id_2",
Provider: "r2",
Expression: "true",
Enabled: BoolPtr(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)
}
}

0 comments on commit d5733c2

Please sign in to comment.