Skip to content

Commit

Permalink
Add organization adaptive policy group resource and data source
Browse files Browse the repository at this point in the history
  • Loading branch information
danischm committed Sep 18, 2024
1 parent 137e980 commit 11d0153
Show file tree
Hide file tree
Showing 12 changed files with 1,061 additions and 0 deletions.
46 changes: 46 additions & 0 deletions docs/data-sources/organization_adaptive_policy_group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "meraki_organization_adaptive_policy_group Data Source - terraform-provider-meraki"
subcategory: "Organizations"
description: |-
This data source can read the Organization Adaptive Policy Group configuration.
---

# meraki_organization_adaptive_policy_group (Data Source)

This data source can read the `Organization Adaptive Policy Group` configuration.

## Example Usage

```terraform
data "meraki_organization_adaptive_policy_group" "example" {
id = "12345678"
organization_id = "123456"
}
```

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

### Required

- `organization_id` (String) Organization ID

### Optional

- `id` (String) The id of the object
- `name` (String) Name of the group

### Read-Only

- `description` (String) Description of the group (default: '')
- `policy_objects` (Attributes List) The policy objects that belong to this group; traffic from addresses specified by these policy objects will be tagged with this group`s SGT value if no other tagging scheme is being used (each requires one unique attribute) (default: []) (see [below for nested schema](#nestedatt--policy_objects))
- `sgt` (Number) SGT value of the group

<a id="nestedatt--policy_objects"></a>
### Nested Schema for `policy_objects`

Read-Only:

- `id` (String) The ID of the policy object
- `name` (String) The name of the policy object
62 changes: 62 additions & 0 deletions docs/resources/organization_adaptive_policy_group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "meraki_organization_adaptive_policy_group Resource - terraform-provider-meraki"
subcategory: "Organizations"
description: |-
This resource can manage the Organization Adaptive Policy Group configuration.
---

# meraki_organization_adaptive_policy_group (Resource)

This resource can manage the `Organization Adaptive Policy Group` configuration.

## Example Usage

```terraform
resource "meraki_organization_adaptive_policy_group" "example" {
organization_id = "123456"
description = "Group of XYZ Corp Employees"
name = "Employee Group"
sgt = 1000
policy_objects = [
{
id = "2345"
name = "Example Policy Object"
}
]
}
```

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

### Required

- `name` (String) Name of the group
- `organization_id` (String) Organization ID
- `sgt` (Number) SGT value of the group

### Optional

- `description` (String) Description of the group (default: '')
- `policy_objects` (Attributes List) The policy objects that belong to this group; traffic from addresses specified by these policy objects will be tagged with this group`s SGT value if no other tagging scheme is being used (each requires one unique attribute) (default: []) (see [below for nested schema](#nestedatt--policy_objects))

### Read-Only

- `id` (String) The id of the object

<a id="nestedatt--policy_objects"></a>
### Nested Schema for `policy_objects`

Required:

- `id` (String) The ID of the policy object
- `name` (String) The name of the policy object

## Import

Import is supported using the following syntax:

```shell
terraform import meraki_organization_adaptive_policy_group.example "<organization_id>,<id>"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data "meraki_organization_adaptive_policy_group" "example" {
id = "12345678"
organization_id = "123456"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import meraki_organization_adaptive_policy_group.example "<organization_id>,<id>"
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "meraki_organization_adaptive_policy_group" "example" {
organization_id = "123456"
description = "Group of XYZ Corp Employees"
name = "Employee Group"
sgt = 1000
policy_objects = [
{
id = "2345"
name = "Example Policy Object"
}
]
}
55 changes: 55 additions & 0 deletions gen/definitions/organization_adaptive_policy_group.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Organization Adaptive Policy Group
rest_endpoint: /organizations/%v/adaptivePolicy/groups
id_name: groupId
data_source_name_query: true
doc_category: Organizations
attributes:
- tf_name: organization_id
type: String
reference: true
description: Organization ID
example: "123456"
test_value: data.meraki_organization.test.id
- model_name: description
type: String
description: "Description of the group (default: '')"
example: Group of XYZ Corp Employees
- model_name: name
type: String
mandatory: true
description: Name of the group
example: Employee Group
- model_name: sgt
type: Int64
mandatory: true
description: SGT value of the group
example: "1000"
- model_name: policyObjects
type: List
description: "The policy objects that belong to this group; traffic from addresses specified by these policy objects will be tagged with this group`s SGT value if no other tagging scheme is being used (each requires one unique attribute) (default: [])"
attributes:
- model_name: id
type: String
id: true
mandatory: true
description: The ID of the policy object
example: "2345"
test_value: meraki_organization_policy_object.test.id
- model_name: name
type: String
id: true
mandatory: true
description: The name of the policy object
example: Example Policy Object
test_value: meraki_organization_policy_object.test.name
test_prerequisites: |
data "meraki_organization" "test" {
name = "Dev"
}
resource "meraki_organization_policy_object" "test" {
organization_id = data.meraki_organization.test.id
category = "adaptivePolicy"
cidr = "10.0.0.0/24"
name = "Web Servers - Datacenter 10"
type = "adaptivePolicyIpv4Cidr"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// 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-validators/datasourcevalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"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 model

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &OrganizationAdaptivePolicyGroupDataSource{}
_ datasource.DataSourceWithConfigure = &OrganizationAdaptivePolicyGroupDataSource{}
)

func NewOrganizationAdaptivePolicyGroupDataSource() datasource.DataSource {
return &OrganizationAdaptivePolicyGroupDataSource{}
}

type OrganizationAdaptivePolicyGroupDataSource struct {
client *meraki.Client
}

func (d *OrganizationAdaptivePolicyGroupDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_organization_adaptive_policy_group"
}

func (d *OrganizationAdaptivePolicyGroupDataSource) 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 data source can read the `Organization Adaptive Policy Group` configuration.",

Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The id of the object",
Optional: true,
Computed: true,
},
"organization_id": schema.StringAttribute{
MarkdownDescription: "Organization ID",
Required: true,
},
"description": schema.StringAttribute{
MarkdownDescription: "Description of the group (default: '')",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "Name of the group",
Optional: true,
Computed: true,
},
"sgt": schema.Int64Attribute{
MarkdownDescription: "SGT value of the group",
Computed: true,
},
"policy_objects": schema.ListNestedAttribute{
MarkdownDescription: "The policy objects that belong to this group; traffic from addresses specified by these policy objects will be tagged with this group`s SGT value if no other tagging scheme is being used (each requires one unique attribute) (default: [])",
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "The ID of the policy object",
Computed: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the policy object",
Computed: true,
},
},
},
},
},
}
}
func (d *OrganizationAdaptivePolicyGroupDataSource) ConfigValidators(ctx context.Context) []datasource.ConfigValidator {
return []datasource.ConfigValidator{
datasourcevalidator.ExactlyOneOf(
path.MatchRoot("id"),
path.MatchRoot("name"),
),
}
}

func (d *OrganizationAdaptivePolicyGroupDataSource) 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 *OrganizationAdaptivePolicyGroupDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var config OrganizationAdaptivePolicyGroup

// 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", config.Id.String()))

var res gjson.Result
var err error
if config.Id.IsNull() && !config.Name.IsNull() {
res, err = d.client.Get(config.getPath())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to retrieve objects, got error: %s", err))
return
}
if len(res.Array()) > 0 {
res.ForEach(func(k, v gjson.Result) bool {
if config.Name.ValueString() == v.Get("name").String() {
config.Id = types.StringValue(v.Get("groupId").String())
tflog.Debug(ctx, fmt.Sprintf("%s: Found object with name '%v', id: %v", config.Id.String(), config.Name.ValueString(), config.Id.String()))
res = v
return false
}
return true
})
}

if config.Id.IsNull() {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Failed to find object with name: %s", config.Name.ValueString()))
return
}
}

if !res.Exists() {
res, err = d.client.Get(config.getPath() + "/" + url.QueryEscape(config.Id.ValueString()))
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", config.Id.ValueString()))

diags = resp.State.Set(ctx, &config)
resp.Diagnostics.Append(diags...)
}

// End of section. //template:end read
Loading

0 comments on commit 11d0153

Please sign in to comment.