Skip to content

Commit

Permalink
feat: add zones (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
skatsaounis committed Jan 8, 2024
1 parent e2e876f commit 8123a5d
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
12 changes: 12 additions & 0 deletions api/zone.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions api/zones.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 4 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
48 changes: 48 additions & 0 deletions client/zone.go
Original file line number Diff line number Diff line change
@@ -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()
}
44 changes: 44 additions & 0 deletions client/zones.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions entity/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 8123a5d

Please sign in to comment.