Skip to content

Commit

Permalink
feat: add BCache related entities and API endpoint interactions (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
skatsaounis committed Jan 11, 2024
1 parent aa4013a commit f9e15e9
Show file tree
Hide file tree
Showing 12 changed files with 287 additions and 0 deletions.
12 changes: 12 additions & 0 deletions api/bcache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package api

import (
"github.com/maas/gomaasclient/entity"
)

// BCache declares the API operations related to MAAS `nodes/{systemID}/bcache/{id}` endpoint.
type BCache interface {
Get(systemID string, id int) (*entity.BCache, error)
Update(systemID string, id int, params *entity.BCacheParams) (*entity.BCache, error)
Delete(systemID string, id int) error
}
12 changes: 12 additions & 0 deletions api/bcache_cache_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package api

import (
"github.com/maas/gomaasclient/entity"
)

// BCacheCacheSet declares the API operations related to MAAS `nodes/{systemID}/bcache-cache-set/{id}` endpoint.
type BCacheCacheSet interface {
Get(systemID string, id int) (*entity.BCacheCacheSet, error)
Update(systemID string, id int, params *entity.BCacheCacheSetParams) (*entity.BCacheCacheSet, error)
Delete(systemID string, id int) error
}
11 changes: 11 additions & 0 deletions api/bcache_cache_sets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package api

import (
"github.com/maas/gomaasclient/entity"
)

// BCacheCacheSets declares the API operations related to MAAS `nodes/{systemID}/bcache-cache-sets` endpoint.
type BCacheCacheSets interface {
Get(systemID string) ([]entity.BCacheCacheSet, error)
Create(systemID string, params *entity.BCacheCacheSetParams) (*entity.BCacheCacheSet, error)
}
11 changes: 11 additions & 0 deletions api/bcaches.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package api

import (
"github.com/maas/gomaasclient/entity"
)

// BCaches declares the API operations related to MAAS `nodes/{systemID}/bcaches` endpoint.
type BCaches interface {
Get(systemID string) ([]entity.BCache, error)
Create(systemID string, params *entity.BCacheParams) (*entity.BCache, error)
}
52 changes: 52 additions & 0 deletions client/bcache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//nolint:dupl // disable dupl check on client for now
package client

import (
"encoding/json"
"fmt"
"net/url"

"github.com/google/go-querystring/query"
"github.com/maas/gomaasclient/entity"
)

// BCache Contains functionality for manipulating the BCache entity.
type BCache struct {
APIClient APIClient
}

func (b *BCache) client(systemID string, id int) APIClient {
return b.APIClient.
GetSubObject("nodes").GetSubObject(systemID).
GetSubObject("bcache").GetSubObject(fmt.Sprintf("%v", id))
}

// Get BCache details.
func (b *BCache) Get(systemID string, id int) (*entity.BCache, error) {
bCache := new(entity.BCache)
err := b.client(systemID, id).Get("", url.Values{}, func(data []byte) error {
return json.Unmarshal(data, bCache)
})

return bCache, err
}

// Update BCache.
func (b *BCache) Update(systemID string, id int, params *entity.BCacheParams) (*entity.BCache, error) {
qsp, err := query.Values(params)
if err != nil {
return nil, err
}

bCache := new(entity.BCache)
err = b.client(systemID, id).Put(qsp, func(data []byte) error {
return json.Unmarshal(data, bCache)
})

return bCache, err
}

// Delete BCache.
func (b *BCache) Delete(systemID string, id int) error {
return b.client(systemID, id).Delete()
}
52 changes: 52 additions & 0 deletions client/bcache_cache_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//nolint:dupl // disable dupl check on client for now
package client

import (
"encoding/json"
"fmt"
"net/url"

"github.com/google/go-querystring/query"
"github.com/maas/gomaasclient/entity"
)

// BCacheCacheSet Contains functionality for manipulating the BCacheCacheSet entity.
type BCacheCacheSet struct {
APIClient APIClient
}

func (b *BCacheCacheSet) client(systemID string, id int) APIClient {
return b.APIClient.
GetSubObject("nodes").GetSubObject(systemID).
GetSubObject("bcache-cache-set").GetSubObject(fmt.Sprintf("%v", id))
}

// Get BCacheCacheSet details.
func (b *BCacheCacheSet) Get(systemID string, id int) (*entity.BCacheCacheSet, error) {
bCacheCacheSet := new(entity.BCacheCacheSet)
err := b.client(systemID, id).Get("", url.Values{}, func(data []byte) error {
return json.Unmarshal(data, bCacheCacheSet)
})

return bCacheCacheSet, err
}

// Update BCacheCacheSet.
func (b *BCacheCacheSet) Update(systemID string, id int, params *entity.BCacheCacheSetParams) (*entity.BCacheCacheSet, error) {
qsp, err := query.Values(params)
if err != nil {
return nil, err
}

bCacheCacheSet := new(entity.BCacheCacheSet)
err = b.client(systemID, id).Put(qsp, func(data []byte) error {
return json.Unmarshal(data, bCacheCacheSet)
})

return bCacheCacheSet, err
}

// Delete BCacheCacheSet.
func (b *BCacheCacheSet) Delete(systemID string, id int) error {
return b.client(systemID, id).Delete()
}
45 changes: 45 additions & 0 deletions client/bcache_cache_sets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//nolint:dupl // disable dupl check on client for now
package client

import (
"encoding/json"
"net/url"

"github.com/google/go-querystring/query"

"github.com/maas/gomaasclient/entity"
)

// BCacheCacheSets contains functionality for manipulating the BCacheCacheSets entity.
type BCacheCacheSets struct {
APIClient APIClient
}

func (b *BCacheCacheSets) client(systemID string) APIClient {
return b.APIClient.GetSubObject("nodes").GetSubObject(systemID).GetSubObject("bcache-cache-sets")
}

// Get BCacheCacheSets of a machine.
func (b *BCacheCacheSets) Get(systemID string) ([]entity.BCacheCacheSet, error) {
bCacheCacheSets := make([]entity.BCacheCacheSet, 0)
err := b.client(systemID).Get("", url.Values{}, func(data []byte) error {
return json.Unmarshal(data, &bCacheCacheSets)
})

return bCacheCacheSets, err
}

// Create a BCacheCacheSet of a machine.
func (b *BCacheCacheSets) Create(systemID string, params *entity.BCacheCacheSetParams) (*entity.BCacheCacheSet, error) {
qsp, err := query.Values(params)
if err != nil {
return nil, err
}

bCacheCacheSet := new(entity.BCacheCacheSet)
err = b.client(systemID).Post("", qsp, func(data []byte) error {
return json.Unmarshal(data, bCacheCacheSet)
})

return bCacheCacheSet, err
}
45 changes: 45 additions & 0 deletions client/bcaches.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//nolint:dupl // disable dupl check on client for now
package client

import (
"encoding/json"
"net/url"

"github.com/google/go-querystring/query"

"github.com/maas/gomaasclient/entity"
)

// BCaches contains functionality for manipulating the BCaches entity.
type BCaches struct {
APIClient APIClient
}

func (b *BCaches) client(systemID string) APIClient {
return b.APIClient.GetSubObject("nodes").GetSubObject(systemID).GetSubObject("bcaches")
}

// Get BCaches of a machine.
func (b *BCaches) Get(systemID string) ([]entity.BCache, error) {
bCaches := make([]entity.BCache, 0)
err := b.client(systemID).Get("", url.Values{}, func(data []byte) error {
return json.Unmarshal(data, &bCaches)
})

return bCaches, err
}

// Create a BCache of a machine.
func (b *BCaches) Create(systemID string, params *entity.BCacheParams) (*entity.BCache, error) {
qsp, err := query.Values(params)
if err != nil {
return nil, err
}

bCache := new(entity.BCache)
err = b.client(systemID).Post("", qsp, func(data []byte) error {
return json.Unmarshal(data, bCache)
})

return bCache, err
}
8 changes: 8 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func constructClient(apiClient *APIClient) *Client {
BootResources: &BootResources{APIClient: *apiClient},
Zone: &Zone{APIClient: *apiClient},
Zones: &Zones{APIClient: *apiClient},
BCache: &BCache{APIClient: *apiClient},
BCaches: &BCaches{APIClient: *apiClient},
BCacheCacheSet: &BCacheCacheSet{APIClient: *apiClient},
BCacheCacheSets: &BCacheCacheSets{APIClient: *apiClient},
}

return &client
Expand Down Expand Up @@ -136,6 +140,10 @@ type Client struct {
BootResources api.BootResources
Zone api.Zone
Zones api.Zones
BCache api.BCache
BCaches api.BCaches
BCacheCacheSet api.BCacheCacheSet
BCacheCacheSets api.BCacheCacheSets
}

// GetAPIClient returns a MAAS API client.
Expand Down
1 change: 1 addition & 0 deletions client/raid.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//nolint:dupl // disable dupl check on client for now
package client

import (
Expand Down
24 changes: 24 additions & 0 deletions entity/bcache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package entity

type BCache struct {
HumanSize string `json:"human_size,omitempty"`
UUID string `json:"uuid,omitempty"`
Name string `json:"name,omitempty"`
SystemID string `json:"system_id"`
CacheMode string `json:"cache_mode,omitempty"`
ResourceURI string `json:"resource_uri,omitempty"`
VirtualDevice BlockDevice `json:"virtual_device,omitempty"`
BackingDevice BlockDevice `json:"backing_device,omitempty"`
CacheSet BCacheCacheSet `json:"cache_set"`
ID int `json:"id,omitempty"`
Size int64 `json:"size,omitempty"`
}

type BCacheParams struct {
Name string `url:"name,omitempty"`
UUID string `url:"uuid,omitempty"`
CacheSet string `url:"cache_set,omitempty"`
BackingDevice string `url:"backing_device,omitempty"`
BackingPartition string `url:"backing_partition,omitempty"`
CacheMode string `url:"cache_mode,omitempty"`
}
14 changes: 14 additions & 0 deletions entity/bcache_cache_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package entity

type BCacheCacheSet struct {
Name string `json:"name:omitempty"`
SystemID string `json:"system_id,omitempty"`
ResourceURI string `json:"resource_uri,omitempty"`
CacheDevice BlockDevice `json:"cache_device,omitempty"`
ID int `json:"id,omitempty"`
}

type BCacheCacheSetParams struct {
CacheDevice string `url:"cache_device,omitempty"`
CachePartition string `url:"cache_partition,omitempty"`
}

0 comments on commit f9e15e9

Please sign in to comment.