-
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.
Merge remote-tracking branch 'origin/main' into ay/feat/data-source/g…
…roup
- Loading branch information
Showing
8 changed files
with
305 additions
and
11 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
pkg/config-api-provider/provider/data-sources/wired_network.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,107 @@ | ||
package datasources | ||
|
||
import ( | ||
"context" | ||
|
||
config_api_client "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client" | ||
"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 = &wiredNetworkDataSource{} | ||
_ datasource.DataSourceWithConfigure = &wiredNetworkDataSource{} | ||
) | ||
|
||
func NewWiredNetworkDataSource() datasource.DataSource { | ||
return &wiredNetworkDataSource{} | ||
} | ||
|
||
type wiredNetworkDataSource struct { | ||
client *config_api_client.APIClient | ||
} | ||
|
||
type wiredNetworkDataSourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
Filter struct { | ||
WiredNetworkID string `tfsdk:"wired_network_id"` | ||
} `tfsdk:"filter"` | ||
} | ||
|
||
func (d *wiredNetworkDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_wired_network" | ||
} | ||
|
||
func (d *wiredNetworkDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"filter": schema.SingleNestedAttribute{ | ||
Required: true, | ||
Attributes: map[string]schema.Attribute{ | ||
"wired_network_id": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *wiredNetworkDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var state wiredNetworkDataSourceModel | ||
|
||
// Read configuration from request | ||
diags := req.Config.Get(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
networkResponse, _, err := d.client.ConfigurationAPI. | ||
GetConfigurationAppV1WiredNetworksGet(context.Background()). | ||
Uid(state.Filter.WiredNetworkID). | ||
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 | ||
} | ||
|
||
network := networkResponse.WiredNetworks[0] | ||
state.ID = types.StringValue(network.Uid) | ||
|
||
// Set state | ||
diags = resp.State.Set(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (d *wiredNetworkDataSource) 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: Wired Network. Please report this issue to the provider developers.", | ||
) | ||
return | ||
} | ||
|
||
d.client = client | ||
} |
107 changes: 107 additions & 0 deletions
107
pkg/config-api-provider/provider/data-sources/wireless_network.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,107 @@ | ||
package datasources | ||
|
||
import ( | ||
"context" | ||
|
||
config_api_client "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client" | ||
"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 = &wirelessNetworkDataSource{} | ||
_ datasource.DataSourceWithConfigure = &wirelessNetworkDataSource{} | ||
) | ||
|
||
func NewWirelessNetworkDataSource() datasource.DataSource { | ||
return &wirelessNetworkDataSource{} | ||
} | ||
|
||
type wirelessNetworkDataSource struct { | ||
client *config_api_client.APIClient | ||
} | ||
|
||
type wirelessNetworkDataSourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
Filter struct { | ||
WirelessNetworkID string `tfsdk:"wireless_network_id"` | ||
} `tfsdk:"filter"` | ||
} | ||
|
||
func (d *wirelessNetworkDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_wireless_network" | ||
} | ||
|
||
func (d *wirelessNetworkDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"filter": schema.SingleNestedAttribute{ | ||
Required: true, | ||
Attributes: map[string]schema.Attribute{ | ||
"wireless_network_id": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (d *wirelessNetworkDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var state wirelessNetworkDataSourceModel | ||
|
||
// Read configuration from request | ||
diags := req.Config.Get(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
networkResponse, _, err := d.client.ConfigurationAPI. | ||
GetConfigurationAppV1WirelessNetworksGet(context.Background()). | ||
Uid(state.Filter.WirelessNetworkID). | ||
Execute() | ||
|
||
if err != nil || len(networkResponse.WirelessNetworks) != 1 { | ||
resp.Diagnostics.AddError( | ||
"Error reading Wireless Network", | ||
"Could not retrieve Wireless Network, unexpected error: "+err.Error(), | ||
) | ||
return | ||
} | ||
|
||
network := networkResponse.WirelessNetworks[0] | ||
state.ID = types.StringValue(network.Uid) | ||
|
||
// Set state | ||
diags = resp.State.Set(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (d *wirelessNetworkDataSource) 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: Wireless Network. 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
39 changes: 39 additions & 0 deletions
39
pkg/config-api-provider/test/data_source_wired_group_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,39 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/h2non/gock" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func TestWiredNetworkDataSource(t *testing.T) { | ||
defer gock.Off() | ||
MockOAuth() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Read testing | ||
{ | ||
PreConfig: func() { | ||
MockGetWiredNetwork( | ||
"uid", | ||
GenerateWiredNetworkPaginatedResponse([]map[string]interface{}{GenerateWiredNetworkResponse("uid", "")}), | ||
3, | ||
) | ||
}, | ||
Config: providerConfig + ` | ||
data "uxi_wired_network" "my_wired_network" { | ||
filter = { | ||
wired_network_id = "uid" | ||
} | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "id", "uid"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
39 changes: 39 additions & 0 deletions
39
pkg/config-api-provider/test/data_source_wireless_group_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,39 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/h2non/gock" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func TestWirelessNetworkDataSource(t *testing.T) { | ||
defer gock.Off() | ||
MockOAuth() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Read testing | ||
{ | ||
PreConfig: func() { | ||
MockGetWirelessNetwork( | ||
"uid", | ||
GenerateWirelessNetworkPaginatedResponse([]map[string]interface{}{GenerateWirelessNetworkResponse("uid", "")}), | ||
3, | ||
) | ||
}, | ||
Config: providerConfig + ` | ||
data "uxi_wireless_network" "my_wireless_network" { | ||
filter = { | ||
wireless_network_id = "uid" | ||
} | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "id", "uid"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
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
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
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