Skip to content

Commit

Permalink
Add Dictionary Info GET method. (#122)
Browse files Browse the repository at this point in the history
* Add call to get Dictionary Info

* Update DictionaryInfo to match Fastly docs.

Previously, the documentation did not match API, which returns a
different time format string than the others in the API.

* Fix typo.

* Rename GetDictionaryInfoInput.Service to ServiceID

* Correct docs on GetDictionaryInfoInput

* Update GetDictionaryInfo tests.

* Change type of LastUpdated to match package standards.

* GetDictionaryInfo test fixtures.
  • Loading branch information
jmhobbs authored and philippschulte committed Oct 9, 2019
1 parent 961642a commit ff0b914
Show file tree
Hide file tree
Showing 10 changed files with 502 additions and 1 deletion.
7 changes: 6 additions & 1 deletion fastly/decode_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func stringToTimeHookFunc() mapstructure.DecodeHookFunc {
}

// Convert it by parsing
return time.Parse(time.RFC3339, data.(string))
v, err := time.Parse(time.RFC3339, data.(string))
if err != nil {
// DictionaryInfo#get uses it's own special time format for now.
return time.Parse("2006-01-02 15:04:05", data.(string))
}
return v, err
}
}
57 changes: 57 additions & 0 deletions fastly/dictionary_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package fastly

import (
"fmt"
"time"
)

// DictionaryInfo represents a dictionary metadata response from the Fastly API.
type DictionaryInfo struct {
// LastUpdated is the Time-stamp (GMT) when the dictionary was last updated.
LastUpdated *time.Time `mapstructure:"last_updated"`

// Digest is the hash of the dictionary content.
Digest string `mapstructure:"digest"`

// ItemCount is the number of items belonging to the dictionary.
ItemCount int `mapstructure:"item_count"`
}

// GetDictionaryInfoInput is used as input to the GetDictionary function.
type GetDictionaryInfoInput struct {
// ServiceID is the ID of the service Dictionary belongs to (required).
ServiceID string

// Version is the specific configuration version (required).
Version int

// ID is the alphanumeric string identifying a dictionary.
ID string
}

// GetDictionaryInfo gets the dictionary metadata with the given parameters.
func (c *Client) GetDictionaryInfo(i *GetDictionaryInfoInput) (*DictionaryInfo, error) {
if i.ServiceID == "" {
return nil, ErrMissingService
}

if i.Version == 0 {
return nil, ErrMissingVersion
}

if i.ID == "" {
return nil, ErrMissingID
}

path := fmt.Sprintf("/service/%s/version/%d/dictionary/%s/info", i.ServiceID, i.Version, i.ID)
resp, err := c.Get(path, nil)
if err != nil {
return nil, err
}

var b *DictionaryInfo
if err := decodeJSON(&b, resp.Body); err != nil {
return nil, err
}
return b, nil
}
88 changes: 88 additions & 0 deletions fastly/dictionary_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package fastly

import (
"testing"
)

func TestClient_GetDictionaryInfo(t *testing.T) {

fixtureBase := "dictionary_info/"
nameSuffix := "DictionaryInfo"

testService := createTestService(t, fixtureBase+"create_service", nameSuffix)
defer deleteTestService(t, fixtureBase+"delete_service", testService.ID)

testVersion := createTestVersion(t, fixtureBase+"version", testService.ID)

testDictionary := createTestDictionary(t, fixtureBase+"dictionary", testService.ID, testVersion.Number, nameSuffix)
defer deleteTestDictionary(t, testDictionary, fixtureBase+"delete_dictionary")

var (
err error
info *DictionaryInfo
)

record(t, fixtureBase+"create_dictionary_items", func(c *Client) {
err = c.BatchModifyDictionaryItems(&BatchModifyDictionaryItemsInput{
Service: testService.ID,
Dictionary: testDictionary.ID,
Items: []*BatchDictionaryItem{
{
Operation: CreateBatchOperation,
ItemKey: "test-dictionary-item-0",
ItemValue: "value",
},
{
Operation: CreateBatchOperation,
ItemKey: "test-dictionary-item-1",
ItemValue: "value",
},
},
})
})
if err != nil {
t.Fatal(err)
}

record(t, fixtureBase+"get", func(c *Client) {
info, err = c.GetDictionaryInfo(&GetDictionaryInfoInput{
ServiceID: testService.ID,
Version: testVersion.Number,
ID: testDictionary.ID,
})
})
if err != nil {
t.Fatal(err)
}

if info.ItemCount != 2 {
t.Errorf("bad item_count: %d", info.ItemCount)
}
}

func TestClient_GetDictionaryInfo_validation(t *testing.T) {
var err error
_, err = testClient.GetDictionaryInfo(&GetDictionaryInfoInput{
ServiceID: "",
})
if err != ErrMissingService {
t.Errorf("bad error: %s", err)
}

_, err = testClient.GetDictionaryInfo(&GetDictionaryInfoInput{
ServiceID: "foo",
Version: 0,
})
if err != ErrMissingVersion {
t.Errorf("bad error: %s", err)
}

_, err = testClient.GetDictionaryInfo(&GetDictionaryInfoInput{
ServiceID: "foo",
Version: 1,
ID: "",
})
if err != ErrMissingID {
t.Errorf("bad error: %s", err)
}
}
51 changes: 51 additions & 0 deletions fastly/fixtures/dictionary_info/create_dictionary_items.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
version: 1
interactions:
- request:
body: '{"items":[{"op":"create","item_key":"test-dictionary-item-0","item_value":"value"},{"op":"create","item_key":"test-dictionary-item-1","item_value":"value"}]}'
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
User-Agent:
- FastlyGo/1.3.0 (+github.com/fastly/go-fastly; go1.12.3)
url: https://api.fastly.com/service/7i6HN3TK9wS159v2gPAZ8A/dictionary/6FKsD88tVMhbomod52CWRH/items
method: PATCH
response:
body: '{"status":"ok"}'
headers:
Accept-Ranges:
- bytes
- bytes
Cache-Control:
- no-cache
Content-Type:
- application/json
Date:
- Sat, 05 Oct 2019 21:12:15 GMT
Fastly-Ratelimit-Remaining:
- "996"
Fastly-Ratelimit-Reset:
- "1570312800"
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-slwdc9037-CONTROL-SLWDC, cache-msp20851-MSP
X-Timer:
- S1570309935.179239,VS0,VE270
status: 200 OK
code: 200
duration: ""
53 changes: 53 additions & 0 deletions fastly/fixtures/dictionary_info/create_service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
version: 1
interactions:
- request:
body: comment=go-fastly+client+test&name=test_service_DictionaryInfo
form:
comment:
- go-fastly client test
name:
- test_service_DictionaryInfo
headers:
Content-Type:
- application/x-www-form-urlencoded
User-Agent:
- FastlyGo/1.3.0 (+github.com/fastly/go-fastly; go1.12.3)
url: https://api.fastly.com/service
method: POST
response:
body: '{"customer_id":"61rkYPY1bC3XzLnew9jMi5","comment":"go-fastly client test","name":"test_service_DictionaryInfo","updated_at":"2019-10-05T21:12:14Z","versions":[{"comment":"","created_at":"2019-10-05T21:12:14Z","testing":false,"staging":false,"deployed":false,"active":false,"deleted_at":null,"service_id":"7i6HN3TK9wS159v2gPAZ8A","locked":false,"updated_at":"2019-10-05T21:12:14Z","number":1}],"publish_key":"93c97384c51b3309ef79cf587a4c3baddadc00b0","deleted_at":null,"id":"7i6HN3TK9wS159v2gPAZ8A","created_at":"2019-10-05T21:12:14Z"}'
headers:
Accept-Ranges:
- bytes
- bytes
Cache-Control:
- no-cache
Content-Type:
- application/json
Date:
- Sat, 05 Oct 2019 21:12:14 GMT
Fastly-Ratelimit-Remaining:
- "999"
Fastly-Ratelimit-Reset:
- "1570312800"
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-slwdc9037-CONTROL-SLWDC, cache-msp20851-MSP
X-Timer:
- S1570309934.954698,VS0,VE282
status: 200 OK
code: 200
duration: ""
47 changes: 47 additions & 0 deletions fastly/fixtures/dictionary_info/delete_dictionary.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
version: 1
interactions:
- request:
body: ""
form: {}
headers:
User-Agent:
- FastlyGo/1.3.0 (+github.com/fastly/go-fastly; go1.12.3)
url: https://api.fastly.com/service/7i6HN3TK9wS159v2gPAZ8A/version/2/dictionary/test_dictionary_DictionaryInfo
method: DELETE
response:
body: '{"status":"ok"}'
headers:
Accept-Ranges:
- bytes
- bytes
Cache-Control:
- no-cache
Content-Type:
- application/json
Date:
- Sat, 05 Oct 2019 21:12:16 GMT
Fastly-Ratelimit-Remaining:
- "995"
Fastly-Ratelimit-Reset:
- "1570312800"
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-slwdc9037-CONTROL-SLWDC, cache-msp20851-MSP
X-Timer:
- S1570309936.848957,VS0,VE226
status: 200 OK
code: 200
duration: ""
47 changes: 47 additions & 0 deletions fastly/fixtures/dictionary_info/delete_service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
version: 1
interactions:
- request:
body: ""
form: {}
headers:
User-Agent:
- FastlyGo/1.3.0 (+github.com/fastly/go-fastly; go1.12.3)
url: https://api.fastly.com/service/7i6HN3TK9wS159v2gPAZ8A
method: DELETE
response:
body: '{"status":"ok"}'
headers:
Accept-Ranges:
- bytes
- bytes
Cache-Control:
- no-cache
Content-Type:
- application/json
Date:
- Sat, 05 Oct 2019 21:12:16 GMT
Fastly-Ratelimit-Remaining:
- "994"
Fastly-Ratelimit-Reset:
- "1570312800"
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-slwdc9037-CONTROL-SLWDC, cache-msp20851-MSP
X-Timer:
- S1570309936.157268,VS0,VE232
status: 200 OK
code: 200
duration: ""
Loading

0 comments on commit ff0b914

Please sign in to comment.