-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sensor-group assignment data source
- Loading branch information
1 parent
7f5807a
commit 90089ea
Showing
5 changed files
with
168 additions
and
7 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
pkg/config-api-provider/provider/data-sources/sensor_group_assignment.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package datasources | ||
|
||
import ( | ||
"context" | ||
|
||
config_api_client "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
// Ensure the implementation satisfies the expected interfaces. | ||
var ( | ||
_ datasource.DataSource = &sensorGroupAssignmentDataSource{} | ||
_ datasource.DataSourceWithConfigure = &sensorGroupAssignmentDataSource{} | ||
) | ||
|
||
func NewSensorGroupAssignmentDataSource() datasource.DataSource { | ||
return &sensorGroupAssignmentDataSource{} | ||
} | ||
|
||
type sensorGroupAssignmentDataSource struct { | ||
client *config_api_client.APIClient | ||
} | ||
|
||
type sensorGroupAssignmentDataSourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
SensorID types.String `tfsdk:"sensor_id"` | ||
GroupID types.String `tfsdk:"group_id"` | ||
Filter struct { | ||
SensorGroupAssignmentID string `tfsdk:"sensor_group_assignment_id"` | ||
} `tfsdk:"filter"` | ||
} | ||
|
||
func (d *sensorGroupAssignmentDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_sensor_group_assignment" | ||
} | ||
|
||
func (d *sensorGroupAssignmentDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"sensor_id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"group_id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"filter": schema.SingleNestedAttribute{ | ||
Required: true, | ||
Attributes: map[string]schema.Attribute{ | ||
"sensor_group_assignment_id": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *sensorGroupAssignmentDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var state sensorGroupAssignmentDataSourceModel | ||
|
||
// Read configuration from request | ||
diags := req.Config.Get(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
sensorGroupAssignmentResponse, _, err := d.client.ConfigurationAPI. | ||
GetConfigurationAppV1SensorGroupAssignmentsGet(context.Background()). | ||
Uid(state.Filter.SensorGroupAssignmentID). | ||
Execute() | ||
|
||
if err != nil || len(sensorGroupAssignmentResponse.SensorGroupAssignments) != 1 { | ||
resp.Diagnostics.AddError( | ||
"Error reading Sensor Group Assignment", | ||
"Could not retrieve Sensor Group Assignment, unexpected error: "+err.Error(), | ||
) | ||
return | ||
} | ||
|
||
sensorGroupAssignment := sensorGroupAssignmentResponse.SensorGroupAssignments[0] | ||
state.ID = types.StringValue(sensorGroupAssignment.Uid) | ||
state.SensorID = types.StringValue(sensorGroupAssignment.SensorUid) | ||
state.GroupID = types.StringValue(sensorGroupAssignment.GroupUid) | ||
|
||
// Set state | ||
diags = resp.State.Set(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (d *sensorGroupAssignmentDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
// Add a nil check when handling ProviderData because Terraform | ||
// sets that data after it calls the ConfigureProvider RPC. | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(*config_api_client.APIClient) | ||
|
||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Data Source Configure Type", | ||
"Data Source type: Sensor Group Assignment. Please report this issue to the provider developers.", | ||
) | ||
return | ||
} | ||
|
||
d.client = client | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
pkg/config-api-provider/test/data_source_sensor_group_assignment_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/h2non/gock" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func TestSensorGroupAssignmentDataSource(t *testing.T) { | ||
defer gock.Off() | ||
MockOAuth() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Read testing | ||
{ | ||
PreConfig: func() { | ||
MockGetSensorGroupAssignment( | ||
"uid", | ||
GenerateSensorGroupAssignmentPaginatedResponse([]map[string]interface{}{ | ||
StructToMap(GenerateSensorGroupAssignmentResponse("uid", "")), | ||
}), | ||
3, | ||
) | ||
}, | ||
Config: providerConfig + ` | ||
data "uxi_sensor_group_assignment" "my_sensor_group_assignment" { | ||
filter = { | ||
sensor_group_assignment_id = "uid" | ||
} | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.uxi_sensor_group_assignment.my_sensor_group_assignment", "id", "uid"), | ||
resource.TestCheckResourceAttr("data.uxi_sensor_group_assignment.my_sensor_group_assignment", "group_id", "group_uid"), | ||
resource.TestCheckResourceAttr("data.uxi_sensor_group_assignment.my_sensor_group_assignment", "sensor_id", "sensor_uid"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters