From 88c164350fe6993a9b56d439f5dddbfceedb1c2a Mon Sep 17 00:00:00 2001 From: William Campoli Date: Tue, 12 Feb 2019 17:26:55 -0500 Subject: [PATCH 1/8] field to specify org for datasource --- grafana/provider.go | 2 +- grafana/resource_alert_notification.go | 2 +- grafana/resource_alert_notification_test.go | 2 +- grafana/resource_dashboard.go | 2 +- grafana/resource_dashboard_test.go | 2 +- grafana/resource_data_source.go | 30 ++++++++++++++------- grafana/resource_data_source_test.go | 2 +- grafana/resource_folder.go | 2 +- grafana/resource_folder_test.go | 2 +- grafana/resource_organization.go | 2 +- grafana/resource_organization_test.go | 2 +- 11 files changed, 31 insertions(+), 19 deletions(-) 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..6c167fb99 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 { diff --git a/grafana/resource_alert_notification_test.go b/grafana/resource_alert_notification_test.go index 8af373a4b..87e000fc6 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" diff --git a/grafana/resource_dashboard.go b/grafana/resource_dashboard.go index 21c194762..79dc18084 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 { diff --git a/grafana/resource_dashboard_test.go b/grafana/resource_dashboard_test.go index 902f71155..ba68637ac 100644 --- a/grafana/resource_dashboard_test.go +++ b/grafana/resource_dashboard_test.go @@ -5,7 +5,7 @@ import ( "regexp" "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" diff --git a/grafana/resource_data_source.go b/grafana/resource_data_source.go index 9ce84e2c4..d02a223da 100644 --- a/grafana/resource_data_source.go +++ b/grafana/resource_data_source.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 ResourceDataSource() *schema.Resource { @@ -125,6 +125,11 @@ func ResourceDataSource() *schema.Resource { Optional: true, Default: "proxy", }, + + "org_id": { + Type: schema.TypeInt, + Required: true, + }, }, } } @@ -133,6 +138,12 @@ func ResourceDataSource() *schema.Resource { func CreateDataSource(d *schema.ResourceData, meta interface{}) error { client := meta.(*gapi.Client) + //switch org context + err := client.SwitchCurrentUserOrg(d.Get("org_id").(int64)) + if err != nil { + return err + } + dataSource, err := makeDataSource(d) if err != nil { return err @@ -219,14 +230,15 @@ func makeDataSource(d *schema.ResourceData) (*gapi.DataSource, error) { } return &gapi.DataSource{ - Id: id, - Name: d.Get("name").(string), - Type: d.Get("type").(string), - URL: d.Get("url").(string), - Access: d.Get("access_mode").(string), - Database: d.Get("database_name").(string), - User: d.Get("username").(string), - Password: d.Get("password").(string), + Id: id, + Name: d.Get("name").(string), + Type: d.Get("type").(string), + URL: d.Get("url").(string), + Access: d.Get("access_mode").(string), + Database: d.Get("database_name").(string), + User: d.Get("username").(string), + Password: d.Get("password").(string), + IsDefault: d.Get("is_default").(bool), BasicAuth: d.Get("basic_auth_enabled").(bool), BasicAuthUser: d.Get("basic_auth_username").(string), diff --git a/grafana/resource_data_source_test.go b/grafana/resource_data_source_test.go index 54a45df7f..eddefff4c 100644 --- a/grafana/resource_data_source_test.go +++ b/grafana/resource_data_source_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" diff --git a/grafana/resource_folder.go b/grafana/resource_folder.go index 9a9ec73ce..a7a6cb97c 100644 --- a/grafana/resource_folder.go +++ b/grafana/resource_folder.go @@ -8,7 +8,7 @@ import ( "github.com/hashicorp/terraform/helper/schema" - gapi "github.com/nytm/go-grafana-api" + gapi "github.com/emerald-squad/go-grafana-api" ) func ResourceFolder() *schema.Resource { diff --git a/grafana/resource_folder_test.go b/grafana/resource_folder_test.go index eda1c583d..e2ca4cb00 100644 --- a/grafana/resource_folder_test.go +++ b/grafana/resource_folder_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" diff --git a/grafana/resource_organization.go b/grafana/resource_organization.go index 3f399e1c7..46370549a 100644 --- a/grafana/resource_organization.go +++ b/grafana/resource_organization.go @@ -8,8 +8,8 @@ import ( "strconv" "strings" + gapi "github.com/emerald-squad/go-grafana-api" "github.com/hashicorp/terraform/helper/schema" - gapi "github.com/nytm/go-grafana-api" ) type OrgUser struct { diff --git a/grafana/resource_organization_test.go b/grafana/resource_organization_test.go index 785992001..47dffd32c 100644 --- a/grafana/resource_organization_test.go +++ b/grafana/resource_organization_test.go @@ -6,9 +6,9 @@ import ( "strconv" "testing" + gapi "github.com/emerald-squad/go-grafana-api" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" - gapi "github.com/nytm/go-grafana-api" ) func TestAccOrganization_basic(t *testing.T) { From 942d86a2aff0f0b78b0198081ce4fa8bc8ba786e Mon Sep 17 00:00:00 2001 From: William Campoli Date: Wed, 27 Feb 2019 10:16:55 -0500 Subject: [PATCH 2/8] modified resource data source --- grafana/resource_data_source.go | 19 +++++++------------ grafana/resource_data_source_test.go | 2 ++ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/grafana/resource_data_source.go b/grafana/resource_data_source.go index d02a223da..2001e5141 100644 --- a/grafana/resource_data_source.go +++ b/grafana/resource_data_source.go @@ -138,18 +138,12 @@ func ResourceDataSource() *schema.Resource { func CreateDataSource(d *schema.ResourceData, meta interface{}) error { client := meta.(*gapi.Client) - //switch org context - err := client.SwitchCurrentUserOrg(d.Get("org_id").(int64)) - if err != nil { - return err - } - dataSource, err := makeDataSource(d) if err != nil { return err } - id, err := client.NewDataSource(dataSource) + id, err := client.NewDataSource(dataSource, int64(d.Get("org_id").(int))) if err != nil { return err } @@ -167,13 +161,14 @@ func UpdateDataSource(d *schema.ResourceData, meta interface{}) error { if err != nil { return err } - - return client.UpdateDataSource(dataSource) + orgId := int64(d.Get("org_id").(int)) + return client.UpdateDataSource(dataSource, orgId) } // ReadDataSource reads a Grafana datasource func ReadDataSource(d *schema.ResourceData, meta interface{}) error { client := meta.(*gapi.Client) + orgId := int64(d.Get("org_id").(int)) idStr := d.Id() id, err := strconv.ParseInt(idStr, 10, 64) @@ -186,7 +181,7 @@ func ReadDataSource(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("Invalid id: %#v", idStr) } - dataSource, err := client.DataSource(id) + dataSource, err := client.DataSource(id, orgId) if err != nil { return err @@ -211,14 +206,14 @@ func ReadDataSource(d *schema.ResourceData, meta interface{}) error { // DeleteDataSource deletes a Grafana datasource func DeleteDataSource(d *schema.ResourceData, meta interface{}) error { client := meta.(*gapi.Client) - + orgId := int64(d.Get("org_id").(int)) idStr := d.Id() id, err := strconv.ParseInt(idStr, 10, 64) if err != nil { return fmt.Errorf("Invalid id: %#v", idStr) } - return client.DeleteDataSource(id) + return client.DeleteDataSource(id, orgId) } func makeDataSource(d *schema.ResourceData) (*gapi.DataSource, error) { diff --git a/grafana/resource_data_source_test.go b/grafana/resource_data_source_test.go index eddefff4c..21e265b2a 100644 --- a/grafana/resource_data_source_test.go +++ b/grafana/resource_data_source_test.go @@ -126,12 +126,14 @@ resource "grafana_data_source" "test_influxdb" { username = "terraform_user" password = "terraform_password" basic_auth_password = "basic_password" + org_id = "7" } ` const testAccDataSourceConfig_basicCloudwatch = ` resource "grafana_data_source" "test_cloudwatch" { type = "cloudwatch" name = "terraform-acc-test-cloudwatch" + org_id = "7" json_data { default_region = "us-east-1" From cee1e79b1c0de1975f2836bdca53944e59cf50fe Mon Sep 17 00:00:00 2001 From: William Campoli Date: Thu, 9 May 2019 15:48:07 -0400 Subject: [PATCH 3/8] added org_id field --- grafana/resource_dashboard.go | 11 ++++++----- grafana/resource_folder.go | 12 +++++++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/grafana/resource_dashboard.go b/grafana/resource_dashboard.go index 79dc18084..45fc2d004 100644 --- a/grafana/resource_dashboard.go +++ b/grafana/resource_dashboard.go @@ -47,8 +47,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 +62,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 +93,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_folder.go b/grafana/resource_folder.go index a7a6cb97c..87bda9831 100644 --- a/grafana/resource_folder.go +++ b/grafana/resource_folder.go @@ -28,6 +28,12 @@ func ResourceFolder() *schema.Resource { Required: true, ForceNew: true, }, + + "org_id": { + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, }, } } @@ -37,7 +43,7 @@ func CreateFolder(d *schema.ResourceData, meta interface{}) error { model := d.Get("title").(string) - resp, err := client.NewFolder(model) + resp, err := client.NewFolder(model, int64(d.Get("org_id").(int))) if err != nil { return err } @@ -58,7 +64,7 @@ func ReadFolder(d *schema.ResourceData, meta interface{}) error { return err } - folder, err := client.Folder(id) + folder, err := client.Folder(id, int64(d.Get("org_id").(int))) if err != nil { if err.Error() == "404 Not Found" { log.Printf("[WARN] removing folder %d from state because it no longer exists in grafana", id) @@ -78,7 +84,7 @@ func ReadFolder(d *schema.ResourceData, meta interface{}) error { func DeleteFolder(d *schema.ResourceData, meta interface{}) error { client := meta.(*gapi.Client) - return client.DeleteFolder(d.Get("uid").(string)) + return client.DeleteFolder(d.Get("uid").(string), int64(d.Get("org_id").(int))) } func prepareFolderModel(configJSON string) map[string]interface{} { From f83420b43a9b9a1e57f5663bef6aedb935efcdd1 Mon Sep 17 00:00:00 2001 From: William Campoli Date: Fri, 10 May 2019 14:56:14 -0400 Subject: [PATCH 4/8] added base org to tests --- grafana/resource_alert_notification.go | 19 +++++++++++++++---- grafana/resource_alert_notification_test.go | 4 ++-- grafana/resource_dashboard_test.go | 10 +++++----- grafana/resource_data_source.go | 1 + grafana/resource_data_source_test.go | 4 ++-- grafana/resource_folder_test.go | 6 +++--- 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/grafana/resource_alert_notification.go b/grafana/resource_alert_notification.go index 6c167fb99..9df95dc99 100644 --- a/grafana/resource_alert_notification.go +++ b/grafana/resource_alert_notification.go @@ -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 87e000fc6..132c76492 100644 --- a/grafana/resource_alert_notification_test.go +++ b/grafana/resource_alert_notification_test.go @@ -56,7 +56,7 @@ func testAccAlertNotificationCheckExists(rn string, a *gapi.AlertNotification) r } client := testAccProvider.Meta().(*gapi.Client) - gotAlertNotification, err := client.AlertNotification(id) + gotAlertNotification, err := client.AlertNotification(id, 1) if err != nil { return fmt.Errorf("error getting data source: %s", err) } @@ -70,7 +70,7 @@ func testAccAlertNotificationCheckExists(rn string, a *gapi.AlertNotification) r func testAccAlertNotificationCheckDestroy(a *gapi.AlertNotification) 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, 1) if err == nil && alert != nil { return fmt.Errorf("alert-notification still exists") } diff --git a/grafana/resource_dashboard_test.go b/grafana/resource_dashboard_test.go index ba68637ac..42456ad6b 100644 --- a/grafana/resource_dashboard_test.go +++ b/grafana/resource_dashboard_test.go @@ -91,7 +91,7 @@ func testAccDashboardCheckExists(rn string, dashboard *gapi.Dashboard) resource. } client := testAccProvider.Meta().(*gapi.Client) - gotDashboard, err := client.Dashboard(rs.Primary.ID) + gotDashboard, err := client.Dashboard(rs.Primary.ID, 1) if err != nil { return fmt.Errorf("error getting dashboard: %s", err) } @@ -116,7 +116,7 @@ func testAccDashboardDisappear(dashboard *gapi.Dashboard) resource.TestCheckFunc // 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, 1) return nil } } @@ -124,7 +124,7 @@ func testAccDashboardDisappear(dashboard *gapi.Dashboard) resource.TestCheckFunc func testAccDashboardCheckDestroy(dashboard *gapi.Dashboard) 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, 1) if err == nil { return fmt.Errorf("dashboard still exists") } @@ -135,11 +135,11 @@ func testAccDashboardCheckDestroy(dashboard *gapi.Dashboard) resource.TestCheckF func testAccDashboardFolderCheckDestroy(dashboard *gapi.Dashboard, folder *gapi.Folder) 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, 1) if err == nil { return fmt.Errorf("dashboard still exists") } - _, err = client.Folder(folder.Id) + _, err = client.Folder(folder.Id, 1) if err == nil { return fmt.Errorf("folder still exists") } diff --git a/grafana/resource_data_source.go b/grafana/resource_data_source.go index 2001e5141..07c123951 100644 --- a/grafana/resource_data_source.go +++ b/grafana/resource_data_source.go @@ -129,6 +129,7 @@ func ResourceDataSource() *schema.Resource { "org_id": { Type: schema.TypeInt, Required: true, + ForceNew: true, }, }, } diff --git a/grafana/resource_data_source_test.go b/grafana/resource_data_source_test.go index 21e265b2a..6574d08d6 100644 --- a/grafana/resource_data_source_test.go +++ b/grafana/resource_data_source_test.go @@ -95,7 +95,7 @@ func testAccDataSourceCheckExists(rn string, dataSource *gapi.DataSource) resour } client := testAccProvider.Meta().(*gapi.Client) - gotDataSource, err := client.DataSource(id) + gotDataSource, err := client.DataSource(id, 1) if err != nil { return fmt.Errorf("error getting data source: %s", err) } @@ -109,7 +109,7 @@ func testAccDataSourceCheckExists(rn string, dataSource *gapi.DataSource) resour func testAccDataSourceCheckDestroy(dataSource *gapi.DataSource) resource.TestCheckFunc { return func(s *terraform.State) error { client := testAccProvider.Meta().(*gapi.Client) - _, err := client.DataSource(dataSource.Id) + _, err := client.DataSource(dataSource.Id, 1) if err == nil { return fmt.Errorf("data source still exists") } diff --git a/grafana/resource_folder_test.go b/grafana/resource_folder_test.go index e2ca4cb00..9110f9364 100644 --- a/grafana/resource_folder_test.go +++ b/grafana/resource_folder_test.go @@ -56,7 +56,7 @@ func testAccFolderCheckExists(rn string, folder *gapi.Folder) resource.TestCheck if id == 0 { return fmt.Errorf("got a folder id of 0") } - gotFolder, err := client.Folder(id) + gotFolder, err := client.Folder(id, 1) if err != nil { return fmt.Errorf("error getting folder: %s", err) } @@ -72,14 +72,14 @@ func testAccFolderDisappear(folder *gapi.Folder) resource.TestCheckFunc { // At this point testAccFolderCheckExists should have been called and // folder should have been populated client := testAccProvider.Meta().(*gapi.Client) - return client.DeleteFolder((*folder).Uid) + return client.DeleteFolder((*folder).Uid, 1) } } func testAccFolderCheckDestroy(folder *gapi.Folder) resource.TestCheckFunc { return func(s *terraform.State) error { client := testAccProvider.Meta().(*gapi.Client) - _, err := client.Folder(folder.Id) + _, err := client.Folder(folder.Id, 1) if err == nil { return fmt.Errorf("folder still exists") } From 3c8dfbd5da28338de7f62b5add60270bec058c1c Mon Sep 17 00:00:00 2001 From: William Campoli Date: Sat, 11 May 2019 20:10:49 -0400 Subject: [PATCH 5/8] changed acceptance tests to add org_id field --- grafana/resource_alert_notification_test.go | 1 + grafana/resource_dashboard.go | 6 ++++++ grafana/resource_dashboard_test.go | 3 +++ grafana/resource_data_source_test.go | 4 ++-- grafana/resource_folder_test.go | 1 + 5 files changed, 13 insertions(+), 2 deletions(-) diff --git a/grafana/resource_alert_notification_test.go b/grafana/resource_alert_notification_test.go index 132c76492..92348e1f6 100644 --- a/grafana/resource_alert_notification_test.go +++ b/grafana/resource_alert_notification_test.go @@ -80,6 +80,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 45fc2d004..b9504c7b9 100644 --- a/grafana/resource_dashboard.go +++ b/grafana/resource_dashboard.go @@ -35,6 +35,12 @@ func ResourceDashboard() *schema.Resource { StateFunc: NormalizeDashboardConfigJSON, ValidateFunc: ValidateDashboardConfigJSON, }, + + "org_id": { + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, }, } } diff --git a/grafana/resource_dashboard_test.go b/grafana/resource_dashboard_test.go index 42456ad6b..35ac31b01 100644 --- a/grafana/resource_dashboard_test.go +++ b/grafana/resource_dashboard_test.go @@ -153,6 +153,7 @@ func testAccDashboardFolderCheckDestroy(dashboard *gapi.Dashboard, folder *gapi. // existing dashboard. const testAccDashboardConfig_basic = ` resource "grafana_dashboard" "test" { + org_id = 1 config_json = < Date: Sat, 11 May 2019 21:33:24 -0400 Subject: [PATCH 6/8] fixed tests --- grafana/resource_alert_notification_test.go | 14 ++++++--- grafana/resource_dashboard_test.go | 34 +++++++++++++-------- grafana/resource_data_source_test.go | 17 ++++++++--- grafana/resource_folder_test.go | 19 ++++++++---- 4 files changed, 57 insertions(+), 27 deletions(-) diff --git a/grafana/resource_alert_notification_test.go b/grafana/resource_alert_notification_test.go index 92348e1f6..eb3d21b79 100644 --- a/grafana/resource_alert_notification_test.go +++ b/grafana/resource_alert_notification_test.go @@ -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, 1) + 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, 1) + alert, err := client.AlertNotification(a.Id, orgID) if err == nil && alert != nil { return fmt.Errorf("alert-notification still exists") } diff --git a/grafana/resource_dashboard_test.go b/grafana/resource_dashboard_test.go index 35ac31b01..68a0dab7a 100644 --- a/grafana/resource_dashboard_test.go +++ b/grafana/resource_dashboard_test.go @@ -3,6 +3,7 @@ package grafana import ( "fmt" "regexp" + "strconv" "testing" gapi "github.com/emerald-squad/go-grafana-api" @@ -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, 1) + + 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, 1) + 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, 1) + _, 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, 1) + _, err := client.Dashboard(dashboard.Meta.Slug, orgID) if err == nil { return fmt.Errorf("dashboard still exists") } - _, err = client.Folder(folder.Id, 1) + _, err = client.Folder(folder.Id, orgID) if err == nil { return fmt.Errorf("folder still exists") } diff --git a/grafana/resource_data_source_test.go b/grafana/resource_data_source_test.go index 8b1277a85..d947e1ef8 100644 --- a/grafana/resource_data_source_test.go +++ b/grafana/resource_data_source_test.go @@ -14,11 +14,12 @@ import ( func TestAccDataSource_basic(t *testing.T) { var dataSource gapi.DataSource + var testOrgID int64 = 1 resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testAccDataSourceCheckDestroy(&dataSource), + CheckDestroy: testAccDataSourceCheckDestroy(&dataSource, testOrgID), Steps: []resource.TestStep{ { Config: testAccDataSourceConfig_basic, @@ -44,11 +45,12 @@ func TestAccDataSource_basic(t *testing.T) { func TestAccDataSource_basicCloudwatch(t *testing.T) { var dataSource gapi.DataSource + var testOrgID int64 = 1 resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testAccDataSourceCheckDestroy(&dataSource), + CheckDestroy: testAccDataSourceCheckDestroy(&dataSource, testOrgID), Steps: []resource.TestStep{ { Config: testAccDataSourceConfig_basicCloudwatch, @@ -94,8 +96,13 @@ func testAccDataSourceCheckExists(rn string, dataSource *gapi.DataSource) resour 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) - gotDataSource, err := client.DataSource(id, 1) + gotDataSource, err := client.DataSource(id, orgID) if err != nil { return fmt.Errorf("error getting data source: %s", err) } @@ -106,10 +113,10 @@ func testAccDataSourceCheckExists(rn string, dataSource *gapi.DataSource) resour } } -func testAccDataSourceCheckDestroy(dataSource *gapi.DataSource) resource.TestCheckFunc { +func testAccDataSourceCheckDestroy(dataSource *gapi.DataSource, orgID int64) resource.TestCheckFunc { return func(s *terraform.State) error { client := testAccProvider.Meta().(*gapi.Client) - _, err := client.DataSource(dataSource.Id, 1) + _, err := client.DataSource(dataSource.Id, orgID) if err == nil { return fmt.Errorf("data source still exists") } diff --git a/grafana/resource_folder_test.go b/grafana/resource_folder_test.go index 4ecefbf30..e5dedbee1 100644 --- a/grafana/resource_folder_test.go +++ b/grafana/resource_folder_test.go @@ -14,11 +14,12 @@ import ( func TestAccFolder_basic(t *testing.T) { var folder gapi.Folder + var testOrgID int64 = 1 resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testAccFolderCheckDestroy(&folder), + CheckDestroy: testAccFolderCheckDestroy(&folder, testOrgID), Steps: []resource.TestStep{ { Config: testAccFolderConfig_basic, @@ -56,7 +57,13 @@ func testAccFolderCheckExists(rn string, folder *gapi.Folder) resource.TestCheck if id == 0 { return fmt.Errorf("got a folder id of 0") } - gotFolder, err := client.Folder(id, 1) + + orgID, err := strconv.ParseInt(rs.Primary.Attributes["org_id"], 10, 64) + if err != nil { + return fmt.Errorf("could not find org_id") + } + + gotFolder, err := client.Folder(id, orgID) if err != nil { return fmt.Errorf("error getting folder: %s", err) } @@ -67,19 +74,19 @@ func testAccFolderCheckExists(rn string, folder *gapi.Folder) resource.TestCheck } } -func testAccFolderDisappear(folder *gapi.Folder) resource.TestCheckFunc { +func testAccFolderDisappear(folder *gapi.Folder, orgID int64) resource.TestCheckFunc { return func(s *terraform.State) error { // At this point testAccFolderCheckExists should have been called and // folder should have been populated client := testAccProvider.Meta().(*gapi.Client) - return client.DeleteFolder((*folder).Uid, 1) + return client.DeleteFolder((*folder).Uid, orgID) } } -func testAccFolderCheckDestroy(folder *gapi.Folder) resource.TestCheckFunc { +func testAccFolderCheckDestroy(folder *gapi.Folder, orgID int64) resource.TestCheckFunc { return func(s *terraform.State) error { client := testAccProvider.Meta().(*gapi.Client) - _, err := client.Folder(folder.Id, 1) + _, err := client.Folder(folder.Id, orgID) if err == nil { return fmt.Errorf("folder still exists") } From caab692a94336d4870641cb1f15e9397e5e076e2 Mon Sep 17 00:00:00 2001 From: William Campoli Date: Sun, 12 May 2019 13:49:15 -0400 Subject: [PATCH 7/8] fixed dependencies --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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= From 4a9a1a666adb5b93cd92d9b89abfc93b425cc54d Mon Sep 17 00:00:00 2001 From: William Campoli Date: Sun, 12 May 2019 13:53:35 -0400 Subject: [PATCH 8/8] replaced lib --- .../go-grafana-api/.gitignore | 0 .../go-grafana-api/LICENSE | 0 .../go-grafana-api/README.md | 0 .../go-grafana-api/admin.go | 0 .../go-grafana-api/alertnotification.go | 17 +++++++++--- .../go-grafana-api/client.go | 0 .../go-grafana-api/dashboard.go | 13 +++++++--- .../go-grafana-api/datasource.go | 23 +++++++++++++--- .../go-grafana-api/folder.go | 26 +++++++++++++++---- .../go-grafana-api/org_users.go | 0 .../go-grafana-api/orgs.go | 0 .../go-grafana-api/user.go | 20 ++++++++++++++ vendor/modules.txt | 4 +-- 13 files changed, 85 insertions(+), 18 deletions(-) rename vendor/github.com/{nytm => emerald-squad}/go-grafana-api/.gitignore (100%) rename vendor/github.com/{nytm => emerald-squad}/go-grafana-api/LICENSE (100%) rename vendor/github.com/{nytm => emerald-squad}/go-grafana-api/README.md (100%) rename vendor/github.com/{nytm => emerald-squad}/go-grafana-api/admin.go (100%) rename vendor/github.com/{nytm => emerald-squad}/go-grafana-api/alertnotification.go (75%) rename vendor/github.com/{nytm => emerald-squad}/go-grafana-api/client.go (100%) rename vendor/github.com/{nytm => emerald-squad}/go-grafana-api/dashboard.go (86%) rename vendor/github.com/{nytm => emerald-squad}/go-grafana-api/datasource.go (83%) rename vendor/github.com/{nytm => emerald-squad}/go-grafana-api/folder.go (76%) rename vendor/github.com/{nytm => emerald-squad}/go-grafana-api/org_users.go (100%) rename vendor/github.com/{nytm => emerald-squad}/go-grafana-api/orgs.go (100%) rename vendor/github.com/{nytm => emerald-squad}/go-grafana-api/user.go (73%) diff --git a/vendor/github.com/nytm/go-grafana-api/.gitignore b/vendor/github.com/emerald-squad/go-grafana-api/.gitignore similarity index 100% rename from vendor/github.com/nytm/go-grafana-api/.gitignore rename to vendor/github.com/emerald-squad/go-grafana-api/.gitignore diff --git a/vendor/github.com/nytm/go-grafana-api/LICENSE b/vendor/github.com/emerald-squad/go-grafana-api/LICENSE similarity index 100% rename from vendor/github.com/nytm/go-grafana-api/LICENSE rename to vendor/github.com/emerald-squad/go-grafana-api/LICENSE diff --git a/vendor/github.com/nytm/go-grafana-api/README.md b/vendor/github.com/emerald-squad/go-grafana-api/README.md similarity index 100% rename from vendor/github.com/nytm/go-grafana-api/README.md rename to vendor/github.com/emerald-squad/go-grafana-api/README.md diff --git a/vendor/github.com/nytm/go-grafana-api/admin.go b/vendor/github.com/emerald-squad/go-grafana-api/admin.go similarity index 100% rename from vendor/github.com/nytm/go-grafana-api/admin.go rename to vendor/github.com/emerald-squad/go-grafana-api/admin.go diff --git a/vendor/github.com/nytm/go-grafana-api/alertnotification.go b/vendor/github.com/emerald-squad/go-grafana-api/alertnotification.go similarity index 75% rename from vendor/github.com/nytm/go-grafana-api/alertnotification.go rename to vendor/github.com/emerald-squad/go-grafana-api/alertnotification.go index bd99db9fa..1978da1fa 100644 --- a/vendor/github.com/nytm/go-grafana-api/alertnotification.go +++ b/vendor/github.com/emerald-squad/go-grafana-api/alertnotification.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io/ioutil" + "strconv" ) type AlertNotification struct { @@ -16,13 +17,15 @@ type AlertNotification struct { Settings interface{} `json:"settings"` } -func (c *Client) AlertNotification(id int64) (*AlertNotification, error) { +func (c *Client) AlertNotification(id int64, orgID int64) (*AlertNotification, error) { path := fmt.Sprintf("/api/alert-notifications/%d", id) req, err := c.newRequest("GET", path, nil, nil) if err != nil { return nil, err } + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return nil, err @@ -41,7 +44,7 @@ func (c *Client) AlertNotification(id int64) (*AlertNotification, error) { return result, err } -func (c *Client) NewAlertNotification(a *AlertNotification) (int64, error) { +func (c *Client) NewAlertNotification(a *AlertNotification, orgID int64) (int64, error) { data, err := json.Marshal(a) if err != nil { return 0, err @@ -51,6 +54,8 @@ func (c *Client) NewAlertNotification(a *AlertNotification) (int64, error) { return 0, err } + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return 0, err @@ -71,7 +76,7 @@ func (c *Client) NewAlertNotification(a *AlertNotification) (int64, error) { return result.Id, err } -func (c *Client) UpdateAlertNotification(a *AlertNotification) error { +func (c *Client) UpdateAlertNotification(a *AlertNotification, orgID int64) error { path := fmt.Sprintf("/api/alert-notifications/%d", a.Id) data, err := json.Marshal(a) if err != nil { @@ -82,6 +87,8 @@ func (c *Client) UpdateAlertNotification(a *AlertNotification) error { return err } + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return err @@ -93,13 +100,15 @@ func (c *Client) UpdateAlertNotification(a *AlertNotification) error { return nil } -func (c *Client) DeleteAlertNotification(id int64) error { +func (c *Client) DeleteAlertNotification(id int64, orgID int64) error { path := fmt.Sprintf("/api/alert-notifications/%d", id) req, err := c.newRequest("DELETE", path, nil, nil) if err != nil { return err } + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return err diff --git a/vendor/github.com/nytm/go-grafana-api/client.go b/vendor/github.com/emerald-squad/go-grafana-api/client.go similarity index 100% rename from vendor/github.com/nytm/go-grafana-api/client.go rename to vendor/github.com/emerald-squad/go-grafana-api/client.go diff --git a/vendor/github.com/nytm/go-grafana-api/dashboard.go b/vendor/github.com/emerald-squad/go-grafana-api/dashboard.go similarity index 86% rename from vendor/github.com/nytm/go-grafana-api/dashboard.go rename to vendor/github.com/emerald-squad/go-grafana-api/dashboard.go index 2d6418f15..1d5bf8a22 100644 --- a/vendor/github.com/nytm/go-grafana-api/dashboard.go +++ b/vendor/github.com/emerald-squad/go-grafana-api/dashboard.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "log" "os" + "strconv" ) type DashboardMeta struct { @@ -65,7 +66,7 @@ func (c *Client) SaveDashboard(model map[string]interface{}, overwrite bool) (*D return result, err } -func (c *Client) NewDashboard(dashboard Dashboard) (*DashboardSaveResponse, error) { +func (c *Client) NewDashboard(dashboard Dashboard, orgID int64) (*DashboardSaveResponse, error) { data, err := json.Marshal(dashboard) if err != nil { return nil, err @@ -75,6 +76,8 @@ func (c *Client) NewDashboard(dashboard Dashboard) (*DashboardSaveResponse, erro return nil, err } + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return nil, err @@ -93,13 +96,15 @@ func (c *Client) NewDashboard(dashboard Dashboard) (*DashboardSaveResponse, erro return result, err } -func (c *Client) Dashboard(slug string) (*Dashboard, error) { +func (c *Client) Dashboard(slug string, orgID int64) (*Dashboard, error) { path := fmt.Sprintf("/api/dashboards/db/%s", slug) req, err := c.newRequest("GET", path, nil, nil) if err != nil { return nil, err } + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return nil, err @@ -122,13 +127,15 @@ func (c *Client) Dashboard(slug string) (*Dashboard, error) { return result, err } -func (c *Client) DeleteDashboard(slug string) error { +func (c *Client) DeleteDashboard(slug string, orgID int64) error { path := fmt.Sprintf("/api/dashboards/db/%s", slug) req, err := c.newRequest("DELETE", path, nil, nil) if err != nil { return err } + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return err diff --git a/vendor/github.com/nytm/go-grafana-api/datasource.go b/vendor/github.com/emerald-squad/go-grafana-api/datasource.go similarity index 83% rename from vendor/github.com/nytm/go-grafana-api/datasource.go rename to vendor/github.com/emerald-squad/go-grafana-api/datasource.go index a5b40deac..2dd92b51b 100644 --- a/vendor/github.com/nytm/go-grafana-api/datasource.go +++ b/vendor/github.com/emerald-squad/go-grafana-api/datasource.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io/ioutil" + "strconv" ) type DataSource struct { @@ -45,16 +46,20 @@ type SecureJSONData struct { SecretKey string `json:"secretKey,omitempty"` } -func (c *Client) NewDataSource(s *DataSource) (int64, error) { +func (c *Client) NewDataSource(s *DataSource, orgID int64) (int64, error) { + data, err := json.Marshal(s) if err != nil { return 0, err } + req, err := c.newRequest("POST", "/api/datasources", nil, bytes.NewBuffer(data)) if err != nil { return 0, err } + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return 0, err @@ -75,7 +80,8 @@ func (c *Client) NewDataSource(s *DataSource) (int64, error) { return result.Id, err } -func (c *Client) UpdateDataSource(s *DataSource) error { +func (c *Client) UpdateDataSource(s *DataSource, orgID int64) error { + path := fmt.Sprintf("/api/datasources/%d", s.Id) data, err := json.Marshal(s) if err != nil { @@ -86,6 +92,8 @@ func (c *Client) UpdateDataSource(s *DataSource) error { return err } + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return err @@ -97,13 +105,17 @@ func (c *Client) UpdateDataSource(s *DataSource) error { return nil } -func (c *Client) DataSource(id int64) (*DataSource, error) { +func (c *Client) DataSource(id int64, orgID int64) (*DataSource, error) { + path := fmt.Sprintf("/api/datasources/%d", id) + req, err := c.newRequest("GET", path, nil, nil) if err != nil { return nil, err } + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return nil, err @@ -122,13 +134,16 @@ func (c *Client) DataSource(id int64) (*DataSource, error) { return result, err } -func (c *Client) DeleteDataSource(id int64) error { +func (c *Client) DeleteDataSource(id int64, orgID int64) error { + path := fmt.Sprintf("/api/datasources/%d", id) req, err := c.newRequest("DELETE", path, nil, nil) if err != nil { return err } + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return err diff --git a/vendor/github.com/nytm/go-grafana-api/folder.go b/vendor/github.com/emerald-squad/go-grafana-api/folder.go similarity index 76% rename from vendor/github.com/nytm/go-grafana-api/folder.go rename to vendor/github.com/emerald-squad/go-grafana-api/folder.go index 55613c60b..ebbf65a81 100644 --- a/vendor/github.com/nytm/go-grafana-api/folder.go +++ b/vendor/github.com/emerald-squad/go-grafana-api/folder.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io/ioutil" + "strconv" ) type Folder struct { @@ -14,13 +15,16 @@ type Folder struct { Title string `json:"title"` } -func (c *Client) Folders() ([]Folder, error) { +func (c *Client) Folders(orgID int64) ([]Folder, error) { folders := make([]Folder, 0) req, err := c.newRequest("GET", "/api/folders/", nil, nil) if err != nil { return folders, err } + + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return folders, err @@ -36,12 +40,15 @@ func (c *Client) Folders() ([]Folder, error) { return folders, err } -func (c *Client) Folder(id int64) (*Folder, error) { +func (c *Client) Folder(id int64, orgID int64) (*Folder, error) { folder := &Folder{} req, err := c.newRequest("GET", fmt.Sprintf("/api/folders/id/%d", id), nil, nil) if err != nil { return folder, err } + + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return folder, err @@ -57,7 +64,7 @@ func (c *Client) Folder(id int64) (*Folder, error) { return folder, err } -func (c *Client) NewFolder(title string) (Folder, error) { +func (c *Client) NewFolder(title string, orgID int64) (Folder, error) { folder := Folder{} dataMap := map[string]string{ "title": title, @@ -67,6 +74,9 @@ func (c *Client) NewFolder(title string) (Folder, error) { if err != nil { return folder, err } + + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return folder, err @@ -86,7 +96,7 @@ func (c *Client) NewFolder(title string) (Folder, error) { return folder, err } -func (c *Client) UpdateFolder(id string, name string) error { +func (c *Client) UpdateFolder(id string, name string, orgID int64) error { dataMap := map[string]string{ "name": name, } @@ -95,6 +105,9 @@ func (c *Client) UpdateFolder(id string, name string) error { if err != nil { return err } + + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return err @@ -105,11 +118,14 @@ func (c *Client) UpdateFolder(id string, name string) error { return err } -func (c *Client) DeleteFolder(id string) error { +func (c *Client) DeleteFolder(id string, orgID int64) error { req, err := c.newRequest("DELETE", fmt.Sprintf("/api/folders/%s", id), nil, nil) if err != nil { return err } + + req.Header.Set("X-Grafana-Org-Id", strconv.FormatInt(orgID, 10)) + resp, err := c.Do(req) if err != nil { return err diff --git a/vendor/github.com/nytm/go-grafana-api/org_users.go b/vendor/github.com/emerald-squad/go-grafana-api/org_users.go similarity index 100% rename from vendor/github.com/nytm/go-grafana-api/org_users.go rename to vendor/github.com/emerald-squad/go-grafana-api/org_users.go diff --git a/vendor/github.com/nytm/go-grafana-api/orgs.go b/vendor/github.com/emerald-squad/go-grafana-api/orgs.go similarity index 100% rename from vendor/github.com/nytm/go-grafana-api/orgs.go rename to vendor/github.com/emerald-squad/go-grafana-api/orgs.go diff --git a/vendor/github.com/nytm/go-grafana-api/user.go b/vendor/github.com/emerald-squad/go-grafana-api/user.go similarity index 73% rename from vendor/github.com/nytm/go-grafana-api/user.go rename to vendor/github.com/emerald-squad/go-grafana-api/user.go index 66f563494..2e115d536 100644 --- a/vendor/github.com/nytm/go-grafana-api/user.go +++ b/vendor/github.com/emerald-squad/go-grafana-api/user.go @@ -3,6 +3,7 @@ package gapi import ( "encoding/json" "errors" + "fmt" "io/ioutil" "net/url" ) @@ -74,3 +75,22 @@ func (c *Client) UserByEmail(email string) (User, error) { user = User(tmp) return user, err } + +// SwitchCurrentUserOrg will switch the current organisation of the signed in user +func (c *Client) SwitchCurrentUserOrg(orgID int64) error { + req, err := c.newRequest("POST", fmt.Sprintf("/api/user/using/%d", orgID), nil, nil) + + _, err = c.Do(req) + + return err +} + +// SwitchUserOrg will switch the current organisation of the given user ID (via basic auth) to +// the given organisation ID +func (c *Client) SwitchUserOrg(userID, orgID int64) error { + req, err := c.newRequest("POST", fmt.Sprintf("/api/users/%d/using/%d", userID, orgID), nil, nil) + + _, err = c.Do(req) + + return err +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 24cdddfd5..04cce37f0 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -49,6 +49,8 @@ github.com/bgentry/speakeasy github.com/blang/semver # github.com/davecgh/go-spew v1.1.1 github.com/davecgh/go-spew/spew +# github.com/emerald-squad/go-grafana-api v0.0.0-20190510131408-a95b9575e28e +github.com/emerald-squad/go-grafana-api # github.com/golang/protobuf v1.2.0 github.com/golang/protobuf/proto github.com/golang/protobuf/ptypes @@ -179,8 +181,6 @@ github.com/mitchellh/hashstructure github.com/mitchellh/mapstructure # github.com/mitchellh/reflectwalk v0.0.0-20170726202117-63d60e9d0dbc github.com/mitchellh/reflectwalk -# github.com/nytm/go-grafana-api v0.0.0-20181022152830-9cc848df7385 -github.com/nytm/go-grafana-api # github.com/oklog/run v1.0.0 github.com/oklog/run # github.com/posener/complete v0.0.0-20171219111128-6bee943216c8