-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Brendan Le Glaunec <[email protected]>
- Loading branch information
Showing
19 changed files
with
987 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
} | ||
} | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
} | ||
} | ||
` | ||
} |
Oops, something went wrong.