From 8128cafabe810a5f5329ba500a873827ea626a9a Mon Sep 17 00:00:00 2001 From: Andreas Eriksson Date: Thu, 12 Sep 2024 14:57:02 +0200 Subject: [PATCH] Implement data source glesys_networkadapter --- docs/data-sources/networkadapter.md | 38 +++++++++++ .../glesys_networkadapter/data-source.tf | 8 +++ glesys/datasource_glesys_networkadapter.go | 64 +++++++++++++++++++ glesys/provider.go | 5 +- 4 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 docs/data-sources/networkadapter.md create mode 100644 examples/data-sources/glesys_networkadapter/data-source.tf create mode 100644 glesys/datasource_glesys_networkadapter.go diff --git a/docs/data-sources/networkadapter.md b/docs/data-sources/networkadapter.md new file mode 100644 index 0000000..6566e3a --- /dev/null +++ b/docs/data-sources/networkadapter.md @@ -0,0 +1,38 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "glesys_networkadapter Data Source - terraform-provider-glesys" +subcategory: "" +description: |- + Get information about a NetworkAdapter associated with ServerID. +--- + +# glesys_networkadapter (Data Source) + +Get information about a NetworkAdapter associated with ServerID. + +## Example Usage + +```terraform +# glesys_networkadapter datasource +data "glesys_networkadapter" "nic1" { + id = "bfcb4eac-b831-4124-9e5a-629d6516d205" +} + +output "nic_networkid" { + value = data.glesys_networkadapter.nic1.networkid +} +``` + + +## Schema + +### Required + +- `id` (String) networkadapter ID. + +### Read-Only + +- `adaptertype` (String) networkadapter adaptertype. (VMware) +- `bandwidth` (Number) networkadapter bandwidth. +- `name` (String) networkadapter name. +- `serverid` (String) networkadapter ServerID. diff --git a/examples/data-sources/glesys_networkadapter/data-source.tf b/examples/data-sources/glesys_networkadapter/data-source.tf new file mode 100644 index 0000000..249b6c4 --- /dev/null +++ b/examples/data-sources/glesys_networkadapter/data-source.tf @@ -0,0 +1,8 @@ +# glesys_networkadapter datasource +data "glesys_networkadapter" "nic1" { + id = "bfcb4eac-b831-4124-9e5a-629d6516d205" +} + +output "nic_networkid" { + value = data.glesys_networkadapter.nic1.networkid +} diff --git a/glesys/datasource_glesys_networkadapter.go b/glesys/datasource_glesys_networkadapter.go new file mode 100644 index 0000000..6ddef62 --- /dev/null +++ b/glesys/datasource_glesys_networkadapter.go @@ -0,0 +1,64 @@ +package glesys + +import ( + "context" + + "github.com/glesys/glesys-go/v8" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGlesysNetworkAdapter() *schema.Resource { + return &schema.Resource{ + Description: "Get information about a NetworkAdapter associated with ServerID.", + + ReadContext: dataSourceGlesysNetworkAdapterRead, + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Required: true, + Description: "networkadapter ID.", + }, + "adaptertype": { + Type: schema.TypeString, + Computed: true, + Description: "networkadapter adaptertype. (VMware)", + }, + "bandwidth": { + Type: schema.TypeInt, + Computed: true, + Description: "networkadapter bandwidth.", + }, + "name": { + Type: schema.TypeString, + Computed: true, + Description: "networkadapter name.", + }, + "serverid": { + Type: schema.TypeString, + Computed: true, + Description: "networkadapter ServerID.", + }, + }, + } +} + +func dataSourceGlesysNetworkAdapterRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*glesys.Client) + + var na *glesys.NetworkAdapter + if adapterID, ok := d.GetOk("id"); ok { + nic, err := client.NetworkAdapters.Details(ctx, adapterID.(string)) + if err != nil { + return diag.Errorf("Error retrieving networkadapter: %s", err) + } + na = nic + } + + d.SetId(na.ID) + d.Set("adaptertype", na.AdapterType) + d.Set("bandwidth", na.Bandwidth) + d.Set("name", na.Name) + + return nil +} diff --git a/glesys/provider.go b/glesys/provider.go index a269e80..f13fd11 100644 --- a/glesys/provider.go +++ b/glesys/provider.go @@ -30,8 +30,9 @@ func Provider() *schema.Provider { }, DataSourcesMap: map[string]*schema.Resource{ - "glesys_dnsdomain": dataSourceGlesysDNSDomain(), - "glesys_network": dataSourceGlesysNetwork(), + "glesys_dnsdomain": dataSourceGlesysDNSDomain(), + "glesys_network": dataSourceGlesysNetwork(), + "glesys_networkadapter": dataSourceGlesysNetworkAdapter(), }, ResourcesMap: map[string]*schema.Resource{