diff --git a/internal/client/source.go b/internal/client/source.go index c004c2692..d6123eaac 100644 --- a/internal/client/source.go +++ b/internal/client/source.go @@ -25,6 +25,20 @@ func ListSourcesV0(socket *http.Client) ([]string, *APIResponse, error) { return list.Sources, nil, nil } +// ListSourcesV1 returns a list of source ids +func ListSourcesV1(socket *http.Client) ([]string, *APIResponse, error) { + body, resp, err := GetRaw(socket, "GET", "/api/v1/projects/source/list") + if resp != nil || err != nil { + return nil, resp, err + } + var list weldr.SourceListV1 + err = json.Unmarshal(body, &list) + if err != nil { + return nil, nil, err + } + return list.Sources, nil, nil +} + // GetSourceInfoV0 returns detailed information on the named sources func GetSourceInfoV0(socket *http.Client, sourceNames string) (map[string]weldr.SourceConfigV0, *APIResponse, error) { body, resp, err := GetRaw(socket, "GET", "/api/v0/projects/source/info/"+sourceNames) diff --git a/internal/client/source_test.go b/internal/client/source_test.go index a38d78eaa..266c40c9d 100644 --- a/internal/client/source_test.go +++ b/internal/client/source_test.go @@ -128,7 +128,7 @@ func TestPOSTWrongTOMLSourceV0(t *testing.T) { require.False(t, resp.Status, "did not return an error") } -// list sources +// list sources using the v0 API func TestListSourcesV0(t *testing.T) { sources := []string{`{ "name": "package-repo-1", @@ -174,6 +174,54 @@ func TestListSourcesV0(t *testing.T) { require.Contains(t, list, "package-repo-2") } +// list sources using the v1 API +func TestListSourcesV1(t *testing.T) { + sources := []string{`{ + "name": "package-repo-1", + "url": "file://REPO-PATH", + "type": "yum-baseurl", + "proxy": "https://proxy-url/", + "check_ssl": true, + "check_gpg": true, + "gpgkey_urls": ["https://url/path/to/gpg-key"] + }`, + `{ + "name": "package-repo-2", + "url": "file://REPO-PATH", + "type": "yum-baseurl", + "proxy": "https://proxy-url/", + "check_ssl": true, + "check_gpg": true, + "gpgkey_urls": ["https://url/path/to/gpg-key"] + }`} + + // TODO update for PostJSONSourceV1 + for i := range sources { + source := strings.Replace(sources[i], "REPO-PATH", testState.repoDir, 1) + resp, err := PostJSONSourceV0(testState.socket, source) + require.NoError(t, err, "POST source failed with a client error") + require.True(t, resp.Status, "POST source failed: %#v", resp) + } + + // TODO update for DeleteJSONSourceV1 + // Remove the test sources, ignoring any errors + defer func() { + for _, n := range []string{"package-repo-1", "package-repo-2"} { + resp, err := DeleteSourceV0(testState.socket, n) + require.NoError(t, err, "DELETE source failed with a client error") + require.True(t, resp.Status, "DELETE source failed: %#v", resp) + } + }() + + // Get the list of sources + list, api, err := ListSourcesV1(testState.socket) + require.NoError(t, err, "GET source failed with a client error") + require.Nil(t, api, "ListSources failed: %#v", api) + require.True(t, len(list) > 1, "Not enough sources returned") + require.Contains(t, list, "package-repo-1") + require.Contains(t, list, "package-repo-2") +} + // Get the source info func TestGetSourceInfoV0(t *testing.T) { source := ` diff --git a/internal/store/store.go b/internal/store/store.go index c2a473cbd..4125252b6 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -423,7 +423,23 @@ func (s *Store) DeleteSource(name string) { }) } -func (s *Store) ListSources() []string { +// ListSourcesByName returns the repo source names +// Name is different than Id, it can be a full description of the repo +func (s *Store) ListSourcesByName() []string { + s.mu.RLock() + defer s.mu.RUnlock() + names := make([]string, 0, len(s.sources)) + for _, source := range s.sources { + names = append(names, source.Name) + } + sort.Strings(names) + + return names +} + +// ListSourcesById returns the repo source id +// Id is a short identifier for the repo, not a full name description +func (s *Store) ListSourcesById() []string { s.mu.RLock() defer s.mu.RUnlock() names := make([]string, 0, len(s.sources)) diff --git a/internal/weldr/api.go b/internal/weldr/api.go index 4517adb74..409fa96e6 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -348,7 +348,14 @@ func (api *API) sourceListHandler(writer http.ResponseWriter, request *http.Requ Sources []string `json:"sources"` } - names := api.store.ListSources() + // The v0 API used the repo Name, a descriptive string, as the key + // In the v1 API this was changed to separate the Name and the Id (a short identifier) + var names []string + if isRequestVersionAtLeast(params, 1) { + names = api.store.ListSourcesById() + } else { + names = api.store.ListSourcesByName() + } for _, repo := range api.repos { names = append(names, repo.Name) diff --git a/internal/weldr/json.go b/internal/weldr/json.go index 81d7511fa..272fc02fa 100644 --- a/internal/weldr/json.go +++ b/internal/weldr/json.go @@ -85,6 +85,11 @@ type SourceListV0 struct { Sources []string `json:"sources"` } +// SourceListV1 is the response to /source/list request +type SourceListV1 struct { + Sources []string `json:"sources"` +} + // SourceInfoV0 is the response to a /source/info request type SourceInfoV0 struct { Sources map[string]SourceConfigV0 `json:"sources"`