File: service_packages.go

package info (click to toggle)
golang-github-joyent-gosdc 0.0~git20161202.ec8b350-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 472 kB
  • sloc: makefile: 3
file content (77 lines) | stat: -rw-r--r-- 1,860 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package cloudapi

import (
	"fmt"
	"strconv"

	"github.com/joyent/gosdc/cloudapi"
)

// ListPackages lists packages in the double
func (c *CloudAPI) ListPackages(filters map[string]string) ([]cloudapi.Package, error) {
	if err := c.ProcessFunctionHook(c, filters); err != nil {
		return nil, err
	}

	availablePackages := c.packages

	if filters != nil {
		for k, f := range filters {
			// check if valid filter
			if contains(packagesFilters, k) {
				pkgs := []cloudapi.Package{}
				// filter from availablePackages and add to pkgs
				for _, p := range availablePackages {
					if k == "name" && p.Name == f {
						pkgs = append(pkgs, p)
					} else if k == "memory" {
						i, err := strconv.Atoi(f)
						if err == nil && p.Memory == i {
							pkgs = append(pkgs, p)
						}
					} else if k == "disk" {
						i, err := strconv.Atoi(f)
						if err == nil && p.Disk == i {
							pkgs = append(pkgs, p)
						}
					} else if k == "swap" {
						i, err := strconv.Atoi(f)
						if err == nil && p.Swap == i {
							pkgs = append(pkgs, p)
						}
					} else if k == "version" && p.Version == f {
						pkgs = append(pkgs, p)
					} else if k == "vcpus" {
						i, err := strconv.Atoi(f)
						if err == nil && p.VCPUs == i {
							pkgs = append(pkgs, p)
						}
					} else if k == "group" && p.Group == f {
						pkgs = append(pkgs, p)
					}
				}
				availablePackages = pkgs
			}
		}
	}

	return availablePackages, nil
}

// GetPackage gets a single package in the double
func (c *CloudAPI) GetPackage(packageName string) (*cloudapi.Package, error) {
	if err := c.ProcessFunctionHook(c, packageName); err != nil {
		return nil, err
	}

	for _, pkg := range c.packages {
		if pkg.Name == packageName {
			return &pkg, nil
		}
		if pkg.Id == packageName {
			return &pkg, nil
		}
	}

	return nil, fmt.Errorf("Package %s not found", packageName)
}