Skip to content

Commit

Permalink
data source too
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Oct 2, 2024
1 parent 95b6431 commit d5884ec
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package datasources

import (
"context"

config_api_client "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client"
"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/util"
"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 = &networkGroupAssignmentDataSource{}
_ datasource.DataSourceWithConfigure = &networkGroupAssignmentDataSource{}
)

func NewNetworkGroupAssignmentDataSource() datasource.DataSource {
return &networkGroupAssignmentDataSource{}
}

type networkGroupAssignmentDataSource struct {
client *config_api_client.APIClient
}

type networkGroupAssignmentDataSourceModel struct {
ID types.String `tfsdk:"id"`
NetworkID types.String `tfsdk:"network_id"`
GroupID types.String `tfsdk:"group_id"`
Filter struct {
NetworkGroupAssignmentID string `tfsdk:"network_group_assignment_id"`
} `tfsdk:"filter"`
}

func (d *networkGroupAssignmentDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_network_group_assignment"
}

func (d *networkGroupAssignmentDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"network_id": schema.StringAttribute{
Computed: true,
},
"group_id": schema.StringAttribute{
Computed: true,
},
"filter": schema.SingleNestedAttribute{
Required: true,
Attributes: map[string]schema.Attribute{
"network_group_assignment_id": schema.StringAttribute{
Required: true,
},
},
},
},
}
}

func (d *networkGroupAssignmentDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state networkGroupAssignmentDataSourceModel

// Read configuration from request
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

request := d.client.ConfigurationAPI.
GetUxiV1alpha1NetworkGroupAssignmentsGet(ctx).
Uid(state.Filter.NetworkGroupAssignmentID)
networkGroupAssignmentResponse, _, err := util.RetryFor429(request.Execute)

if err != nil || len(networkGroupAssignmentResponse.NetworkGroupAssignments) != 1 {
resp.Diagnostics.AddError(
"Error reading Network Group Assignment",
"Could not retrieve Network Group Assignment, unexpected error: "+err.Error(),
)
return
}

networkGroupAssignment := networkGroupAssignmentResponse.NetworkGroupAssignments[0]
state.ID = types.StringValue(networkGroupAssignment.Uid)
state.NetworkID = types.StringValue(networkGroupAssignment.NetworkUid)
state.GroupID = types.StringValue(networkGroupAssignment.GroupUid)

// Set state
diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

func (d *networkGroupAssignmentDataSource) 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: Network Group Assignment. Please report this issue to the provider developers.",
)
return
}

d.client = client
}
1 change: 1 addition & 0 deletions pkg/config-api-provider/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func (p *uxiConfigurationProvider) DataSources(_ context.Context) []func() datas
datasources.NewWiredNetworkDataSource,
datasources.NewWirelessNetworkDataSource,
datasources.NewSensorGroupAssignmentDataSource,
datasources.NewNetworkGroupAssignmentDataSource,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package data_source_test

import (
"testing"

"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider"
"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/util"
"github.com/h2non/gock"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/nbio/st"
)

func TestNetworkGroupAssignmentDataSource(t *testing.T) {
defer gock.Off()
mockOAuth := util.MockOAuth()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Read testing
{
PreConfig: func() {
util.MockGetNetworkGroupAssignment(
"uid",
util.GenerateNetworkGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("uid", "")}),
3,
)
},
Config: provider.ProviderConfig + `
data "uxi_network_group_assignment" "my_network_group_assignment" {
filter = {
network_group_assignment_id = "uid"
}
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.uxi_network_group_assignment.my_network_group_assignment", "id", "uid"),
resource.TestCheckResourceAttr("data.uxi_network_group_assignment.my_network_group_assignment", "group_id", "group_uid"),
resource.TestCheckResourceAttr("data.uxi_network_group_assignment.my_network_group_assignment", "network_id", "network_uid"),
),
},
},
})

mockOAuth.Mock.Disable()
}

func TestNetworkGroupAssignmentSource429Handling(t *testing.T) {
defer gock.Off()
mockOAuth := util.MockOAuth()
var mock429 *gock.Response

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Read testing
{
PreConfig: func() {
mock429 = gock.New("https://test.api.capenetworks.com").
Get("/uxi/v1alpha1/network-group-assignments").
Reply(429).
SetHeaders(map[string]string{
"X-RateLimit-Limit": "100",
"X-RateLimit-Remaining": "0",
"X-RateLimit-Reset": "1",
})
util.MockGetNetworkGroupAssignment(
"uid",
util.GenerateNetworkGroupAssignmentPaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("uid", "")}),
3,
)
},
Config: provider.ProviderConfig + `
data "uxi_network_group_assignment" "my_network_group_assignment" {
filter = {
network_group_assignment_id = "uid"
}
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.uxi_network_group_assignment.my_network_group_assignment", "id", "uid"),
func(s *terraform.State) error {
st.Assert(t, mock429.Mock.Request().Counter, 0)
return nil
},
),
},
},
})

mockOAuth.Mock.Disable()
}

0 comments on commit d5884ec

Please sign in to comment.