github.com/aminovpavel/nomad@v0.11.8/command/volume_register_csi.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/hcl"
     7  	"github.com/hashicorp/hcl/hcl/ast"
     8  	"github.com/hashicorp/nomad/api"
     9  	"github.com/hashicorp/nomad/helper"
    10  )
    11  
    12  func (c *VolumeRegisterCommand) csiRegister(client *api.Client, ast *ast.File) int {
    13  	vol, err := csiDecodeVolume(ast)
    14  	if err != nil {
    15  		c.Ui.Error(fmt.Sprintf("Error decoding the volume definition: %s", err))
    16  		return 1
    17  	}
    18  	_, err = client.CSIVolumes().Register(vol, nil)
    19  	if err != nil {
    20  		c.Ui.Error(fmt.Sprintf("Error registering volume: %s", err))
    21  		return 1
    22  	}
    23  
    24  	return 0
    25  }
    26  
    27  // parseVolume is used to parse the quota specification from HCL
    28  func csiDecodeVolume(input *ast.File) (*api.CSIVolume, error) {
    29  	output := &api.CSIVolume{}
    30  	err := hcl.DecodeObject(output, input)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	// api.CSIVolume doesn't have the type field, it's used only for dispatch in
    36  	// parseVolumeType
    37  	helper.RemoveEqualFold(&output.ExtraKeysHCL, "type")
    38  	err = helper.UnusedKeys(output)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	return output, nil
    44  }