From 3c1354b301171cac6c7cf470962d3bbb95247940 Mon Sep 17 00:00:00 2001 From: Sean Marciniak <30928402+MovieStoreGuy@users.noreply.github.com> Date: Tue, 22 Oct 2024 11:44:45 +1030 Subject: [PATCH] Removing error on build URL (#533) Building a URL will always happen after a call to the client has been done. This means that if we are generating a URL, the URL has already been validated. --- internal/definition/team/resource.go | 21 +++------------------ internal/providermeta/meta.go | 12 ++++++++---- internal/providermeta/meta_test.go | 14 ++------------ 3 files changed, 13 insertions(+), 34 deletions(-) diff --git a/internal/definition/team/resource.go b/internal/definition/team/resource.go index f377b8cd..f91262bf 100644 --- a/internal/definition/team/resource.go +++ b/internal/definition/team/resource.go @@ -57,12 +57,7 @@ func newResourceCreate() schema.CreateContextFunc { "id": tm.Id, }) - u, err := pmeta.LoadApplicationURL(ctx, meta, AppPath, tm.Id) - if err != nil { - return diag.FromErr(err) - } - - if err := rd.Set("url", u); err != nil { + if err := rd.Set("url", pmeta.LoadApplicationURL(ctx, meta, AppPath, tm.Id)); err != nil { return diag.FromErr(err) } @@ -83,12 +78,7 @@ func newResourceRead() schema.ReadContextFunc { } tflog.Debug(ctx, "Successfully fetched team data") - u, err := pmeta.LoadApplicationURL(ctx, meta, AppPath, tm.Id) - if err != nil { - return diag.FromErr(err) - } - - if err := rd.Set("url", u); err != nil { + if err := rd.Set("url", pmeta.LoadApplicationURL(ctx, meta, AppPath, tm.Id)); err != nil { return diag.FromErr(err) } @@ -118,12 +108,7 @@ func newResourceUpdate() schema.UpdateContextFunc { return diag.FromErr(err) } - u, err := pmeta.LoadApplicationURL(ctx, meta, AppPath, tm.Id) - if err != nil { - return diag.FromErr(err) - } - - if err := rd.Set("url", u); err != nil { + if err := rd.Set("url", pmeta.LoadApplicationURL(ctx, meta, AppPath, tm.Id)); err != nil { return diag.FromErr(err) } diff --git a/internal/providermeta/meta.go b/internal/providermeta/meta.go index 360fc5df..b1caabf2 100644 --- a/internal/providermeta/meta.go +++ b/internal/providermeta/meta.go @@ -13,6 +13,8 @@ import ( "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/signalfx/signalfx-go" "go.uber.org/multierr" + + tfext "github.com/splunk-terraform/terraform-provider-signalfx/internal/tfextension" ) var ( @@ -45,14 +47,16 @@ func LoadClient(ctx context.Context, meta any) (*signalfx.Client, error) { } // LoadApplicationURL will generate the FQDN using the set CustomAppURL from the meta value. -func LoadApplicationURL(_ context.Context, meta any, fragments ...string) (string, error) { +func LoadApplicationURL(ctx context.Context, meta any, fragments ...string) string { m, ok := meta.(*Meta) if !ok { - return "", ErrMetaNotProvided + tflog.Error(ctx, "Unable to convert to expected type") + return "" } u, err := url.ParseRequestURI(m.CustomAppURL) if err != nil { - return "", err + tflog.Error(ctx, "Issue trying to parse custom app url", tfext.NewLogFields().Error(err)) + return "" } // In order to currently set that fragment, // the path needs to end with `/` @@ -61,7 +65,7 @@ func LoadApplicationURL(_ context.Context, meta any, fragments ...string) (strin u.Path += "/" } u.Fragment = path.Join(fragments...) - return u.String(), nil + return u.String() } func (s *Meta) Validate() (errs error) { diff --git a/internal/providermeta/meta_test.go b/internal/providermeta/meta_test.go index 87062344..97136c68 100644 --- a/internal/providermeta/meta_test.go +++ b/internal/providermeta/meta_test.go @@ -46,14 +46,12 @@ func TestLoadApplicationURL(t *testing.T) { meta any fragments []string url string - errVal string }{ { name: "no meta set", meta: nil, fragments: []string{}, url: "", - errVal: "expected to implement type Meta", }, { name: "custom domain set", @@ -62,7 +60,6 @@ func TestLoadApplicationURL(t *testing.T) { }, fragments: []string{}, url: "http://custom.signalfx.com/", - errVal: "", }, { name: "custom domain with fragments", @@ -74,8 +71,7 @@ func TestLoadApplicationURL(t *testing.T) { "aaaa", "edit", }, - url: "http://custom.signalfx.com/#detector/aaaa/edit", - errVal: "", + url: "http://custom.signalfx.com/#detector/aaaa/edit", }, { name: "invalid domain set", @@ -84,19 +80,13 @@ func TestLoadApplicationURL(t *testing.T) { }, fragments: []string{}, url: "", - errVal: "parse \"domain\": invalid URI for request", }, } { t.Run(tc.name, func(t *testing.T) { t.Parallel() - u, err := LoadApplicationURL(context.Background(), tc.meta, tc.fragments...) + u := LoadApplicationURL(context.Background(), tc.meta, tc.fragments...) require.Equal(t, tc.url, u, "Must match the expected url") - if tc.errVal != "" { - require.EqualError(t, err, tc.errVal, "Must match expected error message") - } else { - require.NoError(t, err, "Must not error when loading url") - } }) } }