diff --git a/api/bcache.go b/api/bcache.go new file mode 100644 index 0000000..42198c4 --- /dev/null +++ b/api/bcache.go @@ -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 +} diff --git a/api/bcache_cache_set.go b/api/bcache_cache_set.go new file mode 100644 index 0000000..f6e3d89 --- /dev/null +++ b/api/bcache_cache_set.go @@ -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 +} diff --git a/api/bcache_cache_sets.go b/api/bcache_cache_sets.go new file mode 100644 index 0000000..6b6b35c --- /dev/null +++ b/api/bcache_cache_sets.go @@ -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) +} diff --git a/api/bcaches.go b/api/bcaches.go new file mode 100644 index 0000000..5a87c94 --- /dev/null +++ b/api/bcaches.go @@ -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) +} diff --git a/client/bcache.go b/client/bcache.go new file mode 100644 index 0000000..bbce190 --- /dev/null +++ b/client/bcache.go @@ -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() +} diff --git a/client/bcache_cache_set.go b/client/bcache_cache_set.go new file mode 100644 index 0000000..7678cca --- /dev/null +++ b/client/bcache_cache_set.go @@ -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() +} diff --git a/client/bcache_cache_sets.go b/client/bcache_cache_sets.go new file mode 100644 index 0000000..175eb7e --- /dev/null +++ b/client/bcache_cache_sets.go @@ -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 +} diff --git a/client/bcaches.go b/client/bcaches.go new file mode 100644 index 0000000..c6970c9 --- /dev/null +++ b/client/bcaches.go @@ -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 +} diff --git a/client/client.go b/client/client.go index c60abce..a252ab9 100644 --- a/client/client.go +++ b/client/client.go @@ -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 @@ -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. diff --git a/client/raid.go b/client/raid.go index 6f9ace8..7d7af6c 100644 --- a/client/raid.go +++ b/client/raid.go @@ -1,3 +1,4 @@ +//nolint:dupl // disable dupl check on client for now package client import ( diff --git a/entity/bcache.go b/entity/bcache.go new file mode 100644 index 0000000..bdaaff0 --- /dev/null +++ b/entity/bcache.go @@ -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"` +} diff --git a/entity/bcache_cache_set.go b/entity/bcache_cache_set.go new file mode 100644 index 0000000..209b1d0 --- /dev/null +++ b/entity/bcache_cache_set.go @@ -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"` +}