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

Redesign webhook event parsing to avoid event/data conflicts #85

Merged
merged 6 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
209 changes: 61 additions & 148 deletions dnsimple/webhook/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ import (
"github.com/dnsimple/dnsimple-go/dnsimple"
)

func switchEvent(name string, payload []byte) (Event, error) {
var event Event
func switchEventData(event *EventContainer) (EventDataContainer, error) {
var dataContainer EventDataContainer

switch name {
switch event.Name {
case // account
"account.billing_settings_update",
"account.update",
"account.user_invitation_accept",
"account.user_invite":
event = &AccountEvent{}
dataContainer = &AccountEventData{}
case // certificate
"certificate.remove_private_key":
event = &CertificateEvent{}
dataContainer = &CertificateEventData{}
case // contact
"contact.create",
"contact.delete",
"contact.update":
event = &ContactEvent{}
dataContainer = &ContactEventData{}
case // dnssec
"dnssec.rotation_complete",
"dnssec.rotation_start":
event = &DNSSECEvent{}
dataContainer = &DNSSECEventData{}
case // domain
"domain.auto_renewal_disable",
"domain.auto_renewal_enable",
Expand All @@ -38,268 +38,181 @@ func switchEvent(name string, payload []byte) (Event, error) {
"domain.resolution_disable",
"domain.resolution_enable",
"domain.transfer": // TODO
event = &DomainEvent{}
dataContainer = &DomainEventData{}
case // email forward
"email_forward.create",
"email_forward.delete",
"email_forward.update":
event = &EmailForwardEvent{}
dataContainer = &EmailForwardEventData{}
case // webhook
"webhook.create",
"webhook.delete":
event = &WebhookEvent{}
dataContainer = &WebhookEventData{}
case // whois privacy
"whois_privacy.disable",
"whois_privacy.enable",
"whois_privacy.purchase",
"whois_privacy.renew": // TODO
event = &WhoisPrivacyEvent{}
dataContainer = &WhoisPrivacyEventData{}
case // zone
"zone.create",
"zone.delete":
event = &ZoneEvent{}
dataContainer = &ZoneEventData{}
case // zone record
"zone_record.create",
"zone_record.delete",
"zone_record.update":
event = &ZoneRecordEvent{}
dataContainer = &ZoneRecordEventData{}
default:
event = &GenericEvent{}
dataContainer = &GenericEventData{}
}

return event, event.parse(payload)
err := dataContainer.unmarshalEventData(event.payload)
return dataContainer, err
}

//
// GenericEvent
//

// GenericEvent represents a generic event, where the data is a simple map of strings.
type GenericEvent struct {
EventHeader
Data interface{} `json:"data"`
}

func (e *GenericEvent) parse(payload []byte) error {
e.payload = payload
return unmashalEvent(payload, e)
}
// GenericEventData represents the data node for a generic event, where the data is a simple map of strings.
type GenericEventData map[string]interface{}

// ParseGenericEvent unpacks the data into a GenericEvent.
func ParseGenericEvent(e *GenericEvent, payload []byte) error {
return e.parse(payload)
func (d *GenericEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// AccountEvent
//

// AccountEvent represents the base event sent for an account action.
type AccountEvent struct {
EventHeader
Data *AccountEvent `json:"data"`
// AccountEventData represents the data node for an Account event.
type AccountEventData struct {
Account *dnsimple.Account `json:"account"`
}

// ParseAccountEvent unpacks the data into an AccountEvent.
func ParseAccountEvent(e *AccountEvent, payload []byte) error {
return e.parse(payload)
}

func (e *AccountEvent) parse(payload []byte) error {
e.payload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *AccountEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// CertificateEvent
//

// CertificateEvent represents the base event sent for a certificate action.
type CertificateEvent struct {
EventHeader
Data *CertificateEvent `json:"data"`
// CertificateEventData represents the data node for a Certificate event.
type CertificateEventData struct {
Certificate *dnsimple.Certificate `json:"certificate"`
}

// ParseCertificateEvent unpacks the data into a CertificateEvent.
func ParseCertificateEvent(e *CertificateEvent, payload []byte) error {
return e.parse(payload)
}

func (e *CertificateEvent) parse(payload []byte) error {
e.payload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *CertificateEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// ContactEvent
//

// ContactEvent represents the base event sent for a contact action.
type ContactEvent struct {
EventHeader
Data *ContactEvent `json:"data"`
// ContactEventData represents the data node for a Contact event.
type ContactEventData struct {
Contact *dnsimple.Contact `json:"contact"`
}

// ParseContactEvent unpacks the data into a ContactEvent.
func ParseContactEvent(e *ContactEvent, payload []byte) error {
return e.parse(payload)
}

func (e *ContactEvent) parse(payload []byte) error {
e.payload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *ContactEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// DNSSECEvent
//

// DNSSECEvent represents the base event sent for a DNSSEC action.
type DNSSECEvent struct {
EventHeader
Data *DNSSECEvent `json:"data"`
// DNSSECEventData represents the data node for a DNSSEC event.
type DNSSECEventData struct {
DelegationSignerRecord *dnsimple.DelegationSignerRecord `json:"delegation_signer_record"`
//DNSSECConfig *dnsimple.DNSSECConfig `json:"dnssec"`
}

// ParseDNSSECEvent unpacks the payload into a DNSSECEvent.
func ParseDNSSECEvent(e *DNSSECEvent, payload []byte) error {
return e.parse(payload)
}

func (e *DNSSECEvent) parse(payload []byte) error {
e.payload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *DNSSECEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// DomainEvent
//

// DomainEvent represents the base event sent for a domain action.
type DomainEvent struct {
EventHeader
Data *DomainEvent `json:"data"`
// DomainEventData represents the data node for a Contact event.
type DomainEventData struct {
Auto bool `json:"auto"`
Domain *dnsimple.Domain `json:"domain"`
Registrant *dnsimple.Contact `json:"registrant"`
Delegation *dnsimple.Delegation `json:"name_servers"`
}

// ParseDomainEvent unpacks the payload into a DomainEvent.
func ParseDomainEvent(e *DomainEvent, payload []byte) error {
return e.parse(payload)
}

func (e *DomainEvent) parse(payload []byte) error {
e.payload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *DomainEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// EmailForwardEvent
//

// EmailForwardEvent represents the base event sent for an email forward action.
type EmailForwardEvent struct {
EventHeader
Data *EmailForwardEvent `json:"data"`
// EmailForwardEventData represents the data node for a EmailForward event.
type EmailForwardEventData struct {
EmailForward *dnsimple.EmailForward `json:"email_forward"`
}

// ParseEmailForwardEvent unpacks the payload into a EmailForwardEvent.
func ParseEmailForwardEvent(e *EmailForwardEvent, payload []byte) error {
return e.parse(payload)
}

func (e *EmailForwardEvent) parse(payload []byte) error {
e.payload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *EmailForwardEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// WebhookEvent
//

// WebhookEvent represents the base event sent for a webhook action.
type WebhookEvent struct {
EventHeader
Data *WebhookEvent `json:"data"`
// WebhookEventData represents the data node for a Webhook event.
type WebhookEventData struct {
Webhook *dnsimple.Webhook `json:"webhook"`
}

// ParseWebhookEvent unpacks the data into a WebhookEvent.
func ParseWebhookEvent(e *WebhookEvent, payload []byte) error {
return e.parse(payload)
}

func (e *WebhookEvent) parse(payload []byte) error {
e.payload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *WebhookEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// WhoisPrivacyEvent
//

// WhoisPrivacyEvent represents the base event sent for a whois privacy action.
type WhoisPrivacyEvent struct {
EventHeader
Data *WhoisPrivacyEvent `json:"data"`
// WhoisPrivacyEventData represents the data node for a WhoisPrivacy event.
type WhoisPrivacyEventData struct {
Domain *dnsimple.Domain `json:"domain"`
WhoisPrivacy *dnsimple.WhoisPrivacy `json:"whois_privacy"`
}

// ParseWhoisPrivacyEvent unpacks the data into a WhoisPrivacyEvent.
func ParseWhoisPrivacyEvent(e *WhoisPrivacyEvent, payload []byte) error {
return e.parse(payload)
}

func (e *WhoisPrivacyEvent) parse(payload []byte) error {
e.payload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *WhoisPrivacyEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// ZoneEvent
//

// ZoneEvent represents the base event sent for a zone action.
type ZoneEvent struct {
EventHeader
Data *ZoneEvent `json:"data"`
// ZoneEventData represents the data node for a Zone event.
type ZoneEventData struct {
Zone *dnsimple.Zone `json:"zone"`
}

// ParseZoneEvent unpacks the data into a ZoneEvent.
func ParseZoneEvent(e *ZoneEvent, payload []byte) error {
return e.parse(payload)
}

func (e *ZoneEvent) parse(payload []byte) error {
e.payload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *ZoneEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}

//
// ZoneRecordEvent
//

// ZoneRecordEvent represents the base event sent for a zone record action.
type ZoneRecordEvent struct {
EventHeader
Data *ZoneRecordEvent `json:"data"`
// ZoneRecordEventData represents the data node for a ZoneRecord event.
type ZoneRecordEventData struct {
ZoneRecord *dnsimple.ZoneRecord `json:"zone_record"`
}

// ParseZoneRecordEvent unpacks the data into a ZoneRecordEvent.
func ParseZoneRecordEvent(e *ZoneRecordEvent, payload []byte) error {
return e.parse(payload)
}

func (e *ZoneRecordEvent) parse(payload []byte) error {
e.payload, e.Data = payload, e
return unmashalEvent(payload, e)
func (d *ZoneRecordEventData) unmarshalEventData(payload []byte) error {
return unmashalEventData(payload, d)
}
Loading