Skip to content

Commit

Permalink
Mark old client as deprecated (#1227)
Browse files Browse the repository at this point in the history
Just making it clear so we don't get any reverts like #1225
The client itself will be marked as deprecated once all resources are migrated here and when cloud resources are also migrated off
  • Loading branch information
julienduchesne authored Dec 13, 2023
1 parent 3c57def commit cdac2ab
Show file tree
Hide file tree
Showing 28 changed files with 81 additions and 83 deletions.
10 changes: 5 additions & 5 deletions internal/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
)

type Client struct {
GrafanaAPIURL string
GrafanaAPIURLParsed *url.URL
GrafanaAPIConfig *gapi.Config
GrafanaAPI *gapi.Client
GrafanaCloudAPI *gapi.Client
GrafanaAPIURL string
GrafanaAPIURLParsed *url.URL
GrafanaAPIConfig *gapi.Config
DeprecatedGrafanaAPI *gapi.Client
GrafanaCloudAPI *gapi.Client

GrafanaOAPI *goapi.GrafanaHTTPAPI

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/configure_clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func createClients(providerConfig frameworkProviderConfig) (*common.Client, erro
var err error
c := &common.Client{}
if !providerConfig.Auth.IsNull() {
c.GrafanaAPIURL, c.GrafanaAPIConfig, c.GrafanaAPI, err = createGrafanaClient(providerConfig)
c.GrafanaAPIURL, c.GrafanaAPIConfig, c.DeprecatedGrafanaAPI, err = createGrafanaClient(providerConfig)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/legacy_provider_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
type metadataValidation func(resourceName string, m interface{}) error

func grafanaClientPresent(resourceName string, m interface{}) error {
if m.(*common.Client).GrafanaAPI == nil {
if m.(*common.Client).GrafanaOAPI == nil {
return fmt.Errorf("the Grafana client is required for `%s`. Set the auth and url provider attributes", resourceName)
}
return nil
Expand Down
14 changes: 6 additions & 8 deletions internal/resources/grafana/oss_org_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ func SplitOrgResourceID(id string) (int64, string) {
return 0, id
}

// ClientFromExistingOrgResource creates a client from the ID of an org-scoped resource
// Those IDs are in the <orgID>:<resourceID> format
func ClientFromExistingOrgResource(meta interface{}, id string) (*gapi.Client, int64, string) {
// Deprecated: Use OAPIClientFromExistingOrgResource instead
func DeprecatedClientFromExistingOrgResource(meta interface{}, id string) (*gapi.Client, int64, string) {
orgID, restOfID := SplitOrgResourceID(id)
client := meta.(*common.Client).GrafanaAPI
client := meta.(*common.Client).DeprecatedGrafanaAPI
if orgID == 0 {
orgID = meta.(*common.Client).GrafanaAPIConfig.OrgID // It's configured globally. TODO: Remove this once we drop support for the global org_id
} else if orgID > 0 {
Expand All @@ -53,11 +52,10 @@ func ClientFromExistingOrgResource(meta interface{}, id string) (*gapi.Client, i
return client, orgID, restOfID
}

// ClientFromNewOrgResource creates a client from the `org_id` attribute of a resource
// This client is meant to be used in `Create` functions when the ID hasn't already been baked into the resource ID
func ClientFromNewOrgResource(meta interface{}, d *schema.ResourceData) (*gapi.Client, int64) {
// Deprecated: Use OAPIClientFromNewOrgResource instead
func DeprecatedClientFromNewOrgResource(meta interface{}, d *schema.ResourceData) (*gapi.Client, int64) {
orgID := parseOrgID(d)
client := meta.(*common.Client).GrafanaAPI
client := meta.(*common.Client).DeprecatedGrafanaAPI
if orgID == 0 {
orgID = meta.(*common.Client).GrafanaAPIConfig.OrgID // It's configured globally. TODO: Remove this once we drop support for the global org_id
} else if orgID > 0 {
Expand Down
10 changes: 5 additions & 5 deletions internal/resources/grafana/resource_alerting_contact_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ This resource requires Grafana 9.1.0 or later.

func importContactPoint(ctx context.Context, data *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
name := data.Id()
client := meta.(*common.Client).GrafanaAPI
client := meta.(*common.Client).DeprecatedGrafanaAPI

ps, err := client.ContactPointsByName(name)
if err != nil {
Expand All @@ -107,7 +107,7 @@ func importContactPoint(ctx context.Context, data *schema.ResourceData, meta int
}

func readContactPoint(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*common.Client).GrafanaAPI
client := meta.(*common.Client).DeprecatedGrafanaAPI

uidsToFetch := unpackUIDs(data.Id())

Expand Down Expand Up @@ -139,7 +139,7 @@ func readContactPoint(ctx context.Context, data *schema.ResourceData, meta inter

func createContactPoint(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
lock := &meta.(*common.Client).AlertingMutex
client := meta.(*common.Client).GrafanaAPI
client := meta.(*common.Client).DeprecatedGrafanaAPI

ps := unpackContactPoints(data)
uids := make([]string, 0, len(ps))
Expand All @@ -165,7 +165,7 @@ func createContactPoint(ctx context.Context, data *schema.ResourceData, meta int

func updateContactPoint(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
lock := &meta.(*common.Client).AlertingMutex
client := meta.(*common.Client).GrafanaAPI
client := meta.(*common.Client).DeprecatedGrafanaAPI

existingUIDs := unpackUIDs(data.Id())
ps := unpackContactPoints(data)
Expand Down Expand Up @@ -207,7 +207,7 @@ func updateContactPoint(ctx context.Context, data *schema.ResourceData, meta int

func deleteContactPoint(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
lock := &meta.(*common.Client).AlertingMutex
client := meta.(*common.Client).GrafanaAPI
client := meta.(*common.Client).DeprecatedGrafanaAPI

uids := unpackUIDs(data.Id())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func TestAccContactPoint_notifiers(t *testing.T) {
}
uid := rs.Primary.ID

client := testutils.Provider.Meta().(*common.Client).GrafanaAPI
client := testutils.Provider.Meta().(*common.Client).DeprecatedGrafanaAPI
pt, err := client.ContactPoint(uid)
if err != nil {
return fmt.Errorf("error getting resource: %w", err)
Expand Down Expand Up @@ -431,7 +431,7 @@ func testContactPointCheckExists(rname string, pts *[]gapi.ContactPoint, expCoun
return fmt.Errorf("resource name not set")
}

client := testutils.Provider.Meta().(*common.Client).GrafanaAPI
client := testutils.Provider.Meta().(*common.Client).DeprecatedGrafanaAPI
points, err := client.ContactPointsByName(name)
if err != nil {
return fmt.Errorf("error getting resource: %w", err)
Expand All @@ -456,7 +456,7 @@ func testContactPointCheckExists(rname string, pts *[]gapi.ContactPoint, expCoun

func testContactPointCheckDestroy(points []gapi.ContactPoint) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testutils.Provider.Meta().(*common.Client).GrafanaAPI
client := testutils.Provider.Meta().(*common.Client).DeprecatedGrafanaAPI
for _, p := range points {
_, err := client.ContactPoint(p.UID)
if err == nil {
Expand All @@ -470,7 +470,7 @@ func testContactPointCheckDestroy(points []gapi.ContactPoint) resource.TestCheck

func testContactPointCheckAllDestroy(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testutils.Provider.Meta().(*common.Client).GrafanaAPI
client := testutils.Provider.Meta().(*common.Client).DeprecatedGrafanaAPI
points, err := client.ContactPointsByName(name)
if err != nil {
return fmt.Errorf("error getting resource: %w", err)
Expand Down
8 changes: 4 additions & 4 deletions internal/resources/grafana/resource_alerting_mute_timing.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ This resource requires Grafana 9.1.0 or later.
}

func readMuteTiming(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*common.Client).GrafanaAPI
client := meta.(*common.Client).DeprecatedGrafanaAPI

name := data.Id()
mt, err := client.MuteTiming(name)
Expand All @@ -133,7 +133,7 @@ func readMuteTiming(ctx context.Context, data *schema.ResourceData, meta interfa

func createMuteTiming(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
lock := &meta.(*common.Client).AlertingMutex
client := meta.(*common.Client).GrafanaAPI
client := meta.(*common.Client).DeprecatedGrafanaAPI

mt := unpackMuteTiming(data)

Expand All @@ -149,7 +149,7 @@ func createMuteTiming(ctx context.Context, data *schema.ResourceData, meta inter

func updateMuteTiming(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
lock := &meta.(*common.Client).AlertingMutex
client := meta.(*common.Client).GrafanaAPI
client := meta.(*common.Client).DeprecatedGrafanaAPI

mt := unpackMuteTiming(data)

Expand All @@ -163,7 +163,7 @@ func updateMuteTiming(ctx context.Context, data *schema.ResourceData, meta inter

func deleteMuteTiming(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
lock := &meta.(*common.Client).AlertingMutex
client := meta.(*common.Client).GrafanaAPI
client := meta.(*common.Client).DeprecatedGrafanaAPI
name := data.Id()

lock.Lock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func policySchema(depth uint) *schema.Resource {
}

func readNotificationPolicy(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*common.Client).GrafanaAPI
client := meta.(*common.Client).DeprecatedGrafanaAPI

npt, err := client.NotificationPolicyTree()
if err != nil {
Expand All @@ -186,7 +186,7 @@ func readNotificationPolicy(ctx context.Context, data *schema.ResourceData, meta

func createNotificationPolicy(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
lock := &meta.(*common.Client).AlertingMutex
client := meta.(*common.Client).GrafanaAPI
client := meta.(*common.Client).DeprecatedGrafanaAPI

npt, err := unpackNotifPolicy(data)
if err != nil {
Expand All @@ -205,7 +205,7 @@ func createNotificationPolicy(ctx context.Context, data *schema.ResourceData, me

func updateNotificationPolicy(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
lock := &meta.(*common.Client).AlertingMutex
client := meta.(*common.Client).GrafanaAPI
client := meta.(*common.Client).DeprecatedGrafanaAPI

npt, err := unpackNotifPolicy(data)
if err != nil {
Expand All @@ -223,7 +223,7 @@ func updateNotificationPolicy(ctx context.Context, data *schema.ResourceData, me

func deleteNotificationPolicy(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
lock := &meta.(*common.Client).AlertingMutex
client := meta.(*common.Client).GrafanaAPI
client := meta.(*common.Client).DeprecatedGrafanaAPI

lock.Lock()
defer lock.Unlock()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestAccNotificationPolicy_basic(t *testing.T) {

func testNotifPolicyCheckDestroy() resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testutils.Provider.Meta().(*common.Client).GrafanaAPI
client := testutils.Provider.Meta().(*common.Client).DeprecatedGrafanaAPI
npt, err := client.NotificationPolicyTree()
if err != nil {
return fmt.Errorf("failed to get notification policies")
Expand All @@ -114,7 +114,7 @@ func testNotifPolicyCheckExists(rname string) resource.TestCheckFunc {
return fmt.Errorf("resource id not set")
}

client := testutils.Provider.Meta().(*common.Client).GrafanaAPI
client := testutils.Provider.Meta().(*common.Client).DeprecatedGrafanaAPI
npt, err := client.NotificationPolicyTree()
if err != nil {
return fmt.Errorf("failed to get notification policies")
Expand Down
6 changes: 3 additions & 3 deletions internal/resources/grafana/resource_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Manages Grafana API Keys.
}

func resourceAPIKeyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c, orgID := ClientFromNewOrgResource(m, d)
c, orgID := DeprecatedClientFromNewOrgResource(m, d)

request := gapi.CreateAPIKeyRequest{
Name: d.Get("name").(string),
Expand All @@ -82,7 +82,7 @@ func resourceAPIKeyCreate(ctx context.Context, d *schema.ResourceData, m interfa
}

func resourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c, orgID, idStr := ClientFromExistingOrgResource(m, d.Id())
c, orgID, idStr := DeprecatedClientFromExistingOrgResource(m, d.Id())

response, err := c.GetAPIKeys(true)
if err, shouldReturn := common.CheckReadError("API key", d, err); shouldReturn {
Expand Down Expand Up @@ -115,7 +115,7 @@ func resourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, m interface
}

func resourceAPIKeyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c, _, idStr := ClientFromExistingOrgResource(m, d.Id())
c, _, idStr := DeprecatedClientFromExistingOrgResource(m, d.Id())
id, err := strconv.ParseInt(idStr, 10, 32)
if err != nil {
return diag.FromErr(err)
Expand Down
8 changes: 4 additions & 4 deletions internal/resources/grafana/resource_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Manages Grafana dashboards.
}

func CreateDashboard(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, orgID := ClientFromNewOrgResource(meta, d)
client, orgID := DeprecatedClientFromNewOrgResource(meta, d)

dashboard, err := makeDashboard(d)
if err != nil {
Expand All @@ -110,7 +110,7 @@ func CreateDashboard(ctx context.Context, d *schema.ResourceData, meta interface

func ReadDashboard(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
metaClient := meta.(*common.Client)
client, orgID, uid := ClientFromExistingOrgResource(meta, d.Id())
client, orgID, uid := DeprecatedClientFromExistingOrgResource(meta, d.Id())

dashboard, err := client.DashboardByUID(uid)
if err, shouldReturn := common.CheckReadError("dashboard", d, err); shouldReturn {
Expand Down Expand Up @@ -166,7 +166,7 @@ func ReadDashboard(ctx context.Context, d *schema.ResourceData, meta interface{}
}

func UpdateDashboard(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, orgID := ClientFromNewOrgResource(meta, d)
client, orgID := DeprecatedClientFromNewOrgResource(meta, d)

dashboard, err := makeDashboard(d)
if err != nil {
Expand All @@ -183,7 +183,7 @@ func UpdateDashboard(ctx context.Context, d *schema.ResourceData, meta interface
}

func DeleteDashboard(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, _, uid := ClientFromExistingOrgResource(meta, d.Id())
client, _, uid := DeprecatedClientFromExistingOrgResource(meta, d.Id())
err, _ := common.CheckReadError("dashboard", d, client.DeleteDashboardByUID(uid))
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func testAccDashboardPermissionsCheckExistsUID(rn string, dashboardUID *string)
}

orgID, gotDashboardUID := grafana.SplitOrgResourceID(rs.Primary.ID)
client := testutils.Provider.Meta().(*common.Client).GrafanaAPI.WithOrgID(orgID)
client := testutils.Provider.Meta().(*common.Client).DeprecatedGrafanaAPI.WithOrgID(orgID)

_, err := client.DashboardPermissionsByUID(gotDashboardUID)
if err != nil {
Expand All @@ -102,7 +102,7 @@ func testAccDashboardPermissionsCheckExists(rn string, dashboardID *int64) resou
}

orgID, dashboardIDStr := grafana.SplitOrgResourceID(rs.Primary.ID)
client := testutils.Provider.Meta().(*common.Client).GrafanaAPI.WithOrgID(orgID)
client := testutils.Provider.Meta().(*common.Client).DeprecatedGrafanaAPI.WithOrgID(orgID)

gotDashboardID, err := strconv.ParseInt(dashboardIDStr, 10, 64)
if err != nil {
Expand All @@ -122,7 +122,7 @@ func testAccDashboardPermissionsCheckExists(rn string, dashboardID *int64) resou

func testAccDashboardPermissionsCheckEmptyUID(dashboardUID *string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testutils.Provider.Meta().(*common.Client).GrafanaAPI
client := testutils.Provider.Meta().(*common.Client).DeprecatedGrafanaAPI
permissions, err := client.DashboardPermissionsByUID(*dashboardUID)
if err != nil {
return fmt.Errorf("Error getting dashboard permissions %s: %s", *dashboardUID, err)
Expand Down
8 changes: 4 additions & 4 deletions internal/resources/grafana/resource_dashboard_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Manages Grafana public dashboards.
}

func CreatePublicDashboard(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, orgID := ClientFromNewOrgResource(meta, d)
client, orgID := DeprecatedClientFromNewOrgResource(meta, d)
dashboardUID := d.Get("dashboard_uid").(string)

publicDashboardPayload := makePublicDashboard(d)
Expand All @@ -92,7 +92,7 @@ func CreatePublicDashboard(ctx context.Context, d *schema.ResourceData, meta int
}
func UpdatePublicDashboard(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
orgID, dashboardUID, publicDashboardUID := SplitPublicDashboardID(d.Id())
client := meta.(*common.Client).GrafanaAPI.WithOrgID(orgID)
client := meta.(*common.Client).DeprecatedGrafanaAPI.WithOrgID(orgID)

publicDashboard := makePublicDashboard(d)
pd, err := client.UpdatePublicDashboard(dashboardUID, publicDashboardUID, publicDashboard)
Expand All @@ -106,7 +106,7 @@ func UpdatePublicDashboard(ctx context.Context, d *schema.ResourceData, meta int

func DeletePublicDashboard(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
orgID, dashboardUID, publicDashboardUID := SplitPublicDashboardID(d.Id())
client := meta.(*common.Client).GrafanaAPI.WithOrgID(orgID)
client := meta.(*common.Client).DeprecatedGrafanaAPI.WithOrgID(orgID)
return diag.FromErr(client.DeletePublicDashboard(dashboardUID, publicDashboardUID))
}

Expand All @@ -123,7 +123,7 @@ func makePublicDashboard(d *schema.ResourceData) gapi.PublicDashboardPayload {

func ReadPublicDashboard(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
orgID, dashboardUID, _ := SplitPublicDashboardID(d.Id())
client := meta.(*common.Client).GrafanaAPI.WithOrgID(orgID)
client := meta.(*common.Client).DeprecatedGrafanaAPI.WithOrgID(orgID)
pd, err := client.PublicDashboardbyUID(dashboardUID)
if err, shouldReturn := common.CheckReadError("dashboard", d, err); shouldReturn {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func testAccPublicDashboardCheckExistsUID(rn string) resource.TestCheckFunc {

orgID, dashboardUID, _ := grafana.SplitPublicDashboardID(rs.Primary.ID)

client := testutils.Provider.Meta().(*common.Client).GrafanaAPI.WithOrgID(orgID)
client := testutils.Provider.Meta().(*common.Client).DeprecatedGrafanaAPI.WithOrgID(orgID)
pd, err := client.PublicDashboardbyUID(dashboardUID)
if pd == nil || err != nil {
return fmt.Errorf("Error getting public dashboard: %s", err)
Expand Down
6 changes: 3 additions & 3 deletions internal/resources/grafana/resource_data_source_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Manages the entire set of permissions for a datasource. Permissions that aren't
}

func UpdateDatasourcePermissions(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, orgID := ClientFromNewOrgResource(meta, d)
client, orgID := DeprecatedClientFromNewOrgResource(meta, d)

var list []interface{}
if v, ok := d.GetOk("permissions"); ok {
Expand Down Expand Up @@ -128,7 +128,7 @@ func UpdateDatasourcePermissions(ctx context.Context, d *schema.ResourceData, me
}

func ReadDatasourcePermissions(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, _, idStr := ClientFromExistingOrgResource(meta, d.Id())
client, _, idStr := DeprecatedClientFromExistingOrgResource(meta, d.Id())

id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
Expand Down Expand Up @@ -166,7 +166,7 @@ func ReadDatasourcePermissions(ctx context.Context, d *schema.ResourceData, meta
}

func DeleteDatasourcePermissions(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, _, idStr := ClientFromExistingOrgResource(meta, d.Id())
client, _, idStr := DeprecatedClientFromExistingOrgResource(meta, d.Id())

id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
Expand Down
Loading

0 comments on commit cdac2ab

Please sign in to comment.