Skip to content

Commit

Permalink
Merge pull request #49 from netscaler/aclrule
Browse files Browse the repository at this point in the history
Added aclrule resource
  • Loading branch information
sumanth-lingappa authored Apr 29, 2024
2 parents 4bf6db7 + 49af3fc commit 965feca
Show file tree
Hide file tree
Showing 7 changed files with 450 additions and 0 deletions.
43 changes: 43 additions & 0 deletions docs/resources/aclrule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "netscalersdx_aclrule Resource - terraform-provider-netscalersdx"
subcategory: ""
description: |-
Configuration for ACL Rule resource.
---

# netscalersdx_aclrule (Resource)

Configuration for ACL Rule resource.

## Example Usage

```terraform
resource "netscalersdx_aclrule" "tf_aclrule" {
name = "tf_aclrule"
priority = 100
protocol = "TCP"
action = "Allow"
dst_port = 80
src_ip = "10.10.10.10"
}
```

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

### Required

- `action` (String) Action can be [Allow Block]. Minimum length = 4 Maximum length = 5
- `name` (String) Rule Name. Minimum length = 1 Maximum length = 128
- `priority` (Number) Priority. Minimum value = 1 Maximum value =
- `protocol` (String) IP Protocol. The allowed values are [TCP UDP ICMP ANY]. Minimum length = 3 Maximum length = 4
- `src_ip` (String) Source IP Address or Subnet. Minimum length = 3 Maximum length = 128

### Optional

- `dst_port` (String) Enable external authentication.

### Read-Only

- `id` (String) The ID of this resource.
13 changes: 13 additions & 0 deletions examples/resources/netscalersdx_aclrule/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
required_providers {
netscalersdx = {
source = "netscaler/netscalersdx"
}
}
}
provider "netscalersdx" {
host = "https://10.10.10.10" # Optionally use NETSCALERSDX_HOST env var
username = "nsroot" # Optionally use NETSCALERSDX_USERNAME env var
password = "secretpassword" # Optionally use NETSCALERSDX_PASSWORD env var
ssl_verify = false # Optionally use NETSCALERSDX_SSL_VERIFY env var
}
8 changes: 8 additions & 0 deletions examples/resources/netscalersdx_aclrule/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "netscalersdx_aclrule" "tf_aclrule" {
name = "tf_aclrule"
priority = 100
protocol = "TCP"
action = "Allow"
dst_port = "80"
src_ip = "10.10.10.10"
}
88 changes: 88 additions & 0 deletions internal/acctest/resource_netscalersdx_aclrule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package acctest

import (
"fmt"
"testing"

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

const (
testAccAclrulePlaceholder = `
resource "netscalersdx_aclrule" "tf_aclrule" {
name = "tf_aclrule"
priority = 100
protocol = "TCP"
action = "Allow"
dst_port = "80"
src_ip = "10.10.10.10"
}
`
)

func TestAccAclrule_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
CheckDestroy: testAccCheckAclruleDestroy,
Steps: []resource.TestStep{
{
Config: testAccAclrulePlaceholder,
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAclruleExists("netscalersdx_aclrule.tf_aclrule"),
resource.TestCheckResourceAttr("netscalersdx_aclrule.tf_aclrule", "name", "tf_aclrule"),
resource.TestCheckResourceAttr("netscalersdx_aclrule.tf_aclrule", "protocol", "TCP"),
resource.TestCheckResourceAttr("netscalersdx_aclrule.tf_aclrule", "action", "Allow"),
),
},
},
})
}

func testAccCheckAclruleExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Aclrule not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No Aclrule ID is set")
}

client, err := testAccApiClient()
if err != nil {
return err
}

data, err := client.GetResource("aclrule", rs.Primary.ID)
if err != nil {
return err
}

if data == nil {
return fmt.Errorf("Aclrule not found: %s", n)
}
return nil
}
}

func testAccCheckAclruleDestroy(s *terraform.State) error {
client, err := testAccApiClient()
if err != nil {
return err
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "netscalersdx_aclrule" {
continue
}
_, err := client.GetResource("aclrule", rs.Primary.ID)
if err == nil {
return fmt.Errorf("Aclrule still exists")
}
}
return nil
}
189 changes: 189 additions & 0 deletions internal/aclrule/resource_aclrule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package aclrule

import (
"context"
"fmt"

"terraform-provider-netscalersdx/internal/service"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

var _ resource.Resource = (*aclruleResource)(nil)
var _ resource.ResourceWithConfigure = (*aclruleResource)(nil)

func AclruleResource() resource.Resource {
return &aclruleResource{}
}

type aclruleResource struct {
client *service.NitroClient
}

func (r *aclruleResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_aclrule"
}

// Configure configures the client resource.
func (r *aclruleResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
r.client = *req.ProviderData.(**service.NitroClient)
}

func (r *aclruleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = aclruleResourceSchema()
}

func (r *aclruleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
tflog.Debug(ctx, "In Create Method of aclrule Resource")

var data aclruleModel

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

aclruleReq := aclruleGetThePayloadFromtheConfig(ctx, &data)

endpoint := "aclrule"

// Create the request
returnData, err := r.client.AddResource(endpoint, aclruleReq)

if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error creating resource: %s", endpoint),
fmt.Sprintf("Error: %s", err.Error()),
)
return
}

resID := returnData[endpoint].([]interface{})[0].(map[string]interface{})["id"].(string)

// Example data value setting
data.Id = types.StringValue(resID)

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)

rreq := resource.ReadRequest{
State: resp.State,
ProviderMeta: req.ProviderMeta,
}
rresp := resource.ReadResponse{
State: resp.State,
Diagnostics: resp.Diagnostics,
}

r.Read(ctx, rreq, &rresp)

*resp = resource.CreateResponse{
State: rresp.State,
Diagnostics: rresp.Diagnostics,
}

}

func (r *aclruleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {

var resId types.String
req.State.GetAttribute(ctx, path.Root("id"), &resId)
tflog.Debug(ctx, fmt.Sprintf("In Read Method of aclrule Resource with Id: %s", resId))

var data aclruleModel

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

if resp.Diagnostics.HasError() {
return
}

// Read API call logic
endpoint := "aclrule"

responseData, err := r.client.GetResource(endpoint, data.Id.ValueString())
if err != nil {
resp.State.RemoveResource(ctx)
tflog.Warn(ctx, fmt.Sprintf("removing resource aclrule: %v from state because it is not present in the remote", data.Id.ValueString()))
return
}

getResponseData := responseData[endpoint].([]interface{})[0].(map[string]interface{})

aclruleSetAttrFromGet(ctx, &data, getResponseData)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func (r *aclruleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
tflog.Debug(ctx, "In Update Method of aclrule Resource")

var data aclruleModel

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

var state aclruleModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

resourceId := state.Id.ValueString()
endpoint := "aclrule"
requestPayload := aclruleGetThePayloadFromtheConfig(ctx, &data)
data.Id = state.Id

_, err := r.client.UpdateResource(endpoint, requestPayload, resourceId)

if err != nil {
resp.Diagnostics.AddError(
"Error Updating Resource",
fmt.Sprintf("Error updating resource: %s", err.Error()),
)
return
}

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
}

func (r *aclruleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
tflog.Debug(ctx, "In Delete Method of aclrule Resource")

var data aclruleModel

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

if resp.Diagnostics.HasError() {
return
}

// Delete API call logic
endpoint := "aclrule"
_, err := r.client.DeleteResource(endpoint, data.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error deleting resource: %s", endpoint),
fmt.Sprintf("Error: %s", err.Error()),
)
return
}
}
Loading

0 comments on commit 965feca

Please sign in to comment.