Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pools #79

Merged
merged 36 commits into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
4e63095
Implement pools
notjames Jun 7, 2019
3649d17
Fixup missing Pool method...
notjames Jun 7, 2019
48ebc1f
iterative things
notjames Jun 7, 2019
db354f3
Iterative
notjames Jun 7, 2019
f93ec71
Add pool interface.
notjames Jun 7, 2019
3c9464f
bug fixes - iterative
notjames Jun 7, 2019
d3af0b7
bugfixes still
notjames Jun 7, 2019
a171a8d
same same
notjames Jun 7, 2019
fdeca78
iterative.
notjames Jun 7, 2019
4f4c1c4
added pools method
notjames Jun 7, 2019
d8d6e2c
Fixed pool type in allocatemachineargs
notjames Jun 7, 2019
95a1faf
Fixed Pool method to return nil if non-existend. Fixed schema type in…
notjames Jun 7, 2019
5a47fa1
Added Description to pool interface.
notjames Jun 7, 2019
aa6db11
temp syntax test
notjames Jun 7, 2019
2d5c75a
test failed. reverted
notjames Jun 7, 2019
60f7491
test failed. reverted
notjames Jun 7, 2019
0ae1772
iterative
notjames Jun 7, 2019
07d78e8
removed pool from interface_ - probably not needed.
notjames Jun 7, 2019
858cad8
missed one Pool method in interface_ remove. Fixed.
notjames Jun 7, 2019
c8f20c5
Added test file for pool
notjames Jun 7, 2019
f2477bb
Removed .idea after adding to global gitignore.
notjames Jun 7, 2019
cb9a9ab
Added pool_test
notjames Jun 7, 2019
c00d3cc
Implement pools
notjames Jun 7, 2019
66acafd
Removed commented line that was not necessary
notjames Jun 10, 2019
fc114ed
Merge branch 'add-allocation-pools' of https://github.com/notjames/go…
notjames Jun 10, 2019
e5b3028
Removed commented line that was not necessary
notjames Jun 10, 2019
925e48d
Bugfixes missed in implementing Pool.
notjames Jun 10, 2019
25b67fe
Missed arg in controller - fixed
notjames Jun 10, 2019
effee2e
Implement pools
notjames Jun 7, 2019
6316115
struggling to find where to set empty map up. Committing to allow fri…
notjames Jun 13, 2019
1b4fd09
Fixed merge conflicts
notjames Jun 14, 2019
0d1a8ac
Final commit; added tests for controller and interface. Fixed up test…
notjames Jun 14, 2019
92a6f08
parent 8a8cec793ba70659ba95f1b9a491ba807169bfc3
notjames Jun 7, 2019
c3169fb
Last commit. All tests implemented and passing for pools.
notjames Jun 14, 2019
aa6de57
pool.go was not properly committed. This commit fixes that failed merge.
notjames Jun 14, 2019
29f73ab
Fixed case in resource_uri of interfaceResponse const.
notjames Jun 17, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,26 @@ func (c *controller) Zones() ([]Zone, error) {
return result, nil
}

// Pools implements Controller.
func (c *controller) Pools() ([]Pool, error) {
notjames marked this conversation as resolved.
Show resolved Hide resolved
var result []Pool

source, err := c.get("pools")
if err != nil {
return nil, NewUnexpectedError(err)
}

pools, err := readPools(c.apiVersion, source)
if err != nil {
return nil, errors.Trace(err)
}

for _, p := range pools {
result = append(result, p)
}
return result, nil
}

// Domains implements Controller
func (c *controller) Domains() ([]Domain, error) {
source, err := c.get("domains")
Expand All @@ -247,6 +267,7 @@ type DevicesArgs struct {
SystemIDs []string
Domain string
Zone string
Pool string
AgentName string
}

Expand All @@ -258,6 +279,7 @@ func (c *controller) Devices(args DevicesArgs) ([]Device, error) {
params.MaybeAddMany("id", args.SystemIDs)
params.MaybeAdd("domain", args.Domain)
params.MaybeAdd("zone", args.Zone)
params.MaybeAdd("pool", args.Pool)
params.MaybeAdd("agent_name", args.AgentName)
source, err := c.getQuery("devices", params.Values)
if err != nil {
Expand Down Expand Up @@ -321,6 +343,7 @@ type MachinesArgs struct {
SystemIDs []string
Domain string
Zone string
Pool string
AgentName string
OwnerData map[string]string
}
Expand All @@ -333,6 +356,7 @@ func (c *controller) Machines(args MachinesArgs) ([]Machine, error) {
params.MaybeAddMany("id", args.SystemIDs)
params.MaybeAdd("domain", args.Domain)
params.MaybeAdd("zone", args.Zone)
params.MaybeAdd("pool", args.Pool)
params.MaybeAdd("agent_name", args.AgentName)
// At the moment the MAAS API doesn't support filtering by owner
// data so we do that ourselves below.
Expand Down Expand Up @@ -371,7 +395,7 @@ type StorageSpec struct {
Label string
// Size is required and refers to the required minimum size in GB.
Size int
// Zero or more tags assocated to with the disks.
// Zero or more tags associated to the disks.
Tags []string
}

Expand Down Expand Up @@ -402,7 +426,7 @@ func (s *StorageSpec) String() string {
return fmt.Sprintf("%s%d%s", label, s.Size, tags)
}

// InterfaceSpec represents one elemenet of network related constraints.
// InterfaceSpec represents one element of network related constraints.
type InterfaceSpec struct {
// Label is required and an arbitrary string. Labels need to be unique
// across the InterfaceSpec elements specified in the AllocateMachineArgs.
Expand Down Expand Up @@ -451,7 +475,9 @@ type AllocateMachineArgs struct {
Tags []string
NotTags []string
Zone string
Pool string
NotInZone []string
NotInPool []string
// Storage represents the required disks on the Machine. If any are specified
// the first value is used for the root disk.
Storage []StorageSpec
Expand All @@ -466,8 +492,9 @@ type AllocateMachineArgs struct {
DryRun bool
}

// Validate makes sure that any labels specifed in Storage or Interfaces
// are unique, and that the required specifications are valid.
// Validate makes sure that any labels specified in Storage or Interfaces
// are unique, and that the required specifications are valid. It
// also makes sure that any pools specified exist.
func (a *AllocateMachineArgs) Validate() error {
storageLabels := set.NewStrings()
for _, spec := range a.Storage {
Expand Down Expand Up @@ -554,7 +581,9 @@ func (c *controller) AllocateMachine(args AllocateMachineArgs) (Machine, Constra
params.MaybeAdd("interfaces", args.interfaces())
params.MaybeAddMany("not_subnets", args.notSubnets())
params.MaybeAdd("zone", args.Zone)
params.MaybeAdd("pool", args.Pool)
params.MaybeAddMany("not_in_zone", args.NotInZone)
params.MaybeAddMany("not_in_pool", args.NotInPool)
params.MaybeAdd("agent_name", args.AgentName)
params.MaybeAdd("comment", args.Comment)
params.MaybeAddBool("dry_run", args.DryRun)
Expand Down
14 changes: 12 additions & 2 deletions controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (s *controllerSuite) SetUpTest(c *gc.C) {
server.AddGetResponse("/api/2.0/users/?op=whoami", http.StatusOK, `"captain awesome"`)
server.AddGetResponse("/api/2.0/version/", http.StatusOK, versionResponse)
server.AddGetResponse("/api/2.0/zones/", http.StatusOK, zoneResponse)
server.AddGetResponse("/api/2.0/pools/", http.StatusOK, poolResponse)
server.Start()
s.AddCleanup(func(*gc.C) { server.Close() })
s.server = server
Expand Down Expand Up @@ -343,6 +344,13 @@ func (s *controllerSuite) TestZones(c *gc.C) {
c.Assert(zones, gc.HasLen, 2)
}

func (s *controllerSuite) TestPools(c *gc.C) {
controller := s.getController(c)
pools, err := controller.Pools()
c.Assert(err, jc.ErrorIsNil)
c.Assert(pools, gc.HasLen, 2)
}

func (s *controllerSuite) TestMachines(c *gc.C) {
controller := s.getController(c)
machines, err := controller.Machines(MachinesArgs{})
Expand Down Expand Up @@ -409,11 +417,12 @@ func (s *controllerSuite) TestMachinesArgs(c *gc.C) {
SystemIDs: []string{"something-else"},
Domain: "magic",
Zone: "foo",
Pool: "swimming_is_fun",
AgentName: "agent 42",
})
request := s.server.LastRequest()
// There should be one entry in the form values for each of the args.
c.Assert(request.URL.Query(), gc.HasLen, 6)
c.Assert(request.URL.Query(), gc.HasLen, 7)
}

func (s *controllerSuite) TestStorageSpec(c *gc.C) {
Expand Down Expand Up @@ -696,6 +705,7 @@ func (s *controllerSuite) TestAllocateMachineArgsForm(c *gc.C) {
Interfaces: []InterfaceSpec{{Label: "default", Space: "magic"}},
NotSpace: []string{"special"},
Zone: "magic",
Pool: "swimming_is_fun",
NotInZone: []string{"not-magic"},
AgentName: "agent 42",
Comment: "testing",
Expand All @@ -707,7 +717,7 @@ func (s *controllerSuite) TestAllocateMachineArgsForm(c *gc.C) {
request := s.server.LastRequest()
// There should be one entry in the form values for each of the args.
form := request.PostForm
c.Assert(form, gc.HasLen, 15)
c.Assert(form, gc.HasLen, 16)
// Positive space check.
c.Assert(form.Get("interfaces"), gc.Equals, "default:space=magic")
// Negative space check.
Expand Down
18 changes: 18 additions & 0 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type device struct {
ipAddresses []string
interfaceSet []*interface_
zone *zone
pool *pool
}

// SystemID implements Device.
Expand Down Expand Up @@ -68,6 +69,14 @@ func (d *device) Zone() Zone {
return d.zone
}

// Pool implements Device.
func (d *device) Pool() Pool {
if d.pool == nil {
return nil
}
return d.pool
}

// InterfaceSet implements Device.
func (d *device) InterfaceSet() []Interface {
result := make([]Interface, len(d.interfaceSet))
Expand Down Expand Up @@ -252,6 +261,7 @@ func device_2_0(source map[string]interface{}) (*device, error) {
"ip_addresses": schema.List(schema.String()),
"interface_set": schema.List(schema.StringMap(schema.Any())),
"zone": schema.StringMap(schema.Any()),
"pool": schema.StringMap(schema.Any()),
}
defaults := schema.Defaults{
"owner": "",
Expand All @@ -270,7 +280,14 @@ func device_2_0(source map[string]interface{}) (*device, error) {
if err != nil {
return nil, errors.Trace(err)
}

zone, err := zone_2_0(valid["zone"].(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}

pool, err := pool_2_0(valid["pool"].(map[string]interface{}))

if err != nil {
return nil, errors.Trace(err)
}
Expand All @@ -288,6 +305,7 @@ func device_2_0(source map[string]interface{}) (*device, error) {
ipAddresses: convertToStringSlice(valid["ip_addresses"]),
interfaceSet: interfaceSet,
zone: zone,
pool: pool,
}
return result, nil
}
5 changes: 5 additions & 0 deletions device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ const (
"description": "",
"resource_uri": "/MAAS/api/2.0/zones/default/",
"name": "default"
},
"pool": {
"description": "",
"resource_uri": "/MAAS/api/2.0/pools/default/",
"name": "default"
},
"domain": {
"resource_record_count": 0,
Expand Down
2 changes: 1 addition & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/juju/version"
)

// Can't use interface as a type, so add an underscore. Yay.
// Can't use "interface" as a type, so add an underscore. Yay.
type interface_ struct {
controller *controller

Expand Down
2 changes: 1 addition & 1 deletion interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ const (
"parents": ["bond0"],
"id": 40,
"type": "physical",
"resource_uri": "/MAAS/api/2.0/nodes/4y3ha6/interfaces/40/",
"resource_uri": "/maas/api/2.0/nodes/4y3ha6/interfaces/40/",
notjames marked this conversation as resolved.
Show resolved Hide resolved
"tags": null,
"links": [
{
Expand Down
12 changes: 12 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type Controller interface {
// Zones lists all the zones known to the MAAS controller.
Zones() ([]Zone, error)

// Pools lists all the pools known to the MAAS controller.
Pools() ([]Pool, error)

// Machines returns a list of machines that match the params.
Machines(MachinesArgs) ([]Machine, error)

Expand Down Expand Up @@ -145,6 +148,13 @@ type Zone interface {
Description() string
}

// Pool is just a logical separation of resources.
type Pool interface {
// The name of the resource pool
Name() string
Description() string
}

type Domain interface {
// The name of the Domain
Name() string
Expand All @@ -168,6 +178,7 @@ type Device interface {
FQDN() string
IPAddresses() []string
Zone() Zone
Pool() Pool

// Parent returns the SystemID of the Parent. Most often this will be a
// Machine.
Expand Down Expand Up @@ -240,6 +251,7 @@ type Machine interface {
Partition(id int) Partition

Zone() Zone
Pool() Pool

// Start the machine and install the operating system specified in the args.
Start(StartArgs) error
Expand Down
22 changes: 22 additions & 0 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type machine struct {
bootInterface *interface_
interfaceSet []*interface_
zone *zone
pool *pool
// Don't really know the difference between these two lists:
physicalBlockDevices []*blockdevice
blockDevices []*blockdevice
Expand All @@ -60,6 +61,7 @@ func (m *machine) updateFrom(other *machine) {
m.statusName = other.statusName
m.statusMessage = other.statusMessage
m.zone = other.zone
m.pool = other.pool
m.tags = other.tags
m.ownerData = other.ownerData
}
Expand All @@ -84,6 +86,14 @@ func (m *machine) Tags() []string {
return m.tags
}

// Pool implements Machine
func (m *machine) Pool() Pool {
if m.pool == nil {
return nil
}
return m.pool
}

// IPAddresses implements Machine.
func (m *machine) IPAddresses() []string {
return m.ipAddresses
Expand Down Expand Up @@ -510,13 +520,15 @@ func machine_2_0(source map[string]interface{}) (*machine, error) {
"boot_interface": schema.OneOf(schema.Nil(""), schema.StringMap(schema.Any())),
"interface_set": schema.List(schema.StringMap(schema.Any())),
"zone": schema.StringMap(schema.Any()),
"pool": schema.StringMap(schema.Any()),

"physicalblockdevice_set": schema.List(schema.StringMap(schema.Any())),
"blockdevice_set": schema.List(schema.StringMap(schema.Any())),
}
defaults := schema.Defaults{
"architecture": "",
}

checker := schema.FieldMap(fields, defaults)
coerced, err := checker.Coerce(source, nil)
if err != nil {
Expand All @@ -538,14 +550,23 @@ func machine_2_0(source map[string]interface{}) (*machine, error) {
if err != nil {
return nil, errors.Trace(err)
}

zone, err := zone_2_0(valid["zone"].(map[string]interface{}))
if err != nil {
return nil, errors.Trace(err)
}

pool, err := pool_2_0(valid["pool"].(map[string]interface{}))
if err != nil {

return nil, errors.Trace(err)
}

physicalBlockDevices, err := readBlockDeviceList(valid["physicalblockdevice_set"].([]interface{}), blockdevice_2_0)
if err != nil {
return nil, errors.Trace(err)
}

blockDevices, err := readBlockDeviceList(valid["blockdevice_set"].([]interface{}), blockdevice_2_0)
if err != nil {
return nil, errors.Trace(err)
Expand Down Expand Up @@ -575,6 +596,7 @@ func machine_2_0(source map[string]interface{}) (*machine, error) {
bootInterface: bootInterface,
interfaceSet: interfaceSet,
zone: zone,
pool: pool,
physicalBlockDevices: physicalBlockDevices,
blockDevices: blockDevices,
}
Expand Down
Loading