Skip to content

Commit

Permalink
d/system_information: use new provider via framework
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremmfr committed Jan 16, 2025
1 parent b6e19e1 commit 92d2bbf
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 100 deletions.
1 change: 1 addition & 0 deletions .changes/latest-system-resources-with-fwk.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ENHANCEMENTS:
optional boolean attributes doesn't accept value *false*
optional string attributes doesn't accept *empty* value
the resource schema has been upgraded to have one-blocks in single mode instead of list
* **data-source/junos_system_information** data-source now use new [terraform-plugin-framework](https://github.com/hashicorp/terraform-plugin-framework)

BUG FIXES:

Expand Down
12 changes: 6 additions & 6 deletions internal/junos/netconf_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ type rpcGetSystemInformationReply struct {
}

type rpcSystemInformation struct {
HardwareModel string `xml:"hardware-model"`
OsName string `xml:"os-name"`
OsVersion string `xml:"os-version"`
SerialNumber string `xml:"serial-number"`
HostName string `xml:"host-name"`
ClusterNode *bool `xml:"cluster-node"`
HardwareModel string `xml:"hardware-model"`
OSName string `xml:"os-name"`
OSVersion string `xml:"os-version"`
SerialNumber string `xml:"serial-number"`
HostName string `xml:"host-name"`
ClusterNode *struct{} `xml:"cluster-node"`
}

func (i rpcSystemInformation) NotCompatibleMsg() string {
Expand Down
142 changes: 142 additions & 0 deletions internal/providerfwk/data_source_system_information.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package providerfwk

import (
"context"

"github.com/jeremmfr/terraform-provider-junos/internal/junos"

"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 = &systemInformationDataSource{}
_ datasource.DataSourceWithConfigure = &systemInformationDataSource{}
)

type systemInformationDataSource struct {
client *junos.Client
}

func (dsc *systemInformationDataSource) typeName() string {
return providerName + "_system_information"
}

func (dsc *systemInformationDataSource) junosClient() *junos.Client {
return dsc.client
}

func newSystemInformationDataSource() datasource.DataSource {
return &systemInformationDataSource{}
}

func (dsc *systemInformationDataSource) Metadata(
_ context.Context, _ datasource.MetadataRequest, resp *datasource.MetadataResponse,
) {
resp.TypeName = dsc.typeName()
}

func (dsc *systemInformationDataSource) Configure(
ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse,
) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*junos.Client)
if !ok {
unexpectedDataSourceConfigureType(ctx, req, resp)

return
}
dsc.client = client
}

func (dsc *systemInformationDataSource) Schema(
_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse,
) {
resp.Schema = schema.Schema{
Description: "Get information of the Junos device system information.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "Hostname of the Junos device or `Null-Hostname` if not set.",
},
"hardware_model": schema.StringAttribute{
Computed: true,
Description: "Type of hardware/software of Junos device.",
},
"os_name": schema.StringAttribute{
Computed: true,
Description: "Operating system name of Junos.",
},
"os_version": schema.StringAttribute{
Computed: true,
Description: "Software version of Junos.",
},
"serial_number": schema.StringAttribute{
Computed: true,
Description: "Serial number of the device.",
},
"cluster_node": schema.BoolAttribute{
Computed: true,
Description: "Boolean flag that indicates if device is part of a cluster or not.",
},
},
}
}

type systemInformationDataSourceData struct {
ID types.String `tfsdk:"id"`
HardwareModel types.String `tfsdk:"hardware_model"`
OSName types.String `tfsdk:"os_name"`
OSVersion types.String `tfsdk:"os_version"`
SerialNumber types.String `tfsdk:"serial_number"`
ClusterNode types.Bool `tfsdk:"cluster_node"`

hostName string `tfsdk:"-"`
}

func (dsc *systemInformationDataSource) Read(
ctx context.Context, _ datasource.ReadRequest, resp *datasource.ReadResponse,
) {
var data systemInformationDataSourceData

var _ dataSourceDataReadWithoutArg = &data
defaultDataSourceRead(
ctx,
dsc,
nil,
&data,
resp,
)
}

func (dscData *systemInformationDataSourceData) fillID() {
if v := dscData.hostName; v != "" {
dscData.ID = types.StringValue(v)
} else {
dscData.ID = types.StringValue("Null-Hostname")
}
}

func (dscData *systemInformationDataSourceData) read(
_ context.Context, junSess *junos.Session,
) error {
dscData.hostName = junSess.SystemInformation.HostName
dscData.HardwareModel = types.StringValue(junSess.SystemInformation.HardwareModel)
dscData.OSName = types.StringValue(junSess.SystemInformation.OSName)
dscData.OSVersion = types.StringValue(junSess.SystemInformation.OSVersion)
dscData.SerialNumber = types.StringValue(junSess.SystemInformation.SerialNumber)

// Pointer will be nil if the tag does not exist
if junSess.SystemInformation.ClusterNode != nil {
dscData.ClusterNode = types.BoolValue(true)
} else {
dscData.ClusterNode = types.BoolValue(false)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package providersdk_test
package providerfwk_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccDataSourceSystemInformation_basic(t *testing.T) {
Expand All @@ -12,8 +13,9 @@ func TestAccDataSourceSystemInformation_basic(t *testing.T) {
ProtoV5ProviderFactories: testAccProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccSystemInformationConfig(),
ConfigDirectory: config.TestStepDirectory(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.junos_system_information.test", "id"),
resource.TestCheckResourceAttrSet("data.junos_system_information.test", "hardware_model"),
resource.TestCheckResourceAttrSet("data.junos_system_information.test", "os_name"),
resource.TestCheckResourceAttrSet("data.junos_system_information.test", "os_version"),
Expand All @@ -24,9 +26,3 @@ func TestAccDataSourceSystemInformation_basic(t *testing.T) {
},
})
}

func testAccSystemInformationConfig() string {
return `
data "junos_system_information" "test" {}
`
}
1 change: 1 addition & 0 deletions internal/providerfwk/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func (p *junosProvider) DataSources(_ context.Context) []func() datasource.DataS
newRoutesDataSource,
newRoutingInstanceDataSource,
newSecurityZoneDataSource,
newSystemInformationDataSource,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data "junos_system_information" "test" {}
82 changes: 0 additions & 82 deletions internal/providersdk/data_source_system_information.go

This file was deleted.

3 changes: 0 additions & 3 deletions internal/providersdk/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,6 @@ func Provider() *schema.Provider {
"junos_services_user_identification_ad_access_domain": resourceServicesUserIdentAdAccessDomain(),
"junos_services_user_identification_device_identity_profile": resourceServicesUserIdentDeviceIdentityProfile(),
},
DataSourcesMap: map[string]*schema.Resource{
"junos_system_information": dataSourceSystemInformation(),
},
ConfigureContextFunc: configureProvider,
}
}
Expand Down

0 comments on commit 92d2bbf

Please sign in to comment.