Skip to content

Commit

Permalink
add first useable version of the api client
Browse files Browse the repository at this point in the history
  • Loading branch information
redradrat committed Oct 24, 2020
1 parent f9a6826 commit 0537092
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 71 deletions.
5 changes: 4 additions & 1 deletion cmd/removeRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ var removeCmd = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
PrintMsg("Removing repository...")
mod := repositories.RemoveRepository(args[0])
mod, err := repositories.RemoveRepository(args[0])
if err != nil {
PrintError("unable to get registry modifier: %s \n", err)
}
if err := repositories.UpdateRegistry(mod); err != nil {
PrintError("unable to update repository registry: %s \n", err)
}
Expand Down
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ require (
github.com/coreos/etcd v3.3.10+incompatible
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
github.com/facebookgo/grace v0.0.0-20180706040059-75cf19382434 // indirect
github.com/facebookgo/httpdown v0.0.0-20180706035922-5979d39b15c2 // indirect
github.com/facebookgo/stats v0.0.0-20151006221625-1b76add642e4 // indirect
github.com/fatih/color v1.9.0
github.com/fatih/structs v1.1.0
github.com/go-git/go-git/v5 v5.1.0
Expand Down
14 changes: 0 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evanphx/json-patch v4.9.0+incompatible h1:kLcOMZeuLAJvL2BPWLMIj5oaZQobrkAqrL+WFZwQses=
github.com/evanphx/json-patch v4.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a h1:yDWHCSQ40h88yih2JAcL6Ls/kVkSE8GFACTGVnMPruw=
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a/go.mod h1:7Ga40egUymuWXxAe151lTNnCv97MddSOVsjpPPkityA=
github.com/facebookgo/grace v0.0.0-20180706040059-75cf19382434 h1:mOp33BLbcbJ8fvTAmZacbBiOASfxN+MLcLxymZCIrGE=
github.com/facebookgo/grace v0.0.0-20180706040059-75cf19382434/go.mod h1:KigFdumBXUPSwzLDbeuzyt0elrL7+CP7TKuhrhT4bcU=
github.com/facebookgo/httpdown v0.0.0-20180706035922-5979d39b15c2 h1:nXeeRHmgNgjLxi+7dY9l9aDvSS1uwVlNLqUWIY4Ath0=
github.com/facebookgo/httpdown v0.0.0-20180706035922-5979d39b15c2/go.mod h1:TUV/fX3XrTtBQb5+ttSUJzcFgLNpILONFTKmBuk5RSw=
github.com/facebookgo/stats v0.0.0-20151006221625-1b76add642e4 h1:0YtRCqIZs2+Tz49QuH6cJVw/IFqzo39gEqZ0iYLxD2M=
github.com/facebookgo/stats v0.0.0-20151006221625-1b76add642e4/go.mod h1:vsJz7uE339KUCpBXx3JAJzSRH7Uk4iGGyJzR529qDIA=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
Expand Down Expand Up @@ -273,12 +265,6 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k=
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
1 change: 1 addition & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func RegisterHandlersV1(e *echo.Group, serv *Serv) {
e.GET(RepositoriesApiPath, serv.GetRepositories)
e.GET(RepositoriesApiPath+"/:id", serv.GetRepository)
e.PUT(RepositoriesApiPath+"/:id", serv.PutRepository)
e.DELETE(RepositoriesApiPath+"/:id", serv.DeleteRepository)
e.GET(RepositoriesApiPath+"/:id"+ConceptsApiPath, serv.GetRepositoryConcepts)
e.GET(RepositoriesApiPath+"/:id"+ConceptsApiPath+"/:path", serv.GetRepositoryConcept)
}
Expand Down
70 changes: 55 additions & 15 deletions pkg/api/client/v1/concepts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,50 +38,90 @@ func TestConceptsClient_List_None(t *testing.T) {
assert.Equal(t, api.NewConceptsPayload(), *concepts)
}

func TestConceptsClient_List_Some(t *testing.T) {
client, err := addDemoHttps(t)
func TestConceptsClient_List_All(t *testing.T) {
removeMod, client, err := addDemoHttps(t)
assert.NoError(t, err)

concepts, response, err := client.Concepts.List(context.Background(), nil)
assert.NoError(t, err)
assert.Equal(t, 200, response.StatusCode)
act := api.ConceptsPayload{Concepts: api.ConceptsMapPayload{"apps/grafana@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Ralph Kühnert", MaintainerEmail: "[email protected]"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}, "apps/sentry@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Ralph Kühnert", MaintainerEmail: "[email protected]"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}}}
assert.Equal(t, act, *concepts)
exp := api.ConceptsPayload{Concepts: api.ConceptsMapPayload{"e2e-test/testconcept1@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Demo Maintainer", MaintainerEmail: "[email protected]"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}, "e2e-test/testconcept2@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Demo Maintainer", MaintainerEmail: "[email protected]"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}}}
assert.Equal(t, exp, *concepts)

assert.NoError(t, repositories.UpdateRegistry(removeMod))
}

func TestConceptsClient_ListFromRepository_Some(t *testing.T) {
client, err := addDemoHttps(t)
func TestConceptsClient_ListFromRepository_All(t *testing.T) {
removeMod, client, err := addDemoHttps(t)
assert.NoError(t, err)

concepts, response, err := client.Concepts.ListFromRepository(context.Background(), repositories.DemoHttpsRepository.Name, nil)
assert.NoError(t, err)
assert.Equal(t, 200, response.StatusCode)
act := api.ConceptsPayload{Concepts: api.ConceptsMapPayload{"apps/grafana@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Ralph Kühnert", MaintainerEmail: "[email protected]"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}, "apps/sentry@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Ralph Kühnert", MaintainerEmail: "[email protected]"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}}}
assert.Equal(t, act, *concepts)
exp := api.ConceptsPayload{Concepts: api.ConceptsMapPayload{"e2e-test/testconcept1@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Demo Maintainer", MaintainerEmail: "[email protected]"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}, "e2e-test/testconcept2@demo-https": api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Demo Maintainer", MaintainerEmail: "[email protected]"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}}}
assert.Equal(t, exp, *concepts)

concepts, response, err = client.Concepts.ListFromRepository(context.Background(), "dummyname", nil)
assert.Error(t, err)
assert.Equal(t, 404, response.StatusCode)

assert.NoError(t, repositories.UpdateRegistry(removeMod))
}

func TestConceptsClient_GetFromRepository_All(t *testing.T) {
removeMod, client, err := addDemoHttps(t)
assert.NoError(t, err)

concepts, response, err := client.Concepts.GetFromRepository(context.Background(), repositories.DemoHttpsRepository.Name, "e2e-test/testconcept1")
assert.NoError(t, err)
assert.Equal(t, 200, response.StatusCode)
exp := api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Demo Maintainer", MaintainerEmail: "[email protected]"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}
assert.Equal(t, exp, *concepts)

concepts, response, err = client.Concepts.GetFromRepository(context.Background(), repositories.DemoHttpsRepository.Name, "e2e-test/testconcept")
assert.Error(t, err)
assert.Equal(t, 404, response.StatusCode)

concepts, response, err = client.Concepts.GetFromRepository(context.Background(), "dummyname", "e2e-test/testconcept1")
assert.Error(t, err)
assert.Equal(t, 404, response.StatusCode)

assert.NoError(t, repositories.UpdateRegistry(removeMod))
}

func TestConceptsClient_GetFromRepository_Some(t *testing.T) {
client, err := addDemoHttps(t)
func TestConceptsClient_Get(t *testing.T) {
removeMod, client, err := addDemoHttps(t)
assert.NoError(t, err)

concepts, response, err := client.Concepts.GetFromRepository(context.Background(), repositories.DemoHttpsRepository.Name, "apps/sentry")
concepts, response, err := client.Concepts.Get(context.Background(), "e2e-test/testconcept1@demo-https")
assert.NoError(t, err)
assert.Equal(t, 200, response.StatusCode)
act := api.ConceptPayload(api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Ralph Kühnert", MaintainerEmail: "[email protected]"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}})
assert.Equal(t, act, *concepts)
exp := api.ConceptPayload{Type: "jsonnet", Metadata: api.ConceptMetadataPayload{Maintainer: api.ConceptMaintainerPayload{MaintainerName: "Demo Maintainer", MaintainerEmail: "[email protected]"}, Tags: []string(nil)}, Inputs: []api.ConceptInputsPayload{api.ConceptInputsPayload{ID: "instanceName", Type: "string", Mandatory: true}, api.ConceptInputsPayload{ID: "nameSelection", Type: "select", Mandatory: true}}}
assert.Equal(t, exp, *concepts)

concepts, response, err = client.Concepts.Get(context.Background(), "e2e-test/testconcept@demo-https")
assert.Error(t, err)
assert.Equal(t, 404, response.StatusCode)

concepts, response, err = client.Concepts.Get(nil, "e2e-test/testconcept@demo-https")
assert.Error(t, err)

assert.NoError(t, repositories.UpdateRegistry(removeMod))
}

/////////////
// HELPERS //
/////////////

func addDemoHttps(t *testing.T) (*Client, error) {
func addDemoHttps(t *testing.T) (repositories.RegistryModification, *Client, error) {
viper.Set(repositories.StoreKey, repositories.MockStoreConfigMap().Map())
client := NewClient(nil, &uri)

// Add repo to mock store
addMod, err := repositories.AddRepository(repositories.DemoHttpsRepository)
assert.NoError(t, err)
assert.NoError(t, repositories.UpdateRegistry(addMod))
return client, err
removeMod, err := repositories.RemoveRepository(repositories.DemoHttpsRepository.Name)
assert.NoError(t, err)
return removeMod, client, err
}
125 changes: 103 additions & 22 deletions pkg/api/client/v1/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,129 @@ package v1

import (
"context"
"net/http"
"net/url"

"github.com/google/go-querystring/query"
"github.com/redradrat/kable/pkg/api"
"github.com/redradrat/kable/pkg/concepts"
)

const repositoriessBasePath = "v1/repositories"

type RepositoriesService interface {
List(context.Context, *ListOptions) (api.ConceptsPayload, *Response, error)
ListByTag(context.Context, string, *ListOptions) (api.ConceptsPayload, *Response, error)
Get(context.Context, concepts.ConceptIdentifier) (api.ConceptPayload, *Response, error)
GetFromRepository(context.Context, string, string) (api.ConceptPayload, *Response, error)
Delete(context.Context, int) (*Response, error)
DeleteByTag(context.Context, string) (*Response, error)
List(ctx context.Context, options *ListOptions) (*api.RepositoriesPayload, *Response, error)
Get(ctx context.Context, name string) (*api.RepositoryPayload, *Response, error)
Put(ctx context.Context, name, giturl, gitref string) (*Response, error)
Delete(ctx context.Context, name string) (*Response, error)
}

var _ RepositoriesService = &RepositoriesClient{}

type RepositoriesClient struct {
*Client
client *Client
}

func (r RepositoriesClient) List(ctx context.Context, options *ListOptions) (api.ConceptsPayload, *Response, error) {
panic("implement me")
}
func (r RepositoriesClient) List(ctx context.Context, options *ListOptions) (*api.RepositoriesPayload, *Response, error) {
uri, err := url.Parse(repositoriessBasePath)
if err != nil {
return nil, nil, err
}

func (r RepositoriesClient) ListByTag(ctx context.Context, s string, options *ListOptions) (api.ConceptsPayload, *Response, error) {
panic("implement me")
}
vals, err := query.Values(options)
if err != nil {
return nil, nil, err
}

uri.RawQuery = vals.Encode()

req, err := r.client.NewRequest(http.MethodGet, uri.String(), nil)
if err != nil {
return nil, nil, err
}

payload := api.NewRepositoriesPayload()
resp, err := r.client.Do(ctx, req, &payload)
if err != nil {
return nil, resp, err
}

func (r RepositoriesClient) Get(ctx context.Context, identifier concepts.ConceptIdentifier) (api.ConceptPayload, *Response, error) {
panic("implement me")
return &payload, resp, err
}

func (r RepositoriesClient) GetFromRepository(ctx context.Context, s string, s2 string) (api.ConceptPayload, *Response, error) {
panic("implement me")
func (r RepositoriesClient) Get(ctx context.Context, name string) (*api.RepositoryPayload, *Response, error) {
uri, err := url.Parse(repositoriessBasePath + "/")
if err != nil {
return nil, nil, err
}

getUri, err := uri.Parse(name)
if err != nil {
return nil, nil, err
}

req, err := r.client.NewRequest(http.MethodGet, getUri.String(), nil)
if err != nil {
return nil, nil, err
}

payload := api.RepositoryPayload{}
resp, err := r.client.Do(ctx, req, &payload)
if err != nil {
return nil, resp, err
}

return &payload, resp, err
}

func (r RepositoriesClient) Delete(ctx context.Context, i int) (*Response, error) {
panic("implement me")
func (r RepositoriesClient) Delete(ctx context.Context, name string) (*Response, error) {
uri, err := url.Parse(repositoriessBasePath + "/")
if err != nil {
return nil, err
}

getUri, err := uri.Parse(name)
if err != nil {
return nil, err
}

req, err := r.client.NewRequest(http.MethodDelete, getUri.String(), nil)
if err != nil {
return nil, err
}

resp, err := r.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}

return resp, err
}

func (r RepositoriesClient) DeleteByTag(ctx context.Context, s string) (*Response, error) {
panic("implement me")
func (r RepositoriesClient) Put(ctx context.Context, name, giturl, gitref string) (*Response, error) {
uri, err := url.Parse(repositoriessBasePath + "/")
if err != nil {
return nil, err
}

getUri, err := uri.Parse(name)
if err != nil {
return nil, err
}

payload := api.RepositoryPayload{
URL: giturl,
GitRef: gitref,
}

req, err := r.client.NewRequest(http.MethodPut, getUri.String(), payload)
if err != nil {
return nil, err
}

resp, err := r.client.Do(ctx, req, nil)
if err != nil {
return resp, err
}

return resp, err
}
Loading

0 comments on commit 0537092

Please sign in to comment.