github.com/jlmeeker/kismatic@v1.10.1-0.20180612190640-57f9005a1f1a/pkg/data/gluster.go (about)

     1  package data
     2  
     3  import (
     4  	"encoding/xml"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/apprenda/kismatic/pkg/ssh"
     9  )
    10  
    11  type GlusterClient interface {
    12  	ListVolumes() (*GlusterVolumeInfoCliOutput, error)
    13  	GetQuota(volume string) (*GlusterVolumeQuotaCliOutput, error)
    14  }
    15  
    16  type RemoteGlusterCLI struct {
    17  	SSHClient ssh.Client
    18  }
    19  
    20  // ListVolumes returns gluster volume data using gluster command on the first sotrage node
    21  func (g RemoteGlusterCLI) ListVolumes() (*GlusterVolumeInfoCliOutput, error) {
    22  	glusterVolumeInfoRaw, err := g.SSHClient.Output(true, "sudo gluster volume info all --xml")
    23  	if err != nil {
    24  		return nil, fmt.Errorf("error getting volume info data: %v", err)
    25  	}
    26  
    27  	return UnmarshalVolumeData(glusterVolumeInfoRaw)
    28  }
    29  
    30  func UnmarshalVolumeData(raw string) (*GlusterVolumeInfoCliOutput, error) {
    31  	var glusterVolumeInfo GlusterVolumeInfoCliOutput
    32  	err := xml.Unmarshal([]byte(strings.TrimSpace(raw)), &glusterVolumeInfo)
    33  	if err != nil {
    34  		return nil, fmt.Errorf("error unmarshalling volume info data: %v", err)
    35  	}
    36  	if &glusterVolumeInfo == nil || glusterVolumeInfo.VolumeInfo == nil {
    37  		return nil, fmt.Errorf("error getting volume info data")
    38  	}
    39  	if glusterVolumeInfo.VolumeInfo.Volumes == nil || glusterVolumeInfo.VolumeInfo.Volumes.Volume == nil || len(glusterVolumeInfo.VolumeInfo.Volumes.Volume) == 0 {
    40  		return nil, nil
    41  	}
    42  
    43  	return &glusterVolumeInfo, nil
    44  }
    45  
    46  // GetQuota returns gluster volume quota data using gluster command on the first sotrage node
    47  func (g RemoteGlusterCLI) GetQuota(volume string) (*GlusterVolumeQuotaCliOutput, error) {
    48  	glusterVolumeQuotaRaw, err := g.SSHClient.Output(true, fmt.Sprintf("sudo gluster volume quota %s list --xml", volume))
    49  	if err != nil {
    50  		return nil, fmt.Errorf("error getting volume quota data for %s: %v", volume, err)
    51  	}
    52  
    53  	return UnmarshalVolumeQuota(glusterVolumeQuotaRaw)
    54  }
    55  
    56  func UnmarshalVolumeQuota(raw string) (*GlusterVolumeQuotaCliOutput, error) {
    57  	if raw == "" {
    58  		return nil, nil
    59  	}
    60  	var glusterVolumeQuota GlusterVolumeQuotaCliOutput
    61  	err := xml.Unmarshal([]byte(strings.TrimSpace(raw)), &glusterVolumeQuota)
    62  	if err != nil {
    63  		return nil, fmt.Errorf("error unmarshalling volume quota data: %v", err)
    64  	}
    65  
    66  	return &glusterVolumeQuota, nil
    67  }