Skip to content

Commit

Permalink
Beacon id params fix (core-infra-svcs#286)
Browse files Browse the repository at this point in the history
* fix: beacon id params

* fix: beaconId

- added if unknown clause
- updated tests

* bug: BeaconIdParams IsUnknown

- fixed BeaconIdParams IsUnknown state and import issues
  • Loading branch information
iamdexterpark authored Feb 2, 2024
1 parent bf13f6d commit e6ccd36
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 89 deletions.
97 changes: 49 additions & 48 deletions internal/provider/devices_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"fmt"
"github.com/core-infra-svcs/terraform-provider-meraki/tools"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"strings"

"github.com/core-infra-svcs/terraform-provider-meraki/internal/provider/jsontypes"
Expand Down Expand Up @@ -42,24 +44,24 @@ type DevicesResourceModel struct {

// The Id field is mandatory for all resources. It's used for resource identification and is required
// for the acceptance tests to run.
Id jsontypes.String `tfsdk:"id"`
Serial jsontypes.String `tfsdk:"serial"`
Name jsontypes.String `tfsdk:"name"`
Mac jsontypes.String `tfsdk:"mac"`
Model jsontypes.String `tfsdk:"model"`
Tags jsontypes.Set[jsontypes.String] `tfsdk:"tags"`
LanIp jsontypes.String `tfsdk:"lan_ip"`
Firmware jsontypes.String `tfsdk:"firmware"`
Lat jsontypes.Float64 `tfsdk:"lat"`
Lng jsontypes.Float64 `tfsdk:"lng"`
Address jsontypes.String `tfsdk:"address"`
Notes jsontypes.String `tfsdk:"notes"`
Url jsontypes.String `tfsdk:"url"`
FloorPlanId jsontypes.String `tfsdk:"floor_plan_id"`
NetworkId jsontypes.String `tfsdk:"network_id"`
BeaconIdParams DevicesResourceModelBeaconIdParams `tfsdk:"beacon_id_params"`
SwitchProfileId jsontypes.String `tfsdk:"switch_profile_id"`
MoveMapMarker jsontypes.Bool `tfsdk:"move_map_marker"`
Id jsontypes.String `tfsdk:"id"`
Serial jsontypes.String `tfsdk:"serial"`
Name jsontypes.String `tfsdk:"name"`
Mac jsontypes.String `tfsdk:"mac"`
Model jsontypes.String `tfsdk:"model"`
Tags jsontypes.Set[jsontypes.String] `tfsdk:"tags"`
LanIp jsontypes.String `tfsdk:"lan_ip"`
Firmware jsontypes.String `tfsdk:"firmware"`
Lat jsontypes.Float64 `tfsdk:"lat"`
Lng jsontypes.Float64 `tfsdk:"lng"`
Address jsontypes.String `tfsdk:"address"`
Notes jsontypes.String `tfsdk:"notes"`
Url jsontypes.String `tfsdk:"url"`
FloorPlanId jsontypes.String `tfsdk:"floor_plan_id"`
NetworkId jsontypes.String `tfsdk:"network_id"`
BeaconIdParams types.Object `tfsdk:"beacon_id_params"`
SwitchProfileId jsontypes.String `tfsdk:"switch_profile_id"`
MoveMapMarker jsontypes.Bool `tfsdk:"move_map_marker"`
}

type DevicesResourceModelBeaconIdParams struct {
Expand All @@ -68,6 +70,22 @@ type DevicesResourceModelBeaconIdParams struct {
Minor jsontypes.Int64 `tfsdk:"minor"`
}

func DevicesResourceModelBeaconIdParamsAttrTypes() map[string]attr.Type {
return map[string]attr.Type{
"uuid": jsontypes.StringType,
"major": jsontypes.Int64Type,
"minor": jsontypes.Int64Type,
}
}

func DevicesResourceModelBeaconIdParamsNullValues() map[string]attr.Value {
return map[string]attr.Value{
"uuid": jsontypes.StringNull(),
"major": jsontypes.Int64Null(),
"minor": jsontypes.Int64Null(),
}
}

// Metadata provides a way to define information about the resource.
// This method is called by the framework to retrieve metadata about the resource.
func (r *DevicesResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
Expand Down Expand Up @@ -337,15 +355,11 @@ func (r *DevicesResource) Create(ctx context.Context, req resource.CreateRequest
)
return
}
if data.BeaconIdParams.Major.IsUnknown() {
data.BeaconIdParams.Major = jsontypes.Int64Null()
}
if data.BeaconIdParams.Minor.IsUnknown() {
data.BeaconIdParams.Minor = jsontypes.Int64Null()
}
if data.BeaconIdParams.Uuid.IsUnknown() {
data.BeaconIdParams.Uuid = jsontypes.StringNull()

if data.BeaconIdParams.IsUnknown() {
data.BeaconIdParams = types.ObjectNull(DevicesResourceModelBeaconIdParamsAttrTypes())
}

if data.Firmware.IsUnknown() {
data.Firmware = jsontypes.StringNull()
}
Expand All @@ -372,7 +386,7 @@ func (r *DevicesResource) Create(ctx context.Context, req resource.CreateRequest
}

// Set ID for the new resource.
data.Id = jsontypes.StringValue("example-id")
data.Id = jsontypes.StringValue(data.Serial.ValueString())

// Now set the final state of the resource.
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down Expand Up @@ -435,15 +449,10 @@ func (r *DevicesResource) Read(ctx context.Context, req resource.ReadRequest, re
return
}

if data.BeaconIdParams.Major.IsUnknown() {
data.BeaconIdParams.Major = jsontypes.Int64Null()
}
if data.BeaconIdParams.Minor.IsUnknown() {
data.BeaconIdParams.Minor = jsontypes.Int64Null()
}
if data.BeaconIdParams.Uuid.IsUnknown() {
data.BeaconIdParams.Uuid = jsontypes.StringNull()
if data.BeaconIdParams.IsUnknown() {
data.BeaconIdParams = types.ObjectNull(DevicesResourceModelBeaconIdParamsAttrTypes())
}

if data.Firmware.IsUnknown() {
data.Firmware = jsontypes.StringNull()
}
Expand All @@ -470,7 +479,7 @@ func (r *DevicesResource) Read(ctx context.Context, req resource.ReadRequest, re
}

// Set ID for the resource.
data.Id = jsontypes.StringValue("example-id")
data.Id = jsontypes.StringValue(data.Serial.ValueString())

// Now set the final state of the resource.
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down Expand Up @@ -564,15 +573,10 @@ func (r *DevicesResource) Update(ctx context.Context, req resource.UpdateRequest
return
}

if data.BeaconIdParams.Major.IsUnknown() {
data.BeaconIdParams.Major = jsontypes.Int64Null()
}
if data.BeaconIdParams.Minor.IsUnknown() {
data.BeaconIdParams.Minor = jsontypes.Int64Null()
}
if data.BeaconIdParams.Uuid.IsUnknown() {
data.BeaconIdParams.Uuid = jsontypes.StringNull()
if data.BeaconIdParams.IsUnknown() {
data.BeaconIdParams = types.ObjectNull(DevicesResourceModelBeaconIdParamsAttrTypes())
}

if data.Firmware.IsUnknown() {
data.Firmware = jsontypes.StringNull()
}
Expand All @@ -599,7 +603,7 @@ func (r *DevicesResource) Update(ctx context.Context, req resource.UpdateRequest
}

// Set ID for the new resource.
data.Id = jsontypes.StringValue("example-id")
data.Id = jsontypes.StringValue(data.Serial.ValueString())

// Now set the updated state of the resource.
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down Expand Up @@ -685,9 +689,6 @@ func (r *DevicesResource) Delete(ctx context.Context, req resource.DeleteRequest
return
}

// Set ID for the new resource.
data.Id = jsontypes.StringValue("example-id")

resp.State.RemoveResource(ctx)

// Log that the resource was deleted.
Expand Down
81 changes: 40 additions & 41 deletions internal/provider/devices_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,113 +30,112 @@ func TestAccDevicesResource(t *testing.T) {
resource.TestCheckResourceAttr("meraki_network.test", "tags.#", "1"),
resource.TestCheckResourceAttr("meraki_network.test", "tags.0", "tag1"),
resource.TestCheckResourceAttr("meraki_network.test", "product_types.#", "1"),
resource.TestCheckResourceAttr("meraki_network.test", "product_types.0", "appliance"),
resource.TestCheckResourceAttr("meraki_network.test", "product_types.0", "wireless"),
resource.TestCheckResourceAttr("meraki_network.test", "notes", "Additional description of the network"),
),
},

// Claim A Device To A Network
// Claim Device to Network
{
Config: testAccDevicesResourceConfigClaimNetworkDevice(os.Getenv("TF_ACC_MERAKI_ORGANIZATION_ID"), os.Getenv("TF_ACC_MERAKI_MX_SERIAL")),
Config: testAccDevicesResourceConfigDeviceClaim(os.Getenv("TF_ACC_MERAKI_ORGANIZATION_ID"), os.Getenv("TF_ACC_MERAKI_MR_SERIAL")),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("meraki_networks_devices_claim.test", "id", "example-id"),

// Claim A Device To A Network
resource.TestCheckResourceAttr("meraki_networks_devices_claim.test", "serials.0", os.Getenv("TF_ACC_MERAKI_MR_SERIAL")),
),
},

// Update and Read Device Attributes
{
Config: testAccDevicesResourceConfigUpdateDevice(os.Getenv("TF_ACC_MERAKI_ORGANIZATION_ID"), os.Getenv("TF_ACC_MERAKI_MX_SERIAL")),
Config: testAccDevicesResourceConfigUpdateDevice(os.Getenv("TF_ACC_MERAKI_MR_SERIAL")),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("meraki_devices.test", "id", "example-id"),
resource.TestCheckResourceAttr("meraki_devices.test", "serial", os.Getenv("TF_ACC_MERAKI_MX_SERIAL")),
resource.TestCheckResourceAttr("meraki_devices.test", "id", os.Getenv("TF_ACC_MERAKI_MR_SERIAL")),
resource.TestCheckResourceAttr("meraki_devices.test", "serial", os.Getenv("TF_ACC_MERAKI_MR_SERIAL")),
resource.TestCheckResourceAttr("meraki_devices.test", "lat", "37.418095"),
resource.TestCheckResourceAttr("meraki_devices.test", "lng", "-122.09853"),
resource.TestCheckResourceAttr("meraki_devices.test", "address", "new address"),
resource.TestCheckResourceAttr("meraki_devices.test", "name", "test_acc_mx_device"),
resource.TestCheckResourceAttr("meraki_devices.test", "name", "test_acc_test_device"),
resource.TestCheckResourceAttr("meraki_devices.test", "notes", "test notes"),
resource.TestCheckResourceAttr("meraki_devices.test", "tags.#", "1"),
resource.TestCheckResourceAttr("meraki_devices.test", "tags.0", "recently-added"),
//resource.TestCheckResourceAttr("meraki_devices.test", "beacon_id_params.%", "3"),
//resource.TestCheckResourceAttr("meraki_devices.test", "beacon_id_params.uuid", "00000000-0000-0000-0000-000000000000"),
//resource.TestCheckResourceAttr("meraki_devices.test", "beacon_id_params.major", "5"),
//resource.TestCheckResourceAttr("meraki_devices.test", "beacon_id_params.minor", "3"),
),
},
},

// ImportState test case.
/*
{
ResourceName: "meraki_devices.test",
ImportState: true,
ImportStateVerify: false,
ImportStateId: "1234567890, 0987654321",
},
*/

// ImportState test case.
{
ResourceName: "meraki_devices.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

// testAccNetworksDevicesClaimResourceConfigCreateNetwork is a constant string that defines the configuration for creating a network resource in your tests.
// It depends on the organization resource. Thisd will not work if the network already exists
// It depends on the organization resource. This will not work if the network already exists
func testAccDevicesResourceConfigCreateNetwork(orgId string) string {
result := fmt.Sprintf(`
resource "meraki_network" "test" {
organization_id = "%s"
product_types = ["appliance"]
tags = ["tag1"]
name = "test_acc_network_device"
product_types = ["wireless"]
tags = ["tag1"]
timezone = "America/Los_Angeles"
notes = "Additional description of the network"
}
`, orgId)
return result
}

// testAccDevicesResourceConfigClaimNetworkDevice is a constant string that defines the configuration for creating and reading a networks_devices_claim resource in your tests.
// It depends on both the organization and network resources.
func testAccDevicesResourceConfigClaimNetworkDevice(orgId string, serial string) string {
func testAccDevicesResourceConfigDeviceClaim(orgId string, serial string) string {
result := fmt.Sprintf(`
resource "meraki_network" "test" {
organization_id = "%s"
product_types = ["appliance"]
organization_id = "%s"
product_types = ["wireless"]
}
resource "meraki_networks_devices_claim" "test" {
depends_on = [resource.meraki_network.test]
network_id = resource.meraki_network.test.network_id
depends_on = ["resource.meraki_network.test"]
network_id = resource.meraki_network.test.network_id
serials = [
"%s"
]
}
`, orgId, serial)
return result
}

// testAccDevicesResourceConfigUpdateDevice is a constant string that defines the configuration for updating a devices' resource in your tests.
// It depends on both the organization and network resources.
func testAccDevicesResourceConfigUpdateDevice(orgId string, serial string) string {
func testAccDevicesResourceConfigUpdateDevice(serial string) string {
result := fmt.Sprintf(`
resource "meraki_network" "test" {
organization_id = "%s"
product_types = ["appliance"]
product_types = ["wireless"]
}
resource "meraki_networks_devices_claim" "test" {
depends_on = [resource.meraki_network.test]
network_id = resource.meraki_network.test.network_id
serials = [
"%s"
]
depends_on = ["resource.meraki_network.test"]
network_id = resource.meraki_network.test.network_id
}
resource "meraki_devices" "test" {
depends_on = [resource.meraki_network.test, resource.meraki_networks_devices_claim.test]
depends_on = ["resource.meraki_network.test", "resource.meraki_networks_devices_claim.test"]
network_id = resource.meraki_network.test.network_id
serial = "%s"
lat = 37.418095
lng = -122.09853
address = "new address"
name = "test_acc_mx_device"
name = "test_acc_test_device"
notes = "test notes"
beacon_id_params = {}
tags = ["recently-added"]
}
`, orgId, serial, serial)
`, serial)
return result
}

0 comments on commit e6ccd36

Please sign in to comment.