-
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: data source | agent assignment GET
- Loading branch information
1 parent
a643935
commit e71122a
Showing
3 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
internal/provider/datasources/agent_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,134 @@ | ||
package datasources | ||
|
||
import ( | ||
"context" | ||
|
||
config_api_client "github.com/aruba-uxi/terraform-provider-configuration-api/pkg/config-api-client" | ||
"github.com/aruba-uxi/terraform-provider-configuration/internal/provider/util" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var ( | ||
_ datasource.DataSource = &agentGroupAssignmentDataSource{} | ||
_ datasource.DataSourceWithConfigure = &agentGroupAssignmentDataSource{} | ||
) | ||
|
||
func NewAgentGroupAssignmentDataSource() datasource.DataSource { | ||
return &agentGroupAssignmentDataSource{} | ||
} | ||
|
||
type agentGroupAssignmentDataSource struct { | ||
client *config_api_client.APIClient | ||
} | ||
|
||
type agentGroupAssignmentDataSourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
AgentID types.String `tfsdk:"agent_id"` | ||
GroupID types.String `tfsdk:"group_id"` | ||
Filter struct { | ||
AgentGroupAssignmentID string `tfsdk:"agent_group_assignment_id"` | ||
} `tfsdk:"filter"` | ||
} | ||
|
||
func (d *agentGroupAssignmentDataSource) Metadata( | ||
_ context.Context, | ||
req datasource.MetadataRequest, | ||
resp *datasource.MetadataResponse, | ||
) { | ||
resp.TypeName = req.ProviderTypeName + "_agent_group_assignment" | ||
} | ||
|
||
func (d *agentGroupAssignmentDataSource) Schema( | ||
_ context.Context, | ||
_ datasource.SchemaRequest, | ||
resp *datasource.SchemaResponse, | ||
) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"agent_id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"group_id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"filter": schema.SingleNestedAttribute{ | ||
Required: true, | ||
Attributes: map[string]schema.Attribute{ | ||
"agent_group_assignment_id": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *agentGroupAssignmentDataSource) Read( | ||
ctx context.Context, | ||
req datasource.ReadRequest, | ||
resp *datasource.ReadResponse, | ||
) { | ||
var state agentGroupAssignmentDataSourceModel | ||
|
||
diags := req.Config.Get(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
request := d.client.ConfigurationAPI. | ||
AgentGroupAssignmentsGet(ctx). | ||
Id(state.Filter.AgentGroupAssignmentID) | ||
agentGroupAssignmentResponse, response, err := util.RetryFor429(request.Execute) | ||
errorPresent, errorDetail := util.RaiseForStatus(response, err) | ||
|
||
errorSummary := util.GenerateErrorSummary("read", "uxi_agent_group_assignment") | ||
|
||
if errorPresent { | ||
resp.Diagnostics.AddError(errorSummary, errorDetail) | ||
return | ||
} | ||
|
||
if len(agentGroupAssignmentResponse.Items) != 1 { | ||
resp.Diagnostics.AddError(errorSummary, "Could not find specified data source") | ||
return | ||
} | ||
|
||
agentGroupAssignment := agentGroupAssignmentResponse.Items[0] | ||
state.ID = types.StringValue(agentGroupAssignment.Id) | ||
state.AgentID = types.StringValue(agentGroupAssignment.Agent.Id) | ||
state.GroupID = types.StringValue(agentGroupAssignment.Group.Id) | ||
|
||
diags = resp.State.Set(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (d *agentGroupAssignmentDataSource) Configure( | ||
_ context.Context, | ||
req datasource.ConfigureRequest, | ||
resp *datasource.ConfigureResponse, | ||
) { | ||
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: Agent 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
166 changes: 166 additions & 0 deletions
166
test/mocked/data-sources/agent_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,166 @@ | ||
package data_source_test | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/aruba-uxi/terraform-provider-configuration/test/mocked/provider" | ||
"github.com/aruba-uxi/terraform-provider-configuration/test/mocked/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 TestAgentGroupAssignmentDataSource(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.MockGetAgentGroupAssignment( | ||
"uid", | ||
util.GeneratePaginatedResponse( | ||
[]map[string]interface{}{ | ||
util.GenerateAgentGroupAssignmentResponse("uid", ""), | ||
}, | ||
), | ||
3, | ||
) | ||
}, | ||
Config: provider.ProviderConfig + ` | ||
data "uxi_agent_group_assignment" "my_agent_group_assignment" { | ||
filter = { | ||
agent_group_assignment_id = "uid" | ||
} | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"data.uxi_agent_group_assignment.my_agent_group_assignment", | ||
"id", | ||
"uid", | ||
), | ||
resource.TestCheckResourceAttr( | ||
"data.uxi_agent_group_assignment.my_agent_group_assignment", | ||
"group_id", | ||
"group_uid", | ||
), | ||
resource.TestCheckResourceAttr( | ||
"data.uxi_agent_group_assignment.my_agent_group_assignment", | ||
"agent_id", | ||
"agent_uid", | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
|
||
mockOAuth.Mock.Disable() | ||
} | ||
|
||
func TestAgentGroupAssignmentDataSource429Handling(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("/networking-uxi/v1alpha1/agent-group-assignments"). | ||
Reply(429). | ||
SetHeaders(util.RateLimitingHeaders) | ||
util.MockGetAgentGroupAssignment( | ||
"uid", | ||
util.GeneratePaginatedResponse( | ||
[]map[string]interface{}{ | ||
util.GenerateAgentGroupAssignmentResponse("uid", ""), | ||
}, | ||
), | ||
3, | ||
) | ||
}, | ||
Config: provider.ProviderConfig + ` | ||
data "uxi_agent_group_assignment" "my_agent_group_assignment" { | ||
filter = { | ||
agent_group_assignment_id = "uid" | ||
} | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr( | ||
"data.uxi_agent_group_assignment.my_agent_group_assignment", | ||
"id", | ||
"uid", | ||
), | ||
func(s *terraform.State) error { | ||
st.Assert(t, mock429.Mock.Request().Counter, 0) | ||
return nil | ||
}, | ||
), | ||
}, | ||
}, | ||
}) | ||
|
||
mockOAuth.Mock.Disable() | ||
} | ||
|
||
func TestAgentGroupAssignmentDataSourceHttpErrorHandling(t *testing.T) { | ||
defer gock.Off() | ||
mockOAuth := util.MockOAuth() | ||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
PreConfig: func() { | ||
gock.New("https://test.api.capenetworks.com"). | ||
Get("/networking-uxi/v1alpha1/agent-group-assignments"). | ||
Reply(500). | ||
JSON(map[string]interface{}{ | ||
"httpStatusCode": 500, | ||
"errorCode": "HPE_GL_ERROR_INTERNAL_SERVER_ERROR", | ||
"message": "Current request cannot be processed due to unknown issue", | ||
"debugId": "12312-123123-123123-1231212", | ||
}) | ||
}, | ||
Config: provider.ProviderConfig + ` | ||
data "uxi_agent_group_assignment" "my_agent_group_assignment" { | ||
filter = { | ||
agent_group_assignment_id = "uid" | ||
} | ||
} | ||
`, | ||
ExpectError: regexp.MustCompile( | ||
`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`, | ||
), | ||
}, | ||
{ | ||
PreConfig: func() { | ||
util.MockGetAgentGroupAssignment( | ||
"uid", | ||
util.GeneratePaginatedResponse([]map[string]interface{}{}), | ||
1, | ||
) | ||
}, | ||
Config: provider.ProviderConfig + ` | ||
data "uxi_agent_group_assignment" "my_agent_group_assignment" { | ||
filter = { | ||
agent_group_assignment_id = "uid" | ||
} | ||
} | ||
`, | ||
ExpectError: regexp.MustCompile(`Could not find specified data source`), | ||
}, | ||
}, | ||
}) | ||
|
||
mockOAuth.Mock.Disable() | ||
} |