From 133d9ab20fedcd56cfe563eceb091a52cd9baa07 Mon Sep 17 00:00:00 2001 From: Brock Shelton Date: Thu, 19 Oct 2023 22:30:04 -0400 Subject: [PATCH 1/7] only return 1 network --- esc/data_source_network.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/esc/data_source_network.go b/esc/data_source_network.go index 3553ff5..63c3acb 100644 --- a/esc/data_source_network.go +++ b/esc/data_source_network.go @@ -64,26 +64,23 @@ func dataSourceNetworkRead(ctx context.Context, d *schema.ResourceData, meta int return diag.Errorf("There are no networks in project %s", projectID) } - var found []*client.Network + var found *client.Network desiredName := d.Get("name").(string) for _, network := range resp.Networks { - if network.Name == desiredName { - found = append(found, &network) + if network.Name == desiredName && network.Status == "available" { + found = &network break } } - if len(found) == 0 { + if found == nil { return diag.Errorf("Network %s was not found in project %s", desiredName, projectID) } - if len(found) > 1 { - return diag.Errorf("There are more than one network with name %s in project %s", desiredName, projectID) - } - d.SetId(found[0].NetworkID) - d.Set("cidr_block", found[0].CIDRBlock) - d.Set("region", found[0].Region) - d.Set("resource_provider", found[0].Provider) + d.SetId(found.NetworkID) + d.Set("cidr_block", found.CIDRBlock) + d.Set("region", found.Region) + d.Set("resource_provider", found.Provider) return nil } From 14045bcae7b96aaac6318ff3ddb449a6668e3eb3 Mon Sep 17 00:00:00 2001 From: Brock Shelton Date: Tue, 24 Oct 2023 11:16:37 -0400 Subject: [PATCH 2/7] check for multiple network names --- esc/data_source_network.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/esc/data_source_network.go b/esc/data_source_network.go index 63c3acb..8a44b12 100644 --- a/esc/data_source_network.go +++ b/esc/data_source_network.go @@ -65,14 +65,27 @@ func dataSourceNetworkRead(ctx context.Context, d *schema.ResourceData, meta int } var found *client.Network + multipleNetworksFound := false + desiredName := d.Get("name").(string) + count := 0 for _, network := range resp.Networks { - if network.Name == desiredName && network.Status == "available" { - found = &network - break + if network.Name == desiredName { + count++ + if count > 1 { + multipleNetworksFound = true + break + } + if network.Status == "available" { + found = &network + } } } + if multipleNetworksFound { + return diag.Errorf("Error: Multiple networks with the same name '%s' were found. Please specify a more unique name or check your existing resources.", desiredName) + } + if found == nil { return diag.Errorf("Network %s was not found in project %s", desiredName, projectID) } From 4c5f0f6eac6771561bc89dff48226a5b55603129 Mon Sep 17 00:00:00 2001 From: Brock Shelton Date: Tue, 24 Oct 2023 17:43:36 -0400 Subject: [PATCH 3/7] add new eventstorecloud_networks data provider --- docs/data-sources/networks.md | 53 +++++++++++++ esc/data_source_network.go | 77 +++++++++++++++---- esc/provider.go | 5 +- .../eventstorecloud_networks/data-source.tf | 8 ++ 4 files changed, 126 insertions(+), 17 deletions(-) create mode 100644 docs/data-sources/networks.md create mode 100644 examples/data-sources/eventstorecloud_networks/data-source.tf diff --git a/docs/data-sources/networks.md b/docs/data-sources/networks.md new file mode 100644 index 0000000..9943fe8 --- /dev/null +++ b/docs/data-sources/networks.md @@ -0,0 +1,53 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "eventstorecloud_networks Data Source - terraform-provider-eventstorecloud" +subcategory: "" +description: |- + Retrieves data for an existing Network resource +--- + +# eventstorecloud_networks (Data Source) + +Retrieves data for an existing `Network` resource + +## Example Usage + +```terraform +data "eventstorecloud_networks" "example" { + name = "Example Network" + project_id = var.project_id +} + +output "first_network" { + value = data.eventstorecloud_network.example.networks[0] +} +``` + + +## Schema + +### Required + +- **name** (String) +- **project_id** (String) + +### Optional + +- **id** (String) The ID of this resource. + +### Read-Only + +- **networks** (List of Object) (see [below for nested schema](#nestedatt--networks)) + + +### Nested Schema for `networks` + +Read-Only: + +- **cidr_block** (String) +- **name** (String) +- **project_id** (String) +- **region** (String) +- **resource_provider** (String) + + diff --git a/esc/data_source_network.go b/esc/data_source_network.go index 8a44b12..78987f7 100644 --- a/esc/data_source_network.go +++ b/esc/data_source_network.go @@ -65,27 +65,15 @@ func dataSourceNetworkRead(ctx context.Context, d *schema.ResourceData, meta int } var found *client.Network - multipleNetworksFound := false desiredName := d.Get("name").(string) - count := 0 for _, network := range resp.Networks { - if network.Name == desiredName { - count++ - if count > 1 { - multipleNetworksFound = true - break - } - if network.Status == "available" { - found = &network - } + if network.Name == desiredName && network.Status == "available" { + found = &network + break } } - if multipleNetworksFound { - return diag.Errorf("Error: Multiple networks with the same name '%s' were found. Please specify a more unique name or check your existing resources.", desiredName) - } - if found == nil { return diag.Errorf("Network %s was not found in project %s", desiredName, projectID) } @@ -97,3 +85,62 @@ func dataSourceNetworkRead(ctx context.Context, d *schema.ResourceData, meta int return nil } + +func dataSourceNetworkList() *schema.Resource { + return &schema.Resource{ + Description: "Retrieves data for an existing `Network` resource", + ReadContext: dataSourceNetworkListRead, + Schema: map[string]*schema.Schema{ + "project_id": { + Type: schema.TypeString, + Required: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + }, + "networks": { + Type: schema.TypeList, + Elem: dataSourceNetwork(), + Computed: true, + }, + }, + } +} + +func dataSourceNetworkListRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + c := meta.(*providerContext) + + projectID := d.Get("project_id").(string) + desiredName := d.Get("name").(string) + + resp, err := c.client.NetworkList(ctx, &client.ListNetworksRequest{ + OrganizationID: c.organizationId, + ProjectID: projectID, + }) + if err != nil { + return err + } + + networkResources := make([]map[string]interface{}, 0) + + for _, network := range resp.Networks { + if network.Name == desiredName && network.Status == "available" { + networkResource := map[string]interface{}{ + "cidr_block": network.CIDRBlock, + "name": network.Name, + "project_id": network.ProjectID, + "region": network.Region, + "resource_provider": network.Provider, + } + networkResources = append(networkResources, networkResource) + } + } + + d.SetId(projectID) + if err := d.Set("networks", networkResources); err != nil { + return diag.FromErr(err) + } + + return nil +} diff --git a/esc/provider.go b/esc/provider.go index 2a3c73d..f5b9b6a 100644 --- a/esc/provider.go +++ b/esc/provider.go @@ -74,8 +74,9 @@ func New(version string) func() *schema.Provider { }, DataSourcesMap: map[string]*schema.Resource{ - "eventstorecloud_project": dataSourceProject(), - "eventstorecloud_network": dataSourceNetwork(), + "eventstorecloud_project": dataSourceProject(), + "eventstorecloud_network": dataSourceNetwork(), + "eventstorecloud_networks": dataSourceNetworkList(), }, ResourcesMap: map[string]*schema.Resource{ diff --git a/examples/data-sources/eventstorecloud_networks/data-source.tf b/examples/data-sources/eventstorecloud_networks/data-source.tf new file mode 100644 index 0000000..f879032 --- /dev/null +++ b/examples/data-sources/eventstorecloud_networks/data-source.tf @@ -0,0 +1,8 @@ +data "eventstorecloud_networks" "example" { + name = "Example Network" + project_id = var.project_id +} + +output "first_network" { + value = data.eventstorecloud_networks.example.networks[0] +} From c4778dec1b63dbb8b8f01c3cb43b5dc4455e8a64 Mon Sep 17 00:00:00 2001 From: Brock Shelton Date: Tue, 24 Oct 2023 20:43:09 -0400 Subject: [PATCH 4/7] generated docs --- docs/data-sources/networks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/data-sources/networks.md b/docs/data-sources/networks.md index 9943fe8..ef68983 100644 --- a/docs/data-sources/networks.md +++ b/docs/data-sources/networks.md @@ -19,7 +19,7 @@ data "eventstorecloud_networks" "example" { } output "first_network" { - value = data.eventstorecloud_network.example.networks[0] + value = data.eventstorecloud_networks.example.networks[0] } ``` From 4fd75763806153873e33bc876ff8476f114b626c Mon Sep 17 00:00:00 2001 From: Brock Shelton Date: Thu, 26 Oct 2023 11:36:56 -0400 Subject: [PATCH 5/7] return deprecation warning --- esc/data_source_network.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/esc/data_source_network.go b/esc/data_source_network.go index 78987f7..d876e74 100644 --- a/esc/data_source_network.go +++ b/esc/data_source_network.go @@ -2,6 +2,7 @@ package esc import ( "context" + "log" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -48,6 +49,9 @@ func dataSourceNetwork() *schema.Resource { } func dataSourceNetworkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + + log.Printf("[WARN] Your warning message here") + c := meta.(*providerContext) projectID := d.Get("project_id").(string) @@ -65,15 +69,25 @@ func dataSourceNetworkRead(ctx context.Context, d *schema.ResourceData, meta int } var found *client.Network + multipleNetworksFound := false + count := 0 desiredName := d.Get("name").(string) for _, network := range resp.Networks { if network.Name == desiredName && network.Status == "available" { + count++ + if count > 1 { + multipleNetworksFound = true + break + } found = &network - break } } + if multipleNetworksFound { + return diag.Errorf("Error: Multiple networks with the same name '%s' were found. Please specify a more unique name or check your existing resources.", desiredName) + } + if found == nil { return diag.Errorf("Network %s was not found in project %s", desiredName, projectID) } @@ -83,7 +97,12 @@ func dataSourceNetworkRead(ctx context.Context, d *schema.ResourceData, meta int d.Set("region", found.Region) d.Set("resource_provider", found.Provider) - return nil + return diag.Diagnostics{ + diag.Diagnostic{ + Severity: diag.Warning, + Summary: "This resource is deprecated. Please use the eventstorecloud_networks data resource instead.", + }, + } } func dataSourceNetworkList() *schema.Resource { From 247d1313fc820215a434238650bebc6e17b76a30 Mon Sep 17 00:00:00 2001 From: Brock Shelton Date: Tue, 12 Dec 2023 23:10:47 -0500 Subject: [PATCH 6/7] undo --- esc/data_source_network.go | 67 -------------------------------------- esc/provider.go | 5 ++- 2 files changed, 2 insertions(+), 70 deletions(-) diff --git a/esc/data_source_network.go b/esc/data_source_network.go index d876e74..f93ba5e 100644 --- a/esc/data_source_network.go +++ b/esc/data_source_network.go @@ -2,7 +2,6 @@ package esc import ( "context" - "log" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -50,8 +49,6 @@ func dataSourceNetwork() *schema.Resource { func dataSourceNetworkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - log.Printf("[WARN] Your warning message here") - c := meta.(*providerContext) projectID := d.Get("project_id").(string) @@ -97,69 +94,5 @@ func dataSourceNetworkRead(ctx context.Context, d *schema.ResourceData, meta int d.Set("region", found.Region) d.Set("resource_provider", found.Provider) - return diag.Diagnostics{ - diag.Diagnostic{ - Severity: diag.Warning, - Summary: "This resource is deprecated. Please use the eventstorecloud_networks data resource instead.", - }, - } -} - -func dataSourceNetworkList() *schema.Resource { - return &schema.Resource{ - Description: "Retrieves data for an existing `Network` resource", - ReadContext: dataSourceNetworkListRead, - Schema: map[string]*schema.Schema{ - "project_id": { - Type: schema.TypeString, - Required: true, - }, - "name": { - Type: schema.TypeString, - Required: true, - }, - "networks": { - Type: schema.TypeList, - Elem: dataSourceNetwork(), - Computed: true, - }, - }, - } -} - -func dataSourceNetworkListRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - c := meta.(*providerContext) - - projectID := d.Get("project_id").(string) - desiredName := d.Get("name").(string) - - resp, err := c.client.NetworkList(ctx, &client.ListNetworksRequest{ - OrganizationID: c.organizationId, - ProjectID: projectID, - }) - if err != nil { - return err - } - - networkResources := make([]map[string]interface{}, 0) - - for _, network := range resp.Networks { - if network.Name == desiredName && network.Status == "available" { - networkResource := map[string]interface{}{ - "cidr_block": network.CIDRBlock, - "name": network.Name, - "project_id": network.ProjectID, - "region": network.Region, - "resource_provider": network.Provider, - } - networkResources = append(networkResources, networkResource) - } - } - - d.SetId(projectID) - if err := d.Set("networks", networkResources); err != nil { - return diag.FromErr(err) - } - return nil } diff --git a/esc/provider.go b/esc/provider.go index f5b9b6a..2a3c73d 100644 --- a/esc/provider.go +++ b/esc/provider.go @@ -74,9 +74,8 @@ func New(version string) func() *schema.Provider { }, DataSourcesMap: map[string]*schema.Resource{ - "eventstorecloud_project": dataSourceProject(), - "eventstorecloud_network": dataSourceNetwork(), - "eventstorecloud_networks": dataSourceNetworkList(), + "eventstorecloud_project": dataSourceProject(), + "eventstorecloud_network": dataSourceNetwork(), }, ResourcesMap: map[string]*schema.Resource{ From 780944b92740c1198863aa77471325956fc562ac Mon Sep 17 00:00:00 2001 From: Brock Shelton Date: Tue, 12 Dec 2023 23:13:58 -0500 Subject: [PATCH 7/7] re-generate --- docs/data-sources/networks.md | 53 ------------------- .../eventstorecloud_networks/data-source.tf | 8 --- 2 files changed, 61 deletions(-) delete mode 100644 docs/data-sources/networks.md delete mode 100644 examples/data-sources/eventstorecloud_networks/data-source.tf diff --git a/docs/data-sources/networks.md b/docs/data-sources/networks.md deleted file mode 100644 index ef68983..0000000 --- a/docs/data-sources/networks.md +++ /dev/null @@ -1,53 +0,0 @@ ---- -# generated by https://github.com/hashicorp/terraform-plugin-docs -page_title: "eventstorecloud_networks Data Source - terraform-provider-eventstorecloud" -subcategory: "" -description: |- - Retrieves data for an existing Network resource ---- - -# eventstorecloud_networks (Data Source) - -Retrieves data for an existing `Network` resource - -## Example Usage - -```terraform -data "eventstorecloud_networks" "example" { - name = "Example Network" - project_id = var.project_id -} - -output "first_network" { - value = data.eventstorecloud_networks.example.networks[0] -} -``` - - -## Schema - -### Required - -- **name** (String) -- **project_id** (String) - -### Optional - -- **id** (String) The ID of this resource. - -### Read-Only - -- **networks** (List of Object) (see [below for nested schema](#nestedatt--networks)) - - -### Nested Schema for `networks` - -Read-Only: - -- **cidr_block** (String) -- **name** (String) -- **project_id** (String) -- **region** (String) -- **resource_provider** (String) - - diff --git a/examples/data-sources/eventstorecloud_networks/data-source.tf b/examples/data-sources/eventstorecloud_networks/data-source.tf deleted file mode 100644 index f879032..0000000 --- a/examples/data-sources/eventstorecloud_networks/data-source.tf +++ /dev/null @@ -1,8 +0,0 @@ -data "eventstorecloud_networks" "example" { - name = "Example Network" - project_id = var.project_id -} - -output "first_network" { - value = data.eventstorecloud_networks.example.networks[0] -}