Skip to content

Commit

Permalink
Merge branch 'main' into ay/feat/acceptance-tests/services
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 authored Nov 7, 2024
2 parents 11ba15f + d45609e commit ea8945c
Show file tree
Hide file tree
Showing 62 changed files with 2,134 additions and 123 deletions.
151 changes: 151 additions & 0 deletions internal/provider/datasources/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
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 = &serviceTestDataSource{}
_ datasource.DataSourceWithConfigure = &serviceTestDataSource{}
)

func NewServiceTestDataSource() datasource.DataSource {
return &serviceTestDataSource{}
}

type serviceTestDataSource struct {
client *config_api_client.APIClient
}

type serviceTestDataSourceModel struct {
Id types.String `tfsdk:"id"`
Category types.String `tfsdk:"category"`
Name types.String `tfsdk:"name"`
Target types.String `tfsdk:"target"`
Template types.String `tfsdk:"template"`
IsEnabled types.String `tfsdk:"is_enabled"`
Filter struct {
ServiceTestID types.String `tfsdk:"service_test_id"`
} `tfsdk:"filter"`
}

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

func (d *serviceTestDataSource) Schema(
_ context.Context,
_ datasource.SchemaRequest,
resp *datasource.SchemaResponse,
) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
},
"category": schema.StringAttribute{
Computed: true,
},
"name": schema.StringAttribute{
Computed: true,
},
"target": schema.StringAttribute{
Computed: true,
},
"template": schema.StringAttribute{
Computed: true,
},
"is_enabled": schema.StringAttribute{
Computed: true,
},
"filter": schema.SingleNestedAttribute{
Required: true,
Attributes: map[string]schema.Attribute{
"service_test_id": schema.StringAttribute{
Required: true,
},
},
},
},
}
}

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

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

request := d.client.ConfigurationAPI.
ServiceTestsGet(ctx).
Id(state.Filter.ServiceTestID.ValueString())

serviceTestResponse, response, err := util.RetryFor429(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

errorSummary := util.GenerateErrorSummary("read", "uxi_service_test")

if errorPresent {
resp.Diagnostics.AddError(errorSummary, errorDetail)
return
}

if len(serviceTestResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified data source")
return
}

serviceTest := serviceTestResponse.Items[0]

state.Id = types.StringValue(serviceTest.Id)
state.Name = types.StringValue(serviceTest.Name)

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

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

d.client = client
}
15 changes: 8 additions & 7 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,27 +169,28 @@ func (p *uxiConfigurationProvider) DataSources(_ context.Context) []func() datas
return []func() datasource.DataSource{
datasources.NewAgentDataSource,
datasources.NewGroupDataSource,
datasources.NewNetworkGroupAssignmentDataSource,
datasources.NewSensorDataSource,
datasources.NewSensorGroupAssignmentDataSource,
datasources.NewServiceTestDataSource,
datasources.NewWiredNetworkDataSource,
datasources.NewWirelessNetworkDataSource,
datasources.NewSensorGroupAssignmentDataSource,
datasources.NewNetworkGroupAssignmentDataSource,
}
}

// Resources defines the resources implemented in the provider.
func (p *uxiConfigurationProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
resources.NewAgentGroupAssignmentResource,
resources.NewAgentResource,
resources.NewGroupResource,
resources.NewNetworkGroupAssignmentResource,
resources.NewSensorGroupAssignmentResource,
resources.NewSensorResource,
resources.NewServiceTestGroupAssignmentResource,
resources.NewServiceTestResource,
resources.NewWiredNetworkResource,
resources.NewWirelessNetworkResource,
resources.NewServiceTestResource,
resources.NewSensorGroupAssignmentResource,
resources.NewAgentGroupAssignmentResource,
resources.NewNetworkGroupAssignmentResource,
resources.NewServiceTestGroupAssignmentResource,
}
}

Expand Down
55 changes: 40 additions & 15 deletions internal/provider/resources/agent_group_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package resources

import (
"context"
"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/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -38,7 +40,9 @@ func NewAgentGroupAssignmentResource() resource.Resource {
return &agentGroupAssignmentResource{}
}

type agentGroupAssignmentResource struct{}
type agentGroupAssignmentResource struct {
client *config_api_client.APIClient
}

func (r *agentGroupAssignmentResource) Metadata(
ctx context.Context,
Expand Down Expand Up @@ -82,6 +86,21 @@ func (r *agentGroupAssignmentResource) Configure(
req resource.ConfigureRequest,
resp *resource.ConfigureResponse,
) {
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*config_api_client.APIClient)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
"Resource type: Network Group Assignment. Please report this issue to the provider developers.",
)
return
}

r.client = client
}

func (r *agentGroupAssignmentResource) Create(
Expand Down Expand Up @@ -129,12 +148,28 @@ func (r *agentGroupAssignmentResource) Read(
return
}

// TODO: Call client getAgentGroupAssignment method
agentGroupAssignment := GetAgentGroupAssignment(state.ID.ValueString())
request := r.client.ConfigurationAPI.
AgentGroupAssignmentsGet(ctx).
Id(state.ID.ValueString())
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.State.RemoveResource(ctx)
return
}
agentGroupAssignment := agentGroupAssignmentResponse.Items[0]

// Update state from client response
state.GroupID = types.StringValue(agentGroupAssignment.GroupUID)
state.AgentID = types.StringValue(agentGroupAssignment.AgentUID)
state.GroupID = types.StringValue(agentGroupAssignment.Group.Id)
state.AgentID = types.StringValue(agentGroupAssignment.Agent.Id)

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -183,16 +218,6 @@ func (r *agentGroupAssignmentResource) ImportState(
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

var GetAgentGroupAssignment = func(uid string) AgentGroupAssignmentResponseModel {
// TODO: Query the agentGroupAssignment using the client

return AgentGroupAssignmentResponseModel{
UID: uid,
GroupUID: "mock_group_uid",
AgentUID: "mock_agent_uid",
}
}

var CreateAgentGroupAssignment = func(request AgentGroupAssignmentRequestModel) AgentGroupAssignmentResponseModel {
// TODO: Query the agentGroupAssignment using the client

Expand Down
6 changes: 6 additions & 0 deletions pkg/config-api-client/.openapi-generator/FILES

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion pkg/config-api-client/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ea8945c

Please sign in to comment.