Skip to content

Commit

Permalink
Self-contained grafana package (#1420)
Browse files Browse the repository at this point in the history
Last one
This moves the mapping of resources and client validation to the SM package. This will make it easier to support TF code generation + upgrade to the new TF plugin framework
  • Loading branch information
julienduchesne authored Mar 14, 2024
1 parent 272030b commit bd938ad
Show file tree
Hide file tree
Showing 44 changed files with 153 additions and 180 deletions.
62 changes: 2 additions & 60 deletions internal/provider/legacy_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,64 +34,6 @@ func init() {
// Provider returns a terraform-provider-sdk2 provider.
// This is the deprecated way of creating a provider, and should only be used for legacy resources.
func Provider(version string) *schema.Provider {
var (
// Resources that require the Grafana client to exist.
grafanaClientResources = addCreateReadResourcesMetadataValidation(
readGrafanaClientValidation,
createGrafanaClientValidation,
map[string]*schema.Resource{
// Grafana
"grafana_annotation": grafana.ResourceAnnotation(),
"grafana_api_key": grafana.ResourceAPIKey(),
"grafana_contact_point": grafana.ResourceContactPoint(),
"grafana_dashboard": grafana.ResourceDashboard(),
"grafana_dashboard_public": grafana.ResourcePublicDashboard(),
"grafana_dashboard_permission": grafana.ResourceDashboardPermission(),
"grafana_data_source": grafana.ResourceDataSource(),
"grafana_data_source_permission": grafana.ResourceDatasourcePermission(),
"grafana_folder": grafana.ResourceFolder(),
"grafana_folder_permission": grafana.ResourceFolderPermission(),
"grafana_library_panel": grafana.ResourceLibraryPanel(),
"grafana_message_template": grafana.ResourceMessageTemplate(),
"grafana_mute_timing": grafana.ResourceMuteTiming(),
"grafana_notification_policy": grafana.ResourceNotificationPolicy(),
"grafana_organization": grafana.ResourceOrganization(),
"grafana_organization_preferences": grafana.ResourceOrganizationPreferences(),
"grafana_playlist": grafana.ResourcePlaylist(),
"grafana_report": grafana.ResourceReport(),
"grafana_role": grafana.ResourceRole(),
"grafana_role_assignment": grafana.ResourceRoleAssignment(),
"grafana_rule_group": grafana.ResourceRuleGroup(),
"grafana_team": grafana.ResourceTeam(),
"grafana_team_external_group": grafana.ResourceTeamExternalGroup(),
"grafana_service_account_token": grafana.ResourceServiceAccountToken(),
"grafana_service_account": grafana.ResourceServiceAccount(),
"grafana_service_account_permission": grafana.ResourceServiceAccountPermission(),
"grafana_sso_settings": grafana.ResourceSSOSettings(),
"grafana_user": grafana.ResourceUser(),
})

// Datasources that require the Grafana client to exist.
grafanaClientDatasources = addCreateReadResourcesMetadataValidation(
readGrafanaClientValidation,
createGrafanaClientValidation,
map[string]*schema.Resource{
"grafana_dashboard": grafana.DatasourceDashboard(),
"grafana_dashboards": grafana.DatasourceDashboards(),
"grafana_data_source": grafana.DatasourceDatasource(),
"grafana_folder": grafana.DatasourceFolder(),
"grafana_folders": grafana.DatasourceFolders(),
"grafana_library_panel": grafana.DatasourceLibraryPanel(),
"grafana_user": grafana.DatasourceUser(),
"grafana_users": grafana.DatasourceUsers(),
"grafana_role": grafana.DatasourceRole(),
"grafana_service_account": grafana.DatasourceServiceAccount(),
"grafana_team": grafana.DatasourceTeam(),
"grafana_organization": grafana.DatasourceOrganization(),
"grafana_organization_preferences": grafana.DatasourceOrganizationPreferences(),
})
)

p := &schema.Provider{
Schema: map[string]*schema.Schema{
"url": {
Expand Down Expand Up @@ -210,7 +152,7 @@ func Provider(version string) *schema.Provider {
},

ResourcesMap: mergeResourceMaps(
grafanaClientResources,
grafana.ResourcesMap,
machinelearning.ResourcesMap,
slo.ResourcesMap,
syntheticmonitoring.ResourcesMap,
Expand All @@ -219,7 +161,7 @@ func Provider(version string) *schema.Provider {
),

DataSourcesMap: mergeResourceMaps(
grafanaClientDatasources,
grafana.DatasourcesMap,
machinelearning.DatasourcesMap,
slo.DatasourcesMap,
syntheticmonitoring.DatasourcesMap,
Expand Down
70 changes: 0 additions & 70 deletions internal/provider/legacy_provider_validation.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/resources/grafana/data_source_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DatasourceDashboard() *schema.Resource {
func datasourceDashboard() *schema.Resource {
return &schema.Resource{
Description: `
* [Official documentation](https://grafana.com/docs/grafana/latest/dashboards/)
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/grafana/data_source_dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DatasourceDashboards() *schema.Resource {
func datasourceDashboards() *schema.Resource {
return &schema.Resource{
Description: `
Datasource for retrieving all dashboards. Specify list of folder IDs to search in for dashboards.
Expand Down
4 changes: 2 additions & 2 deletions internal/resources/grafana/data_source_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DatasourceDatasource() *schema.Resource {
func datasourceDatasource() *schema.Resource {
return &schema.Resource{
Description: "Get details about a Grafana Datasource querying by either name, uid or ID",
ReadContext: datasourceDatasourceRead,
Schema: common.CloneResourceSchemaForDatasource(ResourceDataSource(), map[string]*schema.Schema{
Schema: common.CloneResourceSchemaForDatasource(resourceDataSource(), map[string]*schema.Schema{
"org_id": orgIDAttribute(),
"id": {
Type: schema.TypeString,
Expand Down
4 changes: 2 additions & 2 deletions internal/resources/grafana/data_source_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DatasourceFolder() *schema.Resource {
func datasourceFolder() *schema.Resource {
return &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/)
`,
ReadContext: dataSourceFolderRead,
Schema: common.CloneResourceSchemaForDatasource(ResourceFolder(), map[string]*schema.Schema{
Schema: common.CloneResourceSchemaForDatasource(resourceFolder(), map[string]*schema.Schema{
"org_id": orgIDAttribute(),
"title": {
Type: schema.TypeString,
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/grafana/data_source_folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DatasourceFolders() *schema.Resource {
func datasourceFolders() *schema.Resource {
return &schema.Resource{
ReadContext: readFolders,
Importer: &schema.ResourceImporter{
Expand Down
4 changes: 2 additions & 2 deletions internal/resources/grafana/data_source_library_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DatasourceLibraryPanel() *schema.Resource {
func datasourceLibraryPanel() *schema.Resource {
return &schema.Resource{
Description: "Data source for retrieving a single library panel by name or uid.",
ReadContext: dataSourceLibraryPanelRead,
Schema: common.CloneResourceSchemaForDatasource(ResourceLibraryPanel(), map[string]*schema.Schema{
Schema: common.CloneResourceSchemaForDatasource(resourceLibraryPanel(), map[string]*schema.Schema{
"org_id": orgIDAttribute(),
"name": {
Type: schema.TypeString,
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/grafana/data_source_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DatasourceOrganization() *schema.Resource {
func datasourceOrganization() *schema.Resource {
return &schema.Resource{
Description: `
* [Official documentation](https://grafana.com/docs/grafana/latest/administration/organization-management/)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DatasourceOrganizationPreferences() *schema.Resource {
func datasourceOrganizationPreferences() *schema.Resource {
return &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)
`,
ReadContext: dataSourceOrganizationPreferencesRead,
Schema: common.CloneResourceSchemaForDatasource(ResourceOrganizationPreferences(), map[string]*schema.Schema{
Schema: common.CloneResourceSchemaForDatasource(resourceOrganizationPreferences(), map[string]*schema.Schema{
"org_id": orgIDAttribute(),
}),
}
Expand Down
4 changes: 2 additions & 2 deletions internal/resources/grafana/data_source_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DatasourceRole() *schema.Resource {
func datasourceRole() *schema.Resource {
return &schema.Resource{
Description: `
**Note:** This resource is available only with Grafana Enterprise 8.+.
Expand All @@ -18,7 +18,7 @@ func DatasourceRole() *schema.Resource {
* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/access_control/)
`,
ReadContext: dataSourceRoleRead,
Schema: common.CloneResourceSchemaForDatasource(ResourceRole(), map[string]*schema.Schema{
Schema: common.CloneResourceSchemaForDatasource(resourceRole(), map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Expand Down
8 changes: 4 additions & 4 deletions internal/resources/grafana/data_source_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DatasourceServiceAccount() *schema.Resource {
func datasourceServiceAccount() *schema.Resource {
return &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)
`,
ReadContext: DatasourceServiceAccountRead,
Schema: common.CloneResourceSchemaForDatasource(ResourceServiceAccount(), map[string]*schema.Schema{
ReadContext: datasourceServiceAccountRead,
Schema: common.CloneResourceSchemaForDatasource(resourceServiceAccount(), map[string]*schema.Schema{
"org_id": orgIDAttribute(),
"name": {
Type: schema.TypeString,
Expand All @@ -30,7 +30,7 @@ func DatasourceServiceAccount() *schema.Resource {
}
}

func DatasourceServiceAccountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func datasourceServiceAccountRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, orgID := OAPIClientFromNewOrgResource(meta, d)
name := d.Get("name").(string)
sa, err := findServiceAccountByName(client, name)
Expand Down
4 changes: 2 additions & 2 deletions internal/resources/grafana/data_source_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DatasourceTeam() *schema.Resource {
func datasourceTeam() *schema.Resource {
return &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/)
`,
ReadContext: dataSourceTeamRead,
Schema: common.CloneResourceSchemaForDatasource(ResourceTeam(), map[string]*schema.Schema{
Schema: common.CloneResourceSchemaForDatasource(resourceTeam(), map[string]*schema.Schema{
"org_id": orgIDAttribute(),
"name": {
Type: schema.TypeString,
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/grafana/data_source_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DatasourceUser() *schema.Resource {
func datasourceUser() *schema.Resource {
return &schema.Resource{
Description: `
* [Official documentation](https://grafana.com/docs/grafana/latest/administration/user-management/server-user-management/)
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/grafana/data_source_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DatasourceUsers() *schema.Resource {
func datasourceUsers() *schema.Resource {
return &schema.Resource{
ReadContext: readUsers,
Importer: &schema.ResourceImporter{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var notifiers = []notifier{
wecomNotifier{},
}

func ResourceContactPoint() *schema.Resource {
func resourceContactPoint() *schema.Resource {
resource := &schema.Resource{
Description: `
Manages Grafana Alerting contact points.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func ResourceMessageTemplate() *schema.Resource {
func resourceMessageTemplate() *schema.Resource {
return &schema.Resource{
Description: `
Manages Grafana Alerting message templates.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func ResourceMuteTiming() *schema.Resource {
func resourceMuteTiming() *schema.Resource {
return &schema.Resource{
Description: `
Manages Grafana Alerting mute timings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/grafana/terraform-provider-grafana/internal/common"
)

func ResourceNotificationPolicy() *schema.Resource {
func resourceNotificationPolicy() *schema.Resource {
return &schema.Resource{
Description: `
Sets the global notification policy for Grafana.
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/grafana/resource_alerting_rule_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/grafana/terraform-provider-grafana/internal/common"
)

func ResourceRuleGroup() *schema.Resource {
func resourceRuleGroup() *schema.Resource {
return &schema.Resource{
Description: `
Manages Grafana Alerting rule groups.
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/grafana/resource_annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func ResourceAnnotation() *schema.Resource {
func resourceAnnotation() *schema.Resource {
return &schema.Resource{

Description: `
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/grafana/resource_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func ResourceAPIKey() *schema.Resource {
func resourceAPIKey() *schema.Resource {
return &schema.Resource{
Description: `
Manages Grafana API Keys.
Expand Down
Loading

0 comments on commit bd938ad

Please sign in to comment.