Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(specs): Initial panos_loopback_interface codegen spec #253

Merged
merged 4 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions assets/pango/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,16 @@ func checkLoopback(c *pango.Client, ctx context.Context) {
},
Comment: util.String("This is a loopback entry"),
Mtu: util.Int(1280),
Ips: []string{"1.1.1.1", "2.2.2.2"},
Ip: []loopback.Ip{
{
Name: "1.1.1.1",
},
{
Name: "2.2.2.2",
},
},
Ipv6: &loopback.Ipv6{
Addresses: []loopback.Ipv6Addresses{
Address: []loopback.Ipv6Address{
{
EnableOnInterface: util.Bool(false),
Name: "2001:0000:130F:0000:0000:09C0:876A:130B",
Expand Down
171 changes: 171 additions & 0 deletions assets/terraform/test/resource_loopback_interface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package provider_test

import (
"context"
"errors"
"fmt"
"testing"

sdkErrors "github.com/PaloAltoNetworks/pango/errors"
"github.com/PaloAltoNetworks/pango/network/interface/loopback"

"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
)

func TestAccLoopbackInterface(t *testing.T) {
t.Parallel()

interfaceName := "loopback.1"
nameSuffix := acctest.RandStringFromCharSet(6, acctest.CharSetAlphaNum)
prefix := fmt.Sprintf("test-acc-%s", nameSuffix)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckPanosLoopbackInterfaceDestroy(
prefix, interfaceName,
),
Steps: []resource.TestStep{
{
Config: loopbackInterfaceResource1,
ConfigVariables: map[string]config.Variable{
"prefix": config.StringVariable(prefix),
"interface_name": config.StringVariable(interfaceName),
},
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(
"panos_loopback_interface.iface",
tfjsonpath.New("name"),
knownvalue.StringExact("loopback.1"),
),
statecheck.ExpectKnownValue(
"panos_loopback_interface.iface",
tfjsonpath.New("comment"),
knownvalue.StringExact("loopback interface comment"),
),
statecheck.ExpectKnownValue(
"panos_loopback_interface.iface",
tfjsonpath.New("interface_management_profile"),
knownvalue.StringExact(fmt.Sprintf("%s-profile", prefix)),
),
statecheck.ExpectKnownValue(
"panos_loopback_interface.iface",
tfjsonpath.New("adjust_tcp_mss"),
knownvalue.ObjectExact(map[string]knownvalue.Check{
"enable": knownvalue.Bool(true),
"ipv4_mss_adjustment": knownvalue.Int64Exact(100),
"ipv6_mss_adjustment": knownvalue.Int64Exact(200),
}),
),
statecheck.ExpectKnownValue(
"panos_loopback_interface.iface",
tfjsonpath.New("ip"),
knownvalue.ListExact([]knownvalue.Check{
knownvalue.ObjectExact(map[string]knownvalue.Check{
"name": knownvalue.StringExact("127.0.0.1"),
}),
}),
),
statecheck.ExpectKnownValue(
"panos_loopback_interface.iface",
tfjsonpath.New("ipv6"),
knownvalue.ObjectExact(map[string]knownvalue.Check{
"enabled": knownvalue.Bool(true),
"interface_id": knownvalue.StringExact("100"),
"address": knownvalue.ListExact([]knownvalue.Check{
knownvalue.ObjectExact(map[string]knownvalue.Check{
"name": knownvalue.StringExact("::1"),
"enable_on_interface": knownvalue.Bool(true),
"anycast": knownvalue.ObjectExact(nil),
"prefix": knownvalue.ObjectExact(nil),
}),
}),
}),
),
},
},
},
})
}

const loopbackInterfaceResource1 = `
variable "prefix" { type = string }
variable "interface_name" { type = string }

locals {
template_name = format("%s-tmpl", var.prefix)
}

resource "panos_template" "template" {
location = { panorama = {} }
name = local.template_name
}

resource "panos_interface_management_profile" "profile" {
location = { template = { name = panos_template.template.name }}

name = format("%s-profile", var.prefix)
}

resource "panos_loopback_interface" "iface" {
location = { template = { name = panos_template.template.name } }

name = var.interface_name
comment = "loopback interface comment"

interface_management_profile = panos_interface_management_profile.profile.name
mtu = "9126"
#netflow_profile = format("%s-profile", var.prefix)
adjust_tcp_mss = {
enable = true
ipv4_mss_adjustment = 100
ipv6_mss_adjustment = 200
}
ip = [{
name = "127.0.0.1"
}]
ipv6 = {
enabled = true
interface_id = "100"
address = [{
name = "::1"
enable_on_interface = true
anycast = {}
prefix = {}
}]

}
}
`

func testAccCheckPanosLoopbackInterfaceDestroy(prefix string, entry string) func(s *terraform.State) error {
return func(s *terraform.State) error {
api := loopback.NewService(sdkClient)
ctx := context.TODO()

location := loopback.NewTemplateLocation()
location.Template.Template = fmt.Sprintf("%s-tmpl", prefix)

reply, err := api.Read(ctx, *location, entry, "show")
if err != nil && !sdkErrors.IsObjectNotFound(err) {
return fmt.Errorf("reading ethernet entry via sdk: %v", err)
}

if reply != nil {
err := fmt.Errorf("terraform didn't delete the server entry properly")
delErr := api.Delete(ctx, *location, entry)
if delErr != nil {
return errors.Join(err, delErr)
}
return err
}

return nil
}
}
Loading
Loading