Skip to content

Commit 00f1971

Browse files
committed
More tests and review comments
1 parent 93ea158 commit 00f1971

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

internal/provider/group_resource.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ func (d *GroupResourceModel) CreateRequestBody() ([]byte, diag.Diagnostics) {
105105

106106
// Variables
107107
if IsValueProvided(d.Variables) {
108-
// var vars map[string]interface{}
109-
// diags.Append(d.Variables.Unmarshal(&vars)...)
110108
body["variables"] = d.Variables.ValueString()
111109
}
112110

@@ -138,8 +136,17 @@ func (d *GroupResourceModel) ParseHttpResponse(body []byte) error {
138136
}
139137

140138
d.Name = types.StringValue(result["name"].(string))
141-
d.Description = types.StringValue(result["description"].(string))
139+
if result["description"] != "" {
140+
d.Description = types.StringValue(result["description"].(string))
141+
} else {
142+
d.Description = types.StringNull()
143+
}
142144
d.URL = types.StringValue(result["url"].(string))
145+
if result["variables"] != nil {
146+
d.Variables = jsontypes.NewNormalizedValue(result["variables"].(string))
147+
} else {
148+
d.Variables = jsontypes.NewNormalizedNull()
149+
}
143150

144151
return nil
145152
}
@@ -287,15 +294,8 @@ func (r GroupResource) UpdateGroup(data GroupResourceModelInterface) diag.Diagno
287294

288295
func (r GroupResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
289296
var data GroupResourceModel
290-
var data_with_URL GroupResourceModel
291-
292-
// Read Terraform plan and state data into the model
293-
// The URL is generated once the group is created. To update the correct group, we retrieve the state data and append the URL from the state data to the plan data.
294297

295298
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
296-
resp.Diagnostics.Append(req.State.Get(ctx, &data_with_URL)...)
297-
data.URL = data_with_URL.URL
298-
299299
resp.Diagnostics.Append(r.UpdateGroup(&data)...)
300300
if resp.Diagnostics.HasError() {
301301
return

internal/provider/group_resource_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,40 @@ import (
1818

1919
func TestParseHttpResponse(t *testing.T) {
2020
t.Run("Basic Test", func(t *testing.T) {
21-
g := GroupResourceModel{
22-
Name: types.StringValue("group1"),
23-
URL: types.StringValue("/api/v2/groups/24/"),
21+
expected := GroupResourceModel{
22+
Name: types.StringValue("group1"),
23+
Description: types.StringValue(""),
24+
URL: types.StringValue("/api/v2/groups/24/"),
2425
}
26+
g := GroupResourceModel{}
2527
body := []byte(`{"name": "group1", "url": "/api/v2/groups/24/", "description": ""}`)
2628
err := g.ParseHttpResponse(body)
2729
assert.NoError(t, err)
30+
if expected != g {
31+
t.Errorf("Expected (%s) not equal to actual (%s)", expected, g)
32+
}
2833
})
2934
t.Run("Test with variables", func(t *testing.T) {
30-
g := GroupResourceModel{
31-
Name: types.StringValue("group1"),
32-
URL: types.StringValue("/api/v2/groups/24/"),
35+
expected := GroupResourceModel{
36+
Name: types.StringValue("group1"),
37+
URL: types.StringValue("/api/v2/groups/24/"),
38+
Description: types.StringValue(""),
39+
Variables: jsontypes.NewNormalizedValue("{\"ansible_network_os\":\"ios\"}"),
3340
}
41+
g := GroupResourceModel{}
3442
body := []byte(`{"name": "group1", "url": "/api/v2/groups/24/", "description": "", "variables": "{\"ansible_network_os\":\"ios\"}"}`)
3543
err := g.ParseHttpResponse(body)
3644
assert.NoError(t, err)
45+
if expected != g {
46+
t.Errorf("Expected (%s) not equal to actual (%s)", expected, g)
47+
}
48+
})
49+
t.Run("JSON error", func(t *testing.T) {
50+
g := GroupResourceModel{}
51+
body := []byte("Not valid JSON")
52+
err := g.ParseHttpResponse(body)
53+
assert.Error(t, err)
54+
3755
})
3856
}
3957

0 commit comments

Comments
 (0)