From df9ea771eef47d58f7319303476b05ee4e361445 Mon Sep 17 00:00:00 2001 From: danischm Date: Tue, 5 Nov 2024 12:02:14 +0100 Subject: [PATCH] Add network policies by client data source --- CHANGELOG.md | 1 + .../network_policies_by_client.md | 57 +++++++ docs/guides/changelog.md | 1 + .../data-source.tf | 3 + .../network_policies_by_client.yaml | 61 +++++++ ...ource_meraki_network_policies_by_client.go | 158 ++++++++++++++++++ ..._meraki_network_policies_by_client_test.go | 77 +++++++++ ...model_meraki_network_policies_by_client.go | 129 ++++++++++++++ internal/provider/provider.go | 1 + templates/guides/changelog.md.tmpl | 1 + 10 files changed, 489 insertions(+) create mode 100644 docs/data-sources/network_policies_by_client.md create mode 100644 examples/data-sources/meraki_network_policies_by_client/data-source.tf create mode 100644 gen/definitions/network_policies_by_client.yaml create mode 100644 internal/provider/data_source_meraki_network_policies_by_client.go create mode 100644 internal/provider/data_source_meraki_network_policies_by_client_test.go create mode 100644 internal/provider/model_meraki_network_policies_by_client.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 479a8fc..0a1e9bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ - Add `meraki_organization_devices` data source - Add `meraki_organization_firmware_upgrades` data source - Add `meraki_organization_inventory_devices` data source +- Add `meraki_network_policies_by_client` data source ## 0.1.2 diff --git a/docs/data-sources/network_policies_by_client.md b/docs/data-sources/network_policies_by_client.md new file mode 100644 index 0000000..063503e --- /dev/null +++ b/docs/data-sources/network_policies_by_client.md @@ -0,0 +1,57 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "meraki_network_policies_by_client Data Source - terraform-provider-meraki" +subcategory: "" +description: |- + This data source can read the Network Policies By Client configuration. +--- + +# meraki_network_policies_by_client (Data Source) + +This data source can read the `Network Policies By Client` configuration. + +## Example Usage + +```terraform +data "meraki_network_policies_by_client" "example" { + network_id = "L_123456" +} +``` + + +## Schema + +### Required + +- `network_id` (String) Network ID + +### Read-Only + +- `items` (Attributes List) The list of items (see [below for nested schema](#nestedatt--items)) + + +### Nested Schema for `items` + +Read-Only: + +- `assigned` (Attributes List) Assigned policies (see [below for nested schema](#nestedatt--items--assigned)) +- `client_id` (String) ID of client +- `id` (String) The id of the object +- `name` (String) Name of client + + +### Nested Schema for `items.assigned` + +Read-Only: + +- `group_policy_id` (String) id of policy +- `name` (String) name of policy +- `ssid` (Attributes List) ssid (see [below for nested schema](#nestedatt--items--assigned--ssid)) +- `type` (String) type of policy + + +### Nested Schema for `items.assigned.ssid` + +Read-Only: + +- `ssid_number` (Number) number of ssid diff --git a/docs/guides/changelog.md b/docs/guides/changelog.md index e856b62..27412c4 100644 --- a/docs/guides/changelog.md +++ b/docs/guides/changelog.md @@ -31,6 +31,7 @@ description: |- - Add `meraki_organization_devices` data source - Add `meraki_organization_firmware_upgrades` data source - Add `meraki_organization_inventory_devices` data source +- Add `meraki_network_pii_keys` data source ## 0.1.2 diff --git a/examples/data-sources/meraki_network_policies_by_client/data-source.tf b/examples/data-sources/meraki_network_policies_by_client/data-source.tf new file mode 100644 index 0000000..1554618 --- /dev/null +++ b/examples/data-sources/meraki_network_policies_by_client/data-source.tf @@ -0,0 +1,3 @@ +data "meraki_network_policies_by_client" "example" { + network_id = "L_123456" +} diff --git a/gen/definitions/network_policies_by_client.yaml b/gen/definitions/network_policies_by_client.yaml new file mode 100644 index 0000000..9d3d079 --- /dev/null +++ b/gen/definitions/network_policies_by_client.yaml @@ -0,0 +1,61 @@ +name: Network Policies By Client +bulk_name: Network Policies By Client +spec_endpoint: /networks/{networkId}/policies/byClient +rest_endpoint: /networks/%v/policies/byClient +no_data_source: true +no_resource: true +bulk_data_source: true +no_update: true +no_delete: true +data_source_name_query: true +doc_category: Networks +test_variables: [test_org, test_network] +attributes: + - tf_name: network_id + type: String + id: true + reference: true + description: Network ID + example: L_123456 + test_value: meraki_network.test.id + - model_name: clientId + type: String + description: ID of client + example: abc + - model_name: name + type: String + description: Name of client + example: my phone + - model_name: assigned + type: List + description: Assigned policies + attributes: + - model_name: groupPolicyId + type: String + description: id of policy + example: "100" + - model_name: name + type: String + description: name of policy + example: Allowed + - model_name: type + type: String + description: type of policy + example: ssid + - model_name: ssid + type: List + description: ssid + attributes: + - model_name: ssidNumber + type: Int64 + description: number of ssid + example: "1" +test_prerequisites: |2 + data "meraki_organization" "test" { + name = var.test_org + } + resource "meraki_network" "test" { + organization_id = data.meraki_organization.test.id + name = var.test_network + product_types = ["switch", "wireless", "appliance"] + } diff --git a/internal/provider/data_source_meraki_network_policies_by_client.go b/internal/provider/data_source_meraki_network_policies_by_client.go new file mode 100644 index 0000000..281ddba --- /dev/null +++ b/internal/provider/data_source_meraki_network_policies_by_client.go @@ -0,0 +1,158 @@ +// Copyright © 2024 Cisco Systems, Inc. and its affiliates. +// 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 +// +// https://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. +// +// SPDX-License-Identifier: MPL-2.0 + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "context" + "fmt" + + "github.com/CiscoDevNet/terraform-provider-meraki/internal/provider/helpers" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/netascode/go-meraki" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin model + +// Ensure the implementation satisfies the expected interfaces. +var ( + _ datasource.DataSource = &NetworkPoliciesByClientDataSource{} + _ datasource.DataSourceWithConfigure = &NetworkPoliciesByClientDataSource{} +) + +func NewNetworkPoliciesByClientDataSource() datasource.DataSource { + return &NetworkPoliciesByClientDataSource{} +} + +type NetworkPoliciesByClientDataSource struct { + client *meraki.Client +} + +func (d *NetworkPoliciesByClientDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_network_policies_by_client" +} + +func (d *NetworkPoliciesByClientDataSource) 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: helpers.NewAttributeDescription("This data source can read the `Network Policies By Client` configuration.").String, + + Attributes: map[string]schema.Attribute{ + "network_id": schema.StringAttribute{ + MarkdownDescription: "Network ID", + Required: true, + }, + "items": schema.ListNestedAttribute{ + MarkdownDescription: "The list of items", + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "id": schema.StringAttribute{ + MarkdownDescription: "The id of the object", + Computed: true, + }, + "client_id": schema.StringAttribute{ + MarkdownDescription: "ID of client", + Computed: true, + }, + "name": schema.StringAttribute{ + MarkdownDescription: "Name of client", + Computed: true, + }, + "assigned": schema.ListNestedAttribute{ + MarkdownDescription: "Assigned policies", + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "group_policy_id": schema.StringAttribute{ + MarkdownDescription: "id of policy", + Computed: true, + }, + "name": schema.StringAttribute{ + MarkdownDescription: "name of policy", + Computed: true, + }, + "type": schema.StringAttribute{ + MarkdownDescription: "type of policy", + Computed: true, + }, + "ssid": schema.ListNestedAttribute{ + MarkdownDescription: "ssid", + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "ssid_number": schema.Int64Attribute{ + MarkdownDescription: "number of ssid", + Computed: true, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + } +} + +func (d *NetworkPoliciesByClientDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { + if req.ProviderData == nil { + return + } + + d.client = req.ProviderData.(*MerakiProviderData).Client +} + +// End of section. //template:end model + +// Section below is generated&owned by "gen/generator.go". //template:begin read + +func (d *NetworkPoliciesByClientDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var config NetworkPoliciesByClient + + // Read config + diags := req.Config.Get(ctx, &config) + resp.Diagnostics.Append(diags...) + if resp.Diagnostics.HasError() { + return + } + + tflog.Debug(ctx, fmt.Sprintf("%s: Beginning Read", "NetworkPoliciesByClientDataSource")) + + res, err := d.client.Get(config.getPath()) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve object, got error: %s", err)) + return + } + + config.fromBody(ctx, res) + + tflog.Debug(ctx, fmt.Sprintf("%s: Read finished successfully", "NetworkPoliciesByClientDataSource")) + + diags = resp.State.Set(ctx, &config) + resp.Diagnostics.Append(diags...) +} + +// End of section. //template:end read diff --git a/internal/provider/data_source_meraki_network_policies_by_client_test.go b/internal/provider/data_source_meraki_network_policies_by_client_test.go new file mode 100644 index 0000000..68f248b --- /dev/null +++ b/internal/provider/data_source_meraki_network_policies_by_client_test.go @@ -0,0 +1,77 @@ +// Copyright © 2024 Cisco Systems, Inc. and its affiliates. +// 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 +// +// https://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. +// +// SPDX-License-Identifier: MPL-2.0 + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccDataSource + +func TestAccDataSourceMerakiNetworkPoliciesByClient(t *testing.T) { + if os.Getenv("TF_VAR_test_org") == "" || os.Getenv("TF_VAR_test_network") == "" { + t.Skip("skipping test, set environment variable TF_VAR_test_org and TF_VAR_test_network") + } + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceMerakiNetworkPoliciesByClientPrerequisitesConfig + testAccDataSourceMerakiNetworkPoliciesByClientConfig(), + }, + }, + }) +} + +// End of section. //template:end testAccDataSource + +// Section below is generated&owned by "gen/generator.go". //template:begin testPrerequisites + +const testAccDataSourceMerakiNetworkPoliciesByClientPrerequisitesConfig = ` +variable "test_org" {} +variable "test_network" {} +data "meraki_organization" "test" { + name = var.test_org +} +resource "meraki_network" "test" { + organization_id = data.meraki_organization.test.id + name = var.test_network + product_types = ["switch", "wireless", "appliance"] +} + +` + +// End of section. //template:end testPrerequisites + +// Section below is generated&owned by "gen/generator.go". //template:begin testAccDataSourceConfig + +func testAccDataSourceMerakiNetworkPoliciesByClientConfig() string { + config := `data "meraki_network_policies_by_client" "test" { + network_id = meraki_network.test.id + } + ` + return config +} + +// End of section. //template:end testAccDataSourceConfig diff --git a/internal/provider/model_meraki_network_policies_by_client.go b/internal/provider/model_meraki_network_policies_by_client.go new file mode 100644 index 0000000..14505f1 --- /dev/null +++ b/internal/provider/model_meraki_network_policies_by_client.go @@ -0,0 +1,129 @@ +// Copyright © 2024 Cisco Systems, Inc. and its affiliates. +// 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 +// +// https://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. +// +// SPDX-License-Identifier: MPL-2.0 + +package provider + +// Section below is generated&owned by "gen/generator.go". //template:begin imports +import ( + "context" + "fmt" + "net/url" + + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/netascode/go-meraki" + "github.com/tidwall/gjson" +) + +// End of section. //template:end imports + +// Section below is generated&owned by "gen/generator.go". //template:begin types + +type NetworkPoliciesByClient struct { + NetworkId types.String `tfsdk:"network_id"` + Items []NetworkPoliciesByClientItems `tfsdk:"items"` +} + +type NetworkPoliciesByClientItems struct { + Id types.String `tfsdk:"id"` + ClientId types.String `tfsdk:"client_id"` + Name types.String `tfsdk:"name"` + Assigned []NetworkPoliciesByClientAssigned `tfsdk:"assigned"` +} + +type NetworkPoliciesByClientAssigned struct { + GroupPolicyId types.String `tfsdk:"group_policy_id"` + Name types.String `tfsdk:"name"` + Type types.String `tfsdk:"type"` + Ssid []NetworkPoliciesByClientAssignedSsid `tfsdk:"ssid"` +} + +type NetworkPoliciesByClientAssignedSsid struct { + SsidNumber types.Int64 `tfsdk:"ssid_number"` +} + +// End of section. //template:end types + +// Section below is generated&owned by "gen/generator.go". //template:begin getPath + +func (data NetworkPoliciesByClient) getPath() string { + return fmt.Sprintf("/networks/%v/policies/byClient", url.QueryEscape(data.NetworkId.ValueString())) +} + +// End of section. //template:end getPath + +// Section below is generated&owned by "gen/generator.go". //template:begin fromBody + +func (data *NetworkPoliciesByClient) fromBody(ctx context.Context, res meraki.Res) { + data.Items = make([]NetworkPoliciesByClientItems, 0) + res.ForEach(func(k, res gjson.Result) bool { + parent := &data + data := NetworkPoliciesByClientItems{} + data.Id = types.StringValue(res.Get("").String()) + if value := res.Get("clientId"); value.Exists() && value.Value() != nil { + data.ClientId = types.StringValue(value.String()) + } else { + data.ClientId = types.StringNull() + } + if value := res.Get("name"); value.Exists() && value.Value() != nil { + data.Name = types.StringValue(value.String()) + } else { + data.Name = types.StringNull() + } + if value := res.Get("assigned"); value.Exists() && value.Value() != nil { + data.Assigned = make([]NetworkPoliciesByClientAssigned, 0) + value.ForEach(func(k, res gjson.Result) bool { + parent := &data + data := NetworkPoliciesByClientAssigned{} + if value := res.Get("groupPolicyId"); value.Exists() && value.Value() != nil { + data.GroupPolicyId = types.StringValue(value.String()) + } else { + data.GroupPolicyId = types.StringNull() + } + if value := res.Get("name"); value.Exists() && value.Value() != nil { + data.Name = types.StringValue(value.String()) + } else { + data.Name = types.StringNull() + } + if value := res.Get("type"); value.Exists() && value.Value() != nil { + data.Type = types.StringValue(value.String()) + } else { + data.Type = types.StringNull() + } + if value := res.Get("ssid"); value.Exists() && value.Value() != nil { + data.Ssid = make([]NetworkPoliciesByClientAssignedSsid, 0) + value.ForEach(func(k, res gjson.Result) bool { + parent := &data + data := NetworkPoliciesByClientAssignedSsid{} + if value := res.Get("ssidNumber"); value.Exists() && value.Value() != nil { + data.SsidNumber = types.Int64Value(value.Int()) + } else { + data.SsidNumber = types.Int64Null() + } + (*parent).Ssid = append((*parent).Ssid, data) + return true + }) + } + (*parent).Assigned = append((*parent).Assigned, data) + return true + }) + } + (*parent).Items = append((*parent).Items, data) + return true + }) +} + +// End of section. //template:end fromBody diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 0a98e6b..85c790f 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -417,6 +417,7 @@ func (p *MerakiProvider) DataSources(ctx context.Context) []func() datasource.Da NewNetworkMQTTBrokerDataSource, NewNetworkMQTTBrokersDataSource, NewNetworkNetflowDataSource, + NewNetworkPoliciesByClientDataSource, NewNetworkSettingsDataSource, NewNetworkSNMPDataSource, NewNetworkSyslogServersDataSource, diff --git a/templates/guides/changelog.md.tmpl b/templates/guides/changelog.md.tmpl index e856b62..27412c4 100644 --- a/templates/guides/changelog.md.tmpl +++ b/templates/guides/changelog.md.tmpl @@ -31,6 +31,7 @@ description: |- - Add `meraki_organization_devices` data source - Add `meraki_organization_firmware_upgrades` data source - Add `meraki_organization_inventory_devices` data source +- Add `meraki_network_pii_keys` data source ## 0.1.2