diff --git a/ns1/data_source_dnssec.go b/ns1/data_source_dnssec.go index 821866ac..6a5fe0e6 100644 --- a/ns1/data_source_dnssec.go +++ b/ns1/data_source_dnssec.go @@ -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 diff --git a/ns1/error_handler.go b/ns1/error_handler.go new file mode 100644 index 00000000..e2308ecc --- /dev/null +++ b/ns1/error_handler.go @@ -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()} +} diff --git a/ns1/resource_apikey.go b/ns1/resource_apikey.go index 2b13069b..7a1a9278 100644 --- a/ns1/resource_apikey.go +++ b/ns1/resource_apikey.go @@ -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 @@ -126,7 +126,7 @@ 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()) @@ -134,7 +134,7 @@ func ApikeyRead(d *schema.ResourceData, meta interface{}) error { return nil } - return err + return ConvertToNs1Error(resp, err) } return apikeyToResourceData(d, k) } @@ -142,9 +142,9 @@ func ApikeyRead(d *schema.ResourceData, meta interface{}) error { //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 @@ -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) diff --git a/ns1/resource_datafeed.go b/ns1/resource_datafeed.go index 19e1fcee..cb288a67 100644 --- a/ns1/resource_datafeed.go +++ b/ns1/resource_datafeed.go @@ -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 @@ -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") { @@ -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 @@ -80,9 +80,9 @@ 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 @@ -90,8 +90,8 @@ 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 diff --git a/ns1/resource_datasource.go b/ns1/resource_datasource.go index 8a1136af..4f9232ce 100644 --- a/ns1/resource_datasource.go +++ b/ns1/resource_datasource.go @@ -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 @@ -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") { @@ -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 @@ -74,9 +74,9 @@ 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 @@ -84,8 +84,8 @@ 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 diff --git a/ns1/resource_monitoringjob.go b/ns1/resource_monitoringjob.go index f98d9d80..49597571 100644 --- a/ns1/resource_monitoringjob.go +++ b/ns1/resource_monitoringjob.go @@ -261,8 +261,8 @@ 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) } @@ -270,7 +270,7 @@ func MonitoringJobCreate(d *schema.ResourceData, meta interface{}) error { // 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") { @@ -279,7 +279,7 @@ func MonitoringJobRead(d *schema.ResourceData, meta interface{}) error { return nil } - return err + return ConvertToNs1Error(resp, err) } return monitoringJobToResourceData(d, j) } @@ -287,9 +287,9 @@ func MonitoringJobRead(d *schema.ResourceData, meta interface{}) error { // 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 @@ -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) } diff --git a/ns1/resource_notifylist.go b/ns1/resource_notifylist.go index 0a44e529..f144dd54 100644 --- a/ns1/resource_notifylist.go +++ b/ns1/resource_notifylist.go @@ -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) @@ -113,7 +113,7 @@ 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()) @@ -121,7 +121,7 @@ func NotifyListRead(d *schema.ResourceData, meta interface{}) error { return nil } - return err + return ConvertToNs1Error(resp, err) } return notifyListToResourceData(d, nl) @@ -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 @@ -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) diff --git a/ns1/resource_record.go b/ns1/resource_record.go index 2008fda9..e8f43e30 100644 --- a/ns1/resource_record.go +++ b/ns1/resource_record.go @@ -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) } @@ -433,7 +433,7 @@ 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()) @@ -441,7 +441,7 @@ func RecordRead(d *schema.ResourceData, meta interface{}) error { return nil } - return err + return ConvertToNs1Error(resp, err) } return recordToResourceData(d, r) @@ -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 @@ -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) } diff --git a/ns1/resource_team.go b/ns1/resource_team.go index 87a53f69..6ca64cea 100644 --- a/ns1/resource_team.go +++ b/ns1/resource_team.go @@ -107,8 +107,8 @@ func TeamCreate(d *schema.ResourceData, meta interface{}) error { if err := resourceDataToTeam(&t, d); err != nil { return err } - if _, err := client.Teams.Create(&t); err != nil { - return err + if resp, err := client.Teams.Create(&t); err != nil { + return ConvertToNs1Error(resp, err) } return teamToResourceData(d, &t) } @@ -116,7 +116,7 @@ func TeamCreate(d *schema.ResourceData, meta interface{}) error { // TeamRead reads the team data from ns1 func TeamRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*ns1.Client) - t, _, err := client.Teams.Get(d.Id()) + t, resp, err := client.Teams.Get(d.Id()) if err != nil { if err == ns1.ErrTeamMissing { log.Printf("[DEBUG] NS1 team (%s) not found", d.Id()) @@ -124,7 +124,7 @@ func TeamRead(d *schema.ResourceData, meta interface{}) error { return nil } - return err + return ConvertToNs1Error(resp, err) } return teamToResourceData(d, t) } @@ -132,9 +132,9 @@ func TeamRead(d *schema.ResourceData, meta interface{}) error { // TeamDelete deletes the given team from ns1 func TeamDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*ns1.Client) - _, err := client.Teams.Delete(d.Id()) + resp, err := client.Teams.Delete(d.Id()) d.SetId("") - return err + return ConvertToNs1Error(resp, err) } // TeamUpdate updates the given team in ns1 @@ -146,8 +146,8 @@ func TeamUpdate(d *schema.ResourceData, meta interface{}) error { if err := resourceDataToTeam(&t, d); err != nil { return err } - if _, err := client.Teams.Update(&t); err != nil { - return err + if resp, err := client.Teams.Update(&t); err != nil { + return ConvertToNs1Error(resp, err) } // @TODO - when a teams permissions are updated, all users and keys assigned to that team diff --git a/ns1/resource_user.go b/ns1/resource_user.go index cac713ea..6a9ad49a 100644 --- a/ns1/resource_user.go +++ b/ns1/resource_user.go @@ -127,16 +127,16 @@ func UserCreate(d *schema.ResourceData, meta interface{}) error { if err := resourceDataToUser(&u, d); err != nil { return err } - if _, err := client.Users.Create(&u); err != nil { - return err + if resp, err := client.Users.Create(&u); err != nil { + return ConvertToNs1Error(resp, err) } // If a user is assigned to at least one team, then it's permissions need to be refreshed // because the current user permissions in Terraform state will be out of date. if len(u.TeamIDs) > 0 { - updatedUser, _, err := client.Users.Get(u.Username) + updatedUser, resp, err := client.Users.Get(u.Username) if err != nil { - return err + return ConvertToNs1Error(resp, err) } return userToResourceData(d, updatedUser) @@ -148,7 +148,7 @@ func UserCreate(d *schema.ResourceData, meta interface{}) error { // UserRead reads the given users data from ns1 func UserRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*ns1.Client) - u, _, err := client.Users.Get(d.Id()) + u, resp, err := client.Users.Get(d.Id()) if err != nil { // No custom error type is currently defined in the SDK for a non-existent user. if strings.Contains(err.Error(), "User not found") { @@ -157,7 +157,7 @@ func UserRead(d *schema.ResourceData, meta interface{}) error { return nil } - return err + return ConvertToNs1Error(resp, err) } return userToResourceData(d, u) } @@ -165,9 +165,9 @@ func UserRead(d *schema.ResourceData, meta interface{}) error { // UserDelete deletes the given user from ns1 func UserDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*ns1.Client) - _, err := client.Users.Delete(d.Id()) + resp, err := client.Users.Delete(d.Id()) d.SetId("") - return err + return ConvertToNs1Error(resp, err) } // UserUpdate updates the user with given parameters in ns1 @@ -180,16 +180,16 @@ func UserUpdate(d *schema.ResourceData, meta interface{}) error { return err } - if _, err := client.Users.Update(&u); err != nil { - return err + if resp, err := client.Users.Update(&u); err != nil { + return ConvertToNs1Error(resp, err) } // If a user's teams has changed then the permissions on the user need to be refreshed // because the current user permissions in Terraform state will be out of date. if d.HasChange("teams") { - updatedUser, _, err := client.Users.Get(d.Id()) + updatedUser, resp, err := client.Users.Get(d.Id()) if err != nil { - return err + return ConvertToNs1Error(resp, err) } return userToResourceData(d, updatedUser) diff --git a/ns1/resource_zone.go b/ns1/resource_zone.go index 4a95b8ad..831e2067 100644 --- a/ns1/resource_zone.go +++ b/ns1/resource_zone.go @@ -287,13 +287,13 @@ func zoneCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*ns1.Client) z := dns.NewZone(d.Get("zone").(string)) resourceDataToZone(z, d) - if _, err := client.Zones.Create(z); err != nil { - return err + if resp, err := client.Zones.Create(z); err != nil { + return ConvertToNs1Error(resp, err) } if !d.Get("autogenerate_ns_record").(bool) { log.Printf("autogenerate_ns_record set to false: deleting NS record for zone %s", z.Zone) - if _, err := client.Records.Delete(z.Zone, z.Zone, "NS"); err != nil { - return err + if resp, err := client.Records.Delete(z.Zone, z.Zone, "NS"); err != nil { + return ConvertToNs1Error(resp, err) } } if err := resourceZoneToResourceData(d, z); err != nil { @@ -305,7 +305,7 @@ func zoneCreate(d *schema.ResourceData, meta interface{}) error { // zoneRead reads the given zone data from ns1 func zoneRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*ns1.Client) - z, _, err := client.Zones.Get(d.Get("zone").(string)) + z, resp, err := client.Zones.Get(d.Get("zone").(string)) if err != nil { if err == ns1.ErrZoneMissing { log.Printf("[DEBUG] NS1 zone (%s) not found", d.Id()) @@ -313,7 +313,7 @@ func zoneRead(d *schema.ResourceData, meta interface{}) error { return nil } - return err + return ConvertToNs1Error(resp, err) } if err := resourceZoneToResourceData(d, z); err != nil { return err @@ -324,9 +324,9 @@ func zoneRead(d *schema.ResourceData, meta interface{}) error { // zoneDelete deletes the given zone from ns1 func zoneDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*ns1.Client) - _, err := client.Zones.Delete(d.Get("zone").(string)) + resp, err := client.Zones.Delete(d.Get("zone").(string)) d.SetId("") - return err + return ConvertToNs1Error(resp, err) } // zoneUpdate updates the zone with given params in ns1 @@ -334,8 +334,8 @@ func zoneUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*ns1.Client) z := dns.NewZone(d.Get("zone").(string)) resourceDataToZone(z, d) - if _, err := client.Zones.Update(z); err != nil { - return err + if resp, err := client.Zones.Update(z); err != nil { + return ConvertToNs1Error(resp, err) } if err := resourceZoneToResourceData(d, z); err != nil { return err