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: support protection #35

Merged
merged 2 commits into from
Sep 12, 2024
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
12 changes: 11 additions & 1 deletion ec/ec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"

"github.com/ettle/strcase"
"github.com/nitrado/tfconv"
"gitlab.com/nitrado/b2b/ec/core/pkg/apiclient/clientset"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -69,7 +70,7 @@ func SplitName(key string) (env, name string) {

// Converter returns the configured converter.
func Converter() *tfconv.Converter {
c := tfconv.New("json")
c := tfconv.NewWithName(FieldName, "json")
c.Register(resource.Quantity{}, expandQuantity, flattenQuantity)
return c
}
Expand All @@ -82,3 +83,12 @@ func flattenQuantity(v any) (any, error) {
q := v.(resource.Quantity)
return (&q).String(), nil
}

// FieldName returns the terraform-styled field name from the given name.
func FieldName(name string) string {
name = strcase.ToSnake(name)
if strings.Contains(name, "cid_rs") {
name = strings.ReplaceAll(name, "cid_rs", "cidrs")
}
return name
}
24 changes: 24 additions & 0 deletions ec/protection/data_source_gatewaypolicy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package protection

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// DataSourceGatewayPolicy returns the data source resource for a Gateway Policy.
func DataSourceGatewayPolicy() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to access information about an existing Gateway Policy.",
ReadContext: dataSourceGatewayPolicyRead,
Schema: gatewayPolicySchema(),
}
}

func dataSourceGatewayPolicyRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
name := d.Get("metadata.0.name").(string)
d.SetId(name)

return resourceGatewayPolicyRead(ctx, d, m)
}
64 changes: 64 additions & 0 deletions ec/protection/data_source_gatewaypolicy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package protection_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/nitrado/terraform-provider-ec/ec/provider/providertest"
)

func TestDataSourceGatewayPolicy(t *testing.T) {
name := "my-gateway-policy"
pf, _ := providertest.SetupProviderFactories(t)

resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProviderFactories: pf,
Steps: []resource.TestStep{
{
Config: testDataSourceGatewayPolicyConfigBasic(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("ec_protection_gatewaypolicy.test", "metadata.0.name", name),
resource.TestCheckResourceAttr("ec_protection_gatewaypolicy.test", "spec.#", "1"),
resource.TestCheckResourceAttr("ec_protection_gatewaypolicy.test", "spec.0.description", "My Gateway Policy"),
resource.TestCheckResourceAttr("ec_protection_gatewaypolicy.test", "spec.0.destination_cidrs.#", "1"),
resource.TestCheckResourceAttr("ec_protection_gatewaypolicy.test", "spec.0.destination_cidrs.0", "1.2.3.4/32"),
),
},
{
Config: testDataSourceGatewayPolicyConfigBasic(name) +
testDataSourceGatewayPolicyConfigRead(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.ec_protection_gatewaypolicy.test", "metadata.0.name", name),
resource.TestCheckResourceAttr("data.ec_protection_gatewaypolicy.test", "spec.#", "1"),
resource.TestCheckResourceAttr("data.ec_protection_gatewaypolicy.test", "spec.0.description", "My Gateway Policy"),
resource.TestCheckResourceAttr("data.ec_protection_gatewaypolicy.test", "spec.0.destination_cidrs.#", "1"),
resource.TestCheckResourceAttr("data.ec_protection_gatewaypolicy.test", "spec.0.destination_cidrs.0", "1.2.3.4/32"),
),
},
},
})
}

func testDataSourceGatewayPolicyConfigBasic(name string) string {
return fmt.Sprintf(`resource "ec_protection_gatewaypolicy" "test" {
metadata {
name = "%s"
}
spec {
description = "My Gateway Policy"
destination_cidrs = ["1.2.3.4/32"]
}
}
`, name)
}

func testDataSourceGatewayPolicyConfigRead() string {
return `data "ec_protection_gatewaypolicy" "test" {
metadata {
name = "${ec_protection_gatewaypolicy.test.metadata.0.name}"
}
}
`
}
58 changes: 58 additions & 0 deletions ec/protection/data_source_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package protection

import (
"context"
"errors"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/nitrado/terraform-provider-ec/ec"
"github.com/nitrado/terraform-provider-ec/pkg/resource"
apierrors "gitlab.com/nitrado/b2b/ec/apicore/api/errors"
metav1 "gitlab.com/nitrado/b2b/ec/apicore/apis/meta/v1"
)

// DataSourceMigration returns the data source resource for a Migration.
func DataSourceMigration() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to access information about an existing Migration.",
ReadContext: dataSourceMigrationRead,
Schema: migrationSchema(),
}
}

func dataSourceMigrationRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
inst, _ := d.Get("instance").(string)
clientSet, err := ec.ResolveClientSet(m, inst)
if err != nil {
return diag.FromErr(err)
}

name, hasName := d.GetOk("metadata.0.name")
if !hasName {
return diag.FromErr(errors.New("metadata.0.name is required"))
}

obj, err := clientSet.ProtectionV1Alpha1().Mitigations().Get(ctx, name.(string), metav1.GetOptions{})
if err != nil {
switch {
case apierrors.IsNotFound(err):
d.SetId("")
return nil
default:
return diag.FromErr(err)
}
}

d.SetId(obj.Name)

data, err := ec.Converter().Flatten(obj, migrationSchema())
if err != nil {
return diag.FromErr(err)
}

if err = resource.SetData(d, data); err != nil {
return diag.FromErr(err)
}
return nil
}
51 changes: 51 additions & 0 deletions ec/protection/data_source_migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package protection_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/nitrado/terraform-provider-ec/ec/provider/providertest"
metav1 "gitlab.com/nitrado/b2b/ec/apicore/apis/meta/v1"
protectionv1alpha1 "gitlab.com/nitrado/b2b/ec/core/pkg/api/protection/v1alpha1"
)

func TestDataSourceMigrations(t *testing.T) {
migration1 := &protectionv1alpha1.Mitigation{
ObjectMeta: metav1.ObjectMeta{Name: "my-migration-1"},
Spec: protectionv1alpha1.MitigationSpec{
DisplayName: "my migration 1",
},
}
migration2 := &protectionv1alpha1.Mitigation{
ObjectMeta: metav1.ObjectMeta{Name: "my-migration-2"},
Spec: protectionv1alpha1.MitigationSpec{
DisplayName: "my migration 2",
},
}

pf, _ := providertest.SetupProviderFactories(t, migration1, migration2)

resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProviderFactories: pf,
Steps: []resource.TestStep{
{
Config: testDataSourceImageNameConfigRead(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.ec_protection_migration.by_name", "metadata.0.name", "my-migration-1"),
resource.TestCheckResourceAttr("data.ec_protection_migration.by_name", "spec.#", "1"),
resource.TestCheckResourceAttr("data.ec_protection_migration.by_name", "spec.0.display_name", "my migration 1"),
),
},
},
})
}

func testDataSourceImageNameConfigRead() string {
return `data "ec_protection_migration" "by_name" {
metadata {
name = "my-migration-1"
}
}
`
}
24 changes: 24 additions & 0 deletions ec/protection/data_source_protocol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package protection

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// DataSourceProtocol returns the data source resource for a Protocol.
func DataSourceProtocol() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to access information about an existing Protocol.",
ReadContext: dataSourceProtocolRead,
Schema: protocolSchema(),
}
}

func dataSourceProtocolRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
name := d.Get("metadata.0.name").(string)
d.SetId(name)

return resourceProtocolRead(ctx, d, m)
}
65 changes: 65 additions & 0 deletions ec/protection/data_source_protocol_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package protection_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/nitrado/terraform-provider-ec/ec/provider/providertest"
)

func TestDataSourceProtocols(t *testing.T) {
name := "my-protocol"
pf, _ := providertest.SetupProviderFactories(t)

resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProviderFactories: pf,
Steps: []resource.TestStep{
{
Config: testDataSourceProtocolsConfigBasic(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("ec_protection_protocol.test", "metadata.0.name", name),
resource.TestCheckResourceAttr("ec_protection_protocol.test", "spec.#", "1"),
resource.TestCheckResourceAttr("ec_protection_protocol.test", "spec.0.description", "My Protocol"),
resource.TestCheckResourceAttr("ec_protection_protocol.test", "spec.0.mitigation_name", "my-mitigation"),
resource.TestCheckResourceAttr("ec_protection_protocol.test", "spec.0.protocol", "UDP"),
),
},
{
Config: testDataSourceProtocolsConfigBasic(name) +
testDataSourceProtocolConfigRead(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.ec_protection_protocol.test", "metadata.0.name", name),
resource.TestCheckResourceAttr("data.ec_protection_protocol.test", "spec.#", "1"),
resource.TestCheckResourceAttr("data.ec_protection_protocol.test", "spec.0.description", "My Protocol"),
resource.TestCheckResourceAttr("data.ec_protection_protocol.test", "spec.0.mitigation_name", "my-mitigation"),
resource.TestCheckResourceAttr("data.ec_protection_protocol.test", "spec.0.protocol", "UDP"),
),
},
},
})
}

func testDataSourceProtocolsConfigBasic(name string) string {
return fmt.Sprintf(`resource "ec_protection_protocol" "test" {
metadata {
name = "%s"
}
spec {
description = "My Protocol"
mitigation_name = "my-mitigation"
protocol = "UDP"
}
}
`, name)
}

func testDataSourceProtocolConfigRead() string {
return `data "ec_protection_protocol" "test" {
metadata {
name = "${ec_protection_protocol.test.metadata.0.name}"
}
}
`
}
Loading
Loading