diff --git a/v2/internal/runner/cloud.go b/v2/internal/runner/cloud.go index 5d8c46ef2..d1c90aaf1 100644 --- a/v2/internal/runner/cloud.go +++ b/v2/internal/runner/cloud.go @@ -98,12 +98,14 @@ func (r *Runner) listTemplates() error { } func (r *Runner) removeDatasource(datasource string) error { - ID, _ := strconv.ParseInt(datasource, 10, 64) + var source string + ID, parseErr := strconv.ParseInt(datasource, 10, 64) + if parseErr != nil { + source = datasource + } - err := r.cloudClient.RemoveDatasource(ID) - if err != nil { - gologger.Error().Msgf("Error in deleting datasource %s: %s", datasource, err) - } else { + err := r.cloudClient.RemoveDatasource(ID, source) + if err == nil { gologger.Info().Msgf("Datasource deleted %s", datasource) } return err @@ -189,7 +191,6 @@ func (r *Runner) initializeCloudDataSources() error { } func (r *Runner) processDataSourceItem(repo, token, Type string) (int64, error) { - var secret string ID, err := r.cloudClient.StatusDataSource(nucleicloud.StatusDataSourceRequest{Repo: repo, Token: token}) if err != nil { if !strings.Contains(err.Error(), "no rows in result set") { @@ -205,17 +206,16 @@ func (r *Runner) processDataSourceItem(repo, token, Type string) (int64, error) if err = r.cloudClient.SyncDataSource(resp.ID); err != nil { return 0, errors.Wrap(err, "could not sync data source") } - if secret != "" { + if resp.Secret != "" { gologger.Info().Msgf("Webhook URL for added source: %s/datasources/%s/webhook", r.options.CloudURL, resp.Hash) - gologger.Info().Msgf("Secret for webhook: %s", secret) + gologger.Info().Msgf("Secret for webhook: %s", resp.Secret) } } if r.options.UpdateTemplates { - gologger.Info().Msgf("Syncing data source: %s (%s)\n", repo, ID) + gologger.Info().Msgf("Syncing data source: %s (%d)\n", repo, ID) if err = r.cloudClient.SyncDataSource(ID); err != nil { return 0, errors.Wrap(err, "could not sync data source") } } - gologger.Info().Msgf("Got connected data source: %s\n", ID) return ID, nil } diff --git a/v2/internal/runner/nucleicloud/cloud.go b/v2/internal/runner/nucleicloud/cloud.go index 1b5befa1d..a8d919857 100644 --- a/v2/internal/runner/nucleicloud/cloud.go +++ b/v2/internal/runner/nucleicloud/cloud.go @@ -10,6 +10,7 @@ import ( "net/url" "os" "path/filepath" + "strconv" "strings" "time" @@ -175,12 +176,11 @@ func (c *Client) StatusDataSource(statusRequest StatusDataSourceRequest) (int64, } defer resp.Body.Close() - var data map[string]interface{} + var data StatusDataSourceResponse if err := jsoniter.NewDecoder(resp.Body).Decode(&data); err != nil { return 0, errors.Wrap(err, "could not decode resp") } - id := data["id"].(int64) - return id, nil + return data.ID, nil } // AddDataSource adds a new data source @@ -315,8 +315,20 @@ func (c *Client) ListTemplates(query string) ([]GetTemplatesResponse, error) { return items, nil } -func (c *Client) RemoveDatasource(datasource int64) error { - httpReq, err := retryablehttp.NewRequest(http.MethodDelete, fmt.Sprintf("%s/datasources/%d", c.baseURL, datasource), nil) +func (c *Client) RemoveDatasource(datasource int64, name string) error { + var builder strings.Builder + _, _ = builder.WriteString(c.baseURL) + _, _ = builder.WriteString("/datasources") + + if name != "" { + _, _ = builder.WriteString("?name=") + _, _ = builder.WriteString(name) + } else if datasource != 0 { + _, _ = builder.WriteString("?id=") + _, _ = builder.WriteString(strconv.FormatInt(datasource, 10)) + } + + httpReq, err := retryablehttp.NewRequest(http.MethodDelete, builder.String(), nil) if err != nil { return errors.Wrap(err, "could not make request") } diff --git a/v2/internal/runner/nucleicloud/types.go b/v2/internal/runner/nucleicloud/types.go index 1643983eb..0b7095e76 100644 --- a/v2/internal/runner/nucleicloud/types.go +++ b/v2/internal/runner/nucleicloud/types.go @@ -56,11 +56,15 @@ type DeleteScanResults struct { // StatusDataSourceRequest is a add data source request item. type StatusDataSourceRequest struct { - ID string `json:"id"` Repo string `json:"repo"` Token string `json:"token"` } +// StatusDataSourceResponse is a add data source response item. +type StatusDataSourceResponse struct { + ID int64 `json:"id"` +} + // AddDataSourceRequest is a add data source request item. type AddDataSourceRequest struct { Type string `json:"type"`