diff --git a/docs/data-sources/ntpsettings.md b/docs/data-sources/ntpsettings.md new file mode 100644 index 0000000..fb01b6c --- /dev/null +++ b/docs/data-sources/ntpsettings.md @@ -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 + +### 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. \ No newline at end of file diff --git a/examples/data-sources/powerscale_ntpsettings/data-source.tf b/examples/data-sources/powerscale_ntpsettings/data-source.tf new file mode 100644 index 0000000..b212f52 --- /dev/null +++ b/examples/data-sources/powerscale_ntpsettings/data-source.tf @@ -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 +} \ No newline at end of file diff --git a/examples/data-sources/powerscale_ntpsettings/provider.tf b/examples/data-sources/powerscale_ntpsettings/provider.tf new file mode 100644 index 0000000..1023f79 --- /dev/null +++ b/examples/data-sources/powerscale_ntpsettings/provider.tf @@ -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 +} \ No newline at end of file diff --git a/powerscale/helper/ntp_settings_helper.go b/powerscale/helper/ntp_settings_helper.go index 8bd8fc6..55d156a 100644 --- a/powerscale/helper/ntp_settings_helper.go +++ b/powerscale/helper/ntp_settings_helper.go @@ -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) diff --git a/powerscale/models/ntp_settings.go b/powerscale/models/ntp_settings.go index dd2c653..ddff89d 100644 --- a/powerscale/models/ntp_settings.go +++ b/powerscale/models/ntp_settings.go @@ -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. diff --git a/powerscale/provider/ntp_settings_datasource.go b/powerscale/provider/ntp_settings_datasource.go new file mode 100644 index 0000000..514be2c --- /dev/null +++ b/powerscale/provider/ntp_settings_datasource.go @@ -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 ") +} diff --git a/powerscale/provider/ntp_settings_datasource_test.go b/powerscale/provider/ntp_settings_datasource_test.go new file mode 100644 index 0000000..7811287 --- /dev/null +++ b/powerscale/provider/ntp_settings_datasource_test.go @@ -0,0 +1,94 @@ +/* +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" + "regexp" + "terraform-provider-powerscale/client" + "terraform-provider-powerscale/powerscale/helper" + "testing" + + "github.com/bytedance/mockey" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestAccNtpSettingsDataSourceAll(t *testing.T) { + var ntpServerTerraformName = "data.powerscale_ntpsettings.all" + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // read all + { + Config: ProviderConfig + NtpSettingsAllDataSourceConfig, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr(ntpServerTerraformName, "key_file", "/ifs/"), + ), + }, + }, + }) +} + +func TestAccNtpSettingsDataSourceGettingErr(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + PreConfig: func() { + FunctionMocker = mockey.Mock(helper.GetNtpSettings).Return(nil, fmt.Errorf("mock error")).Build(). + When(func(ctx context.Context, client *client.Client) bool { + return FunctionMocker.Times() == 2 + }) + }, + Config: ProviderConfig + NtpSettingsAllDataSourceConfig, + ExpectError: regexp.MustCompile(`.*mock error*.`), + }, + }, + }) +} + +func TestAccNtpSettingsDataSourceMappingErr(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + PreConfig: func() { + FunctionMocker = mockey.Mock(helper.NtpSettingsDetailMapper).Return(nil, fmt.Errorf("mock error")).Build() + }, + Config: ProviderConfig + NtpSettingsAllDataSourceConfig, + ExpectError: regexp.MustCompile(`.*mock error*.`), + }, + }, + }) +} + +var NtpSettingsAllDataSourceConfig = ` +resource "powerscale_ntpsettings" "ntp_settings_test" { + key_file = "/ifs/" +} + +data "powerscale_ntpsettings" "all" { + depends_on = [ + powerscale_ntpsettings.ntp_settings_test + ] +} +` diff --git a/powerscale/provider/provider.go b/powerscale/provider/provider.go index 55242c7..4724ad3 100644 --- a/powerscale/provider/provider.go +++ b/powerscale/provider/provider.go @@ -194,6 +194,7 @@ func (p *PscaleProvider) DataSources(ctx context.Context) []func() datasource.Da NewAdsProviderDataSource, NewNetworkPoolDataSource, NewNtpServerDataSource, + NewNtpSettingsDataSource, NewUserGroupDataSource, NewNfsExportDataSource, NewSnapshotDataSource,