diff --git a/docs/data-sources/network.md b/docs/data-sources/network.md new file mode 100644 index 0000000..b4c0cec --- /dev/null +++ b/docs/data-sources/network.md @@ -0,0 +1,39 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "glesys_network Data Source - terraform-provider-glesys" +subcategory: "" +description: |- + Get information about a Network associated with your GleSYS Project. +--- + +# glesys_network (Data Source) + +Get information about a Network associated with your GleSYS Project. + +## Example Usage + +```terraform +# glesys_dnsdomain datasource +data "glesys_network" "examplenet" { + name = "vl12345" +} + +output "network_dc" { + value = data.glesys_network.examplenet.datacenter +} +``` + + +## Schema + +### Required + +- `id` (String) network ID. + +### Read-Only + +- `datacenter` (String) network datacenter. +- `description` (String) network description. +- `public` (String) network public, yes/no. + + diff --git a/glesys/datasource_glesys_network.go b/glesys/datasource_glesys_network.go new file mode 100644 index 0000000..d9ca1f4 --- /dev/null +++ b/glesys/datasource_glesys_network.go @@ -0,0 +1,59 @@ +package glesys + +import ( + "context" + + "github.com/glesys/glesys-go/v6" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGlesysNetwork() *schema.Resource { + return &schema.Resource{ + Description: "Get information about a Network associated with your GleSYS Project.", + + ReadContext: dataSourceGlesysNetworkRead, + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Required: true, + Description: "network ID.", + }, + "description": { + Type: schema.TypeString, + Computed: true, + Description: "network description.", + }, + "datacenter": { + Type: schema.TypeString, + Computed: true, + Description: "network datacenter.", + }, + "public": { + Type: schema.TypeString, + Computed: true, + Description: "network public, yes/no.", + }, + }, + } +} + +func dataSourceGlesysNetworkRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*glesys.Client) + + var network *glesys.Network + if networkid, ok := d.GetOk("id"); ok { + net, err := client.Networks.Details(ctx, networkid.(string)) + if err != nil { + return diag.Errorf("Error retrieving network: %s", err) + } + network = net + } + + d.SetId(network.ID) + d.Set("description", network.Description) + d.Set("datacenter", network.DataCenter) + d.Set("public", network.Public) + + return nil +} diff --git a/glesys/provider.go b/glesys/provider.go index cb2cce5..23af62b 100644 --- a/glesys/provider.go +++ b/glesys/provider.go @@ -31,6 +31,7 @@ func Provider() *schema.Provider { DataSourcesMap: map[string]*schema.Resource{ "glesys_dnsdomain": dataSourceGlesysDNSDomain(), + "glesys_network": dataSourceGlesysNetwork(), }, ResourcesMap: map[string]*schema.Resource{