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

[WIP] start machine now accepts a base datastructure #100

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
81 changes: 81 additions & 0 deletions base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2023 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package gomaasapi

import (
"fmt"

"github.com/juju/errors"
"github.com/juju/version/v2"
)

var VersionDeploySupportsBases = version.Number{Major: 3, Minor: 3, Patch: 5}

type OSType string

var (
Custom OSType = "custom"
Ubuntu OSType = "ubuntu"
Centos OSType = "centos"
)

type Base struct {
OS OSType
Version string
}

func (b Base) String() string {
if b.OS != "" && b.Version != "" {
return fmt.Sprintf("%s/%s", b.OS, b.Version)
}
if b.Version != "" {
return b.Version
}
return ""
}

func (b Base) toSeries() (string, error) {
if b.OS == "" {
return b.Version, nil
}
switch b.OS {
case Custom:
return b.String(), nil
case Ubuntu:
if series, ok := ubuntuMap[b.Version]; ok {
return series, nil
}
return "", errors.NotValidf("base %s cannot be converted into a series", b)
case Centos:
if series, ok := centosMap[b.Version]; ok {
return series, nil
}
return "", errors.NotValidf("base %s cannot be converted into a series", b)
default:
return "", errors.NotValidf("base %s OS", b)
}

}

const (
centos7 = "centos7"
centos8 = "centos8"
centos9 = "centos9"
)

var ubuntuMap = map[string]string{
"20.04": "ubuntu/focal",
"20.10": "ubuntu/groovy",
"21.04": "ubuntu/hirsute",
"21.10": "ubuntu/impish",
"22.04": "ubuntu/jammy",
"22.10": "ubuntu/kinetic",
"23.04": "ubuntu/lunar",
}

var centosMap = map[string]string{
"7": "centos/centos7",
"8": "centos/centos8",
"9": "centos/centos9",
}
Comment on lines +13 to +81
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite the right approach.

So we don't want to hardcode the versions here. That's for juju to request and for MAAS to say are valid or not. What I was looking for is just a Base type that you can validate up front. Either from a string ParseBase which will return a valid Base or NewBase which takes the concrete types. Both of which should validate the consistency of a base.

For example, you can't set OS without a Version.

What we don't want is to have that lazily. If toSeries was called Validate (minus the concrete version checking), then that would be ideal.

2 changes: 1 addition & 1 deletion blockdevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package gomaasapi
import (
"github.com/juju/errors"
"github.com/juju/schema"
"github.com/juju/version"
"github.com/juju/version/v2"
)

type blockdevice struct {
Expand Down
2 changes: 1 addition & 1 deletion blockdevice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package gomaasapi

import (
jc "github.com/juju/testing/checkers"
"github.com/juju/version"
"github.com/juju/version/v2"
gc "gopkg.in/check.v1"
)

Expand Down
2 changes: 1 addition & 1 deletion bootresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/schema"
"github.com/juju/version"
"github.com/juju/version/v2"
)

type bootResource struct {
Expand Down
2 changes: 1 addition & 1 deletion bootresource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package gomaasapi
import (
"github.com/juju/collections/set"
jc "github.com/juju/testing/checkers"
"github.com/juju/version"
"github.com/juju/version/v2"
gc "gopkg.in/check.v1"
)

Expand Down
22 changes: 13 additions & 9 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/schema"
"github.com/juju/version"
"github.com/juju/version/v2"
)

var (
Expand Down Expand Up @@ -97,7 +97,7 @@ func newControllerWithVersion(baseURL, apiVersion, apiKey string, httpClient *ht
Minor: minor,
}
controller := &controller{client: client, apiVersion: controllerVersion}
_, _, controller.capabilities, err = controller.readAPIVersionInfo()
controller.maasVersion, _, controller.capabilities, err = controller.readAPIVersionInfo()
if err != nil {
logger.Debugf("read version failed: %#v", err)
return nil, errors.Trace(err)
Expand Down Expand Up @@ -132,6 +132,7 @@ func newControllerUnknownVersion(args ControllerArgs) (Controller, error) {
type controller struct {
client *Client
apiVersion version.Number
maasVersion version.Number
capabilities set.Strings
}

Expand Down Expand Up @@ -914,17 +915,17 @@ func indicatesUnsupportedVersion(err error) bool {

// APIVersionInfo returns the version and subversion strings for the MAAS
// controller.
func (c *controller) APIVersionInfo() (string, string, error) {
func (c *controller) APIVersionInfo() (version.Number, string, error) {
version, subversion, _, err := c.readAPIVersionInfo()
return version, subversion, err
}

func (c *controller) readAPIVersionInfo() (string, string, set.Strings, error) {
func (c *controller) readAPIVersionInfo() (version.Number, string, set.Strings, error) {
parsed, err := c.get("version")
if indicatesUnsupportedVersion(err) {
return "", "", nil, WrapWithUnsupportedVersionError(err)
return version.Zero, "", nil, WrapWithUnsupportedVersionError(err)
} else if err != nil {
return "", "", nil, errors.Trace(err)
return version.Zero, "", nil, errors.Trace(err)
}

fields := schema.Fields{
Expand All @@ -935,7 +936,7 @@ func (c *controller) readAPIVersionInfo() (string, string, set.Strings, error) {
checker := schema.FieldMap(fields, nil) // no defaults
coerced, err := checker.Coerce(parsed, nil)
if err != nil {
return "", "", nil, WrapWithDeserializationError(err, "version response")
return version.Zero, "", nil, WrapWithDeserializationError(err, "version response")
}
// For now, we don't append any subversion, but as it becomes used, we
// should parse and check.
Expand All @@ -949,10 +950,13 @@ func (c *controller) readAPIVersionInfo() (string, string, set.Strings, error) {
capabilities.Add(value.(string))
}

version := valid["version"].(string)
maasVersion, err := version.Parse(valid["version"].(string))
if err != nil {
return version.Zero, "", nil, WrapWithDeserializationError(err, "version response")
}
subversion := valid["subversion"].(string)

return version, subversion, capabilities, nil
return maasVersion, subversion, capabilities, nil
}

func parseAllocateConstraintsResponse(source interface{}, machine *machine) (ConstraintMatches, error) {
Expand Down
2 changes: 1 addition & 1 deletion controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/juju/loggo"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/version"
"github.com/juju/version/v2"
gc "gopkg.in/check.v1"
)

Expand Down
2 changes: 1 addition & 1 deletion device.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/juju/errors"
"github.com/juju/schema"
"github.com/juju/version"
"github.com/juju/version/v2"
)

type device struct {
Expand Down
2 changes: 1 addition & 1 deletion device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/juju/errors"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/version"
"github.com/juju/version/v2"
gc "gopkg.in/check.v1"
)

Expand Down
2 changes: 1 addition & 1 deletion domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package gomaasapi
import (
"github.com/juju/errors"
"github.com/juju/schema"
"github.com/juju/version"
"github.com/juju/version/v2"
)

type domain struct {
Expand Down
2 changes: 1 addition & 1 deletion fabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package gomaasapi
import (
"github.com/juju/errors"
"github.com/juju/schema"
"github.com/juju/version"
"github.com/juju/version/v2"
)

type fabric struct {
Expand Down
2 changes: 1 addition & 1 deletion fabric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package gomaasapi

import (
jc "github.com/juju/testing/checkers"
"github.com/juju/version"
"github.com/juju/version/v2"
gc "gopkg.in/check.v1"
)

Expand Down
2 changes: 1 addition & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/juju/errors"
"github.com/juju/schema"
"github.com/juju/version"
"github.com/juju/version/v2"
)

type file struct {
Expand Down
2 changes: 1 addition & 1 deletion file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/version"
"github.com/juju/version/v2"
gc "gopkg.in/check.v1"
)

Expand Down
23 changes: 10 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,24 @@ module github.com/juju/gomaasapi/v2
go 1.17

require (
github.com/juju/collections v0.0.0-20220203020748-febd7cad8a7a
github.com/juju/errors v0.0.0-20220203013757-bd733f3c86b9
github.com/juju/loggo v0.0.0-20210728185423-eebad3a902c4
github.com/juju/collections v1.0.0
github.com/juju/errors v1.0.0
github.com/juju/loggo v1.0.0
github.com/juju/mgo/v2 v2.0.0-20220111072304-f200228f1090
github.com/juju/schema v1.0.1-0.20190814234152-1f8aaeef0989
github.com/juju/testing v0.0.0-20220203020004-a0ff61f03494
github.com/juju/version v0.0.0-20191219164919-81c1be00b9a6
github.com/juju/testing v1.0.1
github.com/juju/version/v2 v2.0.1
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
)

require (
github.com/juju/clock v0.0.0-20220203021603-d9deb868a28a // indirect
github.com/juju/retry v0.0.0-20220204093819-62423bf33287 // indirect
github.com/juju/utils/v3 v3.0.0-20220203023959-c3fbc78a33b0 // indirect
github.com/juju/version/v2 v2.0.0-20220204124744-fc9915e3d935 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/juju/clock v1.0.0 // indirect
github.com/juju/mgo/v3 v3.0.2 // indirect
github.com/juju/utils/v3 v3.0.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/xdg-go/stringprep v1.0.2 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading