Skip to content

Common Resource and DataSource framework #1616

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

Merged
merged 1 commit into from
Jun 11, 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
46 changes: 41 additions & 5 deletions internal/common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,72 @@ import (
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type ResourceCommon struct {
Name string
Schema *schema.Resource // Legacy SDKv2 schema
// Category string // TODO
}

// DataSource represents a Terraform data source, implemented either with the SDKv2 or Terraform Plugin Framework.
type DataSource struct {
ResourceCommon
PluginFrameworkSchema datasource.DataSourceWithConfigure
}

func NewLegacySDKDataSource(name string, schema *schema.Resource) *DataSource {
d := &DataSource{
ResourceCommon: ResourceCommon{
Name: name,
Schema: schema,
},
}
return d
}

func NewDataSource(name string, schema datasource.DataSourceWithConfigure) *DataSource {
d := &DataSource{
ResourceCommon: ResourceCommon{
Name: name,
},
PluginFrameworkSchema: schema,
}
return d
}

// ResourceListIDsFunc is a function that returns a list of resource IDs.
// This is used to generate TF config from existing resources.
// The data arg can be used to pass information between different listers. For example, the list of stacks will be used when listing stack plugins.
type ResourceListIDsFunc func(ctx context.Context, client *Client, data any) ([]string, error)

// Resource represents a Terraform resource, implemented either with the SDKv2 or Terraform Plugin Framework.
type Resource struct {
Name string
ResourceCommon
IDType *ResourceID
ListIDsFunc ResourceListIDsFunc
Schema *schema.Resource
PluginFrameworkSchema resource.ResourceWithConfigure
}

func NewLegacySDKResource(name string, idType *ResourceID, schema *schema.Resource) *Resource {
r := &Resource{
Name: name,
ResourceCommon: ResourceCommon{
Name: name,
Schema: schema,
},
IDType: idType,
Schema: schema,
}
return r
}

func NewResource(name string, idType *ResourceID, schema resource.ResourceWithConfigure) *Resource {
r := &Resource{
Name: name,
ResourceCommon: ResourceCommon{
Name: name,
},
IDType: idType,
PluginFrameworkSchema: schema,
}
Expand Down
6 changes: 4 additions & 2 deletions internal/resources/cloud/data_source_cloud_ips.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"net/http"
"strings"

"github.com/grafana/terraform-provider-grafana/v3/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func datasourceIPs() *schema.Resource {
return &schema.Resource{
func datasourceIPs() *common.DataSource {
schema := &schema.Resource{
Description: "Data source for retrieving sets of cloud IPs. See https://grafana.com/docs/grafana-cloud/reference/allow-list/ for more info",
ReadContext: datasourceIPsRead,
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -57,6 +58,7 @@ func datasourceIPs() *schema.Resource {
},
},
}
return common.NewLegacySDKDataSource("grafana_cloud_ips", schema)
}

func datasourceIPsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
6 changes: 4 additions & 2 deletions internal/resources/cloud/data_source_cloud_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"strconv"

"github.com/grafana/grafana-com-public-clients/go/gcom"
"github.com/grafana/terraform-provider-grafana/v3/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func datasourceOrganization() *schema.Resource {
return &schema.Resource{
func datasourceOrganization() *common.DataSource {
schema := &schema.Resource{
ReadContext: withClient[schema.ReadContextFunc](datasourceOrganizationRead),
Schema: map[string]*schema.Schema{
"id": {
Expand Down Expand Up @@ -41,6 +42,7 @@ func datasourceOrganization() *schema.Resource {
},
},
}
return common.NewLegacySDKDataSource("grafana_cloud_organization", schema)
}

func datasourceOrganizationRead(ctx context.Context, d *schema.ResourceData, client *gcom.APIClient) diag.Diagnostics {
Expand Down
5 changes: 3 additions & 2 deletions internal/resources/cloud/data_source_cloud_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func datasourceStack() *schema.Resource {
return &schema.Resource{
func datasourceStack() *common.DataSource {
schema := &schema.Resource{
Description: "Data source for Grafana Stack",
ReadContext: withClient[schema.ReadContextFunc](datasourceStackRead),
Schema: common.CloneResourceSchemaForDatasource(resourceStack().Schema, map[string]*schema.Schema{
Expand All @@ -30,6 +30,7 @@ available at “https://<stack_slug>.grafana.net".`,
"wait_for_readiness_timeout": nil,
}),
}
return common.NewLegacySDKDataSource("grafana_cloud_stack", schema)
}

func datasourceStackRead(ctx context.Context, d *schema.ResourceData, client *gcom.APIClient) diag.Diagnostics {
Expand Down
9 changes: 4 additions & 5 deletions internal/resources/cloud/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package cloud

import (
"github.com/grafana/terraform-provider-grafana/v3/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var DatasourcesMap = map[string]*schema.Resource{
"grafana_cloud_ips": datasourceIPs(),
"grafana_cloud_organization": datasourceOrganization(),
"grafana_cloud_stack": datasourceStack(),
var DataSources = []*common.DataSource{
datasourceIPs(),
datasourceOrganization(),
datasourceStack(),
}

var Resources = []*common.Resource{
Expand Down
5 changes: 3 additions & 2 deletions internal/resources/grafana/data_source_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func datasourceDashboard() *schema.Resource {
return &schema.Resource{
func datasourceDashboard() *common.DataSource {
schema := &schema.Resource{
Description: `
* [Official documentation](https://grafana.com/docs/grafana/latest/dashboards/)
* [Folder/Dashboard Search HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/folder_dashboard_search/)
Expand Down Expand Up @@ -74,6 +74,7 @@ func datasourceDashboard() *schema.Resource {
},
},
}
return common.NewLegacySDKDataSource("grafana_dashboard", schema)
}

func dataSourceDashboardRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
5 changes: 3 additions & 2 deletions internal/resources/grafana/data_source_dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func datasourceDashboards() *schema.Resource {
return &schema.Resource{
func datasourceDashboards() *common.DataSource {
schema := &schema.Resource{
Description: `
Datasource for retrieving all dashboards. Specify list of folder IDs to search in for dashboards.

Expand Down Expand Up @@ -63,6 +63,7 @@ Datasource for retrieving all dashboards. Specify list of folder IDs to search i
},
},
}
return common.NewLegacySDKDataSource("grafana_dashboards", schema)
}

func dataSourceReadDashboards(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
5 changes: 3 additions & 2 deletions internal/resources/grafana/data_source_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func datasourceDatasource() *schema.Resource {
return &schema.Resource{
func datasourceDatasource() *common.DataSource {
schema := &schema.Resource{
Description: "Get details about a Grafana Datasource querying by either name, uid or ID",
ReadContext: datasourceDatasourceRead,
Schema: common.CloneResourceSchemaForDatasource(resourceDataSource().Schema, map[string]*schema.Schema{
Expand All @@ -31,6 +31,7 @@ func datasourceDatasource() *schema.Resource {
"http_headers": nil,
}),
}
return common.NewLegacySDKDataSource("grafana_data_source", schema)
}

func datasourceDatasourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
5 changes: 3 additions & 2 deletions internal/resources/grafana/data_source_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func datasourceFolder() *schema.Resource {
return &schema.Resource{
func datasourceFolder() *common.DataSource {
schema := &schema.Resource{
Description: `
* [Official documentation](https://grafana.com/docs/grafana/latest/dashboards/manage-dashboards/)
* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/folder/)
Expand All @@ -28,6 +28,7 @@ func datasourceFolder() *schema.Resource {
"prevent_destroy_if_not_empty": nil,
}),
}
return common.NewLegacySDKDataSource("grafana_folder", schema)
}

func findFolderWithTitle(client *goapi.GrafanaHTTPAPI, title string) (string, error) {
Expand Down
5 changes: 3 additions & 2 deletions internal/resources/grafana/data_source_folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func datasourceFolders() *schema.Resource {
return &schema.Resource{
func datasourceFolders() *common.DataSource {
schema := &schema.Resource{
ReadContext: readFolders,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand Down Expand Up @@ -55,6 +55,7 @@ func datasourceFolders() *schema.Resource {
},
},
}
return common.NewLegacySDKDataSource("grafana_folders", schema)
}

func readFolders(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
5 changes: 3 additions & 2 deletions internal/resources/grafana/data_source_library_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func datasourceLibraryPanel() *schema.Resource {
return &schema.Resource{
func datasourceLibraryPanel() *common.DataSource {
schema := &schema.Resource{
Description: "Data source for retrieving a single library panel by name or uid.",
ReadContext: dataSourceLibraryPanelRead,
Schema: common.CloneResourceSchemaForDatasource(resourceLibraryPanel().Schema, map[string]*schema.Schema{
Expand All @@ -26,6 +26,7 @@ func datasourceLibraryPanel() *schema.Resource {
},
}),
}
return common.NewLegacySDKDataSource("grafana_library_panel", schema)
}

func dataSourceLibraryPanelRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
6 changes: 4 additions & 2 deletions internal/resources/grafana/data_source_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"strconv"
"strings"

"github.com/grafana/terraform-provider-grafana/v3/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func datasourceOrganization() *schema.Resource {
return &schema.Resource{
func datasourceOrganization() *common.DataSource {
schema := &schema.Resource{
Description: `
* [Official documentation](https://grafana.com/docs/grafana/latest/administration/organization-management/)
* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/org/)
Expand Down Expand Up @@ -49,6 +50,7 @@ func datasourceOrganization() *schema.Resource {
},
},
}
return common.NewLegacySDKDataSource("grafana_organization", schema)
}

func dataSourceOrganizationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func datasourceOrganizationPreferences() *schema.Resource {
return &schema.Resource{
func datasourceOrganizationPreferences() *common.DataSource {
schema := &schema.Resource{
Description: `
* [Official documentation](https://grafana.com/docs/grafana/latest/administration/organization-management/)
* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/preferences/#get-current-org-prefs)
Expand All @@ -20,6 +20,7 @@ func datasourceOrganizationPreferences() *schema.Resource {
"org_id": orgIDAttribute(),
}),
}
return common.NewLegacySDKDataSource("grafana_organization_preferences", schema)
}

func dataSourceOrganizationPreferencesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
5 changes: 3 additions & 2 deletions internal/resources/grafana/data_source_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func datasourceRole() *schema.Resource {
return &schema.Resource{
func datasourceRole() *common.DataSource {
schema := &schema.Resource{
Description: `
**Note:** This resource is available only with Grafana Enterprise 8.+.

Expand All @@ -27,6 +27,7 @@ func datasourceRole() *schema.Resource {
"auto_increment_version": nil,
}),
}
return common.NewLegacySDKDataSource("grafana_role", schema)
}

func dataSourceRoleRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
5 changes: 3 additions & 2 deletions internal/resources/grafana/data_source_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func datasourceServiceAccount() *schema.Resource {
return &schema.Resource{
func datasourceServiceAccount() *common.DataSource {
schema := &schema.Resource{
Description: `
* [Official documentation](https://grafana.com/docs/grafana/latest/administration/service-accounts/)
* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/serviceaccount/#service-account-api)
Expand All @@ -28,6 +28,7 @@ func datasourceServiceAccount() *schema.Resource {
},
}),
}
return common.NewLegacySDKDataSource("grafana_service_account", schema)
}

func datasourceServiceAccountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
5 changes: 3 additions & 2 deletions internal/resources/grafana/data_source_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func datasourceTeam() *schema.Resource {
return &schema.Resource{
func datasourceTeam() *common.DataSource {
schema := &schema.Resource{
Description: `
* [Official documentation](https://grafana.com/docs/grafana/latest/administration/team-management/)
* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/team/)
Expand All @@ -32,6 +32,7 @@ func datasourceTeam() *schema.Resource {
"ignore_externally_synced_members": nil,
}),
}
return common.NewLegacySDKDataSource("grafana_team", schema)
}

func dataSourceTeamRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
Loading
Loading