-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DataSource): Added new data source
- Added the new data source network to get one network, by name or by id BREAKING CHANGE: No Signed-off-by: Alejandro JNM <[email protected]>
- Loading branch information
1 parent
aeb3932
commit cf116fa
Showing
3 changed files
with
146 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
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 | ||
} |
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,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. |