Skip to content

Commit

Permalink
Implement recursion requests for profiles, fixes issue #476.
Browse files Browse the repository at this point in the history
Signed-off-by: René Jochum <rene@jochums.at>
  • Loading branch information
jochumdev committed Aug 1, 2015
1 parent e0b56f8 commit cec9959
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 18 deletions.
19 changes: 19 additions & 0 deletions lxd/db_profiles.go
Expand Up @@ -27,6 +27,25 @@ func dbProfileIDGet(db *sql.DB, profile string) (int64, error) {
return id, nil
}

// dbProfilesGet returns a string list of profiles.
func dbProfilesGet(db *sql.DB) ([]string, error) {
q := fmt.Sprintf("SELECT name FROM profiles")
inargs := []interface{}{}
var name string
outfmt := []interface{}{name}
result, err := dbQueryScan(db, q, inargs, outfmt)
if err != nil {
return []string{}, err
}

response := []string{}
for _, r := range result {
response = append(response, r[0].(string))
}

return response, nil
}

func dbProfileCreate(db *sql.DB, profile string, config map[string]string,
devices shared.Devices) (int64, error) {

Expand Down
60 changes: 42 additions & 18 deletions lxd/profiles.go
Expand Up @@ -21,22 +21,36 @@ type profilesPostReq struct {
}

func profilesGet(d *Daemon, r *http.Request) Response {
q := fmt.Sprintf("SELECT name FROM profiles")
inargs := []interface{}{}
var name string
outfmt := []interface{}{name}
result, err := dbQueryScan(d.db, q, inargs, outfmt)
results, err := dbProfilesGet(d.db)
if err != nil {
return SmartError(err)
}
response := []string{}
for _, r := range result {
name := r[0].(string)
url := fmt.Sprintf("/%s/profiles/%s", shared.APIVersion, name)
response = append(response, url)

recursion := d.isRecursionRequest(r)

resultString := make([]string, len(results))
resultMap := make([]*shared.ProfileConfig, len(results))
i := 0
for _, name := range results {
if !recursion {
url := fmt.Sprintf("/%s/profiles/%s", shared.APIVersion, name)
resultString[i] = url
} else {
profile, err := doProfileGet(d, name)
if err != nil {
shared.Log.Error("Failed to get profile", log.Ctx{"profile": name})
continue
}
resultMap[i] = profile
}
i++
}

if !recursion {
return SyncResponse(true, resultString)
}

return SyncResponse(true, response)
return SyncResponse(true, resultMap)
}

func profilesPost(d *Daemon, r *http.Request) Response {
Expand All @@ -58,25 +72,35 @@ func profilesPost(d *Daemon, r *http.Request) Response {
return EmptySyncResponse
}

var profilesCmd = Command{name: "profiles", get: profilesGet, post: profilesPost}

func profileGet(d *Daemon, r *http.Request) Response {
name := mux.Vars(r)["name"]
var profilesCmd = Command{
name: "profiles",
get: profilesGet,
post: profilesPost}

func doProfileGet(d *Daemon, name string) (*shared.ProfileConfig, error) {
config, err := dbProfileConfigGet(d.db, name)
if err != nil {
return SmartError(err)
return nil, err
}

devices, err := dbDevicesGet(d.db, name, true)
if err != nil {
return SmartError(err)
return nil, err
}

resp := &shared.ProfileConfig{
return &shared.ProfileConfig{
Name: name,
Config: config,
Devices: devices,
}, nil
}

func profileGet(d *Daemon, r *http.Request) Response {
name := mux.Vars(r)["name"]

resp, err := doProfileGet(d, name)
if err != nil {
return SmartError(err)
}

return SyncResponse(true, resp)
Expand Down

0 comments on commit cec9959

Please sign in to comment.