Skip to content

Commit

Permalink
Removing error on build URL (#533)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MovieStoreGuy authored Oct 22, 2024
1 parent c830427 commit 3c1354b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 34 deletions.
21 changes: 3 additions & 18 deletions internal/definition/team/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down
12 changes: 8 additions & 4 deletions internal/providermeta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 `/`
Expand All @@ -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) {
Expand Down
14 changes: 2 additions & 12 deletions internal/providermeta/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -62,7 +60,6 @@ func TestLoadApplicationURL(t *testing.T) {
},
fragments: []string{},
url: "http://custom.signalfx.com/",
errVal: "",
},
{
name: "custom domain with fragments",
Expand All @@ -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",
Expand All @@ -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")
}
})
}
}
Expand Down

0 comments on commit 3c1354b

Please sign in to comment.