weldr: Add API v1 support to source listing

This is the first patch in a series to add APIv1 support to the
/projects/source routes. The change involves using the store.Sources key
in a different way (as an id instead of as a duplicate of the struct's
Name field) but does not actually involve changing the Sources json in
the store.

In the V0 API the name of the source was used as the identifier, and
there was no short id. In V1 the source is identified by the API using
a short id, and the Name is just a field in the struct to describe the
source. This will become more obvious with the /projects/source/info
response.

This commit changes the following:

Changes store.ListSources to ListSourcesByName and explicitly pulls the
name from the source struct instead of the key. v0 will use this
function call.

Adds store.ListSourcesById which returns the source key as the
identifier. This is used by v1.

Adds a new weldr.SourcesListV1 response type, even though it is exactly
the same as the V1 response in this specific case. I thought it would be
better to have one called V1 than to reuse the V0 struct and possibly
confuse people.

The /projects/source/list API now lists the sources by name for v0 and id for v1.

A test has been added. You will notice it still uses v0 to push and
delete the sources. These will be updated when the new version of the
functions are added in subsequent commits.
This commit is contained in:
Brian C. Lane 2020-04-29 11:22:06 -07:00 committed by Tom Gundersen
parent 1c9ded8e53
commit 55325a8549
5 changed files with 93 additions and 3 deletions

View file

@ -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)

View file

@ -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"`