Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provider | use wired and wireless network methods #25

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions pkg/config-api-provider/provider/resources/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,15 @@ type groupResourceModel struct {
ParentGroupId types.String `tfsdk:"parent_group_id"`
}

// TODO: Remove this once we rely fully on client for groups
type GroupResponseModel struct {
UID string `json:"uid"`
Name string `json:"name"`
ParentUid *string `json:"parent_uid"`
Path string `json:"path"`
}

type GroupCreateRequestModel struct {
Name string
ParentUid string
}

// TODO: Remove this once we rely fully on client for groups
type GroupUpdateRequestModel struct {
Name string
}
Expand Down Expand Up @@ -209,19 +206,6 @@ func (r *groupResource) ImportState(ctx context.Context, req resource.ImportStat
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

var GetGroup = func(uid string) GroupResponseModel {
// TODO: Query the group using the client

parent_uid := "mock_parent_uid"

return GroupResponseModel{
UID: uid,
Name: "mock_name",
ParentUid: &parent_uid,
Path: "mock_path",
}
}

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

Expand Down
74 changes: 36 additions & 38 deletions pkg/config-api-provider/provider/resources/wired_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import (
"context"

// "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client"
"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand All @@ -23,26 +23,13 @@
Alias types.String `tfsdk:"alias"`
}

// TODO: Switch this to use the Client Model when that becomes available
type WiredNetworkResponseModel struct {
Uid string // <network_uid:str>,
Alias string // <network_alias>,
DatetimeCreated string // <created_at:str(isoformat(with timezone?))>,
DatetimeUpdated string // <updated_at:str(isoformat(with timezone?))>,
IpVersion string // <ip_version:str>,
Security string // <security:str>,
DnsLookupDomain string // <dns_lookup_domain:str> | Nullable,
DisableEdns bool // <disable_edns:bool>,
UseDns64 bool // <use_dns64:bool>,
ExternalConnectivity bool // <external_connectivity:bool>
VlanId int // <vlan_id:int>
}

func NewWiredNetworkResource() resource.Resource {
return &wiredNetworkResource{}
}

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

func (r *wiredNetworkResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_wired_network"
Expand All @@ -65,6 +52,23 @@
}

func (r *wiredNetworkResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.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",
"Resource type: Wired Network. Please report this issue to the provider developers.",
)
return

Check warning on line 68 in pkg/config-api-provider/provider/resources/wired_network.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/wired_network.go#L64-L68

Added lines #L64 - L68 were not covered by tests
}

r.client = client
}

func (r *wiredNetworkResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
Expand All @@ -84,10 +88,23 @@
return
}

response := GetWiredNetwork(state.ID.ValueString())
networkResponse, _, err := r.client.ConfigurationAPI.
GetConfigurationAppV1WiredNetworksGet(context.Background()).
Uid(state.ID.ValueString()).
Execute()

if err != nil || len(networkResponse.WiredNetworks) != 1 {
resp.Diagnostics.AddError(
"Error reading Wired Network",
"Could not retrieve Wired Network, unexpected error: "+err.Error(),
)
return

Check warning on line 101 in pkg/config-api-provider/provider/resources/wired_network.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/wired_network.go#L97-L101

Added lines #L97 - L101 were not covered by tests
}

network := networkResponse.WiredNetworks[0]

// Update state from client response
state.Alias = types.StringValue(response.Alias)
state.Alias = types.StringValue(network.Alias)

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -116,22 +133,3 @@
func (r *wiredNetworkResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

// Get the wiredNetwork using the configuration-api client
var GetWiredNetwork = func(uid string) WiredNetworkResponseModel {
// TODO: Query the wiredNetwork using the client

return WiredNetworkResponseModel{
Uid: "mock_uid",
Alias: "mock_alias",
DatetimeCreated: "mock_datetime_created",
DatetimeUpdated: "mock_datetime_updated",
IpVersion: "mock_ip_version",
Security: "mock_security",
DnsLookupDomain: "mock_dns_lookup_domain",
DisableEdns: false,
UseDns64: false,
ExternalConnectivity: false,
VlanId: 123,
}
}
78 changes: 36 additions & 42 deletions pkg/config-api-provider/provider/resources/wireless_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import (
"context"

// "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client"
"github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand All @@ -23,28 +23,13 @@
Alias types.String `tfsdk:"alias"`
}

// TODO: Switch this to use the Client Model when that becomes available
type WirelessNetworkResponseModel struct {
Uid string // <network_uid:str>,
Ssid string // <ssid:str>,
DatetimeCreated string // <created_at:str(isoformat(with timezone?))>,
DatetimeUpdated string // <updated_at:str(isoformat(with timezone?))>,
Alias string // <network_alias>,
IpVersion string // <ip_version:str>,
Security string // <security:str>,
Hidden bool // <hidden:bool>,
BandLocking string // <band_locking:str>,
DnsLookupDomain string // <dns_lookup_domain:str> | Nullable,
DisableEdns bool // <disable_edns:bool>,
UseDns64 bool // <use_dns64:bool>,
ExternalConnectivity bool // <external_connectivity:bool>
}

func NewWirelessNetworkResource() resource.Resource {
return &wirelessNetworkResource{}
}

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

func (r *wirelessNetworkResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_wireless_network"
Expand All @@ -67,6 +52,23 @@
}

func (r *wirelessNetworkResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.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",
"Resource type: Wireless Network. Please report this issue to the provider developers.",
)
return

Check warning on line 68 in pkg/config-api-provider/provider/resources/wireless_network.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/wireless_network.go#L64-L68

Added lines #L64 - L68 were not covered by tests
}

r.client = client
}

func (r *wirelessNetworkResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
Expand All @@ -86,10 +88,23 @@
return
}

response := GetWirelessNetwork(state.ID.ValueString())
networkResponse, _, err := r.client.ConfigurationAPI.
GetConfigurationAppV1WirelessNetworksGet(context.Background()).
Uid(state.ID.ValueString()).
Execute()

if err != nil || len(networkResponse.WirelessNetworks) != 1 {
resp.Diagnostics.AddError(
"Error reading Wireless Networks",
"Could not retrieve Wireless Network, unexpected error: "+err.Error(),
)
return

Check warning on line 101 in pkg/config-api-provider/provider/resources/wireless_network.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/wireless_network.go#L97-L101

Added lines #L97 - L101 were not covered by tests
}

network := networkResponse.WirelessNetworks[0]

// Update state from client response
state.Alias = types.StringValue(response.Alias)
state.Alias = types.StringValue(network.Alias)

// Set refreshed state
diags = resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -118,24 +133,3 @@
func (r *wirelessNetworkResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

// Get the wirelessNetwork using the configuration-api client
var GetWirelessNetwork = func(uid string) WirelessNetworkResponseModel {
// TODO: Query the wirelessNetwork using the client

return WirelessNetworkResponseModel{
Uid: uid,
Ssid: "mock_ssid",
DatetimeCreated: "mock_datetime_created",
DatetimeUpdated: "mock_datetime_updated",
Alias: "mock_alias",
IpVersion: "mock_ip_version",
Security: "mock_security",
Hidden: false,
BandLocking: "mock_band_locking",
DnsLookupDomain: "mock_dns_lookup_domain",
DisableEdns: false,
UseDns64: false,
ExternalConnectivity: false,
}
}
3 changes: 1 addition & 2 deletions pkg/config-api-provider/test/agent_group_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (

func TestAgentGroupAssignmentResource(t *testing.T) {
defer gock.Off()
MockOAuth()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Creating a agent group assignment
{
PreConfig: func() {
MockOAuth()
// required for agent import
resources.GetAgent = func(uid string) resources.AgentResponseModel {
return GenerateAgentResponseModel(uid, "")
Expand Down Expand Up @@ -78,7 +78,6 @@ func TestAgentGroupAssignmentResource(t *testing.T) {
// Update and Read testing
{
PreConfig: func() {
MockOAuth()
resources.GetAgent = func(uid string) resources.AgentResponseModel {
if uid == "agent_uid" {
return GenerateAgentResponseModel(uid, "")
Expand Down
1 change: 1 addition & 0 deletions pkg/config-api-provider/test/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

func TestAgentResource(t *testing.T) {
defer gock.Off()
MockOAuth()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Expand Down
3 changes: 1 addition & 2 deletions pkg/config-api-provider/test/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ type Fetcher interface {

func TestGroupResource(t *testing.T) {
defer gock.Off()
MockOAuth()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and Read testing
{
PreConfig: func() {
MockOAuth()
MockPostGroup(StructToMap(GenerateGroupResponseModel("uid", "", "")), 1)
MockGetGroup("uid", GenerateGroupPaginatedResponse(
[]map[string]interface{}{
Expand Down Expand Up @@ -84,7 +84,6 @@ func TestGroupResource(t *testing.T) {
// Update that does trigger a recreate
{
PreConfig: func() {
MockOAuth()
// existing group
MockGetGroup("uid", GenerateGroupPaginatedResponse(
[]map[string]interface{}{
Expand Down
Loading
Loading