Skip to content

Commit

Permalink
gitops cli templates namespace parameter (#1808)
Browse files Browse the repository at this point in the history
* Add namespace parameter to get templates in gitops cli

* Update http tests with namespace

Add default namespace value when not providede to cli

* Add default value to namespace in terraform template get

* Update default namespace to be in cmd flag declaration instead of when retrieving cmd values
  • Loading branch information
ranatrk authored Nov 3, 2022
1 parent 508b957 commit 0148ef7
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 35 deletions.
8 changes: 5 additions & 3 deletions cmd/gitops/app/get/templates/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type templateCommandFlags struct {
ListTemplateParameters bool
ListTemplateProfiles bool
Provider string
TemplateNamespace string
}

var flags templateCommandFlags
Expand Down Expand Up @@ -58,6 +59,7 @@ gitops get template <template-name> --list-parameters
cmd.Flags().BoolVar(&flags.ListTemplateParameters, "list-parameters", false, "Show parameters of CAPI template")
cmd.Flags().BoolVar(&flags.ListTemplateProfiles, "list-profiles", false, "Show profiles of CAPI template")
cmd.Flags().StringVar(&flags.Provider, "provider", "", fmt.Sprintf("Filter templates by provider. Supported providers: %s", strings.Join(providers, " ")))
cmd.Flags().StringVar(&flags.TemplateNamespace, "template-namespace", "default", "Filter templates by template namespace")

return cmd
}
Expand Down Expand Up @@ -91,15 +93,15 @@ func getTemplateCmdRunE(opts *config.Options, client *adapters.HTTPClient) func(
return errors.New("template name is required")
}

return templates.GetTemplateParameters(templates.CAPITemplateKind, args[0], client, w)
return templates.GetTemplateParameters(templates.CAPITemplateKind, args[0], flags.TemplateNamespace, client, w)
}

if flags.ListTemplateProfiles {
if len(args) == 0 {
return errors.New("template name is required")
}

return templates.GetTemplateProfiles(args[0], client, w)
return templates.GetTemplateProfiles(args[0], flags.TemplateNamespace, client, w)
}

if len(args) == 0 {
Expand All @@ -110,7 +112,7 @@ func getTemplateCmdRunE(opts *config.Options, client *adapters.HTTPClient) func(
return templates.GetTemplates(templates.CAPITemplateKind, client, w)
}

return templates.GetTemplate(args[0], templates.CAPITemplateKind, client, w)
return templates.GetTemplate(args[0], templates.CAPITemplateKind, flags.TemplateNamespace, client, w)
}
}

Expand Down
6 changes: 4 additions & 2 deletions cmd/gitops/app/get/templates/terraform/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

type templateCommandFlags struct {
ListTemplateParameters bool
TemplateNamespace string
}

var flags templateCommandFlags
Expand All @@ -39,6 +40,7 @@ gitops get template terraform <template-name> --list-parameters
}

cmd.Flags().BoolVar(&flags.ListTemplateParameters, "list-parameters", false, "Show parameters of Terraform template")
cmd.Flags().StringVar(&flags.TemplateNamespace, "template-namespace", "default", "Filter templates by template namespace")

return cmd
}
Expand Down Expand Up @@ -68,13 +70,13 @@ func getTerraformTemplateCmdRunE(opts *config.Options, client *adapters.HTTPClie
return errors.New("terraform template name is required")
}

return templates.GetTemplateParameters(templates.GitOpsTemplateKind, args[0], client, w)
return templates.GetTemplateParameters(templates.GitOpsTemplateKind, args[0], flags.TemplateNamespace, client, w)
}

if len(args) == 0 {
return templates.GetTemplates(templates.GitOpsTemplateKind, client, w)
}

return templates.GetTemplate(args[0], templates.GitOpsTemplateKind, client, w)
return templates.GetTemplate(args[0], templates.GitOpsTemplateKind, flags.TemplateNamespace, client, w)
}
}
15 changes: 10 additions & 5 deletions cmd/gitops/pkg/adapters/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (c *HTTPClient) RetrieveTemplates(kind templates.TemplateKind) ([]templates
}

// RetrieveTemplate returns a template from the cluster service.
func (c *HTTPClient) RetrieveTemplate(name string, kind templates.TemplateKind) (*templates.Template, error) {
func (c *HTTPClient) RetrieveTemplate(name string, kind templates.TemplateKind, namespace string) (*templates.Template, error) {
endpoint := "v1/templates/{template_name}"

type GetTemplateResponse struct {
Expand All @@ -245,7 +245,8 @@ func (c *HTTPClient) RetrieveTemplate(name string, kind templates.TemplateKind)
"template_name": name,
}).
SetQueryParams(map[string]string{
"template_kind": kind.String(),
"template_kind": kind.String(),
"template_namespace": namespace,
}).
SetResult(&template).
Get(endpoint)
Expand Down Expand Up @@ -302,7 +303,7 @@ func (c *HTTPClient) RetrieveTemplatesByProvider(kind templates.TemplateKind, pr

// RetrieveTemplateParameters returns the list of all parameters of the
// specified template.
func (c *HTTPClient) RetrieveTemplateParameters(kind templates.TemplateKind, name string) ([]templates.TemplateParameter, error) {
func (c *HTTPClient) RetrieveTemplateParameters(kind templates.TemplateKind, name string, namespace string) ([]templates.TemplateParameter, error) {
endpoint := "v1/templates/{template_name}/params"

type ListTemplateParametersResponse struct {
Expand All @@ -316,7 +317,8 @@ func (c *HTTPClient) RetrieveTemplateParameters(kind templates.TemplateKind, nam
"template_name": name,
}).
SetQueryParams(map[string]string{
"template_kind": kind.String(),
"template_kind": kind.String(),
"template_namespace": namespace,
}).
SetResult(&templateParametersList).
Get(endpoint)
Expand Down Expand Up @@ -672,7 +674,7 @@ func (c *HTTPClient) DeleteClusters(params clusters.DeleteClustersParams) (strin

// RetrieveTemplateProfiles returns the list of all profiles of the
// specified template.
func (c *HTTPClient) RetrieveTemplateProfiles(name string) ([]templates.Profile, error) {
func (c *HTTPClient) RetrieveTemplateProfiles(name string, namespace string) ([]templates.Profile, error) {
endpoint := "v1/templates/{name}/profiles"

type ListTemplatePResponse struct {
Expand All @@ -685,6 +687,9 @@ func (c *HTTPClient) RetrieveTemplateProfiles(name string) ([]templates.Profile,
SetPathParams(map[string]string{
"name": name,
}).
SetQueryParams(map[string]string{
"template_namespace": namespace,
}).
SetResult(&templateProfilesList).
Get(endpoint)

Expand Down
37 changes: 26 additions & 11 deletions cmd/gitops/pkg/adapters/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ func TestRetrieveTemplate(t *testing.T) {
tests := []struct {
name string
templateName string // this isn't actually used, but it's a nice to have
namespace string
responder httpmock.Responder
kind templates.TemplateKind
assertFunc func(t *testing.T, template *templates.Template, err error)
}{
{
name: "capi template returned",
templateName: "cluster-template",
namespace: "default",
kind: templates.CAPITemplateKind,
responder: httpmock.NewJsonResponderOrPanic(200, httpmock.File("./testdata/single_capi_template.json")),
assertFunc: func(t *testing.T, ts *templates.Template, err error) {
Expand All @@ -119,6 +121,7 @@ func TestRetrieveTemplate(t *testing.T) {
name: "terraform template returned",
kind: templates.GitOpsTemplateKind,
templateName: "terraform-template",
namespace: "default",
responder: httpmock.NewJsonResponderOrPanic(200, httpmock.File("./testdata/single_terraform_template.json")),
assertFunc: func(t *testing.T, ts *templates.Template, err error) {
assert.Equal(t, *ts, templates.Template{
Expand All @@ -131,28 +134,31 @@ func TestRetrieveTemplate(t *testing.T) {
{
name: "error returned for capi type",
templateName: "cluster-template",
namespace: "default",
kind: templates.CAPITemplateKind,
responder: httpmock.NewErrorResponder(errors.New("oops")),
assertFunc: func(t *testing.T, ts *templates.Template, err error) {
assert.EqualError(t, err, "unable to GET template from \"https://weave.works/api/v1/templates/cluster-template?template_kind=CAPITemplate\": Get \"https://weave.works/api/v1/templates/cluster-template?template_kind=CAPITemplate\": oops")
assert.EqualError(t, err, "unable to GET template from \"https://weave.works/api/v1/templates/cluster-template?template_kind=CAPITemplate&template_namespace=default\": Get \"https://weave.works/api/v1/templates/cluster-template?template_kind=CAPITemplate&template_namespace=default\": oops")
},
},
{
name: "error returned for gitops type",
templateName: "terraform-template",
namespace: "default",
kind: templates.GitOpsTemplateKind,
responder: httpmock.NewErrorResponder(errors.New("oops")),
assertFunc: func(t *testing.T, ts *templates.Template, err error) {
assert.EqualError(t, err, "unable to GET template from \"https://weave.works/api/v1/templates/terraform-template?template_kind=GitOpsTemplate\": Get \"https://weave.works/api/v1/templates/terraform-template?template_kind=GitOpsTemplate\": oops")
assert.EqualError(t, err, "unable to GET template from \"https://weave.works/api/v1/templates/terraform-template?template_kind=GitOpsTemplate&template_namespace=default\": Get \"https://weave.works/api/v1/templates/terraform-template?template_kind=GitOpsTemplate&template_namespace=default\": oops")
},
},
{
name: "unexpected status code",
templateName: "cluster-template",
namespace: "default",
kind: templates.CAPITemplateKind,
responder: httpmock.NewStringResponder(http.StatusBadRequest, ""),
assertFunc: func(t *testing.T, ts *templates.Template, err error) {
assert.EqualError(t, err, "response status for GET \"https://weave.works/api/v1/templates/cluster-template?template_kind=CAPITemplate\" was 400")
assert.EqualError(t, err, "response status for GET \"https://weave.works/api/v1/templates/cluster-template?template_kind=CAPITemplate&template_namespace=default\" was 400")
},
},
}
Expand All @@ -169,7 +175,7 @@ func TestRetrieveTemplate(t *testing.T) {

err := client.ConfigureClientWithOptions(opts, os.Stdout)
assert.NoError(t, err)
ts, err := client.RetrieveTemplate(tt.templateName, tt.kind)
ts, err := client.RetrieveTemplate(tt.templateName, tt.kind, tt.namespace)
tt.assertFunc(t, ts, err)
})
}
Expand Down Expand Up @@ -231,12 +237,14 @@ func TestRetrieveTemplatesByProvider(t *testing.T) {
func TestRetrieveTemplateParameters(t *testing.T) {
tests := []struct {
name string
namespace string
kind templates.TemplateKind
responder httpmock.Responder
assertFunc func(t *testing.T, templates []templates.TemplateParameter, err error)
}{
{
name: "template parameters returned for capi kind",
namespace: "default",
kind: templates.CAPITemplateKind,
responder: httpmock.NewJsonResponderOrPanic(200, httpmock.File("./testdata/template_parameters.json")),
assertFunc: func(t *testing.T, ts []templates.TemplateParameter, err error) {
Expand All @@ -251,6 +259,7 @@ func TestRetrieveTemplateParameters(t *testing.T) {
},
{
name: "template parameters returned for gitops kind",
namespace: "default",
kind: templates.GitOpsTemplateKind,
responder: httpmock.NewJsonResponderOrPanic(200, httpmock.File("./testdata/template_parameters.json")),
assertFunc: func(t *testing.T, ts []templates.TemplateParameter, err error) {
Expand All @@ -265,26 +274,29 @@ func TestRetrieveTemplateParameters(t *testing.T) {
},
{
name: "error returned for capi kind",
namespace: "default",
kind: templates.CAPITemplateKind,
responder: httpmock.NewErrorResponder(errors.New("oops")),
assertFunc: func(t *testing.T, ts []templates.TemplateParameter, err error) {
assert.EqualError(t, err, "unable to GET template parameters from \"https://weave.works/api/v1/templates/cluster-template/params?template_kind=CAPITemplate\": Get \"https://weave.works/api/v1/templates/cluster-template/params?template_kind=CAPITemplate\": oops")
assert.EqualError(t, err, "unable to GET template parameters from \"https://weave.works/api/v1/templates/cluster-template/params?template_kind=CAPITemplate&template_namespace=default\": Get \"https://weave.works/api/v1/templates/cluster-template/params?template_kind=CAPITemplate&template_namespace=default\": oops")
},
},
{
name: "error returned for gitops kind",
namespace: "default",
kind: templates.GitOpsTemplateKind,
responder: httpmock.NewErrorResponder(errors.New("oops")),
assertFunc: func(t *testing.T, ts []templates.TemplateParameter, err error) {
assert.EqualError(t, err, "unable to GET template parameters from \"https://weave.works/api/v1/templates/cluster-template/params?template_kind=GitOpsTemplate\": Get \"https://weave.works/api/v1/templates/cluster-template/params?template_kind=GitOpsTemplate\": oops")
assert.EqualError(t, err, "unable to GET template parameters from \"https://weave.works/api/v1/templates/cluster-template/params?template_kind=GitOpsTemplate&template_namespace=default\": Get \"https://weave.works/api/v1/templates/cluster-template/params?template_kind=GitOpsTemplate&template_namespace=default\": oops")
},
},
{
name: "unexpected status code",
namespace: "default",
kind: templates.CAPITemplateKind,
responder: httpmock.NewStringResponder(http.StatusBadRequest, ""),
assertFunc: func(t *testing.T, ts []templates.TemplateParameter, err error) {
assert.EqualError(t, err, "response status for GET \"https://weave.works/api/v1/templates/cluster-template/params?template_kind=CAPITemplate\" was 400")
assert.EqualError(t, err, "response status for GET \"https://weave.works/api/v1/templates/cluster-template/params?template_kind=CAPITemplate&template_namespace=default\" was 400")
},
},
}
Expand All @@ -297,11 +309,11 @@ func TestRetrieveTemplateParameters(t *testing.T) {
client := adapters.NewHTTPClient()
httpmock.ActivateNonDefault(client.GetBaseClient())
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("GET", testutils.BaseURI+"/v1/templates/cluster-template/params?template_kind="+tt.kind.String(), tt.responder)
httpmock.RegisterResponder("GET", testutils.BaseURI+"/v1/templates/cluster-template/params?template_kind="+tt.kind.String()+"&template_namespace="+tt.namespace, tt.responder)

err := client.ConfigureClientWithOptions(opts, os.Stdout)
assert.NoError(t, err)
ts, err := client.RetrieveTemplateParameters(tt.kind, "cluster-template")
ts, err := client.RetrieveTemplateParameters(tt.kind, "cluster-template", tt.namespace)
tt.assertFunc(t, ts, err)
})
}
Expand Down Expand Up @@ -803,11 +815,13 @@ func TestEntitlementExpiredHeader(t *testing.T) {
func TestRetrieveTemplateProfiles(t *testing.T) {
tests := []struct {
name string
namespace string
responder httpmock.Responder
assertFunc func(t *testing.T, profile []templates.Profile, err error)
}{
{
name: "template profiles returned",
namespace: "default",
responder: httpmock.NewJsonResponderOrPanic(200, httpmock.File("./testdata/template_profiles.json")),
assertFunc: func(t *testing.T, ts []templates.Profile, err error) {
assert.ElementsMatch(t, ts, []templates.Profile{
Expand Down Expand Up @@ -836,9 +850,10 @@ func TestRetrieveTemplateProfiles(t *testing.T) {
},
{
name: "error returned",
namespace: "default",
responder: httpmock.NewErrorResponder(errors.New("oops")),
assertFunc: func(t *testing.T, fs []templates.Profile, err error) {
assert.EqualError(t, err, "unable to GET template profiles from \"https://weave.works/api/v1/templates/cluster-template/profiles\": Get \"https://weave.works/api/v1/templates/cluster-template/profiles\": oops")
assert.EqualError(t, err, "unable to GET template profiles from \"https://weave.works/api/v1/templates/cluster-template/profiles?template_namespace=default\": Get \"https://weave.works/api/v1/templates/cluster-template/profiles?template_namespace=default\": oops")
},
},
}
Expand All @@ -855,7 +870,7 @@ func TestRetrieveTemplateProfiles(t *testing.T) {

err := client.ConfigureClientWithOptions(opts, os.Stdout)
assert.NoError(t, err)
tps, err := client.RetrieveTemplateProfiles("cluster-template")
tps, err := client.RetrieveTemplateProfiles("cluster-template", tt.namespace)
tt.assertFunc(t, tps, err)
})
}
Expand Down
18 changes: 9 additions & 9 deletions cmd/gitops/pkg/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ func CreatePullRequestFromTemplate(params CreatePullRequestFromTemplateParams, r
// need to implement in order to return an array of templates.
type TemplatesRetriever interface {
Source() string
RetrieveTemplate(name string, kind TemplateKind) (*Template, error)
RetrieveTemplate(name string, kind TemplateKind, namespace string) (*Template, error)
RetrieveTemplates(kind TemplateKind) ([]Template, error)
RetrieveTemplatesByProvider(kind TemplateKind, provider string) ([]Template, error)
RetrieveTemplateParameters(kind TemplateKind, name string) ([]TemplateParameter, error)
RetrieveTemplateProfiles(name string) ([]Profile, error)
RetrieveTemplateParameters(kind TemplateKind, name string, namespace string) ([]TemplateParameter, error)
RetrieveTemplateProfiles(name string, namespace string) ([]Profile, error)
}

// TemplateRenderer defines the interface that adapters
Expand Down Expand Up @@ -174,8 +174,8 @@ func (r *RenderTemplateResponse) String() string {
}

// GetTemplate uses a TemplatesRetriever adapter to show print template to the console.
func GetTemplate(name string, kind TemplateKind, r TemplatesRetriever, w io.Writer) error {
t, err := r.RetrieveTemplate(name, kind)
func GetTemplate(name string, kind TemplateKind, namespace string, r TemplatesRetriever, w io.Writer) error {
t, err := r.RetrieveTemplate(name, kind, namespace)
if err != nil {
return fmt.Errorf("unable to retrieve templates from %q: %w", r.Source(), err)
}
Expand Down Expand Up @@ -247,8 +247,8 @@ func GetTemplatesByProvider(kind TemplateKind, provider string, r TemplatesRetri

// GetTemplateParameters uses a TemplatesRetriever adapter
// to show a list of parameters for a given template.
func GetTemplateParameters(kind TemplateKind, name string, r TemplatesRetriever, w io.Writer) error {
ps, err := r.RetrieveTemplateParameters(kind, name)
func GetTemplateParameters(kind TemplateKind, name string, namespace string, r TemplatesRetriever, w io.Writer) error {
ps, err := r.RetrieveTemplateParameters(kind, name, namespace)
if err != nil {
return fmt.Errorf("unable to retrieve parameters for template %q from %q: %w", name, r.Source(), err)
}
Expand Down Expand Up @@ -327,8 +327,8 @@ func GetCredentials(r CredentialsRetriever, w io.Writer) error {

// GetTemplateProfiles uses a TemplatesRetriever adapter
// to show a list of profiles for a given template.
func GetTemplateProfiles(name string, r TemplatesRetriever, w io.Writer) error {
ps, err := r.RetrieveTemplateProfiles(name)
func GetTemplateProfiles(name string, namespace string, r TemplatesRetriever, w io.Writer) error {
ps, err := r.RetrieveTemplateProfiles(name, namespace)
if err != nil {
return fmt.Errorf("unable to retrieve profiles for template %q from %q: %w", name, r.Source(), err)
}
Expand Down
Loading

0 comments on commit 0148ef7

Please sign in to comment.