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

PERSONA-179/change terraform output message to add verbose #160

Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions ns1/data_source_dnssec.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func dnssecToResourceData(d *schema.ResourceData, z *dns.ZoneDNSSEC) error {

func dnssecRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
z, _, err := client.DNSSEC.Get(d.Get("zone").(string))
z, resp, err := client.DNSSEC.Get(d.Get("zone").(string))
if err != nil {
return err
return ConvertToNs1Error(resp, err)
}
if err := dnssecToResourceData(d, z); err != nil {
return err
Expand Down
23 changes: 23 additions & 0 deletions ns1/error_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ns1

import (
ns1 "gopkg.in/ns1/ns1-go.v2/rest"
"net/http"
)

// ConvertToNs1Error convert messages that GoSDK client overrides to a verbose one
func ConvertToNs1Error(resp *http.Response, err error) error {
if resp == nil {
return err
}

if err == nil {
return nil
}

if _, ok := err.(*ns1.Error); ok {
return err
}

return &ns1.Error{Resp: resp, Message: err.Error()}
}
24 changes: 12 additions & 12 deletions ns1/resource_apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ func ApikeyCreate(d *schema.ResourceData, meta interface{}) error {
if err := resourceDataToApikey(&k, d); err != nil {
return err
}
if _, err := client.APIKeys.Create(&k); err != nil {
return err
if resp, err := client.APIKeys.Create(&k); err != nil {
return ConvertToNs1Error(resp, err)
}

// If a key is assigned to at least one team, then it's permissions need to be refreshed
// because the current key permissions in Terraform state will be out of date.
if len(k.TeamIDs) > 0 {
updatedKey, _, err := client.APIKeys.Get(k.ID)
updatedKey, resp, err := client.APIKeys.Get(k.ID)
if err != nil {
return err
return ConvertToNs1Error(resp, err)
}
// Key attribute only avail on initial GET
updatedKey.Key = k.Key
Expand All @@ -126,25 +126,25 @@ func ApikeyCreate(d *schema.ResourceData, meta interface{}) error {
// ApikeyRead reads API key from ns1
func ApikeyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
k, _, err := client.APIKeys.Get(d.Id())
k, resp, err := client.APIKeys.Get(d.Id())
if err != nil {
if err == ns1.ErrKeyMissing {
log.Printf("[DEBUG] NS1 API key (%s) not found", d.Id())
d.SetId("")
return nil
}

return err
return ConvertToNs1Error(resp, err)
}
return apikeyToResourceData(d, k)
}

//ApikeyDelete deletes the given ns1 api key
func ApikeyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
_, err := client.APIKeys.Delete(d.Id())
resp, err := client.APIKeys.Delete(d.Id())
d.SetId("")
return err
return ConvertToNs1Error(resp, err)
}

//ApikeyUpdate updates the given api key in ns1
Expand All @@ -158,16 +158,16 @@ func ApikeyUpdate(d *schema.ResourceData, meta interface{}) error {
return err
}

if _, err := client.APIKeys.Update(&k); err != nil {
return err
if resp, err := client.APIKeys.Update(&k); err != nil {
return ConvertToNs1Error(resp, err)
}

// If a key's teams have changed then the permissions on the key need to be refreshed
// because the current key permissions in Terraform state will be out of date.
if d.HasChange("teams") {
updatedKey, _, err := client.APIKeys.Get(d.Id())
updatedKey, resp, err := client.APIKeys.Get(d.Id())
if err != nil {
return err
return ConvertToNs1Error(resp, err)
}

return apikeyToResourceData(d, updatedKey)
Expand Down
16 changes: 8 additions & 8 deletions ns1/resource_datafeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func resourceDataToDataFeed(d *schema.ResourceData) *data.Feed {
func DataFeedCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
f := resourceDataToDataFeed(d)
if _, err := client.DataFeeds.Create(d.Get("source_id").(string), f); err != nil {
return err
if resp, err := client.DataFeeds.Create(d.Get("source_id").(string), f); err != nil {
return ConvertToNs1Error(resp, err)
}
dataFeedToResourceData(d, f)
return nil
Expand All @@ -62,7 +62,7 @@ func DataFeedCreate(d *schema.ResourceData, meta interface{}) error {
// DataFeedRead reads the datafeed for the given ID from ns1
func DataFeedRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
f, _, err := client.DataFeeds.Get(d.Get("source_id").(string), d.Id())
f, resp, err := client.DataFeeds.Get(d.Get("source_id").(string), d.Id())
if err != nil {
// No custom error type is currently defined in the SDK for a data feed.
if strings.Contains(err.Error(), "feed not found") {
Expand All @@ -71,7 +71,7 @@ func DataFeedRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

return err
return ConvertToNs1Error(resp, err)
}
dataFeedToResourceData(d, f)
return nil
Expand All @@ -80,18 +80,18 @@ func DataFeedRead(d *schema.ResourceData, meta interface{}) error {
// DataFeedDelete delets the given datafeed from ns1
func DataFeedDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
_, err := client.DataFeeds.Delete(d.Get("source_id").(string), d.Id())
resp, err := client.DataFeeds.Delete(d.Get("source_id").(string), d.Id())
d.SetId("")
return err
return ConvertToNs1Error(resp, err)
}

// DataFeedUpdate updates the given datafeed with modified parameters
func DataFeedUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
f := resourceDataToDataFeed(d)
f.ID = d.Id()
if _, err := client.DataFeeds.Update(d.Get("source_id").(string), f); err != nil {
return err
if resp, err := client.DataFeeds.Update(d.Get("source_id").(string), f); err != nil {
return ConvertToNs1Error(resp, err)
}
dataFeedToResourceData(d, f)
return nil
Expand Down
16 changes: 8 additions & 8 deletions ns1/resource_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func DataSourceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
s := data.NewSource(d.Get("name").(string), d.Get("sourcetype").(string))
s.Config = d.Get("config").(map[string]interface{})
if _, err := client.DataSources.Create(s); err != nil {
return err
if resp, err := client.DataSources.Create(s); err != nil {
return ConvertToNs1Error(resp, err)
}
dataSourceToResourceData(d, s)
return nil
Expand All @@ -56,7 +56,7 @@ func DataSourceCreate(d *schema.ResourceData, meta interface{}) error {
// DataSourceRead fetches info for the given datasource from ns1
func DataSourceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
s, _, err := client.DataSources.Get(d.Id())
s, resp, err := client.DataSources.Get(d.Id())
if err != nil {
// No custom error type is currently defined in the SDK for a data source.
if strings.Contains(err.Error(), "source not found") {
Expand All @@ -65,7 +65,7 @@ func DataSourceRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

return err
return ConvertToNs1Error(resp, err)
}
dataSourceToResourceData(d, s)
return nil
Expand All @@ -74,18 +74,18 @@ func DataSourceRead(d *schema.ResourceData, meta interface{}) error {
// DataSourceDelete deteltes the given datasource from ns1
func DataSourceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
_, err := client.DataSources.Delete(d.Id())
resp, err := client.DataSources.Delete(d.Id())
d.SetId("")
return err
return ConvertToNs1Error(resp, err)
}

// DataSourceUpdate updates the datasource with given parameters
func DataSourceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
s := data.NewSource(d.Get("name").(string), d.Get("sourcetype").(string))
s.ID = d.Id()
if _, err := client.DataSources.Update(s); err != nil {
return err
if resp, err := client.DataSources.Update(s); err != nil {
return ConvertToNs1Error(resp, err)
}
dataSourceToResourceData(d, s)
return nil
Expand Down
16 changes: 8 additions & 8 deletions ns1/resource_monitoringjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,16 @@ func MonitoringJobCreate(d *schema.ResourceData, meta interface{}) error {
if err := resourceDataToMonitoringJob(&j, d); err != nil {
return err
}
if _, err := client.Jobs.Create(&j); err != nil {
return err
if resp, err := client.Jobs.Create(&j); err != nil {
return ConvertToNs1Error(resp, err)
}
return monitoringJobToResourceData(d, &j)
}

// MonitoringJobRead reads the given monitoring job from ns1
func MonitoringJobRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
j, _, err := client.Jobs.Get(d.Id())
j, resp, err := client.Jobs.Get(d.Id())
if err != nil {
// No custom error type is currently defined in the SDK for a monitoring job.
if strings.Contains(err.Error(), "unknown monitoring job") {
Expand All @@ -279,17 +279,17 @@ func MonitoringJobRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

return err
return ConvertToNs1Error(resp, err)
}
return monitoringJobToResourceData(d, j)
}

// MonitoringJobDelete deteltes the given monitoring job from ns1
func MonitoringJobDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
_, err := client.Jobs.Delete(d.Id())
resp, err := client.Jobs.Delete(d.Id())
d.SetId("")
return err
return ConvertToNs1Error(resp, err)
}

// MonitoringJobUpdate updates the given monitoring job
Expand All @@ -301,8 +301,8 @@ func MonitoringJobUpdate(d *schema.ResourceData, meta interface{}) error {
if err := resourceDataToMonitoringJob(&j, d); err != nil {
return err
}
if _, err := client.Jobs.Update(&j); err != nil {
return err
if resp, err := client.Jobs.Update(&j); err != nil {
return ConvertToNs1Error(resp, err)
}
return monitoringJobToResourceData(d, &j)
}
17 changes: 8 additions & 9 deletions ns1/resource_notifylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func NotifyListCreate(d *schema.ResourceData, meta interface{}) error {
return err
}

if _, err := client.Notifications.Create(nl); err != nil {
return err
if resp, err := client.Notifications.Create(nl); err != nil {
return ConvertToNs1Error(resp, err)
}

return notifyListToResourceData(d, nl)
Expand All @@ -113,15 +113,15 @@ func NotifyListCreate(d *schema.ResourceData, meta interface{}) error {
func NotifyListRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)

nl, _, err := client.Notifications.Get(d.Id())
nl, resp, err := client.Notifications.Get(d.Id())
if err != nil {
if err == ns1.ErrListMissing {
log.Printf("[DEBUG] NS1 notify list (%s) not found", d.Id())
d.SetId("")
return nil
}

return err
return ConvertToNs1Error(resp, err)
}

return notifyListToResourceData(d, nl)
Expand All @@ -131,10 +131,9 @@ func NotifyListRead(d *schema.ResourceData, meta interface{}) error {
func NotifyListDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)

_, err := client.Notifications.Delete(d.Id())
resp, err := client.Notifications.Delete(d.Id())
d.SetId("")

return err
return ConvertToNs1Error(resp, err)
}

// NotifyListUpdate updates the notifylist with given parameters
Expand All @@ -147,8 +146,8 @@ func NotifyListUpdate(d *schema.ResourceData, meta interface{}) error {
return err
}

if _, err := client.Notifications.Update(nl); err != nil {
return err
if resp, err := client.Notifications.Update(nl); err != nil {
return ConvertToNs1Error(resp, err)
}

return notifyListToResourceData(d, nl)
Expand Down
16 changes: 8 additions & 8 deletions ns1/resource_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ func RecordCreate(d *schema.ResourceData, meta interface{}) error {
if err := resourceDataToRecord(r, d); err != nil {
return err
}
if _, err := client.Records.Create(r); err != nil {
return err
if resp, err := client.Records.Create(r); err != nil {
return ConvertToNs1Error(resp, err)
}
return recordToResourceData(d, r)
}
Expand All @@ -433,15 +433,15 @@ func RecordCreate(d *schema.ResourceData, meta interface{}) error {
func RecordRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)

r, _, err := client.Records.Get(d.Get("zone").(string), d.Get("domain").(string), d.Get("type").(string))
r, resp, err := client.Records.Get(d.Get("zone").(string), d.Get("domain").(string), d.Get("type").(string))
if err != nil {
if err == ns1.ErrRecordMissing {
log.Printf("[DEBUG] NS1 record (%s) not found", d.Id())
d.SetId("")
return nil
}

return err
return ConvertToNs1Error(resp, err)
}

return recordToResourceData(d, r)
Expand All @@ -450,9 +450,9 @@ func RecordRead(d *schema.ResourceData, meta interface{}) error {
// RecordDelete deletes the DNS record from ns1
func RecordDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ns1.Client)
_, err := client.Records.Delete(d.Get("zone").(string), d.Get("domain").(string), d.Get("type").(string))
resp, err := client.Records.Delete(d.Get("zone").(string), d.Get("domain").(string), d.Get("type").(string))
d.SetId("")
return err
return ConvertToNs1Error(resp, err)
}

// RecordUpdate updates the given dns record in ns1
Expand All @@ -462,8 +462,8 @@ func RecordUpdate(d *schema.ResourceData, meta interface{}) error {
if err := resourceDataToRecord(r, d); err != nil {
return err
}
if _, err := client.Records.Update(r); err != nil {
return err
if resp, err := client.Records.Update(r); err != nil {
return ConvertToNs1Error(resp, err)
}
return recordToResourceData(d, r)
}
Expand Down
Loading