Skip to content

Commit

Permalink
feat: integrate latest client methods into provider (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 authored Oct 10, 2024
1 parent 931c759 commit c630a6c
Show file tree
Hide file tree
Showing 15 changed files with 687 additions and 270 deletions.
30 changes: 13 additions & 17 deletions pkg/config-api-provider/provider/resources/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,19 @@ func (r *groupResource) Update(ctx context.Context, req resource.UpdateRequest,
return
}

// TODO: Call client updateGroup method
group := UpdateGroup(GroupUpdateRequestModel{
Name: plan.Name.ValueString(),
})
patchRequest := config_api_client.NewGroupsPatchRequest("new_name")
request := r.client.ConfigurationAPI.
GroupsPatchUxiV1alpha1GroupsGroupUidPatch(ctx, plan.ID.ValueString()).
GroupsPatchRequest(*patchRequest)
group, _, err := util.RetryFor429(request.Execute)

if err != nil {
resp.Diagnostics.AddError(
"Error updating Group",
"Could not update Group, unexpected error: "+err.Error(),
)
return
}

// Update the state to match the plan (replace with response from client)
plan.Name = types.StringValue(group.Name)
Expand Down Expand Up @@ -203,16 +212,3 @@ func (r *groupResource) Delete(ctx context.Context, req resource.DeleteRequest,
func (r *groupResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

var UpdateGroup = func(request GroupUpdateRequestModel) config_api_client.GroupsPostResponse {
// TODO: Query the group using the client

parent_uid := "mock_parent_uid"

return config_api_client.GroupsPostResponse{
Id: "mock_uid",
Name: "mock_name",
Parent: *config_api_client.NewParent(parent_uid),
Path: "mock_path",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ type NetworkGroupAssignmentResponseModel struct {
NetworkUID string // <network_uid:str>
}

type NetworkGroupAssignmentRequestModel struct {
GroupUID string // <group_uid:str>,
NetworkUID string // <network_uid:str>
}

func NewNetworkGroupAssignmentResource() resource.Resource {
return &networkGroupAssignmentResource{}
}
Expand Down Expand Up @@ -100,16 +95,26 @@ func (r *networkGroupAssignmentResource) Create(ctx context.Context, req resourc
return
}

// TODO: Call client createNetworkGroupAssignment method
networkGroupAssignment := CreateNetworkGroupAssignment(NetworkGroupAssignmentRequestModel{
GroupUID: plan.GroupID.ValueString(),
NetworkUID: plan.NetworkID.ValueString(),
})
postRequest := config_api_client.NewNetworkGroupAssignmentsPostRequest(
plan.GroupID.ValueString(),
plan.NetworkID.ValueString(),
)
request := r.client.ConfigurationAPI.
PostUxiV1alpha1NetworkGroupAssignmentsPost(ctx).
NetworkGroupAssignmentsPostRequest(*postRequest)
networkGroupAssignment, _, err := util.RetryFor429(request.Execute)
if err != nil {
resp.Diagnostics.AddError(
"Error updating Network Group Assignment",
"Could not update Network Group Assignment, unexpected error: "+err.Error(),
)
return
}

// Update the state to match the plan
plan.ID = types.StringValue(networkGroupAssignment.UID)
plan.GroupID = types.StringValue(networkGroupAssignment.GroupUID)
plan.NetworkID = types.StringValue(networkGroupAssignment.NetworkUID)
plan.ID = types.StringValue(networkGroupAssignment.Id)
plan.GroupID = types.StringValue(networkGroupAssignment.Group.Id)
plan.NetworkID = types.StringValue(networkGroupAssignment.Network.Id)

// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
Expand All @@ -128,12 +133,11 @@ func (r *networkGroupAssignmentResource) Read(ctx context.Context, req resource.
return
}

// TODO: Call client getNetworkGroupAssignment method
request := r.client.ConfigurationAPI.
GetUxiV1alpha1NetworkGroupAssignmentsGet(ctx).
Uid(state.ID.ValueString())

networkGroupAssignmentResponse, _, err := util.RetryFor429(request.Execute)

if err != nil || len(networkGroupAssignmentResponse.Items) != 1 {
resp.Diagnostics.AddError(
"Error reading Network Group Assignment",
Expand Down Expand Up @@ -175,19 +179,19 @@ func (r *networkGroupAssignmentResource) Delete(ctx context.Context, req resourc
return
}

// Delete existing networkGroupAssignment using the plan_id
request := r.client.ConfigurationAPI.
DeleteNetworkGroupAssignmentUxiV1alpha1NetworkGroupAssignmentsIdDelete(ctx, state.ID.ValueString())
_, _, err := util.RetryFor429(request.Execute)

if err != nil {
resp.Diagnostics.AddError(
"Error deleting Network Group Assignment",
"Could not delete Network Group Assignment, unexpected error: "+err.Error(),
)
return
}
}

func (r *networkGroupAssignmentResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

var CreateNetworkGroupAssignment = func(request NetworkGroupAssignmentRequestModel) NetworkGroupAssignmentResponseModel {
// TODO: Query the networkGroupAssignment using the client

return NetworkGroupAssignmentResponseModel{
UID: "mock_uid",
GroupUID: "mock_group_uid",
NetworkUID: "mock_network_uid",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ func (r *sensorGroupAssignmentResource) Delete(ctx context.Context, req resource
}

// Delete existing sensorGroupAssignment using the plan_id
request := r.client.ConfigurationAPI.
DeleteSensorGroupAssignmentUxiV1alpha1SensorGroupAssignmentsIdDelete(ctx, state.ID.ValueString())
_, _, err := util.RetryFor429(request.Execute)

if err != nil {
resp.Diagnostics.AddError(
"Error deleting Sensor Group Assignment",
"Could not deleting Sensor Group Assignment, unexpected error: "+err.Error(),
)
return
}
}

func (r *sensorGroupAssignmentResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package resources

import (
"context"
"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/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand All @@ -23,22 +25,13 @@ type serviceTestGroupAssignmentResourceModel struct {
GroupID types.String `tfsdk:"group_id"`
}

type ServiceTestGroupAssignmentResponseModel struct {
UID string // <assignment_uid>
GroupUID string // <group_uid:str>,
ServiceTestUID string // <service_test_uid:str>
}

type ServiceTestGroupAssignmentRequestModel struct {
GroupUID string // <group_uid:str>,
ServiceTestUID string // <service_test_uid:str>
}

func NewServiceTestGroupAssignmentResource() resource.Resource {
return &serviceTestGroupAssignmentResource{}
}

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

func (r *serviceTestGroupAssignmentResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_service_test_group_assignment"
Expand Down Expand Up @@ -70,6 +63,21 @@ func (r *serviceTestGroupAssignmentResource) Schema(_ context.Context, _ resourc
}

func (r *serviceTestGroupAssignmentResource) Configure(_ context.Context, 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: Service Test Group Assignment. Please report this issue to the provider developers.",
)
return
}

r.client = client
}

func (r *serviceTestGroupAssignmentResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
Expand All @@ -81,16 +89,27 @@ func (r *serviceTestGroupAssignmentResource) Create(ctx context.Context, req res
return
}

// TODO: Call client createServiceTestGroupAssignment method
serviceTestGroupAssignment := CreateServiceTestGroupAssignment(ServiceTestGroupAssignmentRequestModel{
GroupUID: plan.GroupID.ValueString(),
ServiceTestUID: plan.ServiceTestID.ValueString(),
})
postRequest := config_api_client.NewServiceTestGroupAssignmentsPostRequest(
plan.GroupID.ValueString(),
plan.ServiceTestID.ValueString(),
)
request := r.client.ConfigurationAPI.
PostUxiV1alpha1ServiceTestGroupAssignmentsPost(ctx).
ServiceTestGroupAssignmentsPostRequest(*postRequest)
serviceTestGroupAssignment, _, err := util.RetryFor429(request.Execute)

if err != nil {
resp.Diagnostics.AddError(
"Error creating Service Test Group Assignment",
"Could not create Network Group Assignment, unexpected error: "+err.Error(),
)
return
}

// Update the state to match the plan
plan.ID = types.StringValue(serviceTestGroupAssignment.UID)
plan.GroupID = types.StringValue(serviceTestGroupAssignment.GroupUID)
plan.ServiceTestID = types.StringValue(serviceTestGroupAssignment.ServiceTestUID)
plan.ID = types.StringValue(serviceTestGroupAssignment.Id)
plan.GroupID = types.StringValue(serviceTestGroupAssignment.Group.Id)
plan.ServiceTestID = types.StringValue(serviceTestGroupAssignment.ServiceTest.Id)

// Set state to fully populated data
diags = resp.State.Set(ctx, plan)
Expand All @@ -113,8 +132,8 @@ func (r *serviceTestGroupAssignmentResource) Read(ctx context.Context, req resou
serviceTestGroupAssignment := GetServiceTestGroupAssignment(state.ID.ValueString())

// Update state from client response
state.GroupID = types.StringValue(serviceTestGroupAssignment.GroupUID)
state.ServiceTestID = types.StringValue(serviceTestGroupAssignment.ServiceTestUID)
state.GroupID = types.StringValue(serviceTestGroupAssignment.Group.Id)
state.ServiceTestID = types.StringValue(serviceTestGroupAssignment.ServiceTest.Id)

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -151,22 +170,13 @@ func (r *serviceTestGroupAssignmentResource) ImportState(ctx context.Context, re
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

var GetServiceTestGroupAssignment = func(uid string) ServiceTestGroupAssignmentResponseModel {
// TODO: Query the serviceTestGroupAssignment using the client

return ServiceTestGroupAssignmentResponseModel{
UID: uid,
GroupUID: "mock_group_uid",
ServiceTestUID: "mock_serviceTest_uid",
}
}

var CreateServiceTestGroupAssignment = func(request ServiceTestGroupAssignmentRequestModel) ServiceTestGroupAssignmentResponseModel {
var GetServiceTestGroupAssignment = func(uid string) config_api_client.ServiceTestGroupAssignmentsPostResponse {
// TODO: Query the serviceTestGroupAssignment using the client

return ServiceTestGroupAssignmentResponseModel{
UID: "mock_uid",
GroupUID: "mock_group_uid",
ServiceTestUID: "mock_serviceTest_uid",
resourceType := "uxi/service-test-group-assignment"
return config_api_client.ServiceTestGroupAssignmentsPostResponse{
Id: uid,
Group: *config_api_client.NewGroup("mock_group_uid"),
ServiceTest: *config_api_client.NewServiceTest("mock_serviceTest_uid"),
Type: &resourceType,
}
}
10 changes: 3 additions & 7 deletions pkg/config-api-provider/test/data-sources/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestGroupDataSource(t *testing.T) {
PreConfig: func() {
util.MockGetGroup(
"uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateGroupResponseGetModel("uid", "", ""))}),
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateGroupResponseModel("uid", "", "")}),
3,
)
},
Expand Down Expand Up @@ -84,14 +84,10 @@ func TestGroupDataSource429Handling(t *testing.T) {
mock429 = gock.New("https://test.api.capenetworks.com").
Get("/uxi/v1alpha1/groups").
Reply(429).
SetHeaders(map[string]string{
"X-RateLimit-Limit": "100",
"X-RateLimit-Remaining": "0",
"X-RateLimit-Reset": "1",
})
SetHeaders(util.RateLimitingHeaders)
util.MockGetGroup(
"uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.StructToMap(util.GenerateGroupResponseGetModel("uid", "", ""))}),
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateGroupResponseModel("uid", "", "")}),
3,
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestNetworkGroupAssignmentDataSource(t *testing.T) {
PreConfig: func() {
util.MockGetNetworkGroupAssignment(
"uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("uid", "")}),
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentResponse("uid", "")}),
3,
)
},
Expand Down Expand Up @@ -60,14 +60,10 @@ func TestNetworkGroupAssignmentSource429Handling(t *testing.T) {
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",
})
SetHeaders(util.RateLimitingHeaders)
util.MockGetNetworkGroupAssignment(
"uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentGetResponse("uid", "")}),
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentResponse("uid", "")}),
3,
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestSensorGroupAssignmentDataSource(t *testing.T) {
PreConfig: func() {
util.MockGetSensorGroupAssignment(
"uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateSensorGroupAssignmentGetResponse("uid", "")}),
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateSensorGroupAssignmentResponse("uid", "")}),
3,
)
},
Expand Down Expand Up @@ -60,14 +60,10 @@ func TestSensorGroupAssignmentSource429Handling(t *testing.T) {
mock429 = gock.New("https://test.api.capenetworks.com").
Get("/uxi/v1alpha1/sensor-group-assignments").
Reply(429).
SetHeaders(map[string]string{
"X-RateLimit-Limit": "100",
"X-RateLimit-Remaining": "0",
"X-RateLimit-Reset": "1",
})
SetHeaders(util.RateLimitingHeaders)
util.MockGetSensorGroupAssignment(
"uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateSensorGroupAssignmentGetResponse("uid", "")}),
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateSensorGroupAssignmentResponse("uid", "")}),
3,
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ func TestWiredNetworkDataSource429Handling(t *testing.T) {
mock429 = gock.New("https://test.api.capenetworks.com").
Get("/uxi/v1alpha1/wired-networks").
Reply(429).
SetHeaders(map[string]string{
"X-RateLimit-Limit": "100",
"X-RateLimit-Remaining": "0",
"X-RateLimit-Reset": "1",
})
SetHeaders(util.RateLimitingHeaders)
util.MockGetWiredNetwork(
"uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("uid", "")}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ func TestWirelessNetworkDataSource429Handling(t *testing.T) {
mock429 = gock.New("https://test.api.capenetworks.com").
Get("/uxi/v1alpha1/wireless-networks").
Reply(429).
SetHeaders(map[string]string{
"X-RateLimit-Limit": "100",
"X-RateLimit-Remaining": "0",
"X-RateLimit-Reset": "1",
})
SetHeaders(util.RateLimitingHeaders)
util.MockGetWirelessNetwork(
"uid",
util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateWirelessNetworkResponse("uid", "")}),
Expand Down
Loading

0 comments on commit c630a6c

Please sign in to comment.