diff --git a/docs/tables/metabase_permission.md b/docs/tables/metabase_permission.md new file mode 100644 index 0000000..88498ec --- /dev/null +++ b/docs/tables/metabase_permission.md @@ -0,0 +1,25 @@ +# Table: metabase_permission + +List all permissions of Metabase. + +## Examples + +### List all permissions + +```sql +select + * +from + metabase_permission; +``` + +### Get permission of one group + +```sql +select + * +from + metabase_permission_group +where + group_id=15; +``` diff --git a/docs/tables/metabase_permission_data.md b/docs/tables/metabase_permission_data.md new file mode 100644 index 0000000..50e24be --- /dev/null +++ b/docs/tables/metabase_permission_data.md @@ -0,0 +1,14 @@ +# Table: metabase_permission_data + +List data permission of Metabase. + +## Examples + +### List all permissions + +```sql +select + * +from + metabase_permission_data; +``` diff --git a/docs/tables/metabase_permission_download.md b/docs/tables/metabase_permission_download.md new file mode 100644 index 0000000..5fe947f --- /dev/null +++ b/docs/tables/metabase_permission_download.md @@ -0,0 +1,14 @@ +# Table: metabase_permission_download + +List download permission of Metabase. + +## Examples + +### List all permissions + +```sql +select + * +from + metabase_permission_download; +``` diff --git a/metabase/plugin.go b/metabase/plugin.go index f6dfc5a..ee6aab7 100644 --- a/metabase/plugin.go +++ b/metabase/plugin.go @@ -19,10 +19,13 @@ func Plugin(ctx context.Context) *plugin.Plugin { Schema: ConfigSchema, }, TableMap: map[string]*plugin.Table{ - "metabase_db": tableMetabaseDb(), - "metabase_db_feature": tableMetabaseDbFeature(), - "metabase_db_table": tableMetabaseDbTable(), - "metabase_permission_group": tableMetabaseGroup(), + "metabase_db": tableMetabaseDb(), + "metabase_db_feature": tableMetabaseDbFeature(), + "metabase_db_table": tableMetabaseDbTable(), + "metabase_permission_group": tableMetabaseGroup(), + "metabase_permission": tableMetabasePermission(), + "metabase_permission_data": tableMetabasePermissionData(), + "metabase_permission_download": tableMetabasePermissionDownload(), }, } return p diff --git a/metabase/table_metabase_db.go b/metabase/table_metabase_db.go index ecf1c5c..0c12e3e 100644 --- a/metabase/table_metabase_db.go +++ b/metabase/table_metabase_db.go @@ -2,7 +2,6 @@ package metabase import ( "context" - "fmt" "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" "github.com/turbot/steampipe-plugin-sdk/v5/plugin" @@ -41,7 +40,7 @@ func tableMetabaseDb() *plugin.Table { } func listDatabase(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { - client, err := connect(ctx, d) + client, err := connect(d) if err != nil { plugin.Logger(ctx).Error("metabase_db.listDatabases", "connection_error", err) @@ -52,12 +51,9 @@ func listDatabase(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateDat dbList, resp, err := client.DatabaseApi.ListDatabasesExecute(request) + err = manageError("metabase_db.listDatabases", ctx, resp, err) + if err != nil { - plugin.Logger(ctx).Error("metabase_db.listDatabases", err) - return nil, err - } else if resp.StatusCode >= 300 { - err = fmt.Errorf("HTTP code = %d", resp.StatusCode) - plugin.Logger(ctx).Error("metabase_db.listDatabases", err) return nil, err } @@ -69,7 +65,7 @@ func listDatabase(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateDat } func getDatabase(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { - client, err := connect(ctx, d) + client, err := connect(d) if err != nil { plugin.Logger(ctx).Error("metabase_db.getDatabase", "connection_error", err) @@ -83,12 +79,9 @@ func getDatabase(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData db, resp, err := client.DatabaseApi.GetDatabaseExecute(request) + err = manageError("metabase_db.getDatabase", ctx, resp, err) + if err != nil { - plugin.Logger(ctx).Error("metabase_db.getDatabase", err) - return nil, err - } else if resp.StatusCode >= 300 { - err = fmt.Errorf("HTTP code = %d", resp.StatusCode) - plugin.Logger(ctx).Error("metabase_db.getDatabase", err) return nil, err } diff --git a/metabase/table_metabase_db_feature.go b/metabase/table_metabase_db_feature.go index 9419a46..c9b9448 100644 --- a/metabase/table_metabase_db_feature.go +++ b/metabase/table_metabase_db_feature.go @@ -2,7 +2,6 @@ package metabase import ( "context" - "fmt" "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" "github.com/turbot/steampipe-plugin-sdk/v5/plugin" @@ -20,7 +19,7 @@ func tableMetabaseDbFeature() *plugin.Table { Description: "Features of one database.", List: &plugin.ListConfig{ KeyColumns: plugin.AllColumns([]string{"db_id"}), - Hydrate: listDatabaseFeatures, + Hydrate: listDatabaseFeature, }, Columns: []*plugin.Column{ @@ -30,11 +29,11 @@ func tableMetabaseDbFeature() *plugin.Table { } } -func listDatabaseFeatures(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { - client, err := connect(ctx, d) +func listDatabaseFeature(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + client, err := connect(d) if err != nil { - plugin.Logger(ctx).Error("metabase_db.getDatabaseFeatures", "connection_error", err) + plugin.Logger(ctx).Error("metabase_db_feature.listDatabaseFeature", "connection_error", err) return nil, err } @@ -45,12 +44,9 @@ func listDatabaseFeatures(ctx context.Context, d *plugin.QueryData, h *plugin.Hy db, resp, err := client.DatabaseApi.GetDatabaseExecute(request) + err = manageError("metabase_db_feature.listDatabaseFeature", ctx, resp, err) + if err != nil { - plugin.Logger(ctx).Error("metabase_db.getDatabaseFeatures", err) - return nil, err - } else if resp.StatusCode >= 300 { - err = fmt.Errorf("HTTP code = %d", resp.StatusCode) - plugin.Logger(ctx).Error("metabase_db.getDatabaseFeatures", err) return nil, err } diff --git a/metabase/table_metabase_db_table.go b/metabase/table_metabase_db_table.go index 94bb94d..934339c 100644 --- a/metabase/table_metabase_db_table.go +++ b/metabase/table_metabase_db_table.go @@ -2,7 +2,6 @@ package metabase import ( "context" - "fmt" "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" "github.com/turbot/steampipe-plugin-sdk/v5/plugin" @@ -41,10 +40,10 @@ func tableMetabaseDbTable() *plugin.Table { } func listDatabaseTable(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { - client, err := connect(ctx, d) + client, err := connect(d) if err != nil { - plugin.Logger(ctx).Error("metabase_db.listDatabaseTable", "connection_error", err) + plugin.Logger(ctx).Error("metabase_db_table.listDatabaseTable", "connection_error", err) return nil, err } @@ -56,15 +55,9 @@ func listDatabaseTable(ctx context.Context, d *plugin.QueryData, _ *plugin.Hydra db, resp, err := client.DatabaseApi.GetDatabaseExecute(request) - if err != nil { - plugin.Logger(ctx).Error("metabase_db.listDatabaseTable", err) - - return nil, err - } else if resp.StatusCode >= 300 { - err = fmt.Errorf("HTTP code = %d", resp.StatusCode) - plugin.Logger(ctx).Debug("metabase_db.listDatabaseTable", "http-response", resp) - plugin.Logger(ctx).Error("metabase_db.listDatabaseTable", err) + err = manageError("metabase_db_table.listDatabaseTable", ctx, resp, err) + if err != nil { return nil, err } @@ -76,10 +69,10 @@ func listDatabaseTable(ctx context.Context, d *plugin.QueryData, _ *plugin.Hydra } func getDatabaseTable(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { - client, err := connect(ctx, d) + client, err := connect(d) if err != nil { - plugin.Logger(ctx).Error("metabase_db.getDatabaseTable", "connection_error", err) + plugin.Logger(ctx).Error("metabase_db_table.getDatabaseTable", "connection_error", err) return nil, err } @@ -92,12 +85,9 @@ func getDatabaseTable(ctx context.Context, d *plugin.QueryData, h *plugin.Hydrat db, resp, err := client.DatabaseApi.GetDatabaseExecute(request) + err = manageError("metabase_db_table.getDatabaseTable", ctx, resp, err) + if err != nil { - plugin.Logger(ctx).Error("metabase_db.getDatabaseTable", err) - return nil, err - } else if resp.StatusCode >= 300 { - err = fmt.Errorf("HTTP code = %d", resp.StatusCode) - plugin.Logger(ctx).Error("metabase_db.getDatabaseTable", err) return nil, err } diff --git a/metabase/table_metabase_group.go b/metabase/table_metabase_group.go index 57ca62e..8e750ff 100644 --- a/metabase/table_metabase_group.go +++ b/metabase/table_metabase_group.go @@ -2,7 +2,6 @@ package metabase import ( "context" - "fmt" "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" "github.com/turbot/steampipe-plugin-sdk/v5/plugin" @@ -22,7 +21,7 @@ func tableMetabaseGroup() *plugin.Table { }, Columns: []*plugin.Column{ // Key column cannot be a pointer. Transform helps us to manage them - {Name: "id", Type: proto.ColumnType_INT, Transform: transform.FromField("Id"), Description: "ID of groud."}, + {Name: "id", Type: proto.ColumnType_INT, Transform: transform.FromField("Id"), Description: "ID of group."}, {Name: "name", Type: proto.ColumnType_STRING, Description: "Name of group"}, {Name: "member_count", Type: proto.ColumnType_STRING, Description: "Number of member."}, }, @@ -30,10 +29,10 @@ func tableMetabaseGroup() *plugin.Table { } func listPermissionGroup(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { - client, err := connect(ctx, d) + client, err := connect(d) if err != nil { - plugin.Logger(ctx).Error("metabase_db.listPermissionGroup", "connection_error", err) + plugin.Logger(ctx).Error("metabase_permission_group.listPermissionGroup", "connection_error", err) return nil, err } @@ -41,15 +40,9 @@ func listPermissionGroup(ctx context.Context, d *plugin.QueryData, _ *plugin.Hyd permissions, resp, err := client.PermissionsApi.GetPermissionsGroupExecute(request) - if err != nil { - plugin.Logger(ctx).Error("metabase_db.listPermissionGroup", err) - - return nil, err - } else if resp.StatusCode >= 300 { - err = fmt.Errorf("HTTP code = %d", resp.StatusCode) - plugin.Logger(ctx).Debug("metabase_db.listPermissionGroup", "http-response", resp) - plugin.Logger(ctx).Error("metabase_db.listPermissionGroup", err) + err = manageError("metabase_permission_group.listPermissionGroup", ctx, resp, err) + if err != nil { return nil, err } @@ -61,10 +54,10 @@ func listPermissionGroup(ctx context.Context, d *plugin.QueryData, _ *plugin.Hyd } func getPermissionGroup(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { - client, err := connect(ctx, d) + client, err := connect(d) if err != nil { - plugin.Logger(ctx).Error("metabase_db.getPermissionGroup", "connection_error", err) + plugin.Logger(ctx).Error("metabase_permission_group.getPermissionGroup", "connection_error", err) return nil, err } @@ -75,12 +68,9 @@ func getPermissionGroup(ctx context.Context, d *plugin.QueryData, h *plugin.Hydr permissions, resp, err := client.PermissionsApi.GetPermissionsGroupExecute(request) + err = manageError("metabase_permission_group.getPermissionGroup", ctx, resp, err) + if err != nil { - plugin.Logger(ctx).Error("metabase_db.getPermissionGroup", err) - return nil, err - } else if resp.StatusCode >= 300 { - err = fmt.Errorf("HTTP code = %d", resp.StatusCode) - plugin.Logger(ctx).Error("metabase_db.getPermissionGroup", err) return nil, err } diff --git a/metabase/table_metabase_permission.go b/metabase/table_metabase_permission.go new file mode 100644 index 0000000..042ad3b --- /dev/null +++ b/metabase/table_metabase_permission.go @@ -0,0 +1,146 @@ +package metabase + +import ( + "context" + "fmt" + "strconv" + + "github.com/1024pix/go-metabase/metabase" + "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" + "github.com/turbot/steampipe-plugin-sdk/v5/plugin" + "github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform" +) + +type Permission struct { + GroupId int + DbId int + DownloadNative *string + DownloadSchema *string + DataNative *string + DataSchema *string +} + +func tableMetabasePermission() *plugin.Table { + return &plugin.Table{ + Name: "metabase_permission", + Description: "List of permissions in Metabase.", + List: &plugin.ListConfig{ + Hydrate: listPermission, + }, + Columns: []*plugin.Column{ + // Key column cannot be a pointer. Transform helps us to manage them + {Name: "group_id", Type: proto.ColumnType_INT, Transform: transform.FromField("GroupId"), Description: "ID of group."}, + {Name: "db_id", Type: proto.ColumnType_INT, Transform: transform.FromField("DbId"), Description: "ID of database"}, + {Name: "download_native", Type: proto.ColumnType_STRING, Transform: transform.FromField("DownloadNative"), Description: "Type of download."}, + {Name: "download_schema", Type: proto.ColumnType_STRING, Transform: transform.FromField("DownloadSchema"), Description: "Schema that you can download."}, + {Name: "data_native", Type: proto.ColumnType_STRING, Transform: transform.FromField("DataNative"), Description: "Type of data."}, + {Name: "data_schema", Type: proto.ColumnType_STRING, Transform: transform.FromField("DataSchema"), Description: "Data that you can download."}, + }, + } +} + +func listPermission(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { + client, err := connect(d) + + if err != nil { + plugin.Logger(ctx).Error("metabase_permission.listPermission", "connection_error", err) + return nil, err + } + + request := client.PermissionsApi.GetPermissionsGraph(context.Background()) + + permission, resp, err := client.PermissionsApi.GetPermissionsGraphExecute(request) + + err = manageError("metabase_permission.listPermission", ctx, resp, err) + + if err != nil { + return nil, err + } + + permissions, err := createPermission("metabase_permission.listPermission", ctx, permission.Groups) + + if err == nil { + for _, perm := range permissions { + d.StreamListItem(ctx, perm) + } + } + + return nil, nil +} + +func createPermission(methodCallStack string, ctx context.Context, groups *map[string]map[string]metabase.PermissionGraphData) ([]Permission, error) { + var permissions []Permission + methodCallStack = fmt.Sprintf("%s.extractSchemasAndTable", methodCallStack) + + for groupId, database := range *groups { + if plugin.Logger(ctx).IsDebug() { + plugin.Logger(ctx).Debug(methodCallStack, "response", fmt.Sprintf("Group id = %s, data = %+v", groupId, database)) + } + + gId, errGID := strconv.Atoi(groupId) + + if errGID != nil { + err := fmt.Errorf("Group id is not a integer '%s'", groupId) + plugin.Logger(ctx).Error(methodCallStack, err) + return nil, err + } + + // If some rights are set then, there are removed. Api return nil. + if database == nil { + continue + } + + for databaseId, data := range database { + dId, errDID := strconv.Atoi(databaseId) + + if errDID != nil { + err := fmt.Errorf("Database id is not a integer '%s'", databaseId) + plugin.Logger(ctx).Error(methodCallStack, err) + return nil, err + } + + downloadNative, downloadSchema := extractNativeAndSchemas(data.Download) + dataNative, dataSchema := extractNativeAndSchemas(data.Data) + + permissions = append(permissions, Permission{ + GroupId: gId, + DbId: dId, + DownloadNative: downloadNative, + DownloadSchema: downloadSchema, + DataNative: dataNative, + DataSchema: dataSchema, + }) + } + } + + return permissions, nil +} + +func extractNativeAndSchemas(data map[string]interface{}) (*string, *string) { + var ( + native *string + schema *string + ) + + dn, ok := data["native"] + + if ok { + tmp := fmt.Sprint(dn) + native = &tmp + } + + ds, ok := data["schemas"] + + if ok && ds != nil { + switch v := ds.(type) { + case string: + tmp := fmt.Sprint(v) + schema = &tmp + default: + tmp := "limited" + schema = &tmp + } + } + + return native, schema +} diff --git a/metabase/table_metabase_permission_data.go b/metabase/table_metabase_permission_data.go new file mode 100644 index 0000000..5cc1cfb --- /dev/null +++ b/metabase/table_metabase_permission_data.go @@ -0,0 +1,214 @@ +package metabase + +import ( + "context" + "fmt" + "strconv" + + "github.com/1024pix/go-metabase/metabase" + "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" + "github.com/turbot/steampipe-plugin-sdk/v5/plugin" + "github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform" +) + +type PermissionType int + +const ( + PermissionData PermissionType = iota + PermissionDownload +) + +var SubPermissionColum = []*plugin.Column{ + // Key column cannot be a pointer. Transform helps us to manage them + {Name: "group_id", Type: proto.ColumnType_INT, Transform: transform.FromField("GroupId"), Description: "ID of group."}, + {Name: "db_id", Type: proto.ColumnType_INT, Transform: transform.FromField("DbId"), Description: "ID of database"}, + {Name: "schema_name", Type: proto.ColumnType_STRING, Transform: transform.FromField("SchemaName"), Description: "Name of schema."}, + {Name: "table_id", Type: proto.ColumnType_INT, Transform: transform.FromField("TableId"), Description: "Table id."}, + {Name: "level_access", Type: proto.ColumnType_STRING, Transform: transform.FromField("LevelAccess"), Description: "Level access of table."}, +} + +type PermissionSchema struct { + GroupId int + DbId int + SchemaName string + TableId int + LevelAccess string +} + +func tableMetabasePermissionData() *plugin.Table { + return &plugin.Table{ + Name: "metabase_permission_data", + Description: "List of permissions for data in Metabase.", + List: &plugin.ListConfig{ + Hydrate: listPermissionData, + }, + Columns: SubPermissionColum, + } +} + +func listPermissionData(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { + return listSubPermission("listPermissionData", ctx, d, PermissionData) +} + +func listSubPermission(methodCallStack string, ctx context.Context, d *plugin.QueryData, permissionType PermissionType) (interface{}, error) { + client, err := connect(d) + methodCallStack = fmt.Sprintf("%s.listSubPermission", methodCallStack) + + if err != nil { + plugin.Logger(ctx).Error(methodCallStack, "connection_error", err) + return nil, err + } + + request := client.PermissionsApi.GetPermissionsGraph(context.Background()) + + permission, resp, err := client.PermissionsApi.GetPermissionsGraphExecute(request) + + err = manageError(methodCallStack, ctx, resp, err) + + if err != nil { + return nil, err + } + + permissions, err := createPermissionData(methodCallStack, ctx, permission.Groups, permissionType) + + if err == nil { + for _, perm := range permissions { + d.StreamListItem(ctx, perm) + } + } + + return nil, nil +} + +func createPermissionData(methodCallStack string, ctx context.Context, groups *map[string]map[string]metabase.PermissionGraphData, permissionType PermissionType) ([]PermissionSchema, error) { + var permissions []PermissionSchema + methodCallStack = fmt.Sprintf("%s.createPermissionData", methodCallStack) + + for groupId, database := range *groups { + if plugin.Logger(ctx).IsDebug() { + plugin.Logger(ctx).Debug(methodCallStack, "response", fmt.Sprintf("Group id = %s, data = %+v", groupId, database)) + } + + gId, errGID := strconv.Atoi(groupId) + + if errGID != nil { + err := fmt.Errorf("Group id is not a integer '%s'", groupId) + plugin.Logger(ctx).Error(methodCallStack, err) + return nil, err + } + + // If some rights are set then, there are removed. Api return nil. + if database == nil { + continue + } + + for databaseId, data := range database { + dId, errDID := strconv.Atoi(databaseId) + + if errDID != nil { + err := fmt.Errorf("Database id is not a integer '%s'", databaseId) + plugin.Logger(ctx).Error(methodCallStack, err) + return nil, err + } + + var d *map[string]map[string]interface{} + + switch permissionType { + case PermissionData: + d = extractSchemasAndTable(methodCallStack, ctx, data.Data) + default: + d = extractSchemasAndTable(methodCallStack, ctx, data.Download) + } + + if d == nil { + continue + } + + perms, err := extractGranularPermissions(methodCallStack, ctx, gId, dId, d) + + if err != nil { + return nil, err + } + + permissions = append(permissions, perms...) + } + } + + return permissions, nil +} + +func extractSchemasAndTable(methodCallStack string, ctx context.Context, data map[string]interface{}) *map[string]map[string]interface{} { + var result *map[string]map[string]interface{} + methodCallStack = fmt.Sprintf("%s.extractSchemasAndTable", methodCallStack) + + ds, ok := data["schemas"] + + if ok && ds != nil { + switch v := ds.(type) { + case string: + result = nil + default: + theRootMap, ok := v.(map[string]interface{}) + + if plugin.Logger(ctx).IsDebug() { + plugin.Logger(ctx).Error(methodCallStack, fmt.Sprintf("ok: %t value: %+v", ok, theRootMap)) + } + + if ok { + convertMap := make(map[string]map[string]interface{}) + + for key, value := range theRootMap { + theChildMap, ok := value.(map[string]interface{}) + + if ok { + convertMap[key] = theChildMap + } + } + + if plugin.Logger(ctx).IsDebug() { + plugin.Logger(ctx).Debug(methodCallStack, fmt.Sprintf("convert map: %+v", convertMap)) + } + + result = &convertMap + } + } + } + + return result +} + +func extractGranularPermissions(methodCallStack string, ctx context.Context, groupId int, dbId int, data *map[string]map[string]interface{}) ([]PermissionSchema, error) { + var permissions []PermissionSchema + methodCallStack = fmt.Sprintf("%s.extractSchemasAndTable", methodCallStack) + + for schemaName, tableList := range *data { + for tableId, levelAccess := range tableList { + tId, errTID := strconv.Atoi(tableId) + + if errTID != nil { + err := fmt.Errorf("Table id is not a integer '%s'", tableId) + plugin.Logger(ctx).Error(methodCallStack, err) + return nil, err + } + + var la string + + switch v := levelAccess.(type) { + case string: + la = fmt.Sprint(v) + default: + la = "" + } + + permissions = append(permissions, PermissionSchema{ + GroupId: groupId, + DbId: dbId, + SchemaName: schemaName, + TableId: tId, + LevelAccess: la, + }) + } + } + + return permissions, nil +} diff --git a/metabase/table_metabase_permission_download.go b/metabase/table_metabase_permission_download.go new file mode 100644 index 0000000..6c60c0b --- /dev/null +++ b/metabase/table_metabase_permission_download.go @@ -0,0 +1,22 @@ +package metabase + +import ( + "context" + + "github.com/turbot/steampipe-plugin-sdk/v5/plugin" +) + +func tableMetabasePermissionDownload() *plugin.Table { + return &plugin.Table{ + Name: "metabase_permission_download", + Description: "List of permissions for download in Metabase.", + List: &plugin.ListConfig{ + Hydrate: listPermissionDownload, + }, + Columns: SubPermissionColum, + } +} + +func listPermissionDownload(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) { + return listSubPermission("listPermissionDownload", ctx, d, PermissionDownload) +} diff --git a/metabase/utils.go b/metabase/utils.go index 33b2b73..6881733 100644 --- a/metabase/utils.go +++ b/metabase/utils.go @@ -3,6 +3,8 @@ package metabase import ( "context" "errors" + "fmt" + "net/http" "os" "github.com/1024pix/go-metabase/metabase" @@ -11,7 +13,7 @@ import ( "github.com/turbot/steampipe-plugin-sdk/v5/plugin" ) -func connect(ctx context.Context, d *plugin.QueryData) (*metabase.APIClient, error) { +func connect(d *plugin.QueryData) (*metabase.APIClient, error) { // get metabase client from cache cacheKey := "metabase" if cachedData, ok := d.ConnectionManager.Cache.Get(cacheKey); ok { @@ -39,13 +41,13 @@ func connect(ctx context.Context, d *plugin.QueryData) (*metabase.APIClient, err password = *metabaseConfig.Password } - if metabaseConfig.Url != nil { - url = *metabaseConfig.Url - } + if metabaseConfig.Url != nil { + url = *metabaseConfig.Url + } - if metabaseConfig.TlsSkipVerify != nil { - tLSSkipVerify = *metabaseConfig.TlsSkipVerify - } + if metabaseConfig.TlsSkipVerify != nil { + tLSSkipVerify = *metabaseConfig.TlsSkipVerify + } if len(token) == 0 && len(user) == 0 && len(password) == 0 { return nil, errors.New("'token' or 'user/password' must be set in the connection configuration. Edit your connection configuration file or set the METABASE_TOKEN or METABASE_USER/METABASE_PASSWORD environment variable and then restart Steampipe") @@ -76,3 +78,19 @@ func connect(ctx context.Context, d *plugin.QueryData) (*metabase.APIClient, err return client, nil } + +func manageError(methodCallStack string, ctx context.Context, resp *http.Response, err error) error { + if err != nil { + plugin.Logger(ctx).Error(fmt.Sprintf("%s.manageError", methodCallStack), err) + + return err + } else if resp.StatusCode >= 300 { + err = fmt.Errorf("HTTP code = %d", resp.StatusCode) + plugin.Logger(ctx).Debug(fmt.Sprintf("%s.manageError", methodCallStack), "http-response", resp) + plugin.Logger(ctx).Error(fmt.Sprintf("%s.manageError", methodCallStack), err) + + return err + } + + return nil +}