From 8123a5d9368c90af8dc5c833e18583b39b0efce7 Mon Sep 17 00:00:00 2001 From: Stamatis Katsaounis Date: Mon, 8 Jan 2024 20:23:33 +0200 Subject: [PATCH] feat: add zones (#46) --- api/zone.go | 12 ++++++++++++ api/zones.go | 11 +++++++++++ client/client.go | 4 ++++ client/zone.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ client/zones.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ entity/zone.go | 5 +++++ 6 files changed, 124 insertions(+) create mode 100644 api/zone.go create mode 100644 api/zones.go create mode 100644 client/zone.go create mode 100644 client/zones.go diff --git a/api/zone.go b/api/zone.go new file mode 100644 index 0000000..3df2c6e --- /dev/null +++ b/api/zone.go @@ -0,0 +1,12 @@ +package api + +import ( + "github.com/maas/gomaasclient/entity" +) + +// Zone is an interface defining API behaviour for zones +type Zone interface { + Get(name string) (*entity.Zone, error) + Update(name string, params *entity.ZoneParams) (*entity.Zone, error) + Delete(name string) error +} diff --git a/api/zones.go b/api/zones.go new file mode 100644 index 0000000..b90894c --- /dev/null +++ b/api/zones.go @@ -0,0 +1,11 @@ +package api + +import ( + "github.com/maas/gomaasclient/entity" +) + +// Zones is an interface for listing and creating Zone objects +type Zones interface { + Get() ([]entity.Zone, error) + Create(params *entity.ZoneParams) (*entity.Zone, error) +} diff --git a/client/client.go b/client/client.go index ce524c5..c60abce 100644 --- a/client/client.go +++ b/client/client.go @@ -78,6 +78,8 @@ func constructClient(apiClient *APIClient) *Client { BootSourceSelections: &BootSourceSelections{APIClient: *apiClient}, BootResource: &BootResource{APIClient: *apiClient}, BootResources: &BootResources{APIClient: *apiClient}, + Zone: &Zone{APIClient: *apiClient}, + Zones: &Zones{APIClient: *apiClient}, } return &client @@ -132,6 +134,8 @@ type Client struct { BootSourceSelections api.BootSourceSelections BootResource api.BootResource BootResources api.BootResources + Zone api.Zone + Zones api.Zones } // GetAPIClient returns a MAAS API client. diff --git a/client/zone.go b/client/zone.go new file mode 100644 index 0000000..2673f55 --- /dev/null +++ b/client/zone.go @@ -0,0 +1,48 @@ +package client + +import ( + "encoding/json" + "net/url" + + "github.com/google/go-querystring/query" + "github.com/maas/gomaasclient/entity" +) + +// Zone Contains functionality for manipulating the Zone entity. +type Zone struct { + APIClient APIClient +} + +func (z *Zone) client(name string) APIClient { + return z.APIClient.GetSubObject("zones").GetSubObject(name) +} + +// Get Zone details. +func (z *Zone) Get(name string) (*entity.Zone, error) { + zone := new(entity.Zone) + err := z.client(name).Get("", url.Values{}, func(data []byte) error { + return json.Unmarshal(data, zone) + }) + + return zone, err +} + +// Update Zone. +func (z *Zone) Update(name string, params *entity.ZoneParams) (*entity.Zone, error) { + qsp, err := query.Values(params) + if err != nil { + return nil, err + } + + zone := new(entity.Zone) + err = z.client(name).Put(qsp, func(data []byte) error { + return json.Unmarshal(data, zone) + }) + + return zone, err +} + +// Delete Zone. +func (z *Zone) Delete(name string) error { + return z.client(name).Delete() +} diff --git a/client/zones.go b/client/zones.go new file mode 100644 index 0000000..3154aee --- /dev/null +++ b/client/zones.go @@ -0,0 +1,44 @@ +//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" +) + +// Zones implements api.Zones +type Zones struct { + APIClient APIClient +} + +func (z *Zones) client() APIClient { + return z.APIClient.GetSubObject("zones") +} + +// Get fetches a list of Zone objects +func (z *Zones) Get() ([]entity.Zone, error) { + zones := make([]entity.Zone, 0) + err := z.client().Get("", url.Values{}, func(data []byte) error { + return json.Unmarshal(data, &zones) + }) + + return zones, err +} + +// Create creates a new Zone +func (z *Zones) Create(params *entity.ZoneParams) (*entity.Zone, error) { + qsp, err := query.Values(params) + if err != nil { + return nil, err + } + + zone := new(entity.Zone) + err = z.client().Post("", qsp, func(data []byte) error { + return json.Unmarshal(data, zone) + }) + + return zone, err +} diff --git a/entity/zone.go b/entity/zone.go index 3a1a858..99a89c0 100644 --- a/entity/zone.go +++ b/entity/zone.go @@ -7,3 +7,8 @@ type Zone struct { ResourceURI string `json:"resource_uri,omitempty"` ID int `json:"id,omitempty"` } + +type ZoneParams struct { + Name string `url:"name,omitempty"` + Description string `url:"description,omitempty"` +}