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

only return 1 network from api #92

Merged
Show file tree
Hide file tree
Changes from 5 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
53 changes: 53 additions & 0 deletions docs/data-sources/networks.md
Original file line number Diff line number Diff line change
@@ -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_networks.example.networks[0]
}
```

<!-- schema generated by tfplugindocs -->
## 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))

<a id="nestedatt--networks"></a>
### Nested Schema for `networks`

Read-Only:

- **cidr_block** (String)
- **name** (String)
- **project_id** (String)
- **region** (String)
- **resource_provider** (String)


98 changes: 87 additions & 11 deletions esc/data_source_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -64,26 +68,98 @@ 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
multipleNetworksFound := false
count := 0

desiredName := d.Get("name").(string)
for _, network := range resp.Networks {
if network.Name == desiredName {
found = append(found, &network)
break
if network.Name == desiredName && network.Status == "available" {
count++
if count > 1 {
multipleNetworksFound = true
break
}
found = &network
}
}

if len(found) == 0 {
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)
}
if len(found) > 1 {
return diag.Errorf("There are more than one network with name %s in project %s", desiredName, projectID)

d.SetId(found.NetworkID)
d.Set("cidr_block", found.CIDRBlock)
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(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(projectID)
if err := d.Set("networks", networkResources); err != nil {
return diag.FromErr(err)
}

return nil
}
5 changes: 3 additions & 2 deletions esc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
8 changes: 8 additions & 0 deletions examples/data-sources/eventstorecloud_networks/data-source.tf
Original file line number Diff line number Diff line change
@@ -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]
}
Loading