Skip to content

Commit

Permalink
Adds catalog zone support (#22)
Browse files Browse the repository at this point in the history
Adds catalog zone support (added with PowerDNS 4.7.0)
  • Loading branch information
fuero authored Jul 17, 2024
1 parent 0b5137f commit fa48081
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
10 changes: 10 additions & 0 deletions apis/zones/types_zonekind.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const (
ZoneKindNative ZoneKind = iota
ZoneKindMaster
ZoneKindSlave
ZoneKindProducer
ZoneKindConsumer
)

func (k ZoneKind) MarshalJSON() ([]byte, error) {
Expand All @@ -19,6 +21,10 @@ func (k ZoneKind) MarshalJSON() ([]byte, error) {
return []byte(`"Master"`), nil
case ZoneKindSlave:
return []byte(`"Slave"`), nil
case ZoneKindProducer:
return []byte(`"Producer"`), nil
case ZoneKindConsumer:
return []byte(`"Consumer"`), nil
default:
return nil, fmt.Errorf("unsupported zone kind: %d", k)
}
Expand All @@ -32,6 +38,10 @@ func (k *ZoneKind) UnmarshalJSON(input []byte) error {
*k = ZoneKindMaster
case `"Slave"`:
*k = ZoneKindSlave
case `"Producer"`:
*k = ZoneKindProducer
case `"Consumer"`:
*k = ZoneKindConsumer
default:
return fmt.Errorf("unsupported zone kind: %s", string(input))
}
Expand Down
69 changes: 69 additions & 0 deletions apis/zones/types_zonekind_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package zones

import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestZoneKindSerializesCorrectly(t *testing.T) {
data := []struct {
v ZoneKind
e string
}{
{ZoneKindNative, `"Native"`},
{ZoneKindMaster, `"Master"`},
{ZoneKindSlave, `"Slave"`},
{ZoneKindProducer, `"Producer"`},
{ZoneKindConsumer, `"Consumer"`},
}

for i := range data {
t.Run(fmt.Sprintf("serializes to %s", data[i].e), func(t *testing.T) {
j, err := json.Marshal(data[i].v)

assert.Nil(t, err)
assert.Equal(t, data[i].e, string(j))
})
}
}

func TestZoneTypeSerializationReturnErrorOnUnknownValue(t *testing.T) {
var v ZoneKind = 123

_, err := json.Marshal(v)
assert.NotNil(t, err)
}

func TestZoneKindUnserializesCorrectly(t *testing.T) {
data := []struct {
v ZoneKind
e string
}{
{ZoneKindNative, `"Native"`},
{ZoneKindMaster, `"Master"`},
{ZoneKindSlave, `"Slave"`},
{ZoneKindProducer, `"Producer"`},
{ZoneKindConsumer, `"Consumer"`},
}

for i := range data {
t.Run(fmt.Sprintf("serializes to %s", data[i].e), func(t *testing.T) {
var out ZoneKind

err := json.Unmarshal([]byte(data[i].e), &out)

assert.Nil(t, err)
assert.Equal(t, data[i].v, out)
})
}
}

func TestZoneKindUnserializationReturnErrorOnUnknownValue(t *testing.T) {
e := []byte(`"FOO"`)

var out ZoneKind

err := json.Unmarshal(e, &out)
assert.NotNil(t, err)
}

0 comments on commit fa48081

Please sign in to comment.