Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Alina Buzachis <[email protected]>
  • Loading branch information
alinabuzachis committed Jan 19, 2024
1 parent ec55757 commit 4a2f37a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 59 deletions.
7 changes: 0 additions & 7 deletions config.tfrc

This file was deleted.

52 changes: 23 additions & 29 deletions internal/provider/host_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 @@ -53,8 +52,9 @@ func (r *HostResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
"inventory_id": schema.Int64Attribute{
Required: true,
},
"instance_id": schema.Int64Attribute{
"instance_id": schema.StringAttribute{
Optional: true,
Computed: true,
},
"name": schema.StringAttribute{
Required: true,
Expand All @@ -70,8 +70,7 @@ func (r *HostResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
},
},
"variables": schema.StringAttribute{
Optional: true,
CustomType: jsontypes.NormalizedType{},
Optional: true,
},
"enabled": schema.BoolAttribute{
Optional: true,
Expand All @@ -93,15 +92,15 @@ func (r *HostResource) Schema(_ context.Context, _ resource.SchemaRequest, resp

// HostResourceModel maps the resource schema data.
type HostResourceModel struct {
InventoryId types.Int64 `tfsdk:"inventory_id"`
InstanceId types.Int64 `tfsdk:"instance_id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
URL types.String `tfsdk:"host_url"`
Variables jsontypes.Normalized `tfsdk:"variables"`
Enabled types.Bool `tfsdk:"enabled"`
GroupId types.Int64 `tfsdk:"group_id"`
DisassociateGroup types.Bool `tfsdk:"disassociate_group"`
InventoryId types.Int64 `tfsdk:"inventory_id"`
InstanceId types.String `tfsdk:"instance_id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
URL types.String `tfsdk:"host_url"`
Variables types.String `tfsdk:"variables"`
Enabled types.Bool `tfsdk:"enabled"`
GroupId types.Int64 `tfsdk:"group_id"`
DisassociateGroup types.Bool `tfsdk:"disassociate_group"`
}

func (d *HostResourceModel) GetURL() string {
Expand All @@ -119,7 +118,7 @@ func (d *HostResourceModel) CreateRequestBody() ([]byte, diag.Diagnostics) {
body["inventory"] = d.InventoryId.ValueInt64()

// Instance id
body["instance_id"] = d.InstanceId.ValueInt64()
body["instance_id"] = d.InstanceId.ValueString()

// Name
body["name"] = d.Name.ValueString()
Expand All @@ -144,11 +143,6 @@ func (d *HostResourceModel) CreateRequestBody() ([]byte, diag.Diagnostics) {
}
}

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

// Description
if IsValueProvided(d.Description) {
body["description"] = d.Description.ValueString()
Expand Down Expand Up @@ -180,24 +174,24 @@ func (d *HostResourceModel) ParseHttpResponse(body []byte) error {
d.Name = types.StringValue(result["name"].(string))
d.URL = types.StringValue(result["url"].(string))

if r, ok := result["instance_id"]; ok {
d.InstanceId = types.StringValue(r.(string))
}

if r, ok := result["inventory"]; ok {
d.InventoryId = types.Int64Value(int64(r.(float64)))
}

if result["description"] != "" {
d.Description = types.StringValue(result["description"].(string))
} else {
d.Description = types.StringNull()
}

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

if r, ok := result["group_id"]; ok {
d.GroupId = basetypes.NewInt64Value(int64(r.(float64)))
}

if r, ok := result["disassociate_group"]; ok && r != nil {
d.DisassociateGroup = basetypes.NewBoolValue(r.(bool))
d.Variables = types.StringNull()
}

if r, ok := result["enabled"]; ok && r != nil {
Expand Down
42 changes: 19 additions & 23 deletions internal/provider/host_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
fwresource "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
Expand Down Expand Up @@ -45,41 +44,41 @@ func TestHostResourceCreateRequestBody(t *testing.T) {
Name: types.StringValue("test host"),
Description: types.StringUnknown(),
URL: types.StringUnknown(),
Variables: jsontypes.NewNormalizedUnknown(),
Variables: types.StringNull(),
GroupId: types.Int64Unknown(),
DisassociateGroup: basetypes.NewBoolValue(false),
Enabled: basetypes.NewBoolValue(false),
InventoryId: types.Int64Unknown(),
InstanceId: types.Int64Unknown(),
InstanceId: types.StringNull(),
},
expected: []byte(`{"enabled":false,"instance_id":0,"inventory":0,"name":"test host"}`),
expected: []byte(`{"enabled":false,"instance_id":"","inventory":0,"name":"test host"}`),
},
{
name: "test with null values",
input: HostResourceModel{
Name: types.StringValue("test host"),
Description: types.StringNull(),
URL: types.StringNull(),
Variables: jsontypes.NewNormalizedNull(),
Variables: types.StringNull(),
GroupId: types.Int64Null(),
DisassociateGroup: basetypes.NewBoolValue(false),
Enabled: basetypes.NewBoolValue(false),
InventoryId: types.Int64Null(),
InstanceId: types.Int64Null(),
InstanceId: types.StringNull(),
},
expected: []byte(`{"enabled":false,"instance_id":0,"inventory":0,"name":"test host"}`),
expected: []byte(`{"enabled":false,"instance_id":"","inventory":0,"name":"test host"}`),
},
{
name: "test with some values",
input: HostResourceModel{
InventoryId: types.Int64Value(1),
Name: types.StringValue("host1"),
Description: types.StringNull(),
URL: types.StringValue("/api/v2/hosts/1/"),
Variables: jsontypes.NewNormalizedValue("{\"foo\":\"bar\"}"),
Variables: types.StringValue("{\"foo\":\"bar\"}"),
},
expected: []byte(
`{"instance_id":0,"inventory":0,"name":"host1","url":"/api/v2/hosts/1/",` +
`"variables":"{\"foo\":\"bar\"}"}`,
`{"instance_id":"","inventory":1,"name":"host1","variables":"{\"foo\":\"bar\"}"}`,
),
},
{
Expand All @@ -88,12 +87,11 @@ func TestHostResourceCreateRequestBody(t *testing.T) {
Name: types.StringValue("host1"),
Description: types.StringNull(),
URL: types.StringValue("/api/v2/hosts/1/"),
Variables: jsontypes.NewNormalizedValue("{\"foo\":\"bar\"}"),
Variables: types.StringValue("{\"foo\":\"bar\"}"),
GroupId: basetypes.NewInt64Value(2),
},
expected: []byte(
`{"id":2,"instance_id":0,"inventory":0,"name":"host1","url":"/api/v2/hosts/1/",` +
`"variables":"{\"foo\":\"bar\"}"}`,
`{"id":2,"instance_id":"","inventory":0,"name":"host1","variables":"{\"foo\":\"bar\"}"}`,
),
},
}
Expand Down Expand Up @@ -141,13 +139,13 @@ func TestHostResourceParseHttpResponse(t *testing.T) {
},
{
name: "test with missing values",
input: []byte(`{"name": "host1", "url": "/api/v2/hosts/1/", "description": "", "variables": "", "group_id": 2}`),
input: []byte(`{"inventory":1,"name": "host1", "url": "/api/v2/hosts/1/", "description": "", "variables": "", "group_id": 2}`),
expected: HostResourceModel{
InventoryId: types.Int64Value(1),
Name: types.StringValue("host1"),
URL: types.StringValue("/api/v2/hosts/1/"),
Description: types.StringNull(),
GroupId: types.Int64Value(2),
Variables: jsontypes.NewNormalizedNull(),
Variables: types.StringNull(),
},
errors: emptyError,
},
Expand All @@ -158,13 +156,11 @@ func TestHostResourceParseHttpResponse(t *testing.T) {
`"enabled":false,"url":"/api/v2/hosts/1/","variables":"{\"foo\":\"bar\",\"nested\":{\"foobar\":\"baz\"}}"}`,
),
expected: HostResourceModel{
Name: types.StringValue("host1"),
URL: types.StringValue("/api/v2/hosts/1/"),
Description: types.StringValue("A basic test host"),
GroupId: types.Int64Value(1),
DisassociateGroup: basetypes.NewBoolValue(false),
Variables: jsontypes.NewNormalizedValue("{\"foo\":\"bar\",\"nested\":{\"foobar\":\"baz\"}}"),
Enabled: basetypes.NewBoolValue(false),
Name: types.StringValue("host1"),
URL: types.StringValue("/api/v2/hosts/1/"),
Description: types.StringValue("A basic test host"),
Variables: types.StringValue("{\"foo\":\"bar\",\"nested\":{\"foobar\":\"baz\"}}"),
Enabled: basetypes.NewBoolValue(false),
},
errors: emptyError,
},
Expand Down

0 comments on commit 4a2f37a

Please sign in to comment.