Skip to content

Commit

Permalink
chore(monitor_downtime): fixes to error handling, account_id defaul…
Browse files Browse the repository at this point in the history
…t to that in `provider{}` (#2561)
  • Loading branch information
pranav-new-relic authored Feb 1, 2024
1 parent 832bbf0 commit a9baf25
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 41 deletions.
27 changes: 13 additions & 14 deletions newrelic/resource_newrelic_monitor_downtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"log"
"os"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -40,10 +39,10 @@ func resourceNewRelicMonitorDowntime() *schema.Resource {
// ValidateFunc: validation included in validateMonitorDowntimeMonitorGUIDs as this is a set and is unsupported by the "validation" package
},
"account_id": {
Type: schema.TypeString,
Description: "The ID of the New Relic account in which the Monitor Downtime shall be created. Defaults to NEW_RELIC_ACCOUNT_ID if not specified.",
Type: schema.TypeInt,
Description: "The ID of the New Relic account in which the Monitor Downtime shall be created. Defaults to the `account_id` in the provider{} configuration if not specified.",
Optional: true,
Default: os.Getenv("NEW_RELIC_ACCOUNT_ID"),
Computed: true,
},
"start_time": {
Type: schema.TypeString,
Expand Down Expand Up @@ -162,7 +161,7 @@ func resourceNewRelicMonitorDowntime() *schema.Resource {
func resourceNewRelicMonitorDowntimeCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConfig := meta.(*ProviderConfig)
client := providerConfig.NewClient
commonArgumentsObject, err := getMonitorDowntimeValuesOfCommonArguments(d)
commonArgumentsObject, err := getMonitorDowntimeValuesOfCommonArguments(d, providerConfig)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -177,7 +176,7 @@ func resourceNewRelicMonitorDowntimeCreate(ctx context.Context, d *schema.Resour
guid, err := oneTimeCreateObject.createMonitorDowntimeOneTime(ctx, client)
if err != nil {
d.SetId("")
diag.FromErr(err)
return diag.FromErr(err)
}

d.SetId(guid)
Expand All @@ -190,7 +189,7 @@ func resourceNewRelicMonitorDowntimeCreate(ctx context.Context, d *schema.Resour
guid, err := dailyCreateObject.createMonitorDowntimeDaily(ctx, client)
if err != nil {
d.SetId("")
diag.FromErr(err)
return diag.FromErr(err)
}

d.SetId(guid)
Expand All @@ -203,7 +202,7 @@ func resourceNewRelicMonitorDowntimeCreate(ctx context.Context, d *schema.Resour
guid, err := weeklyCreateObject.createMonitorDowntimeWeekly(ctx, client)
if err != nil {
d.SetId("")
diag.FromErr(err)
return diag.FromErr(err)
}

d.SetId(guid)
Expand All @@ -216,7 +215,7 @@ func resourceNewRelicMonitorDowntimeCreate(ctx context.Context, d *schema.Resour
guid, err := monthlyCreateObject.createMonitorDowntimeMonthly(ctx, client)
if err != nil {
d.SetId("")
diag.FromErr(err)
return diag.FromErr(err)
}

d.SetId(guid)
Expand Down Expand Up @@ -281,7 +280,7 @@ func resourceNewRelicMonitorDowntimeRead(ctx context.Context, d *schema.Resource
func resourceNewRelicMonitorDowntimeUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConfig := meta.(*ProviderConfig)
client := providerConfig.NewClient
commonArgumentsObject, err := getMonitorDowntimeValuesOfCommonArguments(d)
commonArgumentsObject, err := getMonitorDowntimeValuesOfCommonArguments(d, providerConfig)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -296,7 +295,7 @@ func resourceNewRelicMonitorDowntimeUpdate(ctx context.Context, d *schema.Resour
guid, err := oneTimeUpdateObject.updateMonitorDowntimeOneTime(ctx, client, synthetics.EntityGUID(d.Id()))
if err != nil {
// d.SetId("")
diag.FromErr(err)
return diag.FromErr(err)
}

d.SetId(guid)
Expand All @@ -309,7 +308,7 @@ func resourceNewRelicMonitorDowntimeUpdate(ctx context.Context, d *schema.Resour
guid, err := dailyUpdateObject.updateMonitorDowntimeDaily(ctx, client, synthetics.EntityGUID(d.Id()))
if err != nil {
// d.SetId("")
diag.FromErr(err)
return diag.FromErr(err)
}

d.SetId(guid)
Expand All @@ -322,7 +321,7 @@ func resourceNewRelicMonitorDowntimeUpdate(ctx context.Context, d *schema.Resour
guid, err := weeklyUpdateObject.updateMonitorDowntimeWeekly(ctx, client, synthetics.EntityGUID(d.Id()))
if err != nil {
// d.SetId("")
diag.FromErr(err)
return diag.FromErr(err)
}

d.SetId(guid)
Expand All @@ -335,7 +334,7 @@ func resourceNewRelicMonitorDowntimeUpdate(ctx context.Context, d *schema.Resour
guid, err := monthlyUpdateObject.updateMonitorDowntimeMonthly(ctx, client, synthetics.EntityGUID(d.Id()))
if err != nil {
// d.SetId("")
diag.FromErr(err)
return diag.FromErr(err)
}

d.SetId(guid)
Expand Down
33 changes: 6 additions & 27 deletions newrelic/structures_newrelic_monitor_downtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -232,13 +231,10 @@ type SyntheticsMonitorDowntimeMonthlyInput struct {
}

// GET functions used to fetch values from the configuration
func getMonitorDowntimeValuesOfCommonArguments(d *schema.ResourceData) (*SyntheticsMonitorDowntimeCommonArgumentsInput, error) {
func getMonitorDowntimeValuesOfCommonArguments(d *schema.ResourceData, providerConfig *ProviderConfig) (*SyntheticsMonitorDowntimeCommonArgumentsInput, error) {
commonArgumentsObject := &SyntheticsMonitorDowntimeCommonArgumentsInput{}

accountID, err := getMonitorDowntimeAccountIDFromConfiguration(d)
if err != nil {
return nil, err
}
accountID := selectAccountID(providerConfig, d)

name, err := getMonitorDowntimeNameFromConfiguration(d)
if err != nil {
Expand Down Expand Up @@ -281,25 +277,6 @@ func getMonitorDowntimeValuesOfCommonArguments(d *schema.ResourceData) (*Synthet
return commonArgumentsObject, nil
}

func getMonitorDowntimeAccountIDFromConfiguration(d *schema.ResourceData) (int, error) {
val, ok := d.GetOk("account_id")
if ok {
if val.(string) == "" {
return 0, fmt.Errorf("%s has value \"\"", `account_id`)
}
accountIDAsInteger, err := strconv.Atoi(val.(string))
if err != nil {
return 0, err
}
return accountIDAsInteger, nil
}
accountIDAsInteger, err := strconv.Atoi(os.Getenv("NEW_RELIC_ACCOUNT_ID"))
if err != nil {
return 0, err
}
return accountIDAsInteger, nil
}

func getMonitorDowntimeNameFromConfiguration(d *schema.ResourceData) (string, error) {
val, ok := d.GetOk("name")
if ok {
Expand Down Expand Up @@ -825,8 +802,10 @@ func setMonitorDowntimeEndRepeat(d *schema.ResourceData, tags []entities.EntityT

}

func setMonitorDowntimeAccountID(tags []entities.EntityTag) string {
return getStringEntityTag(tags, "accountId")
func setMonitorDowntimeAccountID(tags []entities.EntityTag) int {
fetchedAccountID := getStringEntityTag(tags, "accountId")
x, _ := strconv.Atoi(fetchedAccountID)
return x
}

func setMonitorDowntimeMode(tags []entities.EntityTag) string {
Expand Down

0 comments on commit a9baf25

Please sign in to comment.