Skip to content

Commit

Permalink
Make getURL() generic
Browse files Browse the repository at this point in the history
Signed-off-by: Alina Buzachis <[email protected]>
  • Loading branch information
alinabuzachis committed Feb 7, 2024
1 parent 127063a commit 4f76af2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
18 changes: 3 additions & 15 deletions internal/provider/host_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"slices"
"sync"

Expand Down Expand Up @@ -134,17 +133,6 @@ type HostResourceModel struct {
Id types.Int64 `tfsdk:"id"`
}

func getURL(hostname string) (string, diag.Diagnostics) {
var diags diag.Diagnostics

result, err := url.JoinPath(hostname, "groups/")
if err != nil {
diags.AddError("Error joining the URL", err.Error())
}

return result, diags
}

// Create creates the host resource and sets the Terraform state on success.
func (r *HostResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data HostResourceModel
Expand Down Expand Up @@ -185,7 +173,7 @@ func (r *HostResource) Create(ctx context.Context, req resource.CreateRequest, r
return
}

url, diags := getURL(data.URL.ValueString())
url, diags := getURL(data.URL.ValueString(), "groups")
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand Down Expand Up @@ -392,7 +380,7 @@ func (r *HostResource) HandleGroupAssociation(ctx context.Context, data HostReso

toBeAdded := sliceDifference(elements, groups)
toBeRemoved := sliceDifference(groups, elements)
url, diags := getURL(data.URL.ValueString())
url, diags := getURL(data.URL.ValueString(), "groups")
diags.Append(diags...)
if diags.HasError() {
return diags
Expand All @@ -419,7 +407,7 @@ func (r *HostResource) ReadAssociatedGroups(data HostResourceModel) ([]int64, di
var diags diag.Diagnostics
var result map[string]interface{}

url, diags := getURL(data.URL.ValueString())
url, diags := getURL(data.URL.ValueString(), "groups")
diags.Append(diags...)
if diags.HasError() {
return nil, diags
Expand Down
15 changes: 15 additions & 0 deletions internal/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"path"
"slices"

"github.com/hashicorp/terraform-plugin-framework/attr"
Expand Down Expand Up @@ -40,3 +42,16 @@ func ValidateResponse(resp *http.Response, body []byte, err error, expected_stat

return diags
}

func getURL(base string, paths ...string) (string, diag.Diagnostics) {
var diags diag.Diagnostics
u, err := url.Parse(base)
if err != nil {
diags.AddError("Error parsing the URL", err.Error())
return "", diags
}

u.Path = path.Join(append([]string{u.Path}, paths...)...)

return u.String(), diags
}
38 changes: 38 additions & 0 deletions internal/provider/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package provider

import (
"testing"
)

func TestGetURL(t *testing.T) {
tests := []struct {
hostname string
paths []string
expectedURL string
expectError bool
}{
{"https://example.com", []string{"groups", "users"}, "https://example.com/groups/users", false},
{"https://example.com/", []string{"groups", "users"}, "https://example.com/groups/users", false},
{"https://example.com", []string{"groups", "users", "123"}, "https://example.com/groups/users/123", false},
}

for _, test := range tests {
t.Run(test.hostname, func(t *testing.T) {
result, diags := getURL(test.hostname, test.paths...)

if test.expectError {
if !diags.HasError() {
t.Errorf("Expected an error, but got nil")
}
} else {
if diags.HasError() {
t.Errorf("Unexpected error: %v", diags.Errors())
}

if result != test.expectedURL {
t.Errorf("Expected %s, but got %s", test.expectedURL, result)
}
}
})
}
}

0 comments on commit 4f76af2

Please sign in to comment.