Skip to content

Commit

Permalink
feat: add package repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
skatsaounis committed Jan 3, 2024
1 parent 6353a21 commit bdada2c
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 0 deletions.
12 changes: 12 additions & 0 deletions api/package_repositories.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package api

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

// PackageRepositories is an interface for listing and creating
// Package Repository records
type PackageRepositories interface {
Get() ([]entity.PackageRepository, error)
Create(params *entity.PackageRepositoryParams) (*entity.PackageRepository, error)
}
12 changes: 12 additions & 0 deletions api/package_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package api

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

// PackageRepository represents the MAAS Server Package Repository endpoint
type PackageRepository interface {
Get(id int) (*entity.PackageRepository, error)
Update(id int, params *entity.PackageRepositoryParams) (*entity.PackageRepository, error)
Delete(id int) error
}
4 changes: 4 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func constructClient(apiClient *APIClient) *Client {
Users: &Users{APIClient: *apiClient},
ResourcePool: &ResourcePool{APIClient: *apiClient},
ResourcePools: &ResourcePools{APIClient: *apiClient},
PackageRepository: &PackageRepository{APIClient: *apiClient},
PackageRepositories: &PackageRepositories{APIClient: *apiClient},
}

return &client
Expand Down Expand Up @@ -114,6 +116,8 @@ type Client struct {
Users api.Users
ResourcePool api.ResourcePool
ResourcePools api.ResourcePools
PackageRepository api.PackageRepository
PackageRepositories api.PackageRepositories
}

// GetAPIClient returns a MAAS API client.
Expand Down
1 change: 1 addition & 0 deletions client/device.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
1 change: 1 addition & 0 deletions client/fabric.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
1 change: 1 addition & 0 deletions client/ip_range.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
44 changes: 44 additions & 0 deletions client/package_repositories.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"
)

// PackageRepositories implements api.PackageRepositories
type PackageRepositories struct {
APIClient APIClient
}

func (p *PackageRepositories) client() APIClient {
return p.APIClient.GetSubObject("package-repositories")
}

// Get fetches a list of PackageRepositories
func (p *PackageRepositories) Get() ([]entity.PackageRepository, error) {
packageRepositories := make([]entity.PackageRepository, 0)
err := p.client().Get("", url.Values{}, func(data []byte) error {
return json.Unmarshal(data, &packageRepositories)
})

return packageRepositories, err
}

// Create creates a new PackageRepository
func (p *PackageRepositories) Create(packageRepositoryParams *entity.PackageRepositoryParams) (*entity.PackageRepository, error) {
qsp, err := query.Values(packageRepositoryParams)
if err != nil {
return nil, err
}

packageRepository := new(entity.PackageRepository)
err = p.client().Post("", qsp, func(data []byte) error {
return json.Unmarshal(data, packageRepository)
})

return packageRepository, err
}
50 changes: 50 additions & 0 deletions client/package_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//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"
)

// PackageRepository implements api.PackageRepository
type PackageRepository struct {
APIClient APIClient
}

func (p *PackageRepository) client(id int) APIClient {
return p.APIClient.GetSubObject("package-repositories").GetSubObject(fmt.Sprintf("%v", id))
}

// Get fetches a packageRepository
func (p *PackageRepository) Get(id int) (*entity.PackageRepository, error) {
packageRepository := new(entity.PackageRepository)
err := p.client(id).Get("", url.Values{}, func(data []byte) error {
return json.Unmarshal(data, packageRepository)
})

return packageRepository, err
}

// Update updates a given PackageRepository
func (p *PackageRepository) Update(id int, packageRepositoryParams *entity.PackageRepositoryParams) (*entity.PackageRepository, error) {
qsp, err := query.Values(packageRepositoryParams)
if err != nil {
return nil, err
}

packageRepository := new(entity.PackageRepository)
err = p.client(id).Put(qsp, func(data []byte) error {
return json.Unmarshal(data, packageRepository)
})

return packageRepository, err
}

// Delete deletes a given PackageRepository
func (p *PackageRepository) Delete(id int) error {
return p.client(id).Delete()
}
29 changes: 29 additions & 0 deletions entity/package_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package entity

type PackageRepository struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Key string `json:"key,omitempty"`
ResourceURI string `json:"resource_uri,omitempty"`
Distributions []string `json:"distributions,omitempty"`
DisabledPockets []string `json:"disabled_pockets,omitempty"`
DisabledComponents []string `json:"disabled_components,omitempty"`
Components []string `json:"components,omitempty"`
Arches []string `json:"arches,omitempty"`
ID int `json:"id,omitempty"`
DisableSources bool `json:"disable_sources,omitempty"`
Enabled bool `json:"enabled,omitempty"`
}

type PackageRepositoryParams struct {
Name string `url:"name,omitempty"`
URL string `url:"url,omitempty"`
Distributions string `url:"distributions,omitempty"`
DisabledPockets string `url:"disabled_pockets,omitempty"`
DisabledComponents string `url:"disabled_components,omitempty"`
Components string `url:"components,omitempty"`
Arches string `url:"arches,omitempty"`
Key string `url:"key,omitempty"`
DisableSources bool `url:"disable_sources,omitempty"`
Enabled bool `url:"enabled,omitempty"`
}

0 comments on commit bdada2c

Please sign in to comment.