Skip to content

Commit

Permalink
NTP Settings Datasource Implementation (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
RayLiu7 authored Jan 31, 2024
1 parent 6be18ae commit e6f41b0
Show file tree
Hide file tree
Showing 8 changed files with 383 additions and 0 deletions.
69 changes: 69 additions & 0 deletions docs/data-sources/ntpsettings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
# Copyright (c) 2023 Dell Inc., or its subsidiaries. All Rights Reserved.
#
# Licensed under the Mozilla Public License Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://mozilla.org/MPL/2.0/
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

title: "powerscale_ntpsettings data source"
linkTitle: "powerscale_ntpsettings"
page_title: "powerscale_ntpsettings Data Source - terraform-provider-powerscale"
subcategory: ""
description: |-
This datasource is used to query the NTP Settings from PowerScale array. The information fetched from this datasource can be used for getting the details or for further processing in resource block. You can use NTP Settings to change the settings of NTP Servers
---

# powerscale_ntpsettings (Data Source)

This datasource is used to query the NTP Settings from PowerScale array. The information fetched from this datasource can be used for getting the details or for further processing in resource block. You can use NTP Settings to change the settings of NTP Servers

## Example Usage

```terraform
/*
Copyright (c) 2023 Dell Inc., or its subsidiaries. All Rights Reserved.
Licensed under the Mozilla Public License Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://mozilla.org/MPL/2.0/
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
# This Terraform DataSource is used to query the details of the NTP settings from PowerScale array.
# Returns the PowerScale NTP settings on PowerScale array
data "powerscale_ntpsettings" "all" {
}
# Output value of above block by executing 'terraform output' command
# You can use the the fetched information by the variable data.powerscale_ntpsettings.all
output "powerscale_ntpsettings_data_all" {
value = data.powerscale_ntpsettings.all
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Read-Only

- `chimers` (Number) Number of nodes that will contact the NTP servers.
- `excluded` (List of String) Node number (LNN) for nodes excluded from chimer duty.
- `key_file` (String) Path to NTP key file within /ifs.
28 changes: 28 additions & 0 deletions examples/data-sources/powerscale_ntpsettings/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright (c) 2023 Dell Inc., or its subsidiaries. All Rights Reserved.
Licensed under the Mozilla Public License Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://mozilla.org/MPL/2.0/
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

# This Terraform DataSource is used to query the details of the NTP settings from PowerScale array.

# Returns the PowerScale NTP settings on PowerScale array
data "powerscale_ntpsettings" "all" {
}

# Output value of above block by executing 'terraform output' command
# You can use the the fetched information by the variable data.powerscale_ntpsettings.all
output "powerscale_ntpsettings_data_all" {
value = data.powerscale_ntpsettings.all
}
30 changes: 30 additions & 0 deletions examples/data-sources/powerscale_ntpsettings/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright (c) 2023 Dell Inc., or its subsidiaries. All Rights Reserved.
Licensed under the Mozilla Public License Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://mozilla.org/MPL/2.0/
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
terraform {
required_providers {
powerscale = {
source = "registry.terraform.io/dell/powerscale"
}
}
}

provider "powerscale" {
username = var.username
password = var.password
endpoint = var.endpoint
insecure = var.insecure
}
10 changes: 10 additions & 0 deletions powerscale/helper/ntp_settings_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ import (
"context"
powerscale "dell/powerscale-go-client"
"terraform-provider-powerscale/client"
"terraform-provider-powerscale/powerscale/models"
)

// NtpSettingsDetailMapper Does the mapping from response to model.
//
//go:noinline
func NtpSettingsDetailMapper(ctx context.Context, ntpSettings *powerscale.V3NtpSettingsSettings) (models.NtpSettingsDataSourceModel, error) {
model := models.NtpSettingsDataSourceModel{}
err := CopyFields(ctx, ntpSettings, &model)
return model, err
}

// GetNtpSettings retrieve NTP Settings information.
func GetNtpSettings(ctx context.Context, client *client.Client) (*powerscale.V3NtpSettings, error) {
queryParam := client.PscaleOpenAPIClient.ProtocolsApi.GetProtocolsv3NtpSettings(ctx)
Expand Down
10 changes: 10 additions & 0 deletions powerscale/models/ntp_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ package models

import "github.com/hashicorp/terraform-plugin-framework/types"

// NtpSettingsDataSourceModel describes the datasource data model.
type NtpSettingsDataSourceModel struct {
// Number of nodes that will contact the NTP servers.
Chimers types.Int64 `tfsdk:"chimers"`
// Node number (LNN) for nodes excluded from chimer duty.
Excluded types.List `tfsdk:"excluded"`
// Path to NTP key file within /ifs.
KeyFile types.String `tfsdk:"key_file"`
}

// NtpSettingsResourceModel describes the resource data model.
type NtpSettingsResourceModel struct {
// Number of nodes that will contact the NTP servers.
Expand Down
141 changes: 141 additions & 0 deletions powerscale/provider/ntp_settings_datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
Copyright (c) 2023 Dell Inc., or its subsidiaries. All Rights Reserved.
Licensed under the Mozilla Public License Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://mozilla.org/MPL/2.0/
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package provider

import (
"context"
"fmt"
"terraform-provider-powerscale/client"
"terraform-provider-powerscale/powerscale/constants"
"terraform-provider-powerscale/powerscale/helper"
"terraform-provider-powerscale/powerscale/models"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &NtpSettingsDataSource{}

// NewNtpSettingsDataSource creates a new data source.
func NewNtpSettingsDataSource() datasource.DataSource {
return &NtpSettingsDataSource{}
}

// NtpSettingsDataSource defines the data source implementation.
type NtpSettingsDataSource struct {
client *client.Client
}

// Metadata describes the data source arguments.
func (d *NtpSettingsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_ntpsettings"
}

// Schema describes the data source arguments.
func (d *NtpSettingsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "This datasource is used to query the NTP Settings from PowerScale array. The information fetched from this datasource can be used for getting the details or for further processing in resource block. You can use NTP Settings to change the settings of NTP Servers",
Description: "This datasource is used to query the NTP Settings from PowerScale array. The information fetched from this datasource can be used for getting the details or for further processing in resource block. You can use NTP Settings to change the settings of NTP Servers",
Attributes: map[string]schema.Attribute{
"chimers": schema.Int64Attribute{
Description: "Number of nodes that will contact the NTP servers.",
MarkdownDescription: "Number of nodes that will contact the NTP servers.",
Computed: true,
},
"excluded": schema.ListAttribute{
Description: "Node number (LNN) for nodes excluded from chimer duty.",
MarkdownDescription: "Node number (LNN) for nodes excluded from chimer duty.",
Computed: true,
ElementType: types.StringType,
},
"key_file": schema.StringAttribute{
Description: "Path to NTP key file within /ifs.",
MarkdownDescription: "Path to NTP key file within /ifs.",
Computed: true,
},
},
}
}

// Configure configures the data source.
func (d *NtpSettingsDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

pscaleClient, ok := req.ProviderData.(*client.Client)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.client = pscaleClient
}

// Read reads data from the data source.
func (d *NtpSettingsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
tflog.Info(ctx, "Reading ntp settings data source")

var state models.NtpSettingsDataSourceModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)

if resp.Diagnostics.HasError() {
return
}

ntpSettingsResp, err := helper.GetNtpSettings(ctx, d.client)

if err != nil {
errStr := constants.ReadNtpSettingsErrorMsg + "with error: "
message := helper.GetErrorString(err, errStr)
resp.Diagnostics.AddError(
"Error getting the ntp settings",
message,
)
return
}

ntpSettings, err := helper.NtpSettingsDetailMapper(ctx, ntpSettingsResp.Settings)
if err != nil {
errStr := constants.ReadNtpSettingsErrorMsg + "with error: "
message := helper.GetErrorString(err, errStr)
resp.Diagnostics.AddError(
"Error mapping the list of ntp settings",
message,
)
return
}

state = ntpSettings

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
tflog.Info(ctx, "Done with reading ntp settings data source ")
}
Loading

0 comments on commit e6f41b0

Please sign in to comment.