Skip to content

Commit

Permalink
feat: added generic collection and function (#124)
Browse files Browse the repository at this point in the history
* chore: updated test

* chore: added test

* feat: added generic collection and function

* chore: added Go 1.18 to workflow
  • Loading branch information
jbristowe authored Jul 27, 2022
1 parent 6e3322b commit 88184c9
Show file tree
Hide file tree
Showing 84 changed files with 283 additions and 723 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-go@v3
with:
go-version: '>=1.18.0'
- name: Build
run: go build -a -race -v ./...
- name: Vet
Expand Down
7 changes: 0 additions & 7 deletions pkg/accounts/account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ import (
uuid "github.com/google/uuid"
)

// AccountResources defines a collection of account resources with built-in
// support for paged results.
type AccountResources struct {
Items []*AccountResource `json:"Items"`
resources.PagedResults
}

// AccountResource represents account details used for deployments, including
// username/password, tokens, Azure and AWS credentials, and SSH key pairs.
type AccountResource struct {
Expand Down
5 changes: 3 additions & 2 deletions pkg/accounts/account_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package accounts
import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/internal"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services"
"github.com/dghubble/sling"
)
Expand Down Expand Up @@ -52,12 +53,12 @@ func (s *AccountService) Get(accountsQuery ...AccountsQuery) (*Accounts, error)
}
}

response, err := services.ApiGet(s.GetClient(), new(AccountResources), path)
response, err := services.ApiGet(s.GetClient(), new(resources.Resources[AccountResource]), path)
if err != nil {
return &Accounts{}, err
}

return ToAccounts(response.(*AccountResources)), nil
return ToAccounts(response.(*resources.Resources[AccountResource])), nil
}

// GetAll returns all accounts. If none are found or an error occurs, it
Expand Down
3 changes: 2 additions & 1 deletion pkg/accounts/account_utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package accounts
import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/internal"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
)

func ToAccount(accountResource *AccountResource) (IAccount, error) {
Expand Down Expand Up @@ -86,7 +87,7 @@ func ToAccount(accountResource *AccountResource) (IAccount, error) {
return account, nil
}

func ToAccounts(accountResources *AccountResources) *Accounts {
func ToAccounts(accountResources *resources.Resources[AccountResource]) *Accounts {
return &Accounts{
Items: ToAccountArray(accountResources.Items),
PagedResults: accountResources.PagedResults,
Expand Down
2 changes: 1 addition & 1 deletion pkg/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package accounts
import (
"encoding/json"

resources "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
)

// Accounts defines a collection of accounts with built-in support for paged
Expand Down
7 changes: 0 additions & 7 deletions pkg/actions/community_action_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ import (
uuid "github.com/google/uuid"
)

// CommunityActionTemplates defines a collection of community action templates
// with built-in support for paged results.
type CommunityActionTemplates struct {
Items []*CommunityActionTemplate `json:"Items"`
resources.PagedResults
}

// CommunityActionTemplate represents a community action template in Octopus
// Deploy.
type CommunityActionTemplate struct {
Expand Down
31 changes: 7 additions & 24 deletions pkg/actions/community_action_template_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package actions
import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/internal"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services"
"github.com/dghubble/sling"
"github.com/google/go-querystring/query"
Expand Down Expand Up @@ -38,41 +39,23 @@ func (s *CommunityActionTemplateService) getInstallationPath(resource CommunityA
return path, err
}

func (s *CommunityActionTemplateService) getPagedResponse(path string) ([]*CommunityActionTemplate, error) {
resources := []*CommunityActionTemplate{}
loadNextPage := true

for loadNextPage {
resp, err := services.ApiGet(s.GetClient(), new(CommunityActionTemplates), path)
if err != nil {
return resources, err
}

responseList := resp.(*CommunityActionTemplates)
resources = append(resources, responseList.Items...)
path, loadNextPage = services.LoadNextPage(responseList.PagedResults)
}

return resources, nil
}

// Get returns a collection of community action templates based on the criteria
// defined by its input query parameter. If an error occurs, an empty
// collection is returned along with the associated error.
func (s *CommunityActionTemplateService) Get(communityActionTemplatesQuery CommunityActionTemplatesQuery) (*CommunityActionTemplates, error) {
func (s *CommunityActionTemplateService) Get(communityActionTemplatesQuery CommunityActionTemplatesQuery) (*resources.Resources[CommunityActionTemplate], error) {
v, _ := query.Values(communityActionTemplatesQuery)
path := s.BasePath
encodedQueryString := v.Encode()
if len(encodedQueryString) > 0 {
path += "?" + encodedQueryString
}

resp, err := services.ApiGet(s.GetClient(), new(CommunityActionTemplates), path)
resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[CommunityActionTemplate]), path)
if err != nil {
return &CommunityActionTemplates{}, err
return &resources.Resources[CommunityActionTemplate]{}, err
}

return resp.(*CommunityActionTemplates), nil
return resp.(*resources.Resources[CommunityActionTemplate]), nil
}

// GetAll returns all community action templates. If none can be found or an
Expand All @@ -83,7 +66,7 @@ func (s *CommunityActionTemplateService) GetAll() ([]*CommunityActionTemplate, e
return []*CommunityActionTemplate{}, err
}

return s.getPagedResponse(path)
return services.GetPagedResponse[CommunityActionTemplate](s, path)
}

// GetByID returns the community action template that matches the input ID. If
Expand Down Expand Up @@ -117,7 +100,7 @@ func (s *CommunityActionTemplateService) GetByIDs(ids []string) ([]*CommunityAct
return []*CommunityActionTemplate{}, err
}

return s.getPagedResponse(path)
return services.GetPagedResponse[CommunityActionTemplate](s, path)
}

// GetByName performs a lookup and returns the community action template with a
Expand Down
7 changes: 0 additions & 7 deletions pkg/actiontemplates/action_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ import (
"github.com/go-playground/validator/v10/non-standard/validators"
)

// ActionTemplates defines a collection of action templates with built-in
// support for paged results.
type ActionTemplates struct {
Items []*ActionTemplate `json:"Items"`
resources.PagedResults
}

// ActionTemplate represents an action template in Octopus Deploy.
type ActionTemplate struct {
ActionType string `json:"ActionType" validate:"required,notblank"`
Expand Down
9 changes: 5 additions & 4 deletions pkg/actiontemplates/action_template_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/OctopusDeploy/go-octopusdeploy/v2/internal"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services"
"github.com/OctopusDeploy/go-octopusdeploy/v2/uritemplates"
"github.com/dghubble/sling"
Expand Down Expand Up @@ -62,20 +63,20 @@ func (s *ActionTemplateService) Add(actionTemplate *ActionTemplate) (*ActionTemp
// Get returns a collection of action templates based on the criteria defined
// by its input query parameter. If an error occurs, an empty collection is
// returned along with the associated error.
func (s *ActionTemplateService) Get(actionTemplatesQuery Query) (*ActionTemplates, error) {
func (s *ActionTemplateService) Get(actionTemplatesQuery Query) (*resources.Resources[ActionTemplate], error) {
v, _ := query.Values(actionTemplatesQuery)
path := s.BasePath
encodedQueryString := v.Encode()
if len(encodedQueryString) > 0 {
path += "?" + encodedQueryString
}

resp, err := services.ApiGet(s.GetClient(), new(ActionTemplates), path)
resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[ActionTemplate]), path)
if err != nil {
return &ActionTemplates{}, err
return &resources.Resources[ActionTemplate]{}, err
}

return resp.(*ActionTemplates), nil
return resp.(*resources.Resources[ActionTemplate]), nil
}

// GetAll returns all action templates. If none can be found or an error
Expand Down
7 changes: 0 additions & 7 deletions pkg/artifacts/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
)

// Artifacts defines a collection of artifacts with built-in support for paged
// results from the API.
type Artifacts struct {
Items []*Artifact `json:"Items"`
resources.PagedResults
}

// Artifact represents an artifact.
type Artifact struct {
Created *time.Time `json:"Created,omitempty"`
Expand Down
29 changes: 6 additions & 23 deletions pkg/artifacts/artifact_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package artifacts
import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/internal"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services"
"github.com/dghubble/sling"
"github.com/google/go-querystring/query"
Expand All @@ -23,24 +24,6 @@ func NewArtifactService(sling *sling.Sling, uriTemplate string) *ArtifactService
}
}

func (s *ArtifactService) getPagedResponse(path string) ([]*Artifact, error) {
resources := []*Artifact{}
loadNextPage := true

for loadNextPage {
resp, err := services.ApiGet(s.GetClient(), new(Artifacts), path)
if err != nil {
return resources, err
}

responseList := resp.(*Artifacts)
resources = append(resources, responseList.Items...)
path, loadNextPage = services.LoadNextPage(responseList.PagedResults)
}

return resources, nil
}

// Add creates a new artifact.
func (s *ArtifactService) Add(artifact *Artifact) (*Artifact, error) {
if IsNil(artifact) {
Expand All @@ -63,20 +46,20 @@ func (s *ArtifactService) Add(artifact *Artifact) (*Artifact, error) {
// Get returns a collection of artifacts based on the criteria defined by its
// input query parameter. If an error occurs, an empty collection is returned
// along with the associated error.
func (s *ArtifactService) Get(artifactsQuery Query) (*Artifacts, error) {
func (s *ArtifactService) Get(artifactsQuery Query) (*resources.Resources[Artifact], error) {
v, _ := query.Values(artifactsQuery)
path := s.BasePath
encodedQueryString := v.Encode()
if len(encodedQueryString) > 0 {
path += "?" + encodedQueryString
}

resp, err := services.ApiGet(s.GetClient(), new(Artifacts), path)
resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[Artifact]), path)
if err != nil {
return &Artifacts{}, err
return &resources.Resources[Artifact]{}, err
}

return resp.(*Artifacts), nil
return resp.(*resources.Resources[Artifact]), nil
}

// GetAll returns all artifacts. If none can be found or an error occurs, it
Expand All @@ -87,7 +70,7 @@ func (s *ArtifactService) GetAll() ([]*Artifact, error) {
return []*Artifact{}, err
}

return s.getPagedResponse(path)
return services.GetPagedResponse[Artifact](s, path)
}

// GetByID returns the artifact that matches the input ID. If one cannot be
Expand Down
5 changes: 0 additions & 5 deletions pkg/certificates/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ type CertificateResource struct {
resources.Resource
}

type CertificateResources struct {
Items []*CertificateResource `json:"Items"`
resources.PagedResults
}

// NewCertificateResource initializes a certificate resource with a name and
// credentials.
func NewCertificateResource(name string, certificateData *core.SensitiveValue, password *core.SensitiveValue) *CertificateResource {
Expand Down
11 changes: 6 additions & 5 deletions pkg/certificates/certificate_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/OctopusDeploy/go-octopusdeploy/v2/internal"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services"
"github.com/dghubble/sling"
)
Expand Down Expand Up @@ -61,18 +62,18 @@ func (s *CertificateService) Archive(resource *CertificateResource) (*Certificat
// Get returns a collection of certificates based on the criteria defined by its input
// query parameter. If an error occurs, an empty collection is returned along
// with the associated error.
func (s *CertificateService) Get(certificatesQuery CertificatesQuery) (*CertificateResources, error) {
func (s *CertificateService) Get(certificatesQuery CertificatesQuery) (*resources.Resources[CertificateResource], error) {
path, err := s.GetURITemplate().Expand(certificatesQuery)
if err != nil {
return &CertificateResources{}, err
return &resources.Resources[CertificateResource]{}, err
}

response, err := services.ApiGet(s.GetClient(), new(CertificateResources), path)
response, err := services.ApiGet(s.GetClient(), new(resources.Resources[CertificateResource]), path)
if err != nil {
return &CertificateResources{}, err
return &resources.Resources[CertificateResource]{}, err
}

return response.(*CertificateResources), nil
return response.(*resources.Resources[CertificateResource]), nil
}

// GetAll returns all certificates. If none are found or an error occurs, it
Expand Down
7 changes: 0 additions & 7 deletions pkg/channels/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ type Channel struct {
resources.Resource
}

// Channels defines a collection of channels with built-in support for paged
// results.
type Channels struct {
Items []*Channel `json:"Items"`
resources.PagedResults
}

func NewChannel(name string, projectID string) *Channel {
return &Channel{
Name: strings.TrimSpace(name),
Expand Down
11 changes: 6 additions & 5 deletions pkg/channels/channel_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package channels
import (
"github.com/OctopusDeploy/go-octopusdeploy/v2/internal"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services"
"github.com/dghubble/sling"
)
Expand Down Expand Up @@ -48,18 +49,18 @@ func (s *ChannelService) Add(channel *Channel) (*Channel, error) {
// Get returns a collection of channels based on the criteria defined by its
// input query parameter. If an error occurs, an empty collection is returned
// along with the associated error.
func (s *ChannelService) Get(channelsQuery Query) (*Channels, error) {
func (s *ChannelService) Get(channelsQuery Query) (*resources.Resources[Channel], error) {
path, err := s.GetURITemplate().Expand(channelsQuery)
if err != nil {
return &Channels{}, err
return &resources.Resources[Channel]{}, err
}

response, err := services.ApiGet(s.GetClient(), new(Channels), path)
response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Channel]), path)
if err != nil {
return &Channels{}, err
return &resources.Resources[Channel]{}, err
}

return response.(*Channels), nil
return response.(*resources.Resources[Channel]), nil
}

// GetAll returns all channels. If none can be found or an error occurs, it
Expand Down
2 changes: 0 additions & 2 deletions pkg/channels/is_nil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ func IsNil(i interface{}) bool {
switch v := i.(type) {
case *Channel:
return v == nil
case *Channels:
return v == nil
default:
return v == nil
}
Expand Down
5 changes: 0 additions & 5 deletions pkg/configuration/configuration_section.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ package configuration

import "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"

type ConfigurationSections struct {
Items []*ConfigurationSection `json:"Items"`
resources.PagedResults
}

type ConfigurationSection struct {
Description string `json:"Description,omitempty"`
Name string `json:"Name,omitempty"`
Expand Down
Loading

0 comments on commit 88184c9

Please sign in to comment.