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

Standardize setting empty ID #1667

Merged
merged 1 commit into from
Jul 8, 2024
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
5 changes: 1 addition & 4 deletions internal/resources/cloud/resource_cloud_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"log"
"net/http"
"net/url"
"regexp"
Expand Down Expand Up @@ -338,9 +337,7 @@ func readStack(ctx context.Context, d *schema.ResourceData, client *gcom.APIClie
}

if stack.Status == "deleted" {
log.Printf("[WARN] removing stack %s from state because it was deleted outside of Terraform", stack.Name)
d.SetId("")
return nil
return common.WarnMissing("stack", d)
}

connectionsReq := client.InstancesAPI.GetConnections(ctx, id.(string))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ func readStackServiceAccount(ctx context.Context, d *schema.ResourceData, cloudC

resp, httpResp, err := cloudClient.InstancesAPI.GetInstanceServiceAccount(ctx, stackSlug, strconv.FormatInt(serviceAccountID, 10)).Execute()
if httpResp != nil && httpResp.StatusCode == 404 {
d.SetId("")
return nil
return common.WarnMissing("stack service account", d)
}
if err != nil {
return diag.FromErr(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cloud
import (
"context"
"fmt"
"log"
"strconv"
"time"

Expand Down Expand Up @@ -155,10 +154,7 @@ func stackServiceAccountTokenRead(ctx context.Context, d *schema.ResourceData, c
}
}

log.Printf("[WARN] removing service account token %d from state because it no longer exists in grafana", id)
d.SetId("")

return nil
return common.WarnMissing("stack service account token", d)
}

func stackServiceAccountTokenDelete(ctx context.Context, d *schema.ResourceData, cloudClient *gcom.APIClient) diag.Diagnostics {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cloud
import (
"context"
"fmt"
"log"
"strings"

"github.com/grafana/grafana-com-public-clients/go/gcom"
Expand Down Expand Up @@ -114,8 +113,7 @@ func resourceInstallationRead(ctx context.Context, d *schema.ResourceData, meta
apiURL := strings.Split(d.Id(), ";")[0]
tempClient := SMAPI.NewClient(apiURL, d.Get("sm_access_token").(string), nil)
if err := tempClient.ValidateToken(ctx); err != nil {
log.Printf("[WARN] removing SM installation from state because it is no longer valid")
d.SetId("")
return common.WarnMissing("synthetic monitoring installation", d)
}

return nil
Expand All @@ -124,9 +122,6 @@ func resourceInstallationRead(ctx context.Context, d *schema.ResourceData, meta
func resourceInstallationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiURL := strings.Split(d.Id(), ";")[0]
tempClient := SMAPI.NewClient(apiURL, d.Get("sm_access_token").(string), nil)
if err := tempClient.DeleteToken(ctx); err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
err := tempClient.DeleteToken(ctx)
return diag.FromErr(err)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package grafana

import (
"context"
"log"
"strconv"

"github.com/grafana/grafana-openapi-client-go/client/service_accounts"
Expand Down Expand Up @@ -138,10 +137,7 @@ func serviceAccountTokenRead(ctx context.Context, d *schema.ResourceData, m inte
}
}

log.Printf("[WARN] removing service account token%d from state because it no longer exists in grafana", id)
d.SetId("")

return nil
return common.WarnMissing("service account token", d)
}

func serviceAccountTokenDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
6 changes: 1 addition & 5 deletions internal/resources/machinelearning/resource_holiday.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,7 @@ func resourceHolidayUpdate(ctx context.Context, d *schema.ResourceData, meta int
func resourceHolidayDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*common.Client).MLAPI
err := c.DeleteHoliday(ctx, d.Id())
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
return diag.FromErr(err)
}

func makeMLHoliday(d *schema.ResourceData) (mlapi.Holiday, error) {
Expand Down
6 changes: 1 addition & 5 deletions internal/resources/machinelearning/resource_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,7 @@ func resourceJobUpdate(ctx context.Context, d *schema.ResourceData, meta interfa
func resourceJobDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*common.Client).MLAPI
err := c.DeleteJob(ctx, d.Id())
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
return diag.FromErr(err)
}

func makeMLJob(d *schema.ResourceData, meta interface{}) (mlapi.Job, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,7 @@ func resourceOutlierUpdate(ctx context.Context, d *schema.ResourceData, meta int
func resourceOutlierDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*common.Client).MLAPI
err := c.DeleteOutlierDetector(ctx, d.Id())
if err != nil {
return diag.FromErr(err)
}
d.SetId("")
return nil
return diag.FromErr(err)
}

func convertToSetStructure(al mlapi.OutlierAlgorithm) []interface{} {
Expand Down
13 changes: 2 additions & 11 deletions internal/resources/oncall/resource_escalation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package oncall
import (
"context"
"fmt"
"log"
"net/http"
"strings"

Expand Down Expand Up @@ -356,9 +355,7 @@ func resourceEscalationRead(ctx context.Context, d *schema.ResourceData, client
escalation, r, err := client.Escalations.GetEscalation(d.Id(), &onCallAPI.GetEscalationOptions{})
if err != nil {
if r != nil && r.StatusCode == http.StatusNotFound {
log.Printf("[WARN] removing escalation %s from state because it no longer exists", d.Id())
d.SetId("")
return nil
return common.WarnMissing("escalation", d)
}
return diag.FromErr(err)
}
Expand Down Expand Up @@ -473,11 +470,5 @@ func resourceEscalationUpdate(ctx context.Context, d *schema.ResourceData, clien

func resourceEscalationDelete(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
_, err := client.Escalations.DeleteEscalation(d.Id(), &onCallAPI.DeleteEscalationOptions{})
if err != nil {
return diag.FromErr(err)
}

d.SetId("")

return nil
return diag.FromErr(err)
}
13 changes: 2 additions & 11 deletions internal/resources/oncall/resource_escalation_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package oncall

import (
"context"
"log"
"net/http"

onCallAPI "github.com/grafana/amixr-api-go-client"
Expand Down Expand Up @@ -80,9 +79,7 @@ func resourceEscalationChainRead(ctx context.Context, d *schema.ResourceData, cl
escalationChain, r, err := client.EscalationChains.GetEscalationChain(d.Id(), &onCallAPI.GetEscalationChainOptions{})
if err != nil {
if r != nil && r.StatusCode == http.StatusNotFound {
log.Printf("[WARN] removing escalation chain %s from state because it no longer exists", d.Get("name").(string))
d.SetId("")
return nil
return common.WarnMissing("escalation chain", d)
}
return diag.FromErr(err)
}
Expand Down Expand Up @@ -113,11 +110,5 @@ func resourceEscalationChainUpdate(ctx context.Context, d *schema.ResourceData,

func resourceEscalationChainDelete(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
_, err := client.EscalationChains.DeleteEscalationChain(d.Id(), &onCallAPI.DeleteEscalationChainOptions{})
if err != nil {
return diag.FromErr(err)
}

d.SetId("")

return nil
return diag.FromErr(err)
}
13 changes: 2 additions & 11 deletions internal/resources/oncall/resource_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package oncall
import (
"context"
"fmt"
"log"
"net/http"
"strings"

Expand Down Expand Up @@ -345,9 +344,7 @@ func resourceIntegrationRead(ctx context.Context, d *schema.ResourceData, client
integration, r, err := client.Integrations.GetIntegration(d.Id(), options)
if err != nil {
if r != nil && r.StatusCode == http.StatusNotFound {
log.Printf("[WARN] removing integreation %s from state because it no longer exists", d.Get("name").(string))
d.SetId("")
return nil
return common.WarnMissing("integration", d)
}
return diag.FromErr(err)
}
Expand All @@ -365,13 +362,7 @@ func resourceIntegrationRead(ctx context.Context, d *schema.ResourceData, client
func resourceIntegrationDelete(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
options := &onCallAPI.DeleteIntegrationOptions{}
_, err := client.Integrations.DeleteIntegration(d.Id(), options)
if err != nil {
return diag.FromErr(err)
}

d.SetId("")

return nil
return diag.FromErr(err)
}

func flattenRouteSlack(in *onCallAPI.SlackRoute) []map[string]interface{} {
Expand Down
13 changes: 2 additions & 11 deletions internal/resources/oncall/resource_outgoing_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package oncall

import (
"context"
"log"
"net/http"

onCallAPI "github.com/grafana/amixr-api-go-client"
Expand Down Expand Up @@ -206,9 +205,7 @@ func resourceOutgoingWebhookRead(ctx context.Context, d *schema.ResourceData, cl
outgoingWebhook, r, err := client.Webhooks.GetWebhook(d.Id(), &onCallAPI.GetWebhookOptions{})
if err != nil {
if r != nil && r.StatusCode == http.StatusNotFound {
log.Printf("[WARN] removing outgoingWebhook %s from state because it no longer exists", d.Get("name").(string))
d.SetId("")
return nil
return common.WarnMissing("outgoing webhook", d)
}
return diag.FromErr(err)
}
Expand Down Expand Up @@ -309,11 +306,5 @@ func resourceOutgoingWebhookUpdate(ctx context.Context, d *schema.ResourceData,

func resourceOutgoingWebhookDelete(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
_, err := client.Webhooks.DeleteWebhook(d.Id(), &onCallAPI.DeleteWebhookOptions{})
if err != nil {
return diag.FromErr(err)
}

d.SetId("")

return nil
return diag.FromErr(err)
}
13 changes: 2 additions & 11 deletions internal/resources/oncall/resource_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package oncall
import (
"context"
"fmt"
"log"
"net/http"
"strings"

Expand Down Expand Up @@ -185,9 +184,7 @@ func resourceRouteRead(ctx context.Context, d *schema.ResourceData, client *onCa
route, r, err := client.Routes.GetRoute(d.Id(), &onCallAPI.GetRouteOptions{})
if err != nil {
if r != nil && r.StatusCode == http.StatusNotFound {
log.Printf("[WARN] removing route %s from state because it no longer exists", d.Id())
d.SetId("")
return nil
return common.WarnMissing("route", d)
}
return diag.FromErr(err)
}
Expand Down Expand Up @@ -246,11 +243,5 @@ func resourceRouteUpdate(ctx context.Context, d *schema.ResourceData, client *on

func resourceRouteDelete(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
_, err := client.Routes.DeleteRoute(d.Id(), &onCallAPI.DeleteRouteOptions{})
if err != nil {
return diag.FromErr(err)
}

d.SetId("")

return nil
return diag.FromErr(err)
}
13 changes: 2 additions & 11 deletions internal/resources/oncall/resource_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package oncall

import (
"context"
"log"
"net/http"
"strings"

Expand Down Expand Up @@ -252,9 +251,7 @@ func resourceScheduleRead(ctx context.Context, d *schema.ResourceData, client *o
schedule, r, err := client.Schedules.GetSchedule(d.Id(), options)
if err != nil {
if r != nil && r.StatusCode == http.StatusNotFound {
log.Printf("[WARN] removing schedule %s from state because it no longer exists", d.Get("name").(string))
d.SetId("")
return nil
return common.WarnMissing("schedule", d)
}
return diag.FromErr(err)
}
Expand All @@ -275,13 +272,7 @@ func resourceScheduleRead(ctx context.Context, d *schema.ResourceData, client *o
func resourceScheduleDelete(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
options := &onCallAPI.DeleteScheduleOptions{}
_, err := client.Schedules.DeleteSchedule(d.Id(), options)
if err != nil {
return diag.FromErr(err)
}

d.SetId("")

return nil
return diag.FromErr(err)
}

func flattenScheduleSlack(in *onCallAPI.SlackSchedule) []map[string]interface{} {
Expand Down
13 changes: 2 additions & 11 deletions internal/resources/oncall/resource_shift.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package oncall
import (
"context"
"fmt"
"log"
"net/http"
"strings"

Expand Down Expand Up @@ -448,9 +447,7 @@ func resourceOnCallShiftRead(ctx context.Context, d *schema.ResourceData, client
onCallShift, r, err := client.OnCallShifts.GetOnCallShift(d.Id(), options)
if err != nil {
if r != nil && r.StatusCode == http.StatusNotFound {
log.Printf("[WARN] removing on-call shift %s from state because it no longer exists", d.Id())
d.SetId("")
return nil
return common.WarnMissing("on-call shift", d)
}
return diag.FromErr(err)
}
Expand Down Expand Up @@ -478,11 +475,5 @@ func resourceOnCallShiftRead(ctx context.Context, d *schema.ResourceData, client
func resourceOnCallShiftDelete(ctx context.Context, d *schema.ResourceData, client *onCallAPI.Client) diag.Diagnostics {
options := &onCallAPI.DeleteOnCallShiftOptions{}
_, err := client.OnCallShifts.DeleteOnCallShift(d.Id(), options)
if err != nil {
return diag.FromErr(err)
}

d.SetId("")

return nil
return diag.FromErr(err)
}
Loading
Loading