github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  )
    12  
    13  type rawProfileClient interface {
    14  	ProfileCreate(name string) error
    15  	ListProfiles() ([]string, error)
    16  	SetProfileConfigItem(name, key, value string) error
    17  	ProfileDelete(profile string) error
    18  	ProfileDeviceAdd(profile, devname, devtype string, props []string) (*lxd.Response, error)
    19  }
    20  
    21  type profileClient struct {
    22  	raw rawProfileClient
    23  }
    24  
    25  // ProfileDelete deletes an existing profile. No check is made to
    26  // verify the profile exists.
    27  func (p profileClient) ProfileDelete(profile string) error {
    28  	if err := p.raw.ProfileDelete(profile); err != nil {
    29  		return errors.Trace(err)
    30  	}
    31  	return nil
    32  }
    33  
    34  // ProfileDeviceAdd adds a profile device, such as a disk or a nic, to
    35  // the specified profile. No check is made to verify the profile
    36  // exists.
    37  func (p profileClient) ProfileDeviceAdd(profile, devname, devtype string, props []string) (*lxd.Response, error) {
    38  	resp, err := p.raw.ProfileDeviceAdd(profile, devname, devtype, props)
    39  	if err != nil {
    40  		return resp, errors.Trace(err)
    41  	}
    42  	return resp, err
    43  }
    44  
    45  // CreateProfile attempts to create a new lxc profile and set the given config.
    46  func (p profileClient) CreateProfile(name string, config map[string]string) error {
    47  	if err := p.raw.ProfileCreate(name); err != nil {
    48  		//TODO(wwitzel3) use HasProfile to generate a more useful AlreadyExists error
    49  		return errors.Trace(err)
    50  	}
    51  
    52  	for k, v := range config {
    53  		if err := p.raw.SetProfileConfigItem(name, k, v); err != nil {
    54  			return errors.Trace(err)
    55  		}
    56  	}
    57  	return nil
    58  }
    59  
    60  // HasProfile returns true/false if the profile exists.
    61  func (p profileClient) HasProfile(name string) (bool, error) {
    62  	profiles, err := p.raw.ListProfiles()
    63  	if err != nil {
    64  		return false, errors.Trace(err)
    65  	}
    66  	for _, profile := range profiles {
    67  		if profile == name {
    68  			return true, nil
    69  		}
    70  	}
    71  	return false, nil
    72  }