diff --git a/civo/datasource_network.go b/civo/datasource_network.go new file mode 100644 index 00000000..bb64378a --- /dev/null +++ b/civo/datasource_network.go @@ -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 +} diff --git a/civo/provider.go b/civo/provider.go index b1708acb..103ae47a 100644 --- a/civo/provider.go +++ b/civo/provider.go @@ -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(), diff --git a/website/docs/d/network.html.markdown b/website/docs/d/network.html.markdown new file mode 100644 index 00000000..902bb6f9 --- /dev/null +++ b/website/docs/d/network.html.markdown @@ -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.