diff --git a/api/content.go b/api/content.go index bc5081c3..89154087 100644 --- a/api/content.go +++ b/api/content.go @@ -33,6 +33,16 @@ type GetPathResponse struct { Path string `json:"path"` } +type LookupTableSyncDefinition struct { + Type string `json:"type"` + Name string `json:"name"` + Description string `json:"description"` + Fields []fields `json:"fields"` + PrimaryKeys []string `json:"primaryKeys"` + TTL int `json:"ttl"` + SizeLimitAction string `json:"sizeLimitAction"` +} + type MetricsSavedSearchSyncDefinition struct { Type string `json:"type"` Name string `json:"name"` @@ -53,16 +63,6 @@ type MetricsSearchSyncDefinition struct { VisualSettings string `json:"visualSettings"` } -type LookupTableSyncDefinition struct { - Type string `json:"type"` - Name string `json:"name"` - Description string `json:"description"` - Fields []fields `json:"fields"` - PrimaryKeys []string `json:"primaryKeys"` - TTL int `json:"ttl"` - SizeLimitAction string `json:"sizeLimitAction"` -} - type MewboardSyncDefinition struct { Type string `json:"type"` Name string `json:"name"` @@ -79,6 +79,11 @@ type MewboardSyncDefinition struct { ColoringRules coloringRulesDefinition `json:"coloringRules"` } +type MoveResponse struct { + Id string `json:"id"` + Errors []moveErrors `json:"errors"` +} + type ResponseType struct { Type string `json:"type"` } @@ -172,6 +177,11 @@ type metricsQueriesDefinition struct { RowId string `json:"rowId"` } +type moveErrors struct { + Code string `json:"code"` + Message string `json:"message"` +} + type panelsDefinition struct { Id string `json:"id"` Key string `json:"key"` @@ -217,7 +227,7 @@ type reportPanelSyncDefinition struct { MetricsQueries []metricsQueriesDefinition `json:"metricsQueries"` TimeRange timeRangeDefinition `json:"timeRange"` X int `json:"x"` - Y int `json:"Y"` + Y int `json:"y"` Width int `json:"width"` Height int `json:"height"` Properties string `json:"properties"` diff --git a/pkg/cmd/content/content.go b/pkg/cmd/content/content.go index 6961977c..cc4e4fb0 100644 --- a/pkg/cmd/content/content.go +++ b/pkg/cmd/content/content.go @@ -2,11 +2,18 @@ package content import ( "github.com/spf13/cobra" + cmdContentCopyStatus "github.com/wizedkyle/sumocli/pkg/cmd/content/copy-status" + cmdContentDeletionStatus "github.com/wizedkyle/sumocli/pkg/cmd/content/deletion-status" cmdContentExportResult "github.com/wizedkyle/sumocli/pkg/cmd/content/export-result" cmdContentExportStatus "github.com/wizedkyle/sumocli/pkg/cmd/content/export-status" cmdContentGet "github.com/wizedkyle/sumocli/pkg/cmd/content/get" cmdContentGetPath "github.com/wizedkyle/sumocli/pkg/cmd/content/get-path" + cmdContentImportStatus "github.com/wizedkyle/sumocli/pkg/cmd/content/import-status" + cmdContentMove "github.com/wizedkyle/sumocli/pkg/cmd/content/move" + cmdContentStartCopy "github.com/wizedkyle/sumocli/pkg/cmd/content/start-copy" + cmdContentStartDeletion "github.com/wizedkyle/sumocli/pkg/cmd/content/start-deletion" cmdContentStartExport "github.com/wizedkyle/sumocli/pkg/cmd/content/start-export" + cmdContentStartImport "github.com/wizedkyle/sumocli/pkg/cmd/content/start-import" ) func NewCmdContent() *cobra.Command { @@ -15,10 +22,17 @@ func NewCmdContent() *cobra.Command { Short: "Manage content", Long: "Commands that allow you to manage content in your Sumo Logic tenant", } + cmd.AddCommand(cmdContentCopyStatus.NewCmdCopyStatus()) + cmd.AddCommand(cmdContentDeletionStatus.NewCmdDeletionStatus()) cmd.AddCommand(cmdContentExportResult.NewCmdExportResult()) cmd.AddCommand(cmdContentExportStatus.NewCmdExportStatus()) cmd.AddCommand(cmdContentGet.NewCmdGet()) cmd.AddCommand(cmdContentGetPath.NewCmdGetPath()) + cmd.AddCommand(cmdContentImportStatus.NewCmdImportStatus()) + cmd.AddCommand(cmdContentMove.NewCmdMove()) + cmd.AddCommand(cmdContentStartDeletion.NewCmdStartDeletion()) + cmd.AddCommand(cmdContentStartCopy.NewCmdStartCopy()) cmd.AddCommand(cmdContentStartExport.NewCmdStartExport()) + cmd.AddCommand(cmdContentStartImport.NewCmdStartImport()) return cmd } diff --git a/pkg/cmd/content/copy-status/copy-status.go b/pkg/cmd/content/copy-status/copy-status.go new file mode 100644 index 00000000..687e5dd0 --- /dev/null +++ b/pkg/cmd/content/copy-status/copy-status.go @@ -0,0 +1,69 @@ +package copy_status + +import ( + "encoding/json" + "fmt" + "github.com/spf13/cobra" + "github.com/wizedkyle/sumocli/api" + "github.com/wizedkyle/sumocli/pkg/cmd/factory" + "github.com/wizedkyle/sumocli/pkg/logging" + "io" +) + +func NewCmdCopyStatus() *cobra.Command { + var ( + id string + jobId string + isAdminMode bool + ) + + cmd := &cobra.Command{ + Use: "copy-status", + Short: "Get the status of the copy request with the given job identifier. On success, field statusMessage will contain identifier of the newly copied content.", + Run: func(cmd *cobra.Command, args []string) { + copyStatus(id, jobId, isAdminMode) + }, + } + cmd.Flags().StringVar(&id, "id", "", "Specify the id of the content that was copied") + cmd.Flags().StringVar(&jobId, "jobId", "", "Specify the job id for the import (returned from running sumocli content start-import)") + cmd.Flags().BoolVar(&isAdminMode, "isAdminMode", false, "Set to true if you want to perform the request as a content administrator") + cmd.MarkFlagRequired("id") + cmd.MarkFlagRequired("jobId") + return cmd +} + +func copyStatus(id string, jobId string, isAdminMode bool) { + var copyStatusResponse api.ExportStatusResponse + log := logging.GetConsoleLogger() + requestUrl := "v2/content/" + id + "/copy/" + jobId + "/status" + client, request := factory.NewHttpRequest("GET", requestUrl) + if isAdminMode == true { + request.Header.Add("isAdminMode", "true") + } + response, err := client.Do(request) + if err != nil { + log.Error().Err(err).Msg("failed to make http request to " + requestUrl) + } + + defer response.Body.Close() + responseBody, err := io.ReadAll(response.Body) + if err != nil { + log.Error().Err(err).Msg("failed to read response body") + } + + err = json.Unmarshal(responseBody, ©StatusResponse) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal response body") + } + + copyStatusJson, err := json.MarshalIndent(copyStatusResponse, "", " ") + if err != nil { + log.Error().Err(err).Msg("failed to marshal exportStatusResponse") + } + + if response.StatusCode != 200 { + factory.HttpError(response.StatusCode, responseBody, log) + } else { + fmt.Println(string(copyStatusJson)) + } +} diff --git a/pkg/cmd/content/deletion-status/deletion-status.go b/pkg/cmd/content/deletion-status/deletion-status.go new file mode 100644 index 00000000..362dc711 --- /dev/null +++ b/pkg/cmd/content/deletion-status/deletion-status.go @@ -0,0 +1,69 @@ +package deletion_status + +import ( + "encoding/json" + "fmt" + "github.com/spf13/cobra" + "github.com/wizedkyle/sumocli/api" + "github.com/wizedkyle/sumocli/pkg/cmd/factory" + "github.com/wizedkyle/sumocli/pkg/logging" + "io" +) + +func NewCmdDeletionStatus() *cobra.Command { + var ( + id string + jobId string + isAdminMode bool + ) + + cmd := &cobra.Command{ + Use: "deletion-status", + Short: "Get the status of an asynchronous content deletion job request for the given job identifier.", + Run: func(cmd *cobra.Command, args []string) { + deletionStatus(id, jobId, isAdminMode) + }, + } + cmd.Flags().StringVar(&id, "id", "", "Specify the id of the content to delete") + cmd.Flags().StringVar(&jobId, "jobId", "", "Specify the job id for the deletion (returned from running sumocli content start-deletion)") + cmd.Flags().BoolVar(&isAdminMode, "isAdminMode", false, "Set to true if you want to perform the request as a content administrator") + cmd.MarkFlagRequired("contentId") + cmd.MarkFlagRequired("jobId") + return cmd +} + +func deletionStatus(id string, jobId string, isAdminMode bool) { + var deletionStatusResponse api.ExportStatusResponse + log := logging.GetConsoleLogger() + requestUrl := "v2/content/" + id + "/delete/" + jobId + "/status" + client, request := factory.NewHttpRequest("GET", requestUrl) + if isAdminMode == true { + request.Header.Add("isAdminMode", "true") + } + response, err := client.Do(request) + if err != nil { + log.Error().Err(err).Msg("failed to make http request to " + requestUrl) + } + + defer response.Body.Close() + responseBody, err := io.ReadAll(response.Body) + if err != nil { + log.Error().Err(err).Msg("failed to read response body") + } + + err = json.Unmarshal(responseBody, &deletionStatusResponse) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal response body") + } + + importStatusJson, err := json.MarshalIndent(deletionStatusResponse, "", " ") + if err != nil { + log.Error().Err(err).Msg("failed to marshal exportStatusResponse") + } + + if response.StatusCode != 200 { + factory.HttpError(response.StatusCode, responseBody, log) + } else { + fmt.Println(string(importStatusJson)) + } +} diff --git a/pkg/cmd/content/get/get.go b/pkg/cmd/content/get/get.go index edf13a6d..dc6021c4 100644 --- a/pkg/cmd/content/get/get.go +++ b/pkg/cmd/content/get/get.go @@ -21,7 +21,7 @@ func NewCmdGet() *cobra.Command { getContent(path) }, } - cmd.Flags().StringVar(&path, "path", "", "Specify the path of the content you want to retrieve") + cmd.Flags().StringVar(&path, "path", "", "Specify the path of the content you want to retrieve (e.g. /Library/Users/user@demo.com/SampleFolder)") cmd.MarkFlagRequired("path") return cmd } diff --git a/pkg/cmd/content/import-status/import-status.go b/pkg/cmd/content/import-status/import-status.go new file mode 100644 index 00000000..329ff865 --- /dev/null +++ b/pkg/cmd/content/import-status/import-status.go @@ -0,0 +1,69 @@ +package import_status + +import ( + "encoding/json" + "fmt" + "github.com/spf13/cobra" + "github.com/wizedkyle/sumocli/api" + "github.com/wizedkyle/sumocli/pkg/cmd/factory" + "github.com/wizedkyle/sumocli/pkg/logging" + "io" +) + +func NewCmdImportStatus() *cobra.Command { + var ( + folderId string + jobId string + isAdminMode bool + ) + + cmd := &cobra.Command{ + Use: "import-status", + Short: "Get the status of an asynchronous content import request for the given job identifier", + Run: func(cmd *cobra.Command, args []string) { + importStatus(folderId, jobId, isAdminMode) + }, + } + cmd.Flags().StringVar(&folderId, "folderId", "", "Specify the id of the folder to import to") + cmd.Flags().StringVar(&jobId, "jobId", "", "Specify the job id for the import (returned from running sumocli content start-import)") + cmd.Flags().BoolVar(&isAdminMode, "isAdminMode", false, "Set to true if you want to perform the request as a content administrator") + cmd.MarkFlagRequired("folderId") + cmd.MarkFlagRequired("jobId") + return cmd +} + +func importStatus(folderId string, jobId string, isAdminMode bool) { + var importStatusResponse api.ExportStatusResponse + log := logging.GetConsoleLogger() + requestUrl := "v2/content/folders/" + folderId + "/import/" + jobId + "/status" + client, request := factory.NewHttpRequest("GET", requestUrl) + if isAdminMode == true { + request.Header.Add("isAdminMode", "true") + } + response, err := client.Do(request) + if err != nil { + log.Error().Err(err).Msg("failed to make http request to " + requestUrl) + } + + defer response.Body.Close() + responseBody, err := io.ReadAll(response.Body) + if err != nil { + log.Error().Err(err).Msg("failed to read response body") + } + + err = json.Unmarshal(responseBody, &importStatusResponse) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal response body") + } + + importStatusJson, err := json.MarshalIndent(importStatusResponse, "", " ") + if err != nil { + log.Error().Err(err).Msg("failed to marshal exportStatusResponse") + } + + if response.StatusCode != 200 { + factory.HttpError(response.StatusCode, responseBody, log) + } else { + fmt.Println(string(importStatusJson)) + } +} diff --git a/pkg/cmd/content/move/move.go b/pkg/cmd/content/move/move.go new file mode 100644 index 00000000..563df81a --- /dev/null +++ b/pkg/cmd/content/move/move.go @@ -0,0 +1,74 @@ +package move + +import ( + "encoding/json" + "fmt" + "github.com/spf13/cobra" + "github.com/wizedkyle/sumocli/api" + "github.com/wizedkyle/sumocli/pkg/cmd/factory" + "github.com/wizedkyle/sumocli/pkg/logging" + "io" + "net/url" +) + +func NewCmdMove() *cobra.Command { + var ( + id string + destinationFolderId string + isAdminMode bool + ) + + cmd := &cobra.Command{ + Use: "move", + Short: "Moves an item from its current location to another folder.", + Run: func(cmd *cobra.Command, args []string) { + move(id, destinationFolderId, isAdminMode) + }, + } + cmd.Flags().StringVar(&id, "id", "", "Specify the id of the content to move") + cmd.Flags().StringVar(&destinationFolderId, "destinationFolderId", "", "Specify the destination folder to move the content to") + cmd.Flags().BoolVar(&isAdminMode, "isAdminMode", false, "Set to true if you want to perform the request as a content administrator") + cmd.MarkFlagRequired("id") + cmd.MarkFlagRequired("destinationFolderId") + return cmd +} + +func move(id string, destinationFolderId string, isAdminMode bool) { + var moveResponse api.MoveResponse + log := logging.GetConsoleLogger() + requestUrl := "v2/content/" + id + "/move" + client, request := factory.NewHttpRequest("POST", requestUrl) + if isAdminMode == true { + request.Header.Add("isAdminMode", "true") + } + query := url.Values{} + query.Add("destinationFolderId", destinationFolderId) + request.URL.RawQuery = query.Encode() + response, err := client.Do(request) + if err != nil { + log.Error().Err(err).Msg("failed to make http request to ") + } + + defer response.Body.Close() + responseBody, err := io.ReadAll(response.Body) + if err != nil { + log.Error().Err(err).Msg("failed to read response body") + } + + err = json.Unmarshal(responseBody, &moveResponse) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal response body") + } + + moveJson, err := json.MarshalIndent(moveResponse, "", " ") + if err != nil { + log.Error().Err(err).Msg("failed to marshal copyResponse") + } + + if response.StatusCode != 200 { + factory.HttpError(response.StatusCode, responseBody, log) + fmt.Println(string(moveJson)) + } else { + fmt.Println("Content successfully moved.") + } +} diff --git a/pkg/cmd/content/start-copy/start-copy.go b/pkg/cmd/content/start-copy/start-copy.go new file mode 100644 index 00000000..9541365f --- /dev/null +++ b/pkg/cmd/content/start-copy/start-copy.go @@ -0,0 +1,73 @@ +package start_copy + +import ( + "encoding/json" + "fmt" + "github.com/spf13/cobra" + "github.com/wizedkyle/sumocli/api" + "github.com/wizedkyle/sumocli/pkg/cmd/factory" + "github.com/wizedkyle/sumocli/pkg/logging" + "io" + "net/url" +) + +func NewCmdStartCopy() *cobra.Command { + var ( + id string + destinationFolder string + isAdminMode bool + ) + + cmd := &cobra.Command{ + Use: "start-copy", + Short: "Start an asynchronous content copy job with the given identifier to the destination folder. If the content item is a folder, everything under the folder is copied recursively.", + Run: func(cmd *cobra.Command, args []string) { + startCopy(id, destinationFolder, isAdminMode) + }, + } + cmd.Flags().StringVar(&id, "id", "", "Specify the id of the content you want to copy") + cmd.Flags().StringVar(&destinationFolder, "destinationFolder", "", "Specify the id of the destination folder") + cmd.Flags().BoolVar(&isAdminMode, "isAdminMode", false, "et to true if you want to perform the request as a content administrator") + cmd.MarkFlagRequired("id") + cmd.MarkFlagRequired("destinationFolder") + return cmd +} + +func startCopy(id string, destinationFolder string, isAdminMode bool) { + var copyResponse api.StartExportResponse + log := logging.GetConsoleLogger() + requestUrl := "v2/content/" + id + "/copy" + client, request := factory.NewHttpRequest("POST", requestUrl) + if isAdminMode == true { + request.Header.Add("isAdminMode", "true") + } + query := url.Values{} + query.Add("destinationFolder", destinationFolder) + request.URL.RawQuery = query.Encode() + response, err := client.Do(request) + if err != nil { + log.Error().Err(err).Msg("failed to make http request to ") + } + + defer response.Body.Close() + responseBody, err := io.ReadAll(response.Body) + if err != nil { + log.Error().Err(err).Msg("failed to read response body") + } + + err = json.Unmarshal(responseBody, ©Response) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal response body") + } + + copyJson, err := json.MarshalIndent(copyResponse, "", " ") + if err != nil { + log.Error().Err(err).Msg("failed to marshal copyResponse") + } + + if response.StatusCode != 200 { + factory.HttpError(response.StatusCode, responseBody, log) + } else { + fmt.Println(string(copyJson)) + } +} diff --git a/pkg/cmd/content/start-deletion/start-deletion.go b/pkg/cmd/content/start-deletion/start-deletion.go new file mode 100644 index 00000000..ffd71b43 --- /dev/null +++ b/pkg/cmd/content/start-deletion/start-deletion.go @@ -0,0 +1,66 @@ +package start_deletion + +import ( + "encoding/json" + "fmt" + "github.com/spf13/cobra" + "github.com/wizedkyle/sumocli/api" + "github.com/wizedkyle/sumocli/pkg/cmd/factory" + "github.com/wizedkyle/sumocli/pkg/logging" + "io" +) + +func NewCmdStartDeletion() *cobra.Command { + var ( + id string + isAdminMode bool + ) + + cmd := &cobra.Command{ + Use: "start-deletion", + Short: "Start an asynchronous content deletion job with the given identifier.", + Run: func(cmd *cobra.Command, args []string) { + startDeletion(id, isAdminMode) + }, + } + cmd.Flags().StringVar(&id, "id", "", "Specify the id of the content to delete") + cmd.Flags().BoolVar(&isAdminMode, "isAdminMode", false, "Set to true if you want to perform the request as a content administrator") + cmd.MarkFlagRequired("id") + return cmd +} + +func startDeletion(id string, isAdminMode bool) { + var deletionResponse api.StartExportResponse + log := logging.GetConsoleLogger() + requestUrl := "v2/content/" + id + "/delete" + client, request := factory.NewHttpRequest("DELETE", requestUrl) + if isAdminMode == true { + request.Header.Add("isAdminMode", "true") + } + response, err := client.Do(request) + if err != nil { + log.Error().Err(err).Msg("failed to make http request to " + requestUrl) + } + + defer response.Body.Close() + responseBody, err := io.ReadAll(response.Body) + if err != nil { + log.Error().Err(err).Msg("failed to read response body") + } + + err = json.Unmarshal(responseBody, &deletionResponse) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal response body") + } + + exportJson, err := json.MarshalIndent(deletionResponse, "", " ") + if err != nil { + log.Error().Err(err).Msg("failed to marshal exportResponse") + } + + if response.StatusCode != 200 { + factory.HttpError(response.StatusCode, responseBody, log) + } else { + fmt.Println(string(exportJson)) + } +} diff --git a/pkg/cmd/content/start-export/start-export.go b/pkg/cmd/content/start-export/start-export.go index de996cab..a169674c 100644 --- a/pkg/cmd/content/start-export/start-export.go +++ b/pkg/cmd/content/start-export/start-export.go @@ -11,7 +11,10 @@ import ( ) func NewCmdStartExport() *cobra.Command { - var id string + var ( + id string + isAdminMode bool + ) cmd := &cobra.Command{ Use: "start-export", @@ -19,19 +22,23 @@ func NewCmdStartExport() *cobra.Command { "which can be used with the sumocli content export-status command." + "If the content is a folder everything under that folder is exported recursively.", Run: func(cmd *cobra.Command, args []string) { - startExport(id) + startExport(id, isAdminMode) }, } cmd.Flags().StringVar(&id, "id", "", "Specify the id of the content item to export") + cmd.Flags().BoolVar(&isAdminMode, "isAdminMode", false, "Set to true if you want to perform the request as a content administrator") cmd.MarkFlagRequired("id") return cmd } -func startExport(id string) { +func startExport(id string, isAdminMode bool) { var exportResponse api.StartExportResponse log := logging.GetConsoleLogger() requestUrl := "v2/content/" + id + "/export" client, request := factory.NewHttpRequest("POST", requestUrl) + if isAdminMode == true { + request.Header.Add("isAdminMode", "true") + } response, err := client.Do(request) if err != nil { log.Error().Err(err).Msg("failed to make http request to " + requestUrl) diff --git a/pkg/cmd/content/start-import/start-import.go b/pkg/cmd/content/start-import/start-import.go index 33bf2c50..3a81acb8 100644 --- a/pkg/cmd/content/start-import/start-import.go +++ b/pkg/cmd/content/start-import/start-import.go @@ -2,9 +2,13 @@ package start_import import ( "encoding/json" + "fmt" "github.com/spf13/cobra" "github.com/wizedkyle/sumocli/api" + "github.com/wizedkyle/sumocli/pkg/cmd/factory" "github.com/wizedkyle/sumocli/pkg/logging" + "io" + "net/url" "os" ) @@ -33,19 +37,280 @@ func NewCmdStartImport() *cobra.Command { func startImport(file string, folderId string, isAdminMode bool, overwrite bool) { var responseType api.ResponseType + var responseId api.StartExportResponse + var responseStatusCode int + var responseBodyData []byte log := logging.GetConsoleLogger() + requestUrl := "v2/content/folders/" + folderId + "/import" fileData, err := os.ReadFile(file) if err != nil { log.Fatal().Err(err).Msg("failed to read file") } - err = json.Unmarshal(fileData, responseType) + err = json.Unmarshal(fileData, &responseType) if err != nil { log.Error().Err(err).Msg("failed to unmarshal file data") } + if responseType.Type == "FolderSyncDefinition" { + var folderSyncDefinition api.FolderSyncDefinition + err = json.Unmarshal(fileData, &folderSyncDefinition) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal file data") + } + requestBody, err := json.Marshal(folderSyncDefinition) + if err != nil { + log.Error().Err(err).Msg("failed to marshal request body") + } + client, request := factory.NewHttpRequestWithBody("POST", requestUrl, requestBody) + if isAdminMode == true { + request.Header.Add("isAdminMode", "true") + } + query := url.Values{} + if overwrite == true { + query.Add("overwrite", "true") + } else { + query.Add("overwrite", "false") + } + request.URL.RawQuery = query.Encode() + response, err := client.Do(request) + if err != nil { + log.Error().Err(err).Msg("failed to make http request to ") + } + defer response.Body.Close() + responseBody, err := io.ReadAll(response.Body) + responseBodyData = responseBody + if err != nil { + log.Error().Err(err).Msg("failed to read response body") + } + err = json.Unmarshal(responseBody, &responseId) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal response body") + } + responseStatusCode = response.StatusCode + } else if responseType.Type == "DashboardSyncDefinition" { + var dashboardSyncDefinition api.DashboardSyncDefinition + err = json.Unmarshal(fileData, &dashboardSyncDefinition) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal file data") + } + requestBody, err := json.Marshal(dashboardSyncDefinition) + if err != nil { + log.Error().Err(err).Msg("failed to marshal request body") + } + client, request := factory.NewHttpRequestWithBody("POST", requestUrl, requestBody) + if isAdminMode == true { + request.Header.Add("isAdminMode", "true") + } + query := url.Values{} + if overwrite == true { + query.Add("overwrite", "true") + } else { + query.Add("overwrite", "false") + } + request.URL.RawQuery = query.Encode() + response, err := client.Do(request) + if err != nil { + log.Error().Err(err).Msg("failed to make http request to ") + } + defer response.Body.Close() + responseBody, err := io.ReadAll(response.Body) + responseBodyData = responseBody + if err != nil { + log.Error().Err(err).Msg("failed to read response body") + } + err = json.Unmarshal(responseBody, &responseId) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal response body") + } + responseStatusCode = response.StatusCode + } else if responseType.Type == "MewboardSyncDefinition" { + var mewboardSyncDefinition api.MewboardSyncDefinition + err = json.Unmarshal(fileData, &mewboardSyncDefinition) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal file data") + } + requestBody, err := json.Marshal(mewboardSyncDefinition) + if err != nil { + log.Error().Err(err).Msg("failed to marshal request body") + } + client, request := factory.NewHttpRequestWithBody("POST", requestUrl, requestBody) + if isAdminMode == true { + request.Header.Add("isAdminMode", "true") + } + query := url.Values{} + if overwrite == true { + query.Add("overwrite", "true") + } else { + query.Add("overwrite", "false") + } + request.URL.RawQuery = query.Encode() + response, err := client.Do(request) + if err != nil { + log.Error().Err(err).Msg("failed to make http request to ") + } + defer response.Body.Close() + responseBody, err := io.ReadAll(response.Body) + responseBodyData = responseBody + if err != nil { + log.Error().Err(err).Msg("failed to read response body") + } + err = json.Unmarshal(responseBody, &responseId) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal response body") + } + responseStatusCode = response.StatusCode + } else if responseType.Type == "SavedSearchWithScheduleSyncDefinition" { + var savedSearchWithScheduleSyncDefinition api.SavedSearchWithScheduleSyncDefinition + err = json.Unmarshal(fileData, &savedSearchWithScheduleSyncDefinition) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal file data") + } + requestBody, err := json.Marshal(savedSearchWithScheduleSyncDefinition) + if err != nil { + log.Error().Err(err).Msg("failed to marshal request body") + } + client, request := factory.NewHttpRequestWithBody("POST", requestUrl, requestBody) + if isAdminMode == true { + request.Header.Add("isAdminMode", "true") + } + query := url.Values{} + if overwrite == true { + query.Add("overwrite", "true") + } else { + query.Add("overwrite", "false") + } + request.URL.RawQuery = query.Encode() + response, err := client.Do(request) + if err != nil { + log.Error().Err(err).Msg("failed to make http request to ") + } + defer response.Body.Close() + responseBody, err := io.ReadAll(response.Body) + responseBodyData = responseBody + if err != nil { + log.Error().Err(err).Msg("failed to read response body") + } + err = json.Unmarshal(responseBody, &responseId) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal response body") + } + responseStatusCode = response.StatusCode + } else if responseType.Type == "MetricsSavedSearchSyncDefinition" { + var metricsSavedSearchSyncDefinition api.MetricsSavedSearchSyncDefinition + err = json.Unmarshal(fileData, &metricsSavedSearchSyncDefinition) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal file data") + } + requestBody, err := json.Marshal(metricsSavedSearchSyncDefinition) + if err != nil { + log.Error().Err(err).Msg("failed to marshal request body") + } + client, request := factory.NewHttpRequestWithBody("POST", requestUrl, requestBody) + if isAdminMode == true { + request.Header.Add("isAdminMode", "true") + } + query := url.Values{} + if overwrite == true { + query.Add("overwrite", "true") + } else { + query.Add("overwrite", "false") + } + request.URL.RawQuery = query.Encode() + response, err := client.Do(request) + if err != nil { + log.Error().Err(err).Msg("failed to make http request to ") + } + defer response.Body.Close() + responseBody, err := io.ReadAll(response.Body) + responseBodyData = responseBody + if err != nil { + log.Error().Err(err).Msg("failed to read response body") + } + err = json.Unmarshal(responseBody, &responseId) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal response body") + } + responseStatusCode = response.StatusCode + } else if responseType.Type == "MetricsSearchSyncDefinition" { + var metricsSearchSyncDefinition api.MetricsSearchSyncDefinition + err = json.Unmarshal(fileData, &metricsSearchSyncDefinition) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal file data") + } + requestBody, err := json.Marshal(metricsSearchSyncDefinition) + if err != nil { + log.Error().Err(err).Msg("failed to marshal request body") + } + client, request := factory.NewHttpRequestWithBody("POST", requestUrl, requestBody) + if isAdminMode == true { + request.Header.Add("isAdminMode", "true") + } + query := url.Values{} + if overwrite == true { + query.Add("overwrite", "true") + } else { + query.Add("overwrite", "false") + } + request.URL.RawQuery = query.Encode() + response, err := client.Do(request) + if err != nil { + log.Error().Err(err).Msg("failed to make http request to ") + } + defer response.Body.Close() + responseBody, err := io.ReadAll(response.Body) + responseBodyData = responseBody + if err != nil { + log.Error().Err(err).Msg("failed to read response body") + } + err = json.Unmarshal(responseBody, &responseId) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal response body") + } + responseStatusCode = response.StatusCode + } else if responseType.Type == "LookupTableSyncDefinition" { + var lookupTableSyncDefinition api.LookupTableSyncDefinition + err = json.Unmarshal(fileData, &lookupTableSyncDefinition) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal file data") + } + requestBody, err := json.Marshal(lookupTableSyncDefinition) + if err != nil { + log.Error().Err(err).Msg("failed to marshal request body") + } + client, request := factory.NewHttpRequestWithBody("POST", requestUrl, requestBody) + if isAdminMode == true { + request.Header.Add("isAdminMode", "true") + } + query := url.Values{} + if overwrite == true { + query.Add("overwrite", "true") + } else { + query.Add("overwrite", "false") + } + request.URL.RawQuery = query.Encode() + response, err := client.Do(request) + if err != nil { + log.Error().Err(err).Msg("failed to make http request to ") + } + defer response.Body.Close() + responseBody, err := io.ReadAll(response.Body) + responseBodyData = responseBody + if err != nil { + log.Error().Err(err).Msg("failed to read response body") + } + err = json.Unmarshal(responseBody, &responseId) + if err != nil { + log.Error().Err(err).Msg("failed to unmarshal response body") + } + responseStatusCode = response.StatusCode + } - // Read file - // Unmarshall type and have if statements check which type it is - // create requestBodySchema - - //requestUrl := "v2/content/folders/" + folderId + "/import" + responseIdJson, err := json.MarshalIndent(responseId, "", " ") + if err != nil { + log.Error().Err(err).Msg("failed to marshal responseId") + } + if responseStatusCode != 200 { + factory.HttpError(responseStatusCode, responseBodyData, log) + } else { + fmt.Println(string(responseIdJson)) + } }