Skip to content

Commit

Permalink
Add inventory to response and remove url from body
Browse files Browse the repository at this point in the history
  • Loading branch information
GomathiselviS committed Jan 18, 2024
1 parent c86ba72 commit ee4c5bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
30 changes: 13 additions & 17 deletions internal/provider/group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"net/http"

"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -65,20 +64,19 @@ func (r *GroupResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
},
},
"variables": schema.StringAttribute{
Optional: true,
CustomType: jsontypes.NormalizedType{},
Optional: true,
},
},
}
}

// GroupResourceModel maps the resource schema data.
type GroupResourceModel struct {
InventoryId types.Int64 `tfsdk:"inventory_id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
URL types.String `tfsdk:"group_url"`
Variables jsontypes.Normalized `tfsdk:"variables"`
InventoryId types.Int64 `tfsdk:"inventory_id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
URL types.String `tfsdk:"group_url"`
Variables types.String `tfsdk:"variables"`
}

func (d *GroupResourceModel) GetURL() string {
Expand All @@ -91,7 +89,6 @@ func (d *GroupResourceModel) GetURL() string {
func (d *GroupResourceModel) CreateRequestBody() ([]byte, diag.Diagnostics) {
body := make(map[string]interface{})
var diags diag.Diagnostics

// Inventory id
body["inventory"] = d.InventoryId.ValueInt64()

Expand All @@ -103,11 +100,6 @@ func (d *GroupResourceModel) CreateRequestBody() ([]byte, diag.Diagnostics) {
body["variables"] = d.Variables.ValueString()
}

// URL
if IsValueProvided(d.URL) {
body["url"] = d.URL.ValueString()
}

// Description
if IsValueProvided(d.Description) {
body["description"] = d.Description.ValueString()
Expand All @@ -130,17 +122,21 @@ func (d *GroupResourceModel) ParseHttpResponse(body []byte) error {
return err
}

invid := int64(result["inventory"].(float64))
d.InventoryId = types.Int64Value(invid)
d.Name = types.StringValue(result["name"].(string))

if result["description"] != "" {
d.Description = types.StringValue(result["description"].(string))
} else {
d.Description = types.StringNull()
}
d.URL = types.StringValue(result["url"].(string))
if result["variables"] != nil {
d.Variables = jsontypes.NewNormalizedValue(result["variables"].(string))

if result["variables"] != "" {
d.Variables = types.StringValue(result["variables"].(string))
} else {
d.Variables = jsontypes.NewNormalizedNull()
d.Variables = types.StringNull()
}

return nil
Expand Down
16 changes: 9 additions & 7 deletions internal/provider/group_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"
"testing"

"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
Expand All @@ -16,12 +15,14 @@ import (
func TestGroupParseHttpResponse(t *testing.T) {
t.Run("Basic Test", func(t *testing.T) {
expected := GroupResourceModel{
InventoryId: types.Int64Value(1),
Name: types.StringValue("group1"),
Description: types.StringNull(),
URL: types.StringValue("/api/v2/groups/24/"),
Variables: types.StringNull(),
}
g := GroupResourceModel{}
body := []byte(`{"name": "group1", "url": "/api/v2/groups/24/", "description": ""}`)
body := []byte(`{"inventory": 1, "name": "group1", "url": "/api/v2/groups/24/", "description": "", "variables": ""}`)
err := g.ParseHttpResponse(body)
assert.NoError(t, err)
if expected != g {
Expand All @@ -30,13 +31,14 @@ func TestGroupParseHttpResponse(t *testing.T) {
})
t.Run("Test with variables", func(t *testing.T) {
expected := GroupResourceModel{
InventoryId: types.Int64Value(1),
Name: types.StringValue("group1"),
URL: types.StringValue("/api/v2/groups/24/"),
Description: types.StringNull(),
Variables: jsontypes.NewNormalizedValue("{\"ansible_network_os\":\"ios\"}"),
Variables: types.StringValue("{\"ansible_network_os\":\"ios\"}"),
}
g := GroupResourceModel{}
body := []byte(`{"name": "group1", "url": "/api/v2/groups/24/", "description": "", "variables": "{\"ansible_network_os\":\"ios\"}"}`)
body := []byte(`{"inventory": 1, "name": "group1", "url": "/api/v2/groups/24/", "description": "", "variables": "{\"ansible_network_os\":\"ios\"}"}`)
err := g.ParseHttpResponse(body)
assert.NoError(t, err)
if expected != g {
Expand All @@ -54,7 +56,7 @@ func TestGroupParseHttpResponse(t *testing.T) {
func TestGroupCreateRequestBody(t *testing.T) {
t.Run("Basic Test", func(t *testing.T) {
g := GroupResourceModel{
InventoryId: basetypes.NewInt64Value(1),
InventoryId: types.Int64Value(1),
Name: types.StringValue("group1"),
URL: types.StringValue("/api/v2/groups/24/"),
}
Expand Down Expand Up @@ -82,7 +84,7 @@ func TestGroupCreateRequestBody(t *testing.T) {
InventoryId: basetypes.NewInt64Value(5),
Name: types.StringValue("group1"),
URL: types.StringValue("/api/v2/groups/24/"),
Variables: jsontypes.NewNormalizedValue("{\"ansible_network_os\":\"ios\"}"),
Variables: types.StringValue("{\"ansible_network_os\":\"ios\"}"),
Description: types.StringValue("New Group"),
}
body := []byte(`{"name": "group1", "inventory": 5,
Expand All @@ -101,7 +103,7 @@ func TestGroupCreateRequestBody(t *testing.T) {
InventoryId: basetypes.NewInt64Value(5),
Name: types.StringValue("group1"),
URL: types.StringValue("/api/v2/groups/24/"),
Variables: jsontypes.NewNormalizedValue(
Variables: types.StringValue(
"{\"ansible_network_os\":\"ios\",\"ansible_connection\":\"network_cli\",\"ansible_ssh_user\":\"ansible\",\"ansible_ssh_pass\":\"ansi\"}",
),
Description: types.StringValue("New Group"),
Expand Down

0 comments on commit ee4c5bd

Please sign in to comment.