diff --git a/cmd/client_tester/main.go b/cmd/client_tester/main.go index e12ff94d..18a31574 100644 --- a/cmd/client_tester/main.go +++ b/cmd/client_tester/main.go @@ -15,7 +15,7 @@ func main() { config.Scheme = "http" client := config_api_client.NewAPIClient(config) - resp, httpResp, err := client.HealthAPI.GetLivezHealthLivezGet(context.Background()).Execute() + resp, httpResp, err := client.ConfigurationAPI.GetLivezHealthLivezGet(context.Background()).Execute() fmt.Println(resp) fmt.Println(httpResp) diff --git a/pkg/config-api-provider/go.mod b/pkg/config-api-provider/go.mod index 0f80674b..d4d44603 100644 --- a/pkg/config-api-provider/go.mod +++ b/pkg/config-api-provider/go.mod @@ -5,10 +5,11 @@ go 1.21 toolchain go1.22.5 require ( - github.com/golang/mock v1.6.0 + github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client v0.0.0-00010101000000-000000000000 github.com/hashicorp/terraform-plugin-framework v1.9.0 github.com/hashicorp/terraform-plugin-go v0.23.0 github.com/hashicorp/terraform-plugin-testing v1.10.0 + github.com/stretchr/testify v1.9.0 ) require ( @@ -16,6 +17,7 @@ require ( github.com/agext/levenshtein v1.2.2 // indirect github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect github.com/cloudflare/circl v1.3.7 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/fatih/color v1.16.0 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/go-cmp v0.6.0 // indirect @@ -47,7 +49,8 @@ require ( github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/reflectwalk v1.0.2 // indirect github.com/oklog/run v1.0.0 // indirect - github.com/stretchr/testify v1.9.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.5.2 // indirect github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect @@ -63,4 +66,7 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect google.golang.org/grpc v1.63.2 // indirect google.golang.org/protobuf v1.34.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) + +replace github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client => ../config-api-client diff --git a/pkg/config-api-provider/go.sum b/pkg/config-api-provider/go.sum index a37925f8..4a663b0a 100644 --- a/pkg/config-api-provider/go.sum +++ b/pkg/config-api-provider/go.sum @@ -33,8 +33,6 @@ github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= -github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= @@ -133,6 +131,8 @@ github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A= github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= @@ -145,41 +145,33 @@ github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAh github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= -github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/zclconf/go-cty v1.15.0 h1:tTCRWxsexYUmtt/wVxgDClUe+uQusuI443uL6e+5sXQ= github.com/zclconf/go-cty v1.15.0/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE= github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6ZMSMNJFMOjqrGHynW3DIBuR2H9j0ug+Mo= github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= -golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8= golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -201,14 +193,11 @@ golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg= golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM= google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds= diff --git a/pkg/config-api-provider/provider/resources/agent.go b/pkg/config-api-provider/provider/resources/agent.go index e3533278..a9538ab2 100644 --- a/pkg/config-api-provider/provider/resources/agent.go +++ b/pkg/config-api-provider/provider/resources/agent.go @@ -80,7 +80,7 @@ func (r *agentResource) Create(ctx context.Context, req resource.CreateRequest, // Retrieve values from plan var plan agentResourceModel diags := req.Plan.Get(ctx, &plan) - diags.AddError("operation not supported", "creating an agent resource is not supported; agents can only be imported") + diags.AddError("operation not supported", "creating an agent is not supported; agents can only be imported") resp.Diagnostics.Append(diags...) } @@ -137,7 +137,7 @@ func (r *agentResource) Delete(ctx context.Context, req resource.DeleteRequest, // Retrieve values from state var state agentResourceModel diags := req.State.Get(ctx, &state) - diags.AddError("operation not supported", "deleting an agent resource is not supported; agents can only removed from state") + diags.AddError("operation not supported", "deleting an agent is not supported; agents can only removed from state") resp.Diagnostics.Append(diags...) } diff --git a/pkg/config-api-provider/provider/resources/sensor.go b/pkg/config-api-provider/provider/resources/sensor.go index f095d9d1..bbc0a8fe 100644 --- a/pkg/config-api-provider/provider/resources/sensor.go +++ b/pkg/config-api-provider/provider/resources/sensor.go @@ -3,8 +3,7 @@ package resources import ( "context" - "time" - + // "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -25,10 +24,10 @@ type sensorResourceModel struct { AddressNote types.String `tfsdk:"address_note"` Notes types.String `tfsdk:"notes"` PCapMode types.String `tfsdk:"pcap_mode"` - LastUpdated types.String `tfsdk:"last_updated"` } -type sensorResponseModel struct { +// TODO: Switch this to use the Client Model when that becomes available +type SensorResponseModel struct { UID string Serial string Name string @@ -42,6 +41,14 @@ type sensorResponseModel struct { PCapMode string } +// TODO: Switch this to use the Client Model when that becomes available +type SensorUpdateRequestModel struct { + Name string + AddressNote string + Notes string + PCapMode string +} + func NewSensorResource() resource.Resource { return &sensorResource{} } @@ -61,9 +68,6 @@ func (r *sensorResource) Schema(_ context.Context, _ resource.SchemaRequest, res stringplanmodifier.UseStateForUnknown(), }, }, - "last_updated": schema.StringAttribute{ - Computed: true, - }, "name": schema.StringAttribute{ Required: true, }, @@ -81,13 +85,14 @@ func (r *sensorResource) Schema(_ context.Context, _ resource.SchemaRequest, res } func (r *sensorResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { + } func (r *sensorResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { // Retrieve values from plan var plan sensorResourceModel diags := req.Plan.Get(ctx, &plan) - diags.AddError("operation not supported", "creating a sensor resource is not supported; sensors can only be imported") + diags.AddError("operation not supported", "creating a sensor is not supported; sensors can only be imported") resp.Diagnostics.Append(diags...) } @@ -100,8 +105,6 @@ func (r *sensorResource) Read(ctx context.Context, req resource.ReadRequest, res return } - // TODO: Call client create-sensor method - // We are mocking the response of the client for this early stage of development response := GetSensor() // Update state from client response @@ -109,7 +112,6 @@ func (r *sensorResource) Read(ctx context.Context, req resource.ReadRequest, res state.AddressNote = types.StringValue(response.AddressNote) state.Notes = types.StringValue(response.Notes) state.PCapMode = types.StringValue(response.PCapMode) - state.LastUpdated = types.StringValue(time.Now().Format(time.RFC850)) // Set refreshed state diags = resp.State.Set(ctx, &state) @@ -128,10 +130,19 @@ func (r *sensorResource) Update(ctx context.Context, req resource.UpdateRequest, return } - // TODO: Call client update-sensor method + // Update existing order + response := UpdateSensor(SensorUpdateRequestModel{ + Name: plan.Name.ValueString(), + AddressNote: plan.AddressNote.ValueString(), + Notes: plan.Notes.ValueString(), + PCapMode: plan.PCapMode.ValueString(), + }) - // Update the state to match the plan (replace with response from client) - plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850)) + // Update resource state with updated items + plan.Name = types.StringValue(response.Name) + plan.AddressNote = types.StringValue(response.AddressNote) + plan.Notes = types.StringValue(response.Notes) + plan.PCapMode = types.StringValue(response.PCapMode) // Set state to fully populated data diags = resp.State.Set(ctx, plan) @@ -145,7 +156,7 @@ func (r *sensorResource) Delete(ctx context.Context, req resource.DeleteRequest, // Retrieve values from state var state sensorResourceModel diags := req.State.Get(ctx, &state) - diags.AddError("operation not supported", "deleting a sensor resource is not supported; sensors can only removed from state") + diags.AddError("operation not supported", "deleting a sensor is not supported; sensors can only removed from state") resp.Diagnostics.Append(diags...) } @@ -154,20 +165,39 @@ func (r *sensorResource) ImportState(ctx context.Context, req resource.ImportSta } // Get the sensor using the configuration-api client -func GetSensor() sensorResponseModel { +var GetSensor = func() SensorResponseModel { + // TODO: Query the sensor using the client + + return SensorResponseModel{ + UID: "mock_uid", + Serial: "mock_serial", + Name: "mock_name", + ModelNumber: "mock_model_number", + WifiMacAddress: "mock_wifi_mac_address", + EthernetMacAddress: "mock_ethernet_mac_address", + AddressNote: "mock_address_note", + Longitude: "mock_longitude", + Latitude: "mock_latitude", + Notes: "mock_notes", + PCapMode: "mock_pcap_mode", + } +} + +// Update the sensor using the configuration-api client +var UpdateSensor = func(request SensorUpdateRequestModel) SensorResponseModel { // TODO: Query the sensor using the client - return sensorResponseModel{ - UID: "temporary_uid", - Serial: "temporary_serial", - Name: "temporary_name", - ModelNumber: "temporary_model_number", - WifiMacAddress: "temporary_wifi_mac_address", - EthernetMacAddress: "temporary_ethernet_mac_address", - AddressNote: "temporary_address_note", - Longitude: "temporary_longitude", - Latitude: "temporary_latitude", - Notes: "temporary_notes", - PCapMode: "temporary_pcap_mode", + return SensorResponseModel{ + UID: "mock_uid", + Serial: "mock_serial", + Name: request.Name, + ModelNumber: "mock_model_number", + WifiMacAddress: "mock_wifi_mac_address", + EthernetMacAddress: "mock_ethernet_mac_address", + AddressNote: request.AddressNote, + Longitude: "mock_longitude", + Latitude: "mock_latitude", + Notes: request.Notes, + PCapMode: request.PCapMode, } } diff --git a/pkg/config-api-provider/test/sensor_test.go b/pkg/config-api-provider/test/sensor_test.go new file mode 100644 index 00000000..5eb90ab6 --- /dev/null +++ b/pkg/config-api-provider/test/sensor_test.go @@ -0,0 +1,132 @@ +package test + +import ( + "regexp" + "testing" + + "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/tfversion" +) + +func TestSensorResource(t *testing.T) { + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + // we required terraform 1.7.0 and above for the `removed` block + tfversion.RequireAbove(tfversion.Version1_7_0), + }, + Steps: []resource.TestStep{ + // Creating a sensor is not allowed + { + Config: providerConfig + ` + resource "uxi_sensor" "my_sensor" { + name = "name" + address_note = "address_note" + notes = "note" + pcap_mode = "light" + }`, + + ExpectError: regexp.MustCompile(`creating a sensor is not supported; sensors can only be imported`), + }, + // Importing a sensor + { + PreConfig: func() { + resources.GetSensor = func() resources.SensorResponseModel { + return resources.SensorResponseModel{ + UID: "uid", + Serial: "serial", + Name: "imported_name", + ModelNumber: "model_number", + WifiMacAddress: "wifi_mac_address", + EthernetMacAddress: "ethernet_mac_address", + AddressNote: "imported_address_note", + Longitude: "imported_longitude", + Latitude: "imported_latitude", + Notes: "imported_notes", + PCapMode: "light", + } + } + }, + Config: providerConfig + ` + resource "uxi_sensor" "my_sensor" { + name = "imported_name" + address_note = "imported_address_note" + notes = "imported_notes" + pcap_mode = "light" + } + + import { + to = uxi_sensor.my_sensor + id = "test_uid" + }`, + + Check: resource.ComposeAggregateTestCheckFunc( + // Verify first order item updated + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "name", "imported_name"), + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "address_note", "imported_address_note"), + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "notes", "imported_notes"), + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "pcap_mode", "light"), + // Verify first coffee item has Computed attributes updated. + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "id", "test_uid"), + ), + }, + // ImportState testing + { + ResourceName: "uxi_sensor.my_sensor", + ImportState: true, + ImportStateVerify: true, + }, + // Update and Read testing + { + PreConfig: func() { + resources.GetSensor = func() resources.SensorResponseModel { + return resources.SensorResponseModel{ + UID: "uid", + Serial: "serial", + Name: "updated_name", + ModelNumber: "model_number", + WifiMacAddress: "wifi_mac_address", + EthernetMacAddress: "ethernet_mac_address", + AddressNote: "updated_address_note", + Longitude: "updated_longitude", + Latitude: "updated_latitude", + Notes: "updated_notes", + PCapMode: "not_light", + } + } + }, + Config: providerConfig + ` + resource "uxi_sensor" "my_sensor" { + name = "updated_name" + address_note = "updated_address_note" + notes = "updated_notes" + pcap_mode = "not_light" + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "name", "updated_name"), + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "address_note", "updated_address_note"), + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "notes", "updated_notes"), + resource.TestCheckResourceAttr("uxi_sensor.my_sensor", "pcap_mode", "not_light"), + ), + }, + // Deleting a sensor is not allowed + { + Config: providerConfig + ``, + ExpectError: regexp.MustCompile(`deleting a sensor is not supported; sensors can only removed from state`), + }, + // Remove sensor from state + { + Config: providerConfig + ` + removed { + from = uxi_sensor.my_sensor + + lifecycle { + destroy = false + } + }`, + }, + }, + }) +}