Skip to content

Commit

Permalink
volume: add computed attribute external_id (#410)
Browse files Browse the repository at this point in the history
CSI volumes have an external ID regardless on how they were added to
Nomad, so add a computed attribute `external_id` to `nomad_csi_volume`.

This makes the schema of `nomad_csi_volume` and
`nomad_csi_volume_registration` compatible so that the read method can
be shared between then again.
  • Loading branch information
lgfa29 authored Dec 19, 2023
1 parent e1f9758 commit ee1d7c8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 81 deletions.
77 changes: 70 additions & 7 deletions nomad/resource_csi_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"fmt"
"hash/crc32"
"log"
"strings"
"time"

"github.com/dustin/go-humanize"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
Expand All @@ -24,10 +26,7 @@ func resourceCSIVolume() *schema.Resource {
CreateContext: resourceCSIVolumeCreate,
UpdateContext: resourceCSIVolumeCreate,
DeleteContext: resourceCSIVolumeDelete,

// Once created, CSI volumes are automatically registered as a
// normal volume.
Read: resourceCSIVolumeRead,
Read: resourceCSIVolumeRead,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Expand Down Expand Up @@ -336,13 +335,77 @@ func resourceCSIVolume() *schema.Resource {
},
},
},
"external_id": {
Description: "The ID of the physical volume from the storage provider.",
Computed: true,
Type: schema.TypeString,
},
},
}
}

func resourceCSIVolumeRead(d *schema.ResourceData, meta any) error {
_, err := readCSIVolume(d, meta)
return err
// resourceCSIVolumeRead is shared between nomad_csi_volume and
// nomad_csi_volume_registration because once a volume is stored in Nomad it
// is read from the same endpoint, regardless of how it was created.
func resourceCSIVolumeRead(d *schema.ResourceData, meta interface{}) error {
providerConfig := meta.(ProviderConfig)
client := providerConfig.client

id := d.Id()
opts := &api.QueryOptions{
Namespace: d.Get("namespace").(string),
}
if opts.Namespace == "" {
opts.Namespace = "default"
}
log.Printf("[DEBUG] reading information for CSI volume %q in namespace %q", id, opts.Namespace)
volume, _, err := client.CSIVolumes().Info(id, opts)
if err != nil {
// As of Nomad 0.4.1, the API client returns an error for 404
// rather than a nil result, so we must check this way.
if strings.Contains(err.Error(), "404") {
log.Printf("[DEBUG] CSI volume %q does not exist, so removing", id)
d.SetId("")
return nil
}

return fmt.Errorf("error checking for CSI volume: %s", err)
}
log.Printf("[DEBUG] found CSI volume %q in namespace %q", volume.Name, volume.Namespace)

d.Set("name", volume.Name)
d.Set("namespace", volume.Namespace)
d.Set("volume_id", volume.ID)
d.Set("external_id", volume.ExternalID)

d.Set("capacity", int(volume.Capacity))
d.Set("capacity_min_bytes", volume.RequestedCapacityMin)
d.Set("capacity_max_bytes", volume.RequestedCapacityMax)
// only save capacity min/max in state if the user has set it/them
if cMin := d.Get("capacity_min").(string); cMin != "" {
d.Set("capacity_min", humanize.IBytes(uint64(volume.RequestedCapacityMin)))
}
if cMax := d.Get("capacity_max").(string); cMax != "" {
d.Set("capacity_max", humanize.IBytes(uint64(volume.RequestedCapacityMax)))
}

d.Set("controller_required", volume.ControllerRequired)
d.Set("controllers_expected", volume.ControllersExpected)
d.Set("controllers_healthy", volume.ControllersHealthy)
d.Set("controllers_healthy", volume.ControllersHealthy)
d.Set("plugin_id", volume.PluginID)
d.Set("plugin_provider", volume.Provider)
d.Set("plugin_provider_version", volume.ProviderVersion)
d.Set("nodes_healthy", volume.NodesHealthy)
d.Set("nodes_expected", volume.NodesExpected)
d.Set("schedulable", volume.Schedulable)
d.Set("capability", flattenCSIVolumeCapabilities(volume.RequestedCapabilities))
d.Set("topologies", flattenCSIVolumeTopologies(volume.Topologies))
d.Set("topology_request", flattenCSIVolumeTopologyRequests(volume.RequestedTopologies))
// The Nomad API redacts `mount_options` and `secrets`, so we don't update them
// with the response payload; they will remain as is.

return nil
}

func resourceCSIVolumeCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
76 changes: 2 additions & 74 deletions nomad/resource_csi_volume_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func resourceCSIVolumeRegistration() *schema.Resource {
CreateContext: resourceCSIVolumeRegistrationCreate,
UpdateContext: resourceCSIVolumeRegistrationCreate,
DeleteContext: resourceCSIVolumeRegistrationDelete,
Read: resourceCSIVolumeRegistrationRead,
Read: resourceCSIVolumeRead,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Expand Down Expand Up @@ -421,7 +421,7 @@ func resourceCSIVolumeRegistrationCreate(ctx context.Context, d *schema.Resource
log.Printf("[DEBUG] CSI volume %q registered in namespace %q", volume.ID, volume.Namespace)
d.SetId(volume.ID)

err := resourceCSIVolumeRegistrationRead(d, meta) // populate other computed attributes
err := resourceCSIVolumeRead(d, meta) // populate other computed attributes
if err != nil {
return retry.NonRetryableError(err)
}
Expand Down Expand Up @@ -473,78 +473,6 @@ func resourceCSIVolumeRegistrationDelete(ctx context.Context, d *schema.Resource
}))
}

func resourceCSIVolumeRegistrationRead(d *schema.ResourceData, meta any) error {
vol, err := readCSIVolume(d, meta)
if err != nil {
return err
}
d.Set("external_id", vol.ExternalID)
return nil
}

// readCSIVolume is shared between nomad_csi_volume and
// nomad_csi_volume_registration, but the external_id attribute should only be
// set on nomad_csi_volume_registration.
func readCSIVolume(d *schema.ResourceData, meta interface{}) (*api.CSIVolume, error) {
providerConfig := meta.(ProviderConfig)
client := providerConfig.client

id := d.Id()
opts := &api.QueryOptions{
Namespace: d.Get("namespace").(string),
}
if opts.Namespace == "" {
opts.Namespace = "default"
}
log.Printf("[DEBUG] reading information for CSI volume %q in namespace %q", id, opts.Namespace)
volume, _, err := client.CSIVolumes().Info(id, opts)
if err != nil {
// As of Nomad 0.4.1, the API client returns an error for 404
// rather than a nil result, so we must check this way.
if strings.Contains(err.Error(), "404") {
log.Printf("[DEBUG] CSI volume %q does not exist, so removing", id)
d.SetId("")
return nil, nil
}

return nil, fmt.Errorf("error checking for CSI volume: %s", err)
}
log.Printf("[DEBUG] found CSI volume %q in namespace %q", volume.Name, volume.Namespace)

d.Set("name", volume.Name)
d.Set("namespace", volume.Namespace)
d.Set("volume_id", volume.ID)

d.Set("capacity", int(volume.Capacity))
d.Set("capacity_min_bytes", volume.RequestedCapacityMin)
d.Set("capacity_max_bytes", volume.RequestedCapacityMax)
// only save capacity min/max in state if the user has set it/them
if cMin := d.Get("capacity_min").(string); cMin != "" {
d.Set("capacity_min", humanize.IBytes(uint64(volume.RequestedCapacityMin)))
}
if cMax := d.Get("capacity_max").(string); cMax != "" {
d.Set("capacity_max", humanize.IBytes(uint64(volume.RequestedCapacityMax)))
}

d.Set("controller_required", volume.ControllerRequired)
d.Set("controllers_expected", volume.ControllersExpected)
d.Set("controllers_healthy", volume.ControllersHealthy)
d.Set("controllers_healthy", volume.ControllersHealthy)
d.Set("plugin_id", volume.PluginID)
d.Set("plugin_provider", volume.Provider)
d.Set("plugin_provider_version", volume.ProviderVersion)
d.Set("nodes_healthy", volume.NodesHealthy)
d.Set("nodes_expected", volume.NodesExpected)
d.Set("schedulable", volume.Schedulable)
d.Set("capability", flattenCSIVolumeCapabilities(volume.RequestedCapabilities))
d.Set("topologies", flattenCSIVolumeTopologies(volume.Topologies))
d.Set("topology_request", flattenCSIVolumeTopologyRequests(volume.RequestedTopologies))
// The Nomad API redacts `mount_options` and `secrets`, so we don't update them
// with the response payload; they will remain as is.

return volume, nil
}

func parseCSIVolumeCapabilities(i interface{}) ([]*api.CSIVolumeCapability, error) {
capabilities := []*api.CSIVolumeCapability{}

Expand Down

0 comments on commit ee1d7c8

Please sign in to comment.