diff --git a/go.mod b/go.mod index e9e73bdaf..ee6165d39 100644 --- a/go.mod +++ b/go.mod @@ -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 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 ) diff --git a/go.sum b/go.sum index b70b9cbf5..1c1822f0f 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/grafana/provider.go b/grafana/provider.go index 66efbdb3f..605eed5e3 100644 --- a/grafana/provider.go +++ b/grafana/provider.go @@ -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 { diff --git a/grafana/resource_alert_notification.go b/grafana/resource_alert_notification.go index efbabd89a..9df95dc99 100644 --- a/grafana/resource_alert_notification.go +++ b/grafana/resource_alert_notification.go @@ -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 { @@ -38,6 +38,11 @@ func ResourceAlertNotification() *schema.Resource { Optional: true, Sensitive: true, }, + + "org_id": { + Type: schema.TypeInt, + Required: true, + }, }, } } @@ -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 } @@ -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 { @@ -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)) @@ -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) { diff --git a/grafana/resource_alert_notification_test.go b/grafana/resource_alert_notification_test.go index 8af373a4b..eb3d21b79 100644 --- a/grafana/resource_alert_notification_test.go +++ b/grafana/resource_alert_notification_test.go @@ -6,7 +6,7 @@ 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" @@ -14,11 +14,12 @@ import ( 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, @@ -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) } @@ -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") } @@ -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 = { diff --git a/grafana/resource_dashboard.go b/grafana/resource_dashboard.go index 21c194762..b9504c7b9 100644 --- a/grafana/resource_dashboard.go +++ b/grafana/resource_dashboard.go @@ -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 { @@ -35,6 +35,12 @@ func ResourceDashboard() *schema.Resource { StateFunc: NormalizeDashboardConfigJSON, ValidateFunc: ValidateDashboardConfigJSON, }, + + "org_id": { + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, }, } } @@ -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 } @@ -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) @@ -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{} { diff --git a/grafana/resource_dashboard_test.go b/grafana/resource_dashboard_test.go index 902f71155..68a0dab7a 100644 --- a/grafana/resource_dashboard_test.go +++ b/grafana/resource_dashboard_test.go @@ -3,9 +3,10 @@ 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" @@ -13,11 +14,12 @@ import ( 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, @@ -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, @@ -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, }, @@ -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) } @@ -111,20 +121,20 @@ 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") } @@ -132,14 +142,14 @@ func testAccDashboardCheckDestroy(dashboard *gapi.Dashboard) resource.TestCheckF } } -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") } @@ -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 = <