generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NSOF-5917 metaport_mapped_elements_attachment: introduce resource
- Loading branch information
1 parent
35e2c9d
commit 0d7dc22
Showing
11 changed files
with
319 additions
and
0 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,46 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "pfptmeta_metaport_mapped_elements_attachment Resource - terraform-provider-pfptmeta" | ||
subcategory: "Network" | ||
description: |- | ||
Attaches mapped elements to metaport. | ||
--- | ||
|
||
# pfptmeta_metaport_mapped_elements_attachment (Resource) | ||
|
||
Attaches mapped elements to metaport. | ||
|
||
~> **NOTE:** Having multiple **metaport_mapped_elements_attachment** resources in conjunction with the same MetaPort and mapped elements will result in erratic behavior! | ||
~> **NOTE:** For any given MetaPort, this resource is incompatible with the `pfptmeta_metaport` | ||
[resource](https://registry.terraform.io/providers/nsofnetworks/pfptmeta/latest/docs/resources/metaport) `mapped_elements` argument. | ||
When using this argument and resource simulteneously, both of them will attempt to manage the MetaPort's mapped elements. As a result, Terraform will display a permanent difference. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "pfptmeta_network_element" "mapped-subnet" { | ||
name = "mapped subnet name" | ||
mapped_subnets = ["0.0.0.0/0"] | ||
} | ||
resource "pfptmeta_metaport" "metaport" { | ||
name = "metaport name" | ||
} | ||
resource "pfptmeta_metaport_mapped_elements_attachment" "attachment" { | ||
metaport_id = pfptmeta_metaport.metaport.id | ||
mapped_elements = [pfptmeta_network_element.mapped-subnet.id] | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- **mapped_elements** (Set of String) Mapped element IDs to be attached to the metaport (Mapped Subnet, Mapped Service or Enterprise DNS) | ||
- **metaport_id** (String) | ||
|
||
### Read-Only | ||
|
||
- **id** (String) The ID of this resource. |
13 changes: 13 additions & 0 deletions
13
examples/resources/pfptmeta_metaport_mapped_elements_attachment/resource.tf
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,13 @@ | ||
resource "pfptmeta_network_element" "mapped-subnet" { | ||
name = "mapped subnet name" | ||
mapped_subnets = ["0.0.0.0/0"] | ||
} | ||
|
||
resource "pfptmeta_metaport" "metaport" { | ||
name = "metaport name" | ||
} | ||
|
||
resource "pfptmeta_metaport_mapped_elements_attachment" "attachment" { | ||
metaport_id = pfptmeta_metaport.metaport.id | ||
mapped_elements = [pfptmeta_network_element.mapped-subnet.id] | ||
} |
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
80 changes: 80 additions & 0 deletions
80
internal/provider/acc_tests/metaport_mapped_elements_attachment_test.go
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,80 @@ | ||
package acc_tests | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
const ( | ||
metaportAttachmentDependencies = ` | ||
resource "pfptmeta_network_element" "mapped-service" { | ||
name = "mapped service" | ||
mapped_service = "mapped.service.com" | ||
} | ||
resource "pfptmeta_network_element" "mapped-service2" { | ||
name = "mapped service" | ||
mapped_service = "mapped.service2.com" | ||
} | ||
resource "pfptmeta_metaport" "metaport" { | ||
name = "metaport name1" | ||
} | ||
` | ||
metaportAttachment1 = ` | ||
resource "pfptmeta_metaport_mapped_elements_attachment" "attachment" { | ||
metaport_id = pfptmeta_metaport.metaport.id | ||
mapped_elements = [pfptmeta_network_element.mapped-service.id] | ||
} | ||
` | ||
metaportAttachment2 = ` | ||
resource "pfptmeta_metaport_mapped_elements_attachment" "attachment2" { | ||
metaport_id = pfptmeta_metaport.metaport.id | ||
mapped_elements = [pfptmeta_network_element.mapped-service2.id] | ||
} | ||
` | ||
metaportDataSource = ` | ||
data "pfptmeta_metaport" "metaport" { | ||
id = pfptmeta_metaport.metaport.id | ||
} | ||
` | ||
) | ||
|
||
func TestAccMetaportAttachment(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: providerFactories, | ||
CheckDestroy: validateResourceDestroyed("metaport", "v1/metaports"), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: metaportAttachmentDependencies + metaportAttachment1 + metaportAttachment2, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestMatchResourceAttr( | ||
"pfptmeta_metaport_mapped_elements_attachment.attachment", "metaport_id", regexp.MustCompile("^mp-.+$"), | ||
), | ||
resource.TestCheckResourceAttrPair( | ||
"pfptmeta_network_element.mapped-service", "id", | ||
"pfptmeta_metaport_mapped_elements_attachment.attachment", "mapped_elements.0"), | ||
resource.TestCheckResourceAttrPair( | ||
"pfptmeta_network_element.mapped-service2", "id", | ||
"pfptmeta_metaport_mapped_elements_attachment.attachment2", "mapped_elements.0"), | ||
), | ||
}, | ||
{ | ||
Config: metaportAttachmentDependencies + metaportAttachment2, | ||
}, | ||
{ | ||
Config: metaportAttachmentDependencies + metaportAttachment2 + metaportDataSource, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestMatchResourceAttr( | ||
"data.pfptmeta_metaport.metaport", "id", regexp.MustCompile("^mp-.+$"), | ||
), | ||
resource.TestCheckResourceAttrPair( | ||
"pfptmeta_network_element.mapped-service2", "id", | ||
"data.pfptmeta_metaport.metaport", "mapped_elements.0"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
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
79 changes: 79 additions & 0 deletions
79
internal/provider/metaport_mapped_elements_attachment/common.go
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,79 @@ | ||
package metaport_mapped_elements_attachment | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/nsofnetworks/terraform-provider-pfptmeta/internal/client" | ||
"net/http" | ||
) | ||
|
||
func generateID(mID string, meIDs []string) string { | ||
hash := 0 | ||
for _, meID := range meIDs { | ||
hash += schema.HashString(meID) | ||
} | ||
return fmt.Sprintf("%s-%d", mID, hash) | ||
} | ||
|
||
func attachmentToResource(d *schema.ResourceData, m *client.Metaport) (diags diag.Diagnostics) { | ||
err := d.Set("metaport_id", m.ID) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
mMes := &schema.Set{F: schema.HashString} | ||
for _, i := range m.MappedElements { | ||
mMes.Add(i) | ||
} | ||
schemaMes := d.Get("mapped_elements").(*schema.Set) | ||
schemaMesSet := schema.NewSet(schema.HashString, schemaMes.List()) | ||
intersection := mMes.Intersection(schemaMesSet) | ||
mes := client.ResourceTypeSetToStringSlice(intersection) | ||
err = d.Set("mapped_elements", mes) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.SetId(generateID(m.ID, mes)) | ||
return | ||
} | ||
|
||
func readResource(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) { | ||
c := meta.(*client.Client) | ||
|
||
mID := d.Get("metaport_id").(string) | ||
rg, err := client.GetMetaport(ctx, c, mID) | ||
if err != nil { | ||
errResponse, ok := err.(*client.ErrorResponse) | ||
if ok && errResponse.Status == http.StatusNotFound { | ||
d.SetId("") | ||
} | ||
return diag.FromErr(err) | ||
} | ||
return attachmentToResource(d, rg) | ||
} | ||
func createResource(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
c := meta.(*client.Client) | ||
|
||
mID := d.Get("metaport_id").(string) | ||
mes := client.ResourceTypeSetToStringSlice(d.Get("mapped_elements").(*schema.Set)) | ||
m, err := client.AddMappedElementsToMetaport(ctx, c, mID, mes) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
return attachmentToResource(d, m) | ||
} | ||
|
||
func deleteResource(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) { | ||
c := meta.(*client.Client) | ||
|
||
mID := d.Get("metaport_id").(string) | ||
mes := d.Get("mapped_elements").(*schema.Set) | ||
_, err := client.RemoveMappedElementsFromMetaport(ctx, c, mID, client.ResourceTypeSetToStringSlice(mes)) | ||
if err != nil { | ||
d.SetId("") | ||
return diag.FromErr(err) | ||
} | ||
d.SetId("") | ||
return | ||
} |
38 changes: 38 additions & 0 deletions
38
internal/provider/metaport_mapped_elements_attachment/resource.go
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,38 @@ | ||
package metaport_mapped_elements_attachment | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/nsofnetworks/terraform-provider-pfptmeta/internal/provider/common" | ||
) | ||
|
||
func Resource() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Attaches mapped elements to metaport.", | ||
ReadContext: readResource, | ||
CreateContext: createResource, | ||
DeleteContext: deleteResource, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"metaport_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateDiagFunc: common.ValidateID(false, "mp"), | ||
ForceNew: true, | ||
}, | ||
"mapped_elements": { | ||
Description: "Mapped element IDs to be attached to the metaport (Mapped Subnet, Mapped Service or Enterprise DNS)", | ||
Required: true, | ||
Type: schema.TypeSet, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateDiagFunc: common.ValidateID(true, "ne", "ed"), | ||
}, | ||
MinItems: 1, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
templates/resources/metaport_mapped_elements_attachment.md.tmpl
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,22 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "{{.Type}} {{.Name}} - {{.ProviderName}}" | ||
subcategory: "Network" | ||
description: |- | ||
{{ .Description | plainmarkdown | trimspace | prefixlines " " }} | ||
--- | ||
|
||
# {{.Type}} ({{.Name}}) | ||
|
||
{{ .Description | trimspace }} | ||
|
||
~> **NOTE:** Having multiple **metaport_mapped_elements_attachment** resources in conjunction with the same MetaPort and mapped elements will result in erratic behavior! | ||
~> **NOTE:** For any given MetaPort, this resource is incompatible with the `pfptmeta_metaport` | ||
[resource](https://registry.terraform.io/providers/nsofnetworks/pfptmeta/latest/docs/resources/metaport) `mapped_elements` argument. | ||
When using this argument and resource simulteneously, both of them will attempt to manage the MetaPort's mapped elements. As a result, Terraform will display a permanent difference. | ||
|
||
## Example Usage | ||
|
||
{{tffile "/examples/resources/pfptmeta_metaport_mapped_elements_attachment/resource.tf"}} | ||
|
||
{{ .SchemaMarkdown | trimspace }} |