diff --git a/cmd/gitops/app/get/templates/cmd.go b/cmd/gitops/app/get/templates/cmd.go index 82551e207..9dc47d43a 100644 --- a/cmd/gitops/app/get/templates/cmd.go +++ b/cmd/gitops/app/get/templates/cmd.go @@ -19,6 +19,7 @@ type templateCommandFlags struct { ListTemplateParameters bool ListTemplateProfiles bool Provider string + TemplateNamespace string } var flags templateCommandFlags @@ -58,6 +59,7 @@ gitops get template --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 } @@ -91,7 +93,7 @@ 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 { @@ -99,7 +101,7 @@ func getTemplateCmdRunE(opts *config.Options, client *adapters.HTTPClient) func( 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 { @@ -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) } } diff --git a/cmd/gitops/app/get/templates/terraform/cmd.go b/cmd/gitops/app/get/templates/terraform/cmd.go index 8cc43f564..cafb97a88 100644 --- a/cmd/gitops/app/get/templates/terraform/cmd.go +++ b/cmd/gitops/app/get/templates/terraform/cmd.go @@ -15,6 +15,7 @@ import ( type templateCommandFlags struct { ListTemplateParameters bool + TemplateNamespace string } var flags templateCommandFlags @@ -39,6 +40,7 @@ gitops get template terraform --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 } @@ -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) } } diff --git a/cmd/gitops/pkg/adapters/http.go b/cmd/gitops/pkg/adapters/http.go index cd581dacf..d4a4b3324 100644 --- a/cmd/gitops/pkg/adapters/http.go +++ b/cmd/gitops/pkg/adapters/http.go @@ -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 { @@ -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) @@ -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 { @@ -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) @@ -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 { @@ -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) diff --git a/cmd/gitops/pkg/adapters/http_test.go b/cmd/gitops/pkg/adapters/http_test.go index 46923e3ad..e15c869ea 100644 --- a/cmd/gitops/pkg/adapters/http_test.go +++ b/cmd/gitops/pkg/adapters/http_test.go @@ -98,6 +98,7 @@ 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) @@ -105,6 +106,7 @@ func TestRetrieveTemplate(t *testing.T) { { 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) { @@ -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{ @@ -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") }, }, } @@ -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) }) } @@ -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) { @@ -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) { @@ -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") }, }, } @@ -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) }) } @@ -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{ @@ -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") }, }, } @@ -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) }) } diff --git a/cmd/gitops/pkg/templates/templates.go b/cmd/gitops/pkg/templates/templates.go index 273e59fd7..9d3c7cfe1 100644 --- a/cmd/gitops/pkg/templates/templates.go +++ b/cmd/gitops/pkg/templates/templates.go @@ -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 @@ -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) } @@ -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) } @@ -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) } diff --git a/cmd/gitops/pkg/templates/templates_test.go b/cmd/gitops/pkg/templates/templates_test.go index 434551f99..80507a54a 100644 --- a/cmd/gitops/pkg/templates/templates_test.go +++ b/cmd/gitops/pkg/templates/templates_test.go @@ -353,7 +353,7 @@ func TestGetTemplateParameters(t *testing.T) { t.Run(tt.name, func(t *testing.T) { c := newFakeClient(nil, tt.tps, nil, nil, nil, "", tt.err) w := new(bytes.Buffer) - err := templates.GetTemplateParameters(templates.CAPITemplateKind, "foo", c, w) + err := templates.GetTemplateParameters(templates.CAPITemplateKind, "foo", "default", c, w) assert.Equal(t, tt.expected, w.String()) if err != nil { assert.EqualError(t, err, tt.expectedErrorStr) @@ -566,7 +566,7 @@ func TestGetTemplateProfiles(t *testing.T) { t.Run(tt.name, func(t *testing.T) { c := newFakeClient(nil, nil, nil, tt.fs, nil, "", tt.err) w := new(bytes.Buffer) - err := templates.GetTemplateProfiles("profile-b", c, w) + err := templates.GetTemplateProfiles("profile-b", "default", c, w) assert.Equal(t, tt.expected, w.String()) if err != nil { assert.EqualError(t, err, tt.expectedErrorStr) @@ -609,7 +609,7 @@ func (c *fakeClient) RetrieveTemplates(kind templates.TemplateKind) ([]templates return c.ts, nil } -func (c *fakeClient) RetrieveTemplate(name string, kind templates.TemplateKind) (*templates.Template, error) { +func (c *fakeClient) RetrieveTemplate(name string, kind templates.TemplateKind, namespace string) (*templates.Template, error) { if c.err != nil { return nil, c.err } @@ -629,7 +629,7 @@ func (c *fakeClient) RetrieveTemplatesByProvider(kind templates.TemplateKind, pr return c.ts, nil } -func (c *fakeClient) RetrieveTemplateParameters(kind templates.TemplateKind, name string) ([]templates.TemplateParameter, error) { +func (c *fakeClient) RetrieveTemplateParameters(kind templates.TemplateKind, name string, namespace string) ([]templates.TemplateParameter, error) { if c.err != nil { return nil, c.err } @@ -661,7 +661,7 @@ func (c *fakeClient) RetrieveCredentials() ([]templates.Credentials, error) { return c.cs, nil } -func (c *fakeClient) RetrieveTemplateProfiles(name string) ([]templates.Profile, error) { +func (c *fakeClient) RetrieveTemplateProfiles(name string, namespace string) ([]templates.Profile, error) { if c.err != nil { return nil, c.err }