From 36414e40c983c15019a018e4ad26f014727eeaa4 Mon Sep 17 00:00:00 2001 From: Alex Kalyvitis Date: Fri, 6 Jul 2018 14:21:08 +0200 Subject: [PATCH] update go-auth to use options --- Gopkg.lock | 4 +- auth0/resource_auth0_email.go | 2 +- .../yieldr/go-auth0/management/client.go | 4 +- .../yieldr/go-auth0/management/connection.go | 4 +- .../go-auth0/management/custom_domain.go | 4 +- .../yieldr/go-auth0/management/email.go | 4 +- .../go-auth0/management/email_template.go | 4 +- .../yieldr/go-auth0/management/management.go | 71 +++++++++++++++++-- .../go-auth0/management/resource_server.go | 4 +- .../yieldr/go-auth0/management/rule.go | 4 +- 10 files changed, 83 insertions(+), 22 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index d47f721a..d3c16150 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -352,8 +352,8 @@ [[projects]] name = "github.com/yieldr/go-auth0" packages = ["management"] - revision = "f4be2a789656a59a9796ebc8f36742b26783458d" - version = "v0.0.12" + revision = "b4aaab00a952a0bc9f8b0bf69d48d093fab79b7b" + version = "v0.1.0" [[projects]] branch = "master" diff --git a/auth0/resource_auth0_email.go b/auth0/resource_auth0_email.go index 5a14f636..0fd8db86 100644 --- a/auth0/resource_auth0_email.go +++ b/auth0/resource_auth0_email.go @@ -90,7 +90,7 @@ func createEmail(d *schema.ResourceData, m interface{}) error { func readEmail(d *schema.ResourceData, m interface{}) error { api := m.(*management.Management) - e, err := api.Email.Read() + e, err := api.Email.Read(management.WithFields("name", "enabled", "default_from_address", "credentials")) if err != nil { return err } diff --git a/vendor/github.com/yieldr/go-auth0/management/client.go b/vendor/github.com/yieldr/go-auth0/management/client.go index b90e849b..687945e4 100644 --- a/vendor/github.com/yieldr/go-auth0/management/client.go +++ b/vendor/github.com/yieldr/go-auth0/management/client.go @@ -103,9 +103,9 @@ func (cm *ClientManager) Create(c *Client) (err error) { return cm.m.post(cm.m.uri("clients"), c) } -func (cm *ClientManager) Read(id string) (*Client, error) { +func (cm *ClientManager) Read(id string, opts ...Option) (*Client, error) { c := new(Client) - err := cm.m.get(cm.m.uri("clients", id), c) + err := cm.m.get(cm.m.uri("clients", id)+"?"+cm.m.q(opts), c) return c, err } diff --git a/vendor/github.com/yieldr/go-auth0/management/connection.go b/vendor/github.com/yieldr/go-auth0/management/connection.go index 8d6f158c..62713333 100644 --- a/vendor/github.com/yieldr/go-auth0/management/connection.go +++ b/vendor/github.com/yieldr/go-auth0/management/connection.go @@ -96,9 +96,9 @@ func (cm *ConnectionManager) Create(c *Connection) error { return cm.m.post(cm.m.uri("connections"), c) } -func (cm *ConnectionManager) Read(id string) (*Connection, error) { +func (cm *ConnectionManager) Read(id string, opts ...Option) (*Connection, error) { c := new(Connection) - err := cm.m.get(cm.m.uri("connections", id), c) + err := cm.m.get(cm.m.uri("connections", id)+"?"+cm.m.q(opts), c) return c, err } diff --git a/vendor/github.com/yieldr/go-auth0/management/custom_domain.go b/vendor/github.com/yieldr/go-auth0/management/custom_domain.go index 9ae8662c..ef4c8b67 100644 --- a/vendor/github.com/yieldr/go-auth0/management/custom_domain.go +++ b/vendor/github.com/yieldr/go-auth0/management/custom_domain.go @@ -44,9 +44,9 @@ func (cm *CustomDomainManager) Create(c *CustomDomain) (err error) { return cm.m.post(cm.m.uri("custom-domains"), c) } -func (cm *CustomDomainManager) Read(id string) (*CustomDomain, error) { +func (cm *CustomDomainManager) Read(id string, opts ...Option) (*CustomDomain, error) { c := new(CustomDomain) - err := cm.m.get(cm.m.uri("custom-domains", id), c) + err := cm.m.get(cm.m.uri("custom-domains", id)+"?"+cm.m.q(opts), c) return c, err } diff --git a/vendor/github.com/yieldr/go-auth0/management/email.go b/vendor/github.com/yieldr/go-auth0/management/email.go index 95cc45e4..20e6b344 100644 --- a/vendor/github.com/yieldr/go-auth0/management/email.go +++ b/vendor/github.com/yieldr/go-auth0/management/email.go @@ -49,9 +49,9 @@ func (em *EmailManager) Create(e *Email) error { return em.m.post(em.m.uri("emails", "provider"), e) } -func (em *EmailManager) Read() (*Email, error) { +func (em *EmailManager) Read(opts ...Option) (*Email, error) { e := new(Email) - err := em.m.get(em.m.uri("emails", "provider")+"?fields=name,enabled,default_from_address,credentials", e) + err := em.m.get(em.m.uri("emails", "provider")+"?"+em.m.q(opts), e) return e, err } diff --git a/vendor/github.com/yieldr/go-auth0/management/email_template.go b/vendor/github.com/yieldr/go-auth0/management/email_template.go index baded42f..1753f99a 100644 --- a/vendor/github.com/yieldr/go-auth0/management/email_template.go +++ b/vendor/github.com/yieldr/go-auth0/management/email_template.go @@ -42,9 +42,9 @@ func (em *EmailTemplateManager) Create(e *EmailTemplate) error { return em.m.post(em.m.uri("email-templates"), e) } -func (em *EmailTemplateManager) Read(template string) (*EmailTemplate, error) { +func (em *EmailTemplateManager) Read(template string, opts ...Option) (*EmailTemplate, error) { e := new(EmailTemplate) - err := em.m.get(em.m.uri("email-templates", template), e) + err := em.m.get(em.m.uri("email-templates", template)+"?"+em.m.q(opts), e) return e, err } diff --git a/vendor/github.com/yieldr/go-auth0/management/management.go b/vendor/github.com/yieldr/go-auth0/management/management.go index 46f4fc78..5175cb71 100644 --- a/vendor/github.com/yieldr/go-auth0/management/management.go +++ b/vendor/github.com/yieldr/go-auth0/management/management.go @@ -8,6 +8,8 @@ import ( "io" "net/http" "net/http/httputil" + "net/url" + "strconv" "strings" "time" @@ -161,11 +163,20 @@ func New(domain, clientID, clientSecret string) (*Management, error) { return m, nil } -func (m *Management) uri(parts ...string) string { - return fmt.Sprintf("https://%s/%s/%s", - m.domain, - m.basePath, - strings.Join(parts, "/")) +func (m *Management) uri(path ...string) string { + return (&url.URL{ + Scheme: "https", + Host: m.domain, + Path: m.basePath + "/" + strings.Join(path, "/"), + }).String() +} + +func (m *Management) q(options []Option) string { + v := make(url.Values) + for _, option := range options { + option(v) + } + return v.Encode() } func (m *Management) get(uri string, v interface{}) error { @@ -361,3 +372,53 @@ func (m *managementError) Error() string { func (m *managementError) Status() int { return m.StatusCode } + +// Option configures a call (typically to retrieve a resource) to Auth0 with +// query parameters. +type Option func(v url.Values) + +// WithFields configures a call to include the desired fields. +func WithFields(fields ...string) Option { + return func(v url.Values) { + v.Set("fields", strings.Join(fields, ",")) + v.Set("include_fields", "true") + } +} + +// WithoutFields configures a call to exclude the desired fields. +func WithoutFields(fields ...string) Option { + return func(v url.Values) { + v.Set("fields", strings.Join(fields, ",")) + v.Set("include_fields", "false") + } +} + +// Page configures a call to receive a specific page, if the results where +// concatenated. +func Page(page int) Option { + return func(v url.Values) { + v.Set("page", strconv.FormatInt(int64(page), 10)) + } +} + +// PerPage configures a call to limit the amount of items in the result. +func PerPage(items int) Option { + return func(v url.Values) { + v.Set("per_page", strconv.FormatInt(int64(items), 10)) + } +} + +// IncludeTotals configures a call to include totals. +func IncludeTotals(include bool) Option { + return func(v url.Values) { + v.Set("include_totals", strconv.FormatBool(include)) + } +} + +// Parameter is a generic configuration to add arbitrary query parameters to +// calls made to Auth0. +func Parameter(key, value string) Option { + return func(v url.Values) { + v.Set(key, value) + } +} diff --git a/vendor/github.com/yieldr/go-auth0/management/resource_server.go b/vendor/github.com/yieldr/go-auth0/management/resource_server.go index d1c73b77..c26f3766 100644 --- a/vendor/github.com/yieldr/go-auth0/management/resource_server.go +++ b/vendor/github.com/yieldr/go-auth0/management/resource_server.go @@ -59,9 +59,9 @@ func (r *ResourceServerManager) Create(rs *ResourceServer) (err error) { return r.m.post(r.m.uri("resource-servers"), rs) } -func (r *ResourceServerManager) Read(id string) (*ResourceServer, error) { +func (r *ResourceServerManager) Read(id string, opts ...Option) (*ResourceServer, error) { rs := new(ResourceServer) - err := r.m.get(r.m.uri("resource-servers", id), rs) + err := r.m.get(r.m.uri("resource-servers", id)+"?"+r.m.q(opts), rs) return rs, err } diff --git a/vendor/github.com/yieldr/go-auth0/management/rule.go b/vendor/github.com/yieldr/go-auth0/management/rule.go index 8e32b3c1..2f90a7cf 100644 --- a/vendor/github.com/yieldr/go-auth0/management/rule.go +++ b/vendor/github.com/yieldr/go-auth0/management/rule.go @@ -33,9 +33,9 @@ func (rm *RuleManager) Create(r *Rule) error { return rm.m.post(rm.m.uri("rules"), r) } -func (rm *RuleManager) Read(id string) (*Rule, error) { +func (rm *RuleManager) Read(id string, opts ...Option) (*Rule, error) { r := new(Rule) - err := rm.m.get(rm.m.uri("rules", id), r) + err := rm.m.get(rm.m.uri("rules", id)+"?"+rm.m.q(opts), r) return r, err }