github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/tools/lxdclient/client_profile.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // +build go1.3
     5  
     6  package lxdclient
     7  
     8  import (
     9  	"github.com/juju/errors"
    10  	"github.com/lxc/lxd"
    11  	"github.com/lxc/lxd/shared"
    12  )
    13  
    14  type rawProfileClient interface {
    15  	ProfileCreate(name string) error
    16  	ListProfiles() ([]shared.ProfileConfig, error)
    17  	SetProfileConfigItem(profile, key, value string) error
    18  	GetProfileConfig(profile string) (map[string]string, error)
    19  	ProfileDelete(profile string) error
    20  	ProfileDeviceAdd(profile, devname, devtype string, props []string) (*lxd.Response, error)
    21  	ProfileConfig(profile string) (*shared.ProfileConfig, error)
    22  }
    23  
    24  type profileClient struct {
    25  	raw rawProfileClient
    26  }
    27  
    28  // ProfileDelete deletes an existing profile. No check is made to
    29  // verify the profile exists.
    30  func (p profileClient) ProfileDelete(profile string) error {
    31  	if err := p.raw.ProfileDelete(profile); err != nil {
    32  		return errors.Trace(err)
    33  	}
    34  	return nil
    35  }
    36  
    37  // ProfileDeviceAdd adds a profile device, such as a disk or a nic, to
    38  // the specified profile. No check is made to verify the profile
    39  // exists.
    40  func (p profileClient) ProfileDeviceAdd(profile, devname, devtype string, props []string) (*lxd.Response, error) {
    41  	resp, err := p.raw.ProfileDeviceAdd(profile, devname, devtype, props)
    42  	if err != nil {
    43  		return resp, errors.Trace(err)
    44  	}
    45  	return resp, err
    46  }
    47  
    48  // CreateProfile attempts to create a new lxc profile and set the given config.
    49  func (p profileClient) CreateProfile(name string, config map[string]string) error {
    50  	if err := p.raw.ProfileCreate(name); err != nil {
    51  		//TODO(wwitzel3) use HasProfile to generate a more useful AlreadyExists error
    52  		return errors.Trace(err)
    53  	}
    54  
    55  	for k, v := range config {
    56  		if err := p.raw.SetProfileConfigItem(name, k, v); err != nil {
    57  			return errors.Trace(err)
    58  		}
    59  	}
    60  	return nil
    61  }
    62  
    63  // HasProfile returns true/false if the profile exists.
    64  func (p profileClient) HasProfile(name string) (bool, error) {
    65  	profiles, err := p.raw.ListProfiles()
    66  	if err != nil {
    67  		return false, errors.Trace(err)
    68  	}
    69  	for _, profile := range profiles {
    70  		if profile.Name == name {
    71  			return true, nil
    72  		}
    73  	}
    74  	return false, nil
    75  }
    76  
    77  // SetProfileConfigItem updates the given profile config key to the given value.
    78  func (p profileClient) SetProfileConfigItem(profile, key, value string) error {
    79  	if err := p.raw.SetProfileConfigItem(profile, key, value); err != nil {
    80  		return errors.Trace(err)
    81  	}
    82  	return nil
    83  }
    84  
    85  // GetProfileConfig returns a map with all keys and values for the given
    86  // profile.
    87  func (p profileClient) GetProfileConfig(profile string) (map[string]string, error) {
    88  	config, err := p.raw.GetProfileConfig(profile)
    89  	if err != nil {
    90  		return nil, errors.Trace(err)
    91  	}
    92  	return config, nil
    93  }
    94  
    95  func (p profileClient) ProfileConfig(profile string) (*shared.ProfileConfig, error) {
    96  	return p.raw.ProfileConfig(profile)
    97  }