Skip to content

Commit

Permalink
feat(DataSource): Added new data source
Browse files Browse the repository at this point in the history
- Added the new data source network to get one network, by name or by id

BREAKING CHANGE: No

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed May 18, 2020
1 parent aeb3932 commit cf116fa
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
83 changes: 83 additions & 0 deletions civo/datasource_network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package civo

import (
"fmt"
"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"log"
)

// Data source to get from the api a specific network
// using the id or the label
func dataSourceNetwork() *schema.Resource {
return &schema.Resource{
Read: dataSourceNetworkRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.NoZeroValues,
ExactlyOneOf: []string{"id", "label"},
},
"label": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.NoZeroValues,
ExactlyOneOf: []string{"id", "label"},
},
// Computed resource
"name": {
Type: schema.TypeString,
Computed: true,
},
"region": {
Type: schema.TypeString,
Computed: true,
},
"default": {
Type: schema.TypeBool,
Computed: true,
},
"cidr": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceNetworkRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

var foundNetwork *civogo.Network

if id, ok := d.GetOk("id"); ok {
log.Printf("[INFO] Getting the network by id")
network, err := apiClient.FindNetwork(id.(string))
if err != nil {
fmt.Errorf("[ERR] failed to retrive network: %s", err)
return err
}

foundNetwork = network
} else if label, ok := d.GetOk("label"); ok {
log.Printf("[INFO] Getting the network by label")
network, err := apiClient.FindNetwork(label.(string))
if err != nil {
fmt.Errorf("[ERR] failed to retrive network: %s", err)
return err
}

foundNetwork = network
}

d.SetId(foundNetwork.ID)
d.Set("name", foundNetwork.Name)
d.Set("label", foundNetwork.Label)
d.Set("region", foundNetwork.Region)
d.Set("default", foundNetwork.Default)
d.Set("cidr", foundNetwork.CIDR)

return nil
}
1 change: 1 addition & 0 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func Provider() terraform.ResourceProvider {
"civo_instance": dataSourceInstance(),
"civo_dns_domain_name": dataSourceDnsDomainName(),
"civo_dns_domain_record": dataSourceDnsDomainRecord(),
"civo_network": dataSourceNetwork(),
},
ResourcesMap: map[string]*schema.Resource{
"civo_instance": resourceInstance(),
Expand Down
62 changes: 62 additions & 0 deletions website/docs/d/network.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
layout: "civo"
page_title: "Civo: civo_network"
sidebar_current: "docs-civo-datasource-network"
description: |-
Get information about a Network.
---

# civo_network

Retrieve information about a Network for use in other resources.

This data source provides all of the Network's properties as configured on your
Civo account. This is useful if the Network in question is not managed by
Terraform or you need to utilize any of the Network's data.

Networks may be looked up by `id` or `label`.

## Example Usage

### Network By Name

```hcl
data "civo_network" "test" {
label = "test-network"
}
```

Reuse the data about a Network to assign a Instance to it:

```hcl
data "civo_network" "test" {
label = "test-network"
}
resource "civo_instance" "my-test-instance" {
hostname = "foo.com"
tags = ["python", "nginx"]
notes = "this is a note for the server"
size = data.civo_size.small.id
template = data.civo_template.debian.id
network_id = data.civo_network.test.id
}
```

## Argument Reference

The following arguments are supported and are mutually exclusive:

* `id` - The unique identifier of an existing Network.
* `label` - The name of an existing Network.

## Attributes Reference

The following attributes are exported:

* `id` - A unique ID that can be used to identify and reference a Network.
* `label` - The label used in the configuration.
* `name` - The name of the network.
* `region` - The region where the network was create.
* `default` - If is the default network.
* `cidr` - The block ip assigned to the network.

0 comments on commit cf116fa

Please sign in to comment.