diff --git a/examples/main.tf b/examples/main.tf index 0d8cce9..bf4f560 100644 --- a/examples/main.tf +++ b/examples/main.tf @@ -14,3 +14,9 @@ data "ganeti_networks" "all" {} output "all_networks" { value = data.ganeti_networks.all.networks } + + +data "ganeti_instances" "all" {} +output "instance" { + value = data.ganeti_instances.all.instances +} diff --git a/ganeti/data_source_instance.go b/ganeti/data_source_instance.go new file mode 100644 index 0000000..cc9349d --- /dev/null +++ b/ganeti/data_source_instance.go @@ -0,0 +1,242 @@ +package ganeti + +import ( + "context" + + rapi "burntworld.ca/go-rapi-client" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceInstance() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceInstanceRead, + Schema: map[string]*schema.Schema{ + "instances": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "disk_usage": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + }, + "oper_vcpus": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + }, + "network_port": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + }, + "serial_no": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + }, + "ctime": &schema.Schema{ + Type: schema.TypeFloat, + Computed: true, + }, + "mtime": &schema.Schema{ + Type: schema.TypeFloat, + Computed: true, + }, + "oper_state": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + }, + "disk_template": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "status": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "oper_ram": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + }, + "pnode": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "admin_state": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "os": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "nic.uuids": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Deprecated: "to satisfy terraform?", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "nic_modes": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "nic.names": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Deprecated: "to satisfy terraform?", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "nic.networks.names": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Deprecated: "to satisfy terraform?", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "disk.spindles": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Deprecated: "to satisfy terraform?", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "disk.uuids": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Deprecated: "to satisfy terraform?", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "disk.sizes": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Deprecated: "to satisfy terraform?", + Elem: &schema.Schema{ + Type: schema.TypeInt, + }, + }, + "tags": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "nic.networks": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Deprecated: "to satisfy terraform?", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "nic.macs": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Deprecated: "to satisfy terraform?", + Elem: &schema.Schema{ + Type: schema.TypeString + }, + }, + "nic.bridges": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Deprecated: "to satisfy terraform?", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "nic.ips": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Deprecated: "to satisfy terraform", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "nic.links": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Deprecated: "to satisfy terraform?", + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "snodes": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "hvparams": &schema.Schema{ + Type: schema.TypeMap, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "beparams": &schema.Schema{ + Type: schema.TypeMap, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "custom_hvparams": &schema.Schema{ + Type: schema.TypeMap, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "custom_beparams": &schema.Schema{ + Type: schema.TypeMap, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "custom_nicparams": &schema.Schema{ + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeMap, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + }, + }, + } +} + +func dataSourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + var diags diag.Diagnostics + client := m.(*rapi.Client) + instances, err := client.GetInstancesBulk() + if err != nil { + return diag.FromErr(err) + } + + if err = d.Set("instances", instances); err != nil { + return diag.FromErr(err) + } + return diags; +} diff --git a/ganeti/provider.go b/ganeti/provider.go index 5b0ea37..c6bda84 100644 --- a/ganeti/provider.go +++ b/ganeti/provider.go @@ -42,6 +42,7 @@ func Provider() *schema.Provider { ConfigureContextFunc: providerConfigure, DataSourcesMap: map[string]*schema.Resource { "ganeti_networks": dataSourceNetworks(), + "ganeti_instances": dataSourceInstance(), }, ResourcesMap: map[string]*schema.Resource{}, } diff --git a/ganeti/resource_instance.go b/ganeti/resource_instance.go new file mode 100644 index 0000000..bc20030 --- /dev/null +++ b/ganeti/resource_instance.go @@ -0,0 +1,50 @@ +package ganeti + +import ( + "context" + + rapi "burntworld.ca/go-rapi-client" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func resourceInstance() *schema.Resource { + return &schema.Resource{ + CreateContext: resourceInstanceCreate, + ReadContext: resourceInstanceRead, + UpdateContext: resourceInstanceUpdate, + DeleteContext: resourceInstanceDelete, + } +} + +func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + // Warning or errors can be collected in a slice type + var diags diag.Diagnostics + return diags +} + +func resourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + // Warning or errors can be collected in a slice type + var diags diag.Diagnostics + client := m.(*rapi.Client) + instances, err := client.GetInstancesBulk() + if err != nil { + return diag.FromErr(err) + } + + if err = d.Set("instances", instances); err != nil { + return diag.FromErr(err) + } + return diags +} + +func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + return resourceInstanceRead(ctx, d, m) +} + +func resourceInstanceDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + // Warning or errors can be collected in a slice type + var diags diag.Diagnostics + + return diags +}