Skip to content

Commit

Permalink
feat: sensor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Aug 22, 2024
1 parent 111aec0 commit abe0902
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 46 deletions.
2 changes: 1 addition & 1 deletion cmd/client_tester/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions pkg/config-api-provider/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ 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 (
github.com/ProtonMail/go-crypto v1.1.0-alpha.2 // indirect
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
Expand Down Expand Up @@ -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
Expand All @@ -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
15 changes: 2 additions & 13 deletions pkg/config-api-provider/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions pkg/config-api-provider/provider/resources/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}

Expand Down Expand Up @@ -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...)
}

Expand Down
86 changes: 58 additions & 28 deletions pkg/config-api-provider/provider/resources/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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{}
}
Expand All @@ -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,
},
Expand All @@ -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...)
}

Expand All @@ -100,16 +105,13 @@ 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
state.Name = types.StringValue(response.Name)
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)
Expand All @@ -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)
Expand All @@ -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...)
}

Expand All @@ -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,
}
}
Loading

0 comments on commit abe0902

Please sign in to comment.