Skip to content

Commit

Permalink
MEP 8 (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Jun 11, 2021
1 parent a82f2b4 commit a841ff6
Show file tree
Hide file tree
Showing 21 changed files with 2,805 additions and 239 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM metalstack/builder:latest as builder

FROM alpine:3.12
FROM alpine:3.13
RUN apk -U add ca-certificates
COPY --from=builder /work/bin/metal-api /metal-api
CMD ["/metal-api"]
35 changes: 35 additions & 0 deletions cmd/metal-api/internal/datastore/filesystem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package datastore

import "github.com/metal-stack/metal-api/cmd/metal-api/internal/metal"

// FindFilesystemLayout return a filesystemlayout for a given id.
func (rs *RethinkStore) FindFilesystemLayout(id string) (*metal.FilesystemLayout, error) {
var fl metal.FilesystemLayout
err := rs.findEntityByID(rs.filesystemLayoutTable(), &fl, id)
if err != nil {
return nil, err
}
return &fl, nil
}

// ListFilesystemLayouts returns all filesystemlayouts.
func (rs *RethinkStore) ListFilesystemLayouts() (metal.FilesystemLayouts, error) {
fls := make(metal.FilesystemLayouts, 0)
err := rs.listEntities(rs.filesystemLayoutTable(), &fls)
return fls, err
}

// CreateFilesystemLayout creates a new filesystemlayout.
func (rs *RethinkStore) CreateFilesystemLayout(fl *metal.FilesystemLayout) error {
return rs.createEntity(rs.filesystemLayoutTable(), fl)
}

// DeleteFilesystemLayout deletes a filesystemlayout.
func (rs *RethinkStore) DeleteFilesystemLayout(fl *metal.FilesystemLayout) error {
return rs.deleteEntity(rs.filesystemLayoutTable(), fl)
}

// UpdateFilesystemLayout updates a filesystemlayout.
func (rs *RethinkStore) UpdateFilesystemLayout(oldFilesystemLayout *metal.FilesystemLayout, newFilesystemLayout *metal.FilesystemLayout) error {
return rs.updateEntity(rs.filesystemLayoutTable(), newFilesystemLayout, oldFilesystemLayout)
}
25 changes: 2 additions & 23 deletions cmd/metal-api/internal/datastore/image.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package datastore

import (
"errors"
"fmt"
"sort"
"strings"
"time"

"github.com/Masterminds/semver/v3"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/metal"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/utils"
)

// GetImage return a image for a given id without semver matching.
Expand Down Expand Up @@ -128,7 +128,7 @@ func isOrphanImage(image metal.Image, machines metal.Machines) bool {
// then the most recent ubuntu image (ubuntu-19.10.20200407) is returned
// If patch is specified e.g. ubuntu-20.04.20200502 then this exact image is searched.
func (rs *RethinkStore) getMostRecentImageFor(id string, images metal.Images) (*metal.Image, error) {
os, sv, err := GetOsAndSemver(id)
os, sv, err := utils.GetOsAndSemverFromImage(id)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -165,27 +165,6 @@ func (rs *RethinkStore) getMostRecentImageFor(id string, images metal.Images) (*
return nil, fmt.Errorf("no image for os:%s version:%s found", os, sv)
}

// GetOsAndSemver parses a imageID to OS and Semver, or returns an error
// the last part must be the semantic version, valid ids are:
// ubuntu-19.04 os: ubuntu version: 19.04
// ubuntu-19.04.20200408 os: ubuntu version: 19.04.20200408
// ubuntu-small-19.04.20200408 os: ubuntu-small version: 19.04.20200408
func GetOsAndSemver(id string) (string, *semver.Version, error) {
imageParts := strings.Split(id, "-")
if len(imageParts) < 2 {
return "", nil, errors.New("image does not contain a version")
}

parts := len(imageParts) - 1
os := strings.Join(imageParts[:parts], "-")
version := strings.Join(imageParts[parts:], "")
v, err := semver.NewVersion(version)
if err != nil {
return "", nil, err
}
return os, v, nil
}

func sortImages(images []metal.Image) []metal.Image {
sort.SliceStable(images, func(i, j int) bool {
c := strings.Compare(images[i].OS, images[j].OS)
Expand Down
3 changes: 2 additions & 1 deletion cmd/metal-api/internal/datastore/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/metal"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/testdata"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/utils"
"github.com/stretchr/testify/assert"
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
)
Expand Down Expand Up @@ -534,7 +535,7 @@ func TestGetOsAndSemver(t *testing.T) {
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
os, version, err := GetOsAndSemver(tt.id)
os, version, err := utils.GetOsAndSemverFromImage(tt.id)
if (err != nil) != tt.wantErr {
t.Errorf("GetOsAndSemver() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
2 changes: 1 addition & 1 deletion cmd/metal-api/internal/datastore/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func (rs *RethinkStore) DeleteMachine(m *metal.Machine) error {
}

// UpdateMachine replaces a machine in the database if the 'changed' field of
// the old value equals the 'changed' field of the recored in the database.
// the old value equals the 'changed' field of the recorded in the database.
func (rs *RethinkStore) UpdateMachine(oldMachine *metal.Machine, newMachine *metal.Machine) error {
return rs.updateEntity(rs.machineTable(), newMachine, oldMachine)
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/metal-api/internal/datastore/rethinkdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
)

var tables = []string{
"image", "size", "partition", "machine", "switch", "event", "network", "ip", "migration",
"image", "size", "partition", "machine", "switch", "event", "network", "ip", "migration", "filesystemlayout",
VRFIntegerPool.String(), VRFIntegerPool.String() + "info",
ASNIntegerPool.String(), ASNIntegerPool.String() + "info",
}
Expand Down Expand Up @@ -185,6 +185,11 @@ func (rs *RethinkStore) ipTable() *r.Term {
return &res
}

func (rs *RethinkStore) filesystemLayoutTable() *r.Term {
res := r.DB(rs.dbname).Table("filesystemlayout")
return &res
}

func (rs *RethinkStore) asnTable() *r.Term {
res := r.DB(rs.dbname).Table(ASNIntegerPool.String())
return &res
Expand Down
Loading

0 comments on commit a841ff6

Please sign in to comment.