@@ -3,6 +3,8 @@ package integration_types
33import (
44 "encoding/json"
55 "fmt"
6+ "github.com/google/uuid"
7+ "github.com/jackc/pgtype"
68 "github.com/opengovern/og-util/pkg/config"
79 "github.com/opengovern/og-util/pkg/httpclient"
810 "github.com/opengovern/og-util/pkg/opengovernance-es-sdk"
@@ -85,6 +87,7 @@ func (a *API) Register(e *echo.Group) {
8587 plugin .POST ("/:id/healthcheck" , httpserver .AuthorizeHandler (a .HealthCheck , api .ViewerRole ))
8688 plugin .GET ("/tables" , httpserver .AuthorizeHandler (a .GetPluginsTables , api .ViewerRole ))
8789 plugin .PUT ("/:id/demo/load" , httpserver .AuthorizeHandler (a .LoadPluginDemoData , api .EditorRole ))
90+ plugin .PUT ("/:id/demo/remove" , httpserver .AuthorizeHandler (a .RemovePluginDemoData , api .EditorRole ))
8891}
8992
9093// List godoc
@@ -1343,6 +1346,10 @@ func (a *API) LoadPluginDemoData(c echo.Context) error {
13431346 return echo .NewHTTPError (http .StatusNotFound , "plugin not found" )
13441347 }
13451348
1349+ if plugin .DemoDataLoaded {
1350+ return echo .NewHTTPError (http .StatusConflict , "plugin demo data already loaded" )
1351+ }
1352+
13461353 if plugin .DemoDataURL == "" {
13471354 return echo .NewHTTPError (http .StatusNotFound , "plugin does not contain demo data" )
13481355 }
@@ -1354,7 +1361,111 @@ func (a *API) LoadPluginDemoData(c echo.Context) error {
13541361 ElasticsearchPass : a .elasticConfig .Password ,
13551362 ElasticsearchAddr : a .elasticConfig .Address ,
13561363 }
1357- err = demo_import .LoadDemoData (demoImportConfig , a .logger )
1364+ integrations , err := demo_import .LoadDemoData (demoImportConfig , a .logger )
1365+ if err != nil {
1366+ a .logger .Error ("failed to load demo data" , zap .Error (err ))
1367+ return echo .NewHTTPError (http .StatusInternalServerError , "failed to load demo data" )
1368+ }
1369+
1370+ dummyCredentialID := uuid .New ()
1371+ dummyCredential := models2.Credential {
1372+ ID : dummyCredentialID ,
1373+ IntegrationType : plugin .IntegrationType ,
1374+ CredentialType : "" ,
1375+ Secret : "" ,
1376+ Metadata : func () pgtype.JSONB {
1377+ var jsonb pgtype.JSONB
1378+ if err := jsonb .Set ([]byte ("{}" )); err != nil {
1379+ a .logger .Error ("failed to convert WidgetProps to JSONB" , zap .Error (err ))
1380+ }
1381+ return jsonb
1382+ }(),
1383+ MaskedSecret : func () pgtype.JSONB {
1384+ var jsonb pgtype.JSONB
1385+ if err := jsonb .Set ([]byte ("{}" )); err != nil {
1386+ a .logger .Error ("failed to convert WidgetProps to JSONB" , zap .Error (err ))
1387+ }
1388+ return jsonb
1389+ }(),
1390+ Description : "dummy credential for demo integrations" ,
1391+ }
1392+
1393+ err = a .database .CreateCredential (& dummyCredential )
1394+ if err != nil {
1395+ a .logger .Error ("failed to create credential" , zap .Error (err ))
1396+ return echo .NewHTTPError (http .StatusInternalServerError , "failed to create credential" )
1397+ }
1398+
1399+ for _ , i := range integrations {
1400+ integrationId , err := uuid .Parse (i .IntegrationID )
1401+ if err != nil {
1402+ a .logger .Error ("failed to parse integration id" , zap .Error (err ))
1403+ return echo .NewHTTPError (http .StatusInternalServerError , "failed to parse integration id" )
1404+ }
1405+ dbIntegration := models2.Integration {
1406+ Integration : integration.Integration {
1407+ IntegrationID : integrationId ,
1408+ ProviderID : i .ProviderID ,
1409+ Name : i .Name ,
1410+ IntegrationType : plugin .IntegrationType ,
1411+ Annotations : func () pgtype.JSONB {
1412+ var jsonb pgtype.JSONB
1413+ if err := jsonb .Set (i .Annotations ); err != nil {
1414+ a .logger .Error ("failed to convert WidgetProps to JSONB" , zap .Error (err ))
1415+ }
1416+ return jsonb
1417+ }(),
1418+ Labels : func () pgtype.JSONB {
1419+ var jsonb pgtype.JSONB
1420+ if err := jsonb .Set (i .Labels ); err != nil {
1421+ a .logger .Error ("failed to convert WidgetProps to JSONB" , zap .Error (err ))
1422+ }
1423+ return jsonb
1424+ }(),
1425+ CredentialID : dummyCredentialID ,
1426+ State : integration .IntegrationStateSample ,
1427+ },
1428+ }
1429+ err = a .database .CreateIntegration (& dbIntegration )
1430+ if err != nil {
1431+ a .logger .Error ("failed to create integration" , zap .Error (err ))
1432+ return echo .NewHTTPError (http .StatusInternalServerError , "failed to create integration" )
1433+ }
1434+ }
1435+
1436+ return c .NoContent (http .StatusOK )
1437+ }
1438+
1439+ // RemovePluginDemoData godoc
1440+ //
1441+ // @Summary Remove demo data for plugin
1442+ // @Description Remove demo data for plugin by the given url
1443+ // @Security BearerToken
1444+ // @Tags integration_types
1445+ // @Produce json
1446+ // @Param id path string true "plugin id"
1447+ // @Success 200
1448+ // @Router /integration/api/v1/plugin/{id}/demo/remove [put]
1449+ func (a * API ) RemovePluginDemoData (c echo.Context ) error {
1450+ id := c .Param ("id" )
1451+
1452+ plugin , err := a .database .GetPluginByID (id )
1453+ if err != nil {
1454+ a .logger .Error ("failed to get plugin" , zap .Error (err ))
1455+ return echo .NewHTTPError (http .StatusInternalServerError , "failed to get plugin" )
1456+ }
1457+ if plugin == nil {
1458+ return echo .NewHTTPError (http .StatusNotFound , "plugin not found" )
1459+ }
1460+
1461+ if ! plugin .DemoDataLoaded {
1462+ return echo .NewHTTPError (http .StatusConflict , "plugin demo data not loaded" )
1463+ }
1464+
1465+ if err = a .database .DeletePluginSampleIntegrations (plugin .IntegrationType ); err != nil {
1466+ a .logger .Error ("failed to delete plugin sample integration" , zap .Error (err ))
1467+ return echo .NewHTTPError (http .StatusInternalServerError , "failed to delete plugin sample integration" )
1468+ }
13581469
13591470 return c .NoContent (http .StatusOK )
13601471}
0 commit comments