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

Added ability to specify org_id field in resource definitions #60

Closed
wants to merge 10 commits into from
Closed
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module github.com/terraform-providers/terraform-provider-grafana

require (
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/emerald-squad/go-grafana-api v0.0.0-20190510131408-a95b9575e28e
Copy link
Contributor

@tonglil tonglil Jun 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please open a PR of the required changes to support orgs to the upstream library?

We will review the changes necessary there and then come back to the changes here.

github.com/gobs/pretty v0.0.0-20180724170744-09732c25a95b // indirect
github.com/hashicorp/terraform v0.12.0-alpha4.0.20190114221020-778cf445d42f
github.com/nytm/go-grafana-api v0.0.0-20181022152830-9cc848df7385
github.com/stretchr/testify v1.3.0 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ github.com/dnaeon/go-vcr v0.0.0-20180920040454-5637cf3d8a31/go.mod h1:aBB1+wY4s9
github.com/dylanmei/iso8601 v0.1.0 h1:812NGQDBcqquTfH5Yeo7lwR0nzx/cKdsmf3qMjPURUI=
github.com/dylanmei/iso8601 v0.1.0/go.mod h1:w9KhXSgIyROl1DefbMYIE7UVSIvELTbMrCfx+QkYnoQ=
github.com/dylanmei/winrmtest v0.0.0-20170819153634-c2fbb09e6c08/go.mod h1:VBVDFSBXCIW8JaHQpI8lldSKfYaLMzP9oyq6IJ4fhzY=
github.com/emerald-squad/go-grafana-api v0.0.0-20190510131408-a95b9575e28e h1:hIIFjL473C7rc2SITzqj49tHY1dcF7Y8/pnplzE6RT4=
github.com/emerald-squad/go-grafana-api v0.0.0-20190510131408-a95b9575e28e/go.mod h1:dF0RURrR+R5KH6aQqWzSZKSs1IqZGcgy53GQbntgZ64=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-test/deep v1.0.1 h1:UQhStjbkDClarlmv0am7OXXO4/GaPdCGiUiMTvi28sg=
Expand Down Expand Up @@ -225,8 +227,6 @@ github.com/mitchellh/reflectwalk v0.0.0-20170726202117-63d60e9d0dbc h1:gqYjvctjt
github.com/mitchellh/reflectwalk v0.0.0-20170726202117-63d60e9d0dbc/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
github.com/nytm/go-grafana-api v0.0.0-20181022152830-9cc848df7385 h1:dIMQoUk3IOBD2VJITGU6xIBUAvGhaV1bw4l4SCT6594=
github.com/nytm/go-grafana-api v0.0.0-20181022152830-9cc848df7385/go.mod h1:yDXF+7EYjZU+7CzJmeIxEblNC6lWy8KQhkMS5LYvjbA=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down
2 changes: 1 addition & 1 deletion grafana/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"

gapi "github.com/nytm/go-grafana-api"
gapi "github.com/emerald-squad/go-grafana-api"
)

func Provider() terraform.ResourceProvider {
Expand Down
21 changes: 16 additions & 5 deletions grafana/resource_alert_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"log"
"strconv"

gapi "github.com/emerald-squad/go-grafana-api"
"github.com/hashicorp/terraform/helper/schema"
gapi "github.com/nytm/go-grafana-api"
)

func ResourceAlertNotification() *schema.Resource {
Expand Down Expand Up @@ -38,6 +38,11 @@ func ResourceAlertNotification() *schema.Resource {
Optional: true,
Sensitive: true,
},

"org_id": {
Type: schema.TypeInt,
Required: true,
},
},
}
}
Expand All @@ -50,7 +55,9 @@ func CreateAlertNotification(d *schema.ResourceData, meta interface{}) error {
return err
}

id, err := client.NewAlertNotification(alertNotification)
orgID := int64(d.Get("org_id").(int))

id, err := client.NewAlertNotification(alertNotification, orgID)
if err != nil {
return err
}
Expand All @@ -68,7 +75,9 @@ func UpdateAlertNotification(d *schema.ResourceData, meta interface{}) error {
return err
}

return client.UpdateAlertNotification(alertNotification)
orgID := int64(d.Get("org_id").(int))

return client.UpdateAlertNotification(alertNotification, orgID)
}

func ReadAlertNotification(d *schema.ResourceData, meta interface{}) error {
Expand All @@ -80,7 +89,8 @@ func ReadAlertNotification(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Invalid id: %#v", idStr)
}

alertNotification, err := client.AlertNotification(id)
orgID := int64(d.Get("org_id").(int))
alertNotification, err := client.AlertNotification(id, orgID)
if err != nil {
if err.Error() == "404 Not Found" {
log.Printf("[WARN] removing datasource %s from state because it no longer exists in grafana", d.Get("name").(string))
Expand Down Expand Up @@ -119,8 +129,9 @@ func DeleteAlertNotification(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return fmt.Errorf("Invalid id: %#v", idStr)
}
orgID := int64(d.Get("org_id").(int))

return client.DeleteAlertNotification(id)
return client.DeleteAlertNotification(id, orgID)
}

func makeAlertNotification(d *schema.ResourceData) (*gapi.AlertNotification, error) {
Expand Down
17 changes: 12 additions & 5 deletions grafana/resource_alert_notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import (
"strconv"
"testing"

gapi "github.com/nytm/go-grafana-api"
gapi "github.com/emerald-squad/go-grafana-api"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAlertNotification_basic(t *testing.T) {
var alertNotification gapi.AlertNotification
var testOrgID int64 = 1

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccAlertNotificationCheckDestroy(&alertNotification),
CheckDestroy: testAccAlertNotificationCheckDestroy(&alertNotification, testOrgID),
Steps: []resource.TestStep{
{
Config: testAccAlertNotificationConfig_basic,
Expand Down Expand Up @@ -55,8 +56,13 @@ func testAccAlertNotificationCheckExists(rn string, a *gapi.AlertNotification) r
return fmt.Errorf("resource id is malformed")
}

orgID, err := strconv.ParseInt(rs.Primary.Attributes["org_id"], 10, 64)
if err != nil {
return fmt.Errorf("could not find org_id")
}

client := testAccProvider.Meta().(*gapi.Client)
gotAlertNotification, err := client.AlertNotification(id)
gotAlertNotification, err := client.AlertNotification(id, orgID)
if err != nil {
return fmt.Errorf("error getting data source: %s", err)
}
Expand All @@ -67,10 +73,10 @@ func testAccAlertNotificationCheckExists(rn string, a *gapi.AlertNotification) r
}
}

func testAccAlertNotificationCheckDestroy(a *gapi.AlertNotification) resource.TestCheckFunc {
func testAccAlertNotificationCheckDestroy(a *gapi.AlertNotification, orgID int64) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*gapi.Client)
alert, err := client.AlertNotification(a.Id)
alert, err := client.AlertNotification(a.Id, orgID)
if err == nil && alert != nil {
return fmt.Errorf("alert-notification still exists")
}
Expand All @@ -80,6 +86,7 @@ func testAccAlertNotificationCheckDestroy(a *gapi.AlertNotification) resource.Te

const testAccAlertNotificationConfig_basic = `
resource "grafana_alert_notification" "test" {
org_id = 1
type = "email"
name = "terraform-acc-test"
settings = {
Expand Down
19 changes: 13 additions & 6 deletions grafana/resource_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/hashicorp/terraform/helper/schema"

gapi "github.com/nytm/go-grafana-api"
gapi "github.com/emerald-squad/go-grafana-api"
)

func ResourceDashboard() *schema.Resource {
Expand Down Expand Up @@ -35,6 +35,12 @@ func ResourceDashboard() *schema.Resource {
StateFunc: NormalizeDashboardConfigJSON,
ValidateFunc: ValidateDashboardConfigJSON,
},

"org_id": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
},
}
}
Expand All @@ -47,8 +53,8 @@ func CreateDashboard(d *schema.ResourceData, meta interface{}) error {
dashboard.Model = prepareDashboardModel(d.Get("config_json").(string))

dashboard.Folder = int64(d.Get("folder").(int))

resp, err := client.NewDashboard(dashboard)
orgID := int64(d.Get("org_id").(int))
resp, err := client.NewDashboard(dashboard, orgID)
if err != nil {
return err
}
Expand All @@ -62,8 +68,8 @@ func ReadDashboard(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)

slug := d.Id()

dashboard, err := client.Dashboard(slug)
orgID := int64(d.Get("org_id").(int))
dashboard, err := client.Dashboard(slug, orgID)
if err != nil {
if err.Error() == "404 Not Found" {
log.Printf("[WARN] removing dashboard %s from state because it no longer exists in grafana", slug)
Expand Down Expand Up @@ -93,7 +99,8 @@ func DeleteDashboard(d *schema.ResourceData, meta interface{}) error {
client := meta.(*gapi.Client)

slug := d.Id()
return client.DeleteDashboard(slug)
orgID := int64(d.Get("org_id").(int))
return client.DeleteDashboard(slug, orgID)
}

func prepareDashboardModel(configJSON string) map[string]interface{} {
Expand Down
39 changes: 26 additions & 13 deletions grafana/resource_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ package grafana
import (
"fmt"
"regexp"
"strconv"
"testing"

gapi "github.com/nytm/go-grafana-api"
gapi "github.com/emerald-squad/go-grafana-api"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccDashboard_basic(t *testing.T) {
var dashboard gapi.Dashboard
var testOrgID int64 = 1

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccDashboardCheckDestroy(&dashboard),
CheckDestroy: testAccDashboardCheckDestroy(&dashboard, testOrgID),
Steps: []resource.TestStep{
{
Config: testAccDashboardConfig_basic,
Expand All @@ -35,11 +37,12 @@ func TestAccDashboard_basic(t *testing.T) {
func TestAccDashboard_folder(t *testing.T) {
var dashboard gapi.Dashboard
var folder gapi.Folder
var testOrgID int64 = 1

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccDashboardFolderCheckDestroy(&dashboard, &folder),
CheckDestroy: testAccDashboardFolderCheckDestroy(&dashboard, &folder, testOrgID),
Steps: []resource.TestStep{
{
Config: testAccDashboardConfig_folder,
Expand All @@ -61,17 +64,18 @@ func TestAccDashboard_folder(t *testing.T) {

func TestAccDashboard_disappear(t *testing.T) {
var dashboard gapi.Dashboard
var testOrgID int64 = 1

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccDashboardCheckDestroy(&dashboard),
CheckDestroy: testAccDashboardCheckDestroy(&dashboard, testOrgID),
Steps: []resource.TestStep{
{
Config: testAccDashboardConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccDashboardCheckExists("grafana_dashboard.test", &dashboard),
testAccDashboardDisappear(&dashboard),
testAccDashboardDisappear(&dashboard, testOrgID),
),
ExpectNonEmptyPlan: true,
},
Expand All @@ -90,8 +94,14 @@ func testAccDashboardCheckExists(rn string, dashboard *gapi.Dashboard) resource.
return fmt.Errorf("resource id not set")
}

orgID, err := strconv.ParseInt(rs.Primary.Attributes["org_id"], 10, 64)
if err != nil {
return fmt.Errorf("could not find org_id")
}

client := testAccProvider.Meta().(*gapi.Client)
gotDashboard, err := client.Dashboard(rs.Primary.ID)

gotDashboard, err := client.Dashboard(rs.Primary.ID, orgID)
if err != nil {
return fmt.Errorf("error getting dashboard: %s", err)
}
Expand All @@ -111,35 +121,35 @@ func testAccDashboardCheckExistsInFolder(dashboard *gapi.Dashboard, folder *gapi
}
}

func testAccDashboardDisappear(dashboard *gapi.Dashboard) resource.TestCheckFunc {
func testAccDashboardDisappear(dashboard *gapi.Dashboard, orgID int64) resource.TestCheckFunc {
return func(s *terraform.State) error {
// At this point testAccDashboardCheckExists should have been called and
// dashboard should have been populated
client := testAccProvider.Meta().(*gapi.Client)
client.DeleteDashboard((*dashboard).Meta.Slug)
client.DeleteDashboard((*dashboard).Meta.Slug, orgID)
return nil
}
}

func testAccDashboardCheckDestroy(dashboard *gapi.Dashboard) resource.TestCheckFunc {
func testAccDashboardCheckDestroy(dashboard *gapi.Dashboard, orgID int64) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*gapi.Client)
_, err := client.Dashboard(dashboard.Meta.Slug)
_, err := client.Dashboard(dashboard.Meta.Slug, orgID)
if err == nil {
return fmt.Errorf("dashboard still exists")
}
return nil
}
}

func testAccDashboardFolderCheckDestroy(dashboard *gapi.Dashboard, folder *gapi.Folder) resource.TestCheckFunc {
func testAccDashboardFolderCheckDestroy(dashboard *gapi.Dashboard, folder *gapi.Folder, orgID int64) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*gapi.Client)
_, err := client.Dashboard(dashboard.Meta.Slug)
_, err := client.Dashboard(dashboard.Meta.Slug, orgID)
if err == nil {
return fmt.Errorf("dashboard still exists")
}
_, err = client.Folder(folder.Id)
_, err = client.Folder(folder.Id, orgID)
if err == nil {
return fmt.Errorf("folder still exists")
}
Expand All @@ -153,6 +163,7 @@ func testAccDashboardFolderCheckDestroy(dashboard *gapi.Dashboard, folder *gapi.
// existing dashboard.
const testAccDashboardConfig_basic = `
resource "grafana_dashboard" "test" {
org_id = 1
config_json = <<EOT
{
"title": "Terraform Acceptance Test",
Expand All @@ -166,10 +177,12 @@ EOT
const testAccDashboardConfig_folder = `

resource "grafana_folder" "test_folder" {
org_id = 1
title = "Terraform Dashboard Folder Acceptance Test"
}

resource "grafana_dashboard" "test_folder" {
org_id = 1
folder = "${grafana_folder.test_folder.id}"
config_json = <<EOT
{
Expand Down
Loading