Skip to content

Commit

Permalink
feat(config_store): implement batch endpoint (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Jun 7, 2023
1 parent 52d2658 commit 41eb1ff
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 0 deletions.
39 changes: 39 additions & 0 deletions fastly/config_store_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,42 @@ func (c *Client) UpdateConfigStoreItem(i *UpdateConfigStoreItemInput) (*ConfigSt

return csi, nil
}

// BatchModifyConfigStoreItemsInput is the input parameter to the
// BatchModifyConfigStoreItems function.
type BatchModifyConfigStoreItemsInput struct {
// StoreID is the ID of the Config Store to modify items for (required).
StoreID string `json:"-"`
// Items is a list of Config Store items.
Items []*BatchConfigStoreItem `json:"items"`
}

// BatchDictionaryItem represents a dictionary item.
type BatchConfigStoreItem struct {
// ItemKey is an item key (maximum 256 characters).
ItemKey string `json:"item_key"`
// ItemValue is an item value (maximum 8000 characters).
ItemValue string `json:"item_value"`
// Operation is a batching operation variant.
Operation BatchOperation `json:"op"`
}

// BatchModifyConfigStoreItems bulk updates dictionary items.
func (c *Client) BatchModifyConfigStoreItems(i *BatchModifyConfigStoreItemsInput) error {
if i.StoreID == "" {
return ErrMissingStoreID
}
if len(i.Items) > BatchModifyMaximumOperations {
return ErrMaxExceededItems
}

path := fmt.Sprintf("/resources/stores/config/%s/items", i.StoreID)
resp, err := c.PatchJSON(path, i, nil)
if err != nil {
return err
}
defer resp.Body.Close()

var batchModifyResult map[string]string
return decodeBodyMap(resp.Body, &batchModifyResult)
}
106 changes: 106 additions & 0 deletions fastly/config_store_item_batch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package fastly

import (
"fmt"
"testing"
)

func TestClient_ConfigStoreBatch(t *testing.T) {
t.Parallel()

cs := createConfigStoreForBatch(t)

var err error

record(t, "config_store_batch/create_and_upsert", func(c *Client) {
err = c.BatchModifyConfigStoreItems(&BatchModifyConfigStoreItemsInput{
StoreID: cs.ID,
Items: []*BatchConfigStoreItem{
{
ItemKey: "key1",
ItemValue: "value1",
Operation: CreateBatchOperation,
},
{
ItemKey: "key2",
ItemValue: "value2",
Operation: UpsertBatchOperation,
},
},
})
})
if err != nil {
t.Fatalf("error batch creating config store items: %v", err)
}

record(t, "config_store_batch/update_and_upsert", func(c *Client) {
err = c.BatchModifyConfigStoreItems(&BatchModifyConfigStoreItemsInput{
StoreID: cs.ID,
Items: []*BatchConfigStoreItem{
{
ItemKey: "key1",
ItemValue: "value2",
Operation: UpdateBatchOperation,
},
{
ItemKey: "key2",
ItemValue: "value3",
Operation: UpsertBatchOperation,
},
},
})
})
if err != nil {
t.Fatalf("error batch updating config store items: %v", err)
}

record(t, "config_store_batch/delete", func(c *Client) {
err = c.BatchModifyConfigStoreItems(&BatchModifyConfigStoreItemsInput{
StoreID: cs.ID,
Items: []*BatchConfigStoreItem{
{
ItemKey: "key1",
Operation: DeleteBatchOperation,
},
{
ItemKey: "key2",
Operation: DeleteBatchOperation,
},
},
})
})
if err != nil {
t.Fatalf("error batch deleting config store items: %v", err)
}
}

func createConfigStoreForBatch(t *testing.T) *ConfigStore {
t.Helper()

var (
cs *ConfigStore
err error
)
record(t, fmt.Sprintf("config_store_batch/%s/create_store", t.Name()), func(c *Client) {
cs, err = c.CreateConfigStore(&CreateConfigStoreInput{
Name: t.Name(),
})
})
if err != nil {
t.Fatalf("error creating config store: %v", err)
}

// Ensure Config Store is cleaned up.
t.Cleanup(func() {
record(t, fmt.Sprintf("config_store_batch/%s/delete_store", t.Name()), func(c *Client) {
err = c.DeleteConfigStore(&DeleteConfigStoreInput{
ID: cs.ID,
})
})
if err != nil {
t.Fatalf("error deleting config store %q: %v", cs.ID, err)
}
})

return cs
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
version: 1
interactions:
- request:
body: name=TestClient_ConfigStoreBatch
form:
name:
- TestClient_ConfigStoreBatch
headers:
Accept:
- application/json
Content-Type:
- application/x-www-form-urlencoded
User-Agent:
- FastlyGo/8.4.1 (+github.com/fastly/go-fastly; go1.18.5)
url: https://api.fastly.com/resources/stores/config
method: POST
response:
body: '{"name":"TestClient_ConfigStoreBatch","id":"LO53FrkHHmWWropCH2ICN2","created_at":"2023-06-07T16:29:19Z","updated_at":"2023-06-07T16:29:19Z","deleted_at":null}'
headers:
Accept-Ranges:
- bytes
Cache-Control:
- no-store
Content-Type:
- application/json
Date:
- Wed, 07 Jun 2023 16:29:19 GMT
Fastly-Ratelimit-Remaining:
- "9999"
Fastly-Ratelimit-Reset:
- "1686157200"
Status:
- 200 OK
Strict-Transport-Security:
- max-age=31536000
Vary:
- Accept-Encoding
Via:
- 1.1 varnish, 1.1 varnish
X-Cache:
- MISS, MISS
X-Cache-Hits:
- 0, 0
X-Served-By:
- cache-control-cp-aws-us-east-2-prod-6-CONTROL-AWS-UE2, cache-lhr7336-LHR
X-Timer:
- S1686155360.565387,VS0,VE166
status: 200 OK
code: 200
duration: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
version: 1
interactions:
- request:
body: ""
form: {}
headers:
Accept:
- application/json
User-Agent:
- FastlyGo/8.4.1 (+github.com/fastly/go-fastly; go1.18.5)
url: https://api.fastly.com/resources/stores/config/LO53FrkHHmWWropCH2ICN2
method: DELETE
response:
body: '{"status":"ok"}'
headers:
Accept-Ranges:
- bytes
Cache-Control:
- no-store
Content-Type:
- application/json
Date:
- Wed, 07 Jun 2023 16:29:20 GMT
Fastly-Ratelimit-Remaining:
- "9995"
Fastly-Ratelimit-Reset:
- "1686157200"
Status:
- 200 OK
Strict-Transport-Security:
- max-age=31536000
Vary:
- Accept-Encoding
Via:
- 1.1 varnish, 1.1 varnish
X-Cache:
- MISS, MISS
X-Cache-Hits:
- 0, 0
X-Served-By:
- cache-control-cp-aws-us-east-2-prod-7-CONTROL-AWS-UE2, cache-lhr7336-LHR
X-Timer:
- S1686155360.335549,VS0,VE173
status: 200 OK
code: 200
duration: ""
49 changes: 49 additions & 0 deletions fastly/fixtures/config_store_batch/create_and_upsert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
version: 1
interactions:
- request:
body: '{"items":[{"item_key":"key1","item_value":"value1","op":"create"},{"item_key":"key2","item_value":"value2","op":"upsert"}]}'
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
User-Agent:
- FastlyGo/8.4.1 (+github.com/fastly/go-fastly; go1.18.5)
url: https://api.fastly.com/resources/stores/config/LO53FrkHHmWWropCH2ICN2/items
method: PATCH
response:
body: '{"status":"ok"}'
headers:
Accept-Ranges:
- bytes
Cache-Control:
- no-store
Content-Type:
- application/json
Date:
- Wed, 07 Jun 2023 16:29:19 GMT
Fastly-Ratelimit-Remaining:
- "9998"
Fastly-Ratelimit-Reset:
- "1686157200"
Status:
- 200 OK
Strict-Transport-Security:
- max-age=31536000
Vary:
- Accept-Encoding
Via:
- 1.1 varnish, 1.1 varnish
X-Cache:
- MISS, MISS
X-Cache-Hits:
- 0, 0
X-Served-By:
- cache-control-cp-aws-us-east-2-prod-7-CONTROL-AWS-UE2, cache-lhr7336-LHR
X-Timer:
- S1686155360.758050,VS0,VE153
status: 200 OK
code: 200
duration: ""
49 changes: 49 additions & 0 deletions fastly/fixtures/config_store_batch/delete.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
version: 1
interactions:
- request:
body: '{"items":[{"item_key":"key1","item_value":"","op":"delete"},{"item_key":"key2","item_value":"","op":"delete"}]}'
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
User-Agent:
- FastlyGo/8.4.1 (+github.com/fastly/go-fastly; go1.18.5)
url: https://api.fastly.com/resources/stores/config/LO53FrkHHmWWropCH2ICN2/items
method: PATCH
response:
body: '{"status":"ok"}'
headers:
Accept-Ranges:
- bytes
Cache-Control:
- no-store
Content-Type:
- application/json
Date:
- Wed, 07 Jun 2023 16:29:20 GMT
Fastly-Ratelimit-Remaining:
- "9996"
Fastly-Ratelimit-Reset:
- "1686157200"
Status:
- 200 OK
Strict-Transport-Security:
- max-age=31536000
Vary:
- Accept-Encoding
Via:
- 1.1 varnish, 1.1 varnish
X-Cache:
- MISS, MISS
X-Cache-Hits:
- 0, 0
X-Served-By:
- cache-control-cp-aws-us-east-2-prod-7-CONTROL-AWS-UE2, cache-lhr7336-LHR
X-Timer:
- S1686155360.153553,VS0,VE159
status: 200 OK
code: 200
duration: ""
49 changes: 49 additions & 0 deletions fastly/fixtures/config_store_batch/update_and_upsert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
version: 1
interactions:
- request:
body: '{"items":[{"item_key":"key1","item_value":"value2","op":"update"},{"item_key":"key2","item_value":"value3","op":"upsert"}]}'
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
User-Agent:
- FastlyGo/8.4.1 (+github.com/fastly/go-fastly; go1.18.5)
url: https://api.fastly.com/resources/stores/config/LO53FrkHHmWWropCH2ICN2/items
method: PATCH
response:
body: '{"status":"ok"}'
headers:
Accept-Ranges:
- bytes
Cache-Control:
- no-store
Content-Type:
- application/json
Date:
- Wed, 07 Jun 2023 16:29:20 GMT
Fastly-Ratelimit-Remaining:
- "9997"
Fastly-Ratelimit-Reset:
- "1686157200"
Status:
- 200 OK
Strict-Transport-Security:
- max-age=31536000
Vary:
- Accept-Encoding
Via:
- 1.1 varnish, 1.1 varnish
X-Cache:
- MISS, MISS
X-Cache-Hits:
- 0, 0
X-Served-By:
- cache-control-cp-aws-us-east-2-prod-6-CONTROL-AWS-UE2, cache-lhr7336-LHR
X-Timer:
- S1686155360.940372,VS0,VE176
status: 200 OK
code: 200
duration: ""

0 comments on commit 41eb1ff

Please sign in to comment.