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