From 88184c9ca0f3139824f24e700494b4aca9ae16b6 Mon Sep 17 00:00:00 2001 From: John Bristowe Date: Wed, 27 Jul 2022 17:23:00 +1000 Subject: [PATCH] feat: added generic collection and function (#124) * chore: updated test * chore: added test * feat: added generic collection and function * chore: added Go 1.18 to workflow --- .github/workflows/integration-tests.yml | 3 ++ pkg/accounts/account_resource.go | 7 ---- pkg/accounts/account_service.go | 5 ++- pkg/accounts/account_utilities.go | 3 +- pkg/accounts/accounts.go | 2 +- pkg/actions/community_action_template.go | 7 ---- .../community_action_template_service.go | 31 ++++---------- pkg/actiontemplates/action_template.go | 7 ---- .../action_template_service.go | 9 ++-- pkg/artifacts/artifact.go | 7 ---- pkg/artifacts/artifact_service.go | 29 +++---------- pkg/certificates/certificate.go | 5 --- pkg/certificates/certificate_service.go | 11 ++--- pkg/channels/channel.go | 7 ---- pkg/channels/channel_service.go | 11 ++--- pkg/channels/is_nil.go | 2 - pkg/configuration/configuration_section.go | 5 --- pkg/deployments/deployment.go | 7 ---- pkg/deployments/deployment_process.go | 5 --- pkg/deployments/deployment_process_service.go | 20 +-------- pkg/deployments/deployment_service.go | 37 +++++------------ pkg/environments/environment.go | 13 +++--- pkg/environments/environment_service.go | 35 ++++------------ pkg/events/event.go | 5 --- pkg/events/event_service.go | 11 ++--- pkg/feeds/feed_resource.go | 5 --- pkg/feeds/feed_service.go | 29 ++++++------- pkg/feeds/feed_utilities.go | 3 +- pkg/interruptions/interruption.go | 5 --- pkg/interruptions/interruption_service.go | 20 +-------- pkg/lifecycles/lifecycle.go | 5 --- pkg/lifecycles/lifecycle_service.go | 31 ++++---------- pkg/machines/deployment_target.go | 8 ---- pkg/machines/endpoint_resource.go | 5 --- pkg/machines/machine_policy.go | 5 --- pkg/machines/machine_policy_service.go | 31 ++++---------- pkg/machines/machine_service.go | 33 ++++----------- pkg/machines/worker.go | 7 ---- pkg/machines/worker_service.go | 24 ++--------- pkg/packages/package.go | 5 --- pkg/packages/package_description.go | 5 --- pkg/packages/package_version.go | 5 --- pkg/projectgroups/project_group.go | 5 --- pkg/projectgroups/project_group_service.go | 35 ++++------------ pkg/projects/project.go | 9 +--- pkg/projects/project_service.go | 17 ++++---- pkg/releases/release.go | 6 --- pkg/releases/release_service.go | 23 ++++++----- pkg/resources/resource.go | 4 +- pkg/runbooks/runbook.go | 7 ---- pkg/runbooks/runbook_process.go | 5 --- pkg/runbooks/runbook_snapshot.go | 7 ---- pkg/services/service.go | 18 ++++++++ pkg/spaces/space.go | 5 --- pkg/spaces/space_service.go | 11 ++--- pkg/tagsets/tag_set.go | 5 --- pkg/tagsets/tag_set_service.go | 11 ++--- pkg/tasks/task.go | 5 --- pkg/tasks/task_service.go | 11 ++--- pkg/teams/team.go | 6 --- pkg/teams/team_service.go | 37 +++++------------ pkg/tenants/tenant.go | 5 --- pkg/tenants/tenant_service.go | 35 ++++------------ pkg/triggers/project_trigger.go | 5 --- pkg/triggers/project_trigger_service.go | 20 +-------- pkg/userroles/scoped_user_role.go | 5 --- pkg/userroles/scoped_user_role_service.go | 9 ++-- pkg/userroles/user_role.go | 7 ---- pkg/userroles/user_role_service.go | 11 ++--- pkg/users/api_key.go | 7 ---- pkg/users/api_key_service.go | 5 ++- pkg/users/user.go | 6 --- pkg/users/user_service.go | 17 ++++---- pkg/variables/library_variable_set.go | 5 --- pkg/variables/library_variable_set_service.go | 31 ++++---------- pkg/variables/script_module.go | 5 --- pkg/variables/script_module_service.go | 41 ++++++------------- pkg/workerpools/dynamic_worker_pool.go | 6 --- pkg/workerpools/static_worker_pool.go | 6 --- pkg/workerpools/worker_pool_resource.go | 5 --- pkg/workerpools/worker_pool_service.go | 5 ++- pkg/workerpools/worker_pool_utilities.go | 3 +- test/e2e/environment_service_test.go | 7 ++++ test/e2e/feed_service_test.go | 23 +++++++++++ 84 files changed, 283 insertions(+), 723 deletions(-) diff --git a/.github/workflows/integration-tests.yml b/.github/workflows/integration-tests.yml index c9edbc3b..37193b49 100644 --- a/.github/workflows/integration-tests.yml +++ b/.github/workflows/integration-tests.yml @@ -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 diff --git a/pkg/accounts/account_resource.go b/pkg/accounts/account_resource.go index 502d29e0..1f662a51 100644 --- a/pkg/accounts/account_resource.go +++ b/pkg/accounts/account_resource.go @@ -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 { diff --git a/pkg/accounts/account_service.go b/pkg/accounts/account_service.go index f60fa2cb..24cd3814 100644 --- a/pkg/accounts/account_service.go +++ b/pkg/accounts/account_service.go @@ -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" ) @@ -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 diff --git a/pkg/accounts/account_utilities.go b/pkg/accounts/account_utilities.go index 208b6fd4..a7596888 100644 --- a/pkg/accounts/account_utilities.go +++ b/pkg/accounts/account_utilities.go @@ -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) { @@ -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, diff --git a/pkg/accounts/accounts.go b/pkg/accounts/accounts.go index 82fb74fd..3395b74b 100644 --- a/pkg/accounts/accounts.go +++ b/pkg/accounts/accounts.go @@ -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 diff --git a/pkg/actions/community_action_template.go b/pkg/actions/community_action_template.go index f34c30f9..48e77dcc 100644 --- a/pkg/actions/community_action_template.go +++ b/pkg/actions/community_action_template.go @@ -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 { diff --git a/pkg/actions/community_action_template_service.go b/pkg/actions/community_action_template_service.go index 5df6344d..5f10ffda 100644 --- a/pkg/actions/community_action_template_service.go +++ b/pkg/actions/community_action_template_service.go @@ -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" @@ -38,28 +39,10 @@ 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() @@ -67,12 +50,12 @@ func (s *CommunityActionTemplateService) Get(communityActionTemplatesQuery Commu 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 @@ -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 @@ -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 diff --git a/pkg/actiontemplates/action_template.go b/pkg/actiontemplates/action_template.go index 46b75248..52d11e26 100644 --- a/pkg/actiontemplates/action_template.go +++ b/pkg/actiontemplates/action_template.go @@ -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"` diff --git a/pkg/actiontemplates/action_template_service.go b/pkg/actiontemplates/action_template_service.go index 8766f9fa..4744d62a 100644 --- a/pkg/actiontemplates/action_template_service.go +++ b/pkg/actiontemplates/action_template_service.go @@ -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" @@ -62,7 +63,7 @@ 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() @@ -70,12 +71,12 @@ func (s *ActionTemplateService) Get(actionTemplatesQuery Query) (*ActionTemplate 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 diff --git a/pkg/artifacts/artifact.go b/pkg/artifacts/artifact.go index f8cb1d3e..d1110eb9 100644 --- a/pkg/artifacts/artifact.go +++ b/pkg/artifacts/artifact.go @@ -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"` diff --git a/pkg/artifacts/artifact_service.go b/pkg/artifacts/artifact_service.go index 5e934f9a..8d2c6f0f 100644 --- a/pkg/artifacts/artifact_service.go +++ b/pkg/artifacts/artifact_service.go @@ -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" @@ -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) { @@ -63,7 +46,7 @@ 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() @@ -71,12 +54,12 @@ func (s *ArtifactService) Get(artifactsQuery Query) (*Artifacts, error) { 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 @@ -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 diff --git a/pkg/certificates/certificate.go b/pkg/certificates/certificate.go index 314bfaeb..2986b22b 100644 --- a/pkg/certificates/certificate.go +++ b/pkg/certificates/certificate.go @@ -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 { diff --git a/pkg/certificates/certificate_service.go b/pkg/certificates/certificate_service.go index 5f447036..82a8d59f 100644 --- a/pkg/certificates/certificate_service.go +++ b/pkg/certificates/certificate_service.go @@ -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" ) @@ -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 diff --git a/pkg/channels/channel.go b/pkg/channels/channel.go index 65143596..4375188e 100644 --- a/pkg/channels/channel.go +++ b/pkg/channels/channel.go @@ -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), diff --git a/pkg/channels/channel_service.go b/pkg/channels/channel_service.go index 22545cbe..99da7e51 100644 --- a/pkg/channels/channel_service.go +++ b/pkg/channels/channel_service.go @@ -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" ) @@ -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 diff --git a/pkg/channels/is_nil.go b/pkg/channels/is_nil.go index 7ffcd1f7..266b678f 100644 --- a/pkg/channels/is_nil.go +++ b/pkg/channels/is_nil.go @@ -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 } diff --git a/pkg/configuration/configuration_section.go b/pkg/configuration/configuration_section.go index e19cabcc..efc947e3 100644 --- a/pkg/configuration/configuration_section.go +++ b/pkg/configuration/configuration_section.go @@ -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"` diff --git a/pkg/deployments/deployment.go b/pkg/deployments/deployment.go index a11e5117..56f74700 100644 --- a/pkg/deployments/deployment.go +++ b/pkg/deployments/deployment.go @@ -42,13 +42,6 @@ type Deployment struct { resources.Resource } -// Deployments defines a collection of deployment instances with built-in -// support for paged results. -type Deployments struct { - Items []*Deployment `json:"Items"` - resources.PagedResults -} - // NewDeployment initializes a deployment with a name, environment ID, and // release ID. func NewDeployment(environmentID string, releaseID string) *Deployment { diff --git a/pkg/deployments/deployment_process.go b/pkg/deployments/deployment_process.go index e7b6d44b..5cd45fbc 100644 --- a/pkg/deployments/deployment_process.go +++ b/pkg/deployments/deployment_process.go @@ -2,11 +2,6 @@ package deployments import "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" -type DeploymentProcesses struct { - Items []*DeploymentProcess `json:"Items"` - resources.PagedResults -} - type DeploymentProcess struct { Branch string `json:"-"` LastSnapshotID string `json:"LastSnapshotId,omitempty"` diff --git a/pkg/deployments/deployment_process_service.go b/pkg/deployments/deployment_process_service.go index 15ae4836..26af5da0 100644 --- a/pkg/deployments/deployment_process_service.go +++ b/pkg/deployments/deployment_process_service.go @@ -85,7 +85,7 @@ func (s *DeploymentProcessService) GetAll() ([]*DeploymentProcess, error) { return []*DeploymentProcess{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[DeploymentProcess](s, path) } // GetByID returns the deployment process that matches the input ID. If one @@ -121,21 +121,3 @@ func (s *DeploymentProcessService) Update(deploymentProcess *DeploymentProcess) return resp.(*DeploymentProcess), nil } - -func (s *DeploymentProcessService) getPagedResponse(path string) ([]*DeploymentProcess, error) { - resources := []*DeploymentProcess{} - loadNextPage := true - - for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(DeploymentProcesses), path) - if err != nil { - return resources, err - } - - responseList := resp.(*DeploymentProcesses) - resources = append(resources, responseList.Items...) - path, loadNextPage = services.LoadNextPage(responseList.PagedResults) - } - - return resources, nil -} diff --git a/pkg/deployments/deployment_service.go b/pkg/deployments/deployment_service.go index fe996910..77f1dc39 100644 --- a/pkg/deployments/deployment_service.go +++ b/pkg/deployments/deployment_service.go @@ -4,6 +4,7 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/releases" + "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" @@ -25,24 +26,6 @@ func NewDeploymentService(sling *sling.Sling, uriTemplate string) *DeploymentSer } } -func (s *DeploymentService) getPagedResponse(path string) ([]*Deployment, error) { - resources := []*Deployment{} - loadNextPage := true - - for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(Deployments), path) - if err != nil { - return resources, err - } - - responseList := resp.(*Deployments) - resources = append(resources, responseList.Items...) - path, loadNextPage = services.LoadNextPage(responseList.PagedResults) - } - - return resources, nil -} - // Add creates a new deployment. func (s *DeploymentService) Add(deployment *Deployment) (*Deployment, error) { if IsNil(deployment) { @@ -93,7 +76,7 @@ func (s *DeploymentService) GetByIDs(ids []string) ([]*Deployment, error) { return []*Deployment{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[Deployment](s, path) } // GetByName performs a lookup and returns instances of a Deployment with a matching partial name. @@ -107,7 +90,7 @@ func (s *DeploymentService) GetByName(name string) ([]*Deployment, error) { return []*Deployment{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[Deployment](s, path) } // Update modifies a Deployment based on the one provided as input. @@ -125,35 +108,35 @@ func (s *DeploymentService) Update(resource Deployment) (*Deployment, error) { return resp.(*Deployment), nil } -func (s *DeploymentService) GetDeployments(release *releases.Release, deploymentQuery ...*DeploymentQuery) (*Deployments, error) { +func (s *DeploymentService) GetDeployments(release *releases.Release, deploymentQuery ...*DeploymentQuery) (*resources.Resources[Deployment], error) { if release == nil { return nil, internal.CreateInvalidParameterError("GetDeployments", "release") } uriTemplate, err := uritemplates.Parse(release.GetLinks()[constants.LinkDeployments]) if err != nil { - return &Deployments{}, err + return &resources.Resources[Deployment]{}, err } values := make(map[string]interface{}) path, err := uriTemplate.Expand(values) if err != nil { - return &Deployments{}, err + return &resources.Resources[Deployment]{}, err } if deploymentQuery != nil { path, err = uriTemplate.Expand(deploymentQuery[0]) if err != nil { - return &Deployments{}, err + return &resources.Resources[Deployment]{}, err } } - resp, err := services.ApiGet(s.GetClient(), new(Deployments), path) + resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[Deployment]), path) if err != nil { - return &Deployments{}, err + return &resources.Resources[Deployment]{}, err } - return resp.(*Deployments), nil + return resp.(*resources.Resources[Deployment]), nil } func (s *DeploymentService) GetProgression(release *releases.Release) (*releases.Progression, error) { diff --git a/pkg/environments/environment.go b/pkg/environments/environment.go index 5274d36d..086f13e8 100644 --- a/pkg/environments/environment.go +++ b/pkg/environments/environment.go @@ -5,11 +5,6 @@ import ( "github.com/go-playground/validator/v10" ) -type Environments struct { - Items []*Environment `json:"Items"` - resources.PagedResults -} - type Environment struct { AllowDynamicInfrastructure bool `json:"AllowDynamicInfrastructure"` Description string `json:"Description,omitempty"` @@ -36,6 +31,14 @@ func (e *Environment) Validate() error { return validator.New().Struct(e) } +// GetName returns the name of the environment. func (e *Environment) GetName() string { return e.Name } + +// SetName sets the name of the environment. +func (e *Environment) SetName(name string) { + e.Name = name +} + +var _ resources.IHasName = &Environment{} diff --git a/pkg/environments/environment_service.go b/pkg/environments/environment_service.go index f14a6660..7857a8a9 100644 --- a/pkg/environments/environment_service.go +++ b/pkg/environments/environment_service.go @@ -3,6 +3,7 @@ package environments 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" ) @@ -26,24 +27,6 @@ func NewEnvironmentService(sling *sling.Sling, uriTemplate string, sortOrderPath } } -func (s *EnvironmentService) getPagedResponse(path string) ([]*Environment, error) { - resources := []*Environment{} - loadNextPage := true - - for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(Environments), path) - if err != nil { - return resources, err - } - - responseList := resp.(*Environments) - resources = append(resources, responseList.Items...) - path, loadNextPage = services.LoadNextPage(responseList.PagedResults) - } - - return resources, nil -} - // Add creates a new environment. func (s *EnvironmentService) Add(environment *Environment) (*Environment, error) { if IsNil(environment) { @@ -66,18 +49,18 @@ func (s *EnvironmentService) Add(environment *Environment) (*Environment, error) // Get returns a collection of environments 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 *EnvironmentService) Get(environmentsQuery EnvironmentsQuery) (*Environments, error) { +func (s *EnvironmentService) Get(environmentsQuery EnvironmentsQuery) (*resources.Resources[Environment], error) { path, err := s.GetURITemplate().Expand(environmentsQuery) if err != nil { - return &Environments{}, err + return &resources.Resources[Environment]{}, err } - response, err := services.ApiGet(s.GetClient(), new(Environments), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Environment]), path) if err != nil { - return &Environments{}, err + return &resources.Resources[Environment]{}, err } - return response.(*Environments), nil + return response.(*resources.Resources[Environment]), nil } // GetAll returns all environments. If none can be found or an error occurs, it @@ -124,7 +107,7 @@ func (s *EnvironmentService) GetByIDs(ids []string) ([]*Environment, error) { return []*Environment{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[Environment](s, path) } // GetByName returns the environments with a matching partial name. @@ -138,7 +121,7 @@ func (s *EnvironmentService) GetByName(name string) ([]*Environment, error) { return []*Environment{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[Environment](s, path) } // GetByPartialName performs a lookup and returns enironments with a matching @@ -153,7 +136,7 @@ func (s *EnvironmentService) GetByPartialName(partialName string) ([]*Environmen return []*Environment{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[Environment](s, path) } // Update modifies an environment based on the one provided as input. diff --git a/pkg/events/event.go b/pkg/events/event.go index c9f354d5..ce32847d 100644 --- a/pkg/events/event.go +++ b/pkg/events/event.go @@ -25,8 +25,3 @@ type Event struct { resources.Resource } - -type Events struct { - Items []*Event `json:"Items"` - resources.PagedResults -} diff --git a/pkg/events/event_service.go b/pkg/events/event_service.go index c844a6ef..ee041371 100644 --- a/pkg/events/event_service.go +++ b/pkg/events/event_service.go @@ -4,6 +4,7 @@ import ( "strings" "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" @@ -38,18 +39,18 @@ func NewEventService(sling *sling.Sling, uriTemplate string, agentsPath string, // Get returns a collection of events 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 EventService) Get(query EventsQuery) (*Events, error) { +func (s EventService) Get(query EventsQuery) (*resources.Resources[Event], error) { path, err := s.GetURITemplate().Expand(query) if err != nil { - return &Events{}, err + return &resources.Resources[Event]{}, err } - response, err := services.ApiGet(s.GetClient(), new(Events), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Event]), path) if err != nil { - return &Events{}, err + return &resources.Resources[Event]{}, err } - return response.(*Events), nil + return response.(*resources.Resources[Event]), nil } func (s EventService) GetAgents() (*[]EventAgent, error) { diff --git a/pkg/feeds/feed_resource.go b/pkg/feeds/feed_resource.go index 99d2d4e1..207d5720 100644 --- a/pkg/feeds/feed_resource.go +++ b/pkg/feeds/feed_resource.go @@ -29,11 +29,6 @@ type FeedResource struct { resources.Resource } -type FeedResources struct { - Items []*FeedResource `json:"Items"` - resources.PagedResults -} - func NewFeedResource(name string, feedType FeedType) *FeedResource { return &FeedResource{ FeedType: feedType, diff --git a/pkg/feeds/feed_service.go b/pkg/feeds/feed_service.go index 34c137dd..f5aa8e74 100644 --- a/pkg/feeds/feed_service.go +++ b/pkg/feeds/feed_service.go @@ -4,6 +4,7 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/packages" + "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" @@ -58,12 +59,12 @@ func (s *FeedService) Get(feedsQuery FeedsQuery) (*Feeds, error) { path += "?" + encodedQueryString } - response, err := services.ApiGet(s.GetClient(), new(FeedResources), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[FeedResource]), path) if err != nil { return &Feeds{}, err } - return ToFeeds(response.(*FeedResources)), nil + return ToFeeds(response.(*resources.Resources[FeedResource])), nil } // GetAll returns all feeds. If none can be found or an error occurs, it @@ -106,50 +107,50 @@ func (s *FeedService) GetBuiltInFeedStatistics() (*BuiltInFeedStatistics, error) return resp.(*BuiltInFeedStatistics), nil } -func (s *FeedService) SearchPackageVersions(packageDescription *packages.PackageDescription, searchPackageVersionsQuery SearchPackageVersionsQuery) (*packages.PackageVersions, error) { +func (s *FeedService) SearchPackageVersions(packageDescription *packages.PackageDescription, searchPackageVersionsQuery SearchPackageVersionsQuery) (*resources.Resources[packages.PackageVersion], error) { if packageDescription == nil { return nil, internal.CreateInvalidParameterError("SearchPackageVersions", "packageDescription") } uriTemplate, err := uritemplates.Parse(packageDescription.GetLinks()[constants.LinkSearchPackageVersionsTemplate]) if err != nil { - return &packages.PackageVersions{}, err + return &resources.Resources[packages.PackageVersion]{}, err } path, err := uriTemplate.Expand(searchPackageVersionsQuery) if err != nil { - return &packages.PackageVersions{}, err + return &resources.Resources[packages.PackageVersion]{}, err } - resp, err := services.ApiGet(s.GetClient(), new(packages.PackageVersions), path) + resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[packages.PackageVersion]), path) if err != nil { - return &packages.PackageVersions{}, err + return &resources.Resources[packages.PackageVersion]{}, err } - return resp.(*packages.PackageVersions), nil + return resp.(*resources.Resources[packages.PackageVersion]), nil } -func (s *FeedService) SearchPackages(feed IFeed, searchPackagesQuery SearchPackagesQuery) (*packages.PackageDescriptions, error) { +func (s *FeedService) SearchPackages(feed IFeed, searchPackagesQuery SearchPackagesQuery) (*resources.Resources[packages.PackageDescription], error) { if feed == nil { return nil, internal.CreateInvalidParameterError("SearchPackages", "feed") } uriTemplate, err := uritemplates.Parse(feed.GetLinks()[constants.LinkSearchPackagesTemplate]) if err != nil { - return &packages.PackageDescriptions{}, err + return &resources.Resources[packages.PackageDescription]{}, err } path, err := uriTemplate.Expand(searchPackagesQuery) if err != nil { - return &packages.PackageDescriptions{}, err + return &resources.Resources[packages.PackageDescription]{}, err } - resp, err := services.ApiGet(s.GetClient(), new(packages.PackageDescriptions), path) + resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[packages.PackageDescription]), path) if err != nil { - return &packages.PackageDescriptions{}, err + return &resources.Resources[packages.PackageDescription]{}, err } - return resp.(*packages.PackageDescriptions), nil + return resp.(*resources.Resources[packages.PackageDescription]), nil } // Update modifies a feed based on the one provided as input. diff --git a/pkg/feeds/feed_utilities.go b/pkg/feeds/feed_utilities.go index 24f184a0..9d9cfc7a 100644 --- a/pkg/feeds/feed_utilities.go +++ b/pkg/feeds/feed_utilities.go @@ -2,6 +2,7 @@ package feeds import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" ) func ToFeed(feedResource *FeedResource) (IFeed, error) { @@ -96,7 +97,7 @@ func ToFeed(feedResource *FeedResource) (IFeed, error) { return feed, nil } -func ToFeeds(feedResources *FeedResources) *Feeds { +func ToFeeds(feedResources *resources.Resources[FeedResource]) *Feeds { return &Feeds{ Items: ToFeedArray(feedResources.Items), PagedResults: feedResources.PagedResults, diff --git a/pkg/interruptions/interruption.go b/pkg/interruptions/interruption.go index d9e59b49..447c46d2 100644 --- a/pkg/interruptions/interruption.go +++ b/pkg/interruptions/interruption.go @@ -6,11 +6,6 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" ) -type Interruptions struct { - Items []*Interruption `json:"Items"` - resources.PagedResults -} - type Interruption struct { CanTakeResponsibility bool `json:"CanTakeResponsibility,omitempty"` CorrelationID string `json:"CorrelationId,omitempty"` diff --git a/pkg/interruptions/interruption_service.go b/pkg/interruptions/interruption_service.go index 13a58575..61174f53 100644 --- a/pkg/interruptions/interruption_service.go +++ b/pkg/interruptions/interruption_service.go @@ -18,24 +18,6 @@ func NewInterruptionService(sling *sling.Sling, uriTemplate string) *Interruptio } } -func (s InterruptionService) getPagedResponse(path string) ([]*Interruption, error) { - resources := []*Interruption{} - loadNextPage := true - - for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(Interruptions), path) - if err != nil { - return resources, err - } - - responseList := resp.(*Interruptions) - resources = append(resources, responseList.Items...) - path, loadNextPage = services.LoadNextPage(responseList.PagedResults) - } - - return resources, nil -} - // GetByID returns the interruption that matches the input ID. If one cannot be // found, it returns nil and an error. func (s *InterruptionService) GetByID(id string) (*Interruption, error) { @@ -67,7 +49,7 @@ func (s *InterruptionService) GetByIDs(ids []string) ([]*Interruption, error) { return []*Interruption{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[Interruption](s, path) } // GetAll returns all interruptions. If none can be found or an error occurs, diff --git a/pkg/lifecycles/lifecycle.go b/pkg/lifecycles/lifecycle.go index 68a9502e..c122229e 100644 --- a/pkg/lifecycles/lifecycle.go +++ b/pkg/lifecycles/lifecycle.go @@ -8,11 +8,6 @@ import ( "github.com/go-playground/validator/v10" ) -type Lifecycles struct { - Items []*Lifecycle `json:"Items"` - resources.PagedResults -} - type Lifecycle struct { Description string `json:"Description,omitempty"` Name string `json:"Name" validate:"required"` diff --git a/pkg/lifecycles/lifecycle_service.go b/pkg/lifecycles/lifecycle_service.go index 1d63cc23..99834642 100644 --- a/pkg/lifecycles/lifecycle_service.go +++ b/pkg/lifecycles/lifecycle_service.go @@ -4,6 +4,7 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services" "github.com/dghubble/sling" ) @@ -20,24 +21,6 @@ func NewLifecycleService(sling *sling.Sling, uriTemplate string) *LifecycleServi } } -func (s *LifecycleService) getPagedResponse(path string) ([]*Lifecycle, error) { - resources := []*Lifecycle{} - loadNextPage := true - - for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(Lifecycles), path) - if err != nil { - return resources, err - } - - responseList := resp.(*Lifecycles) - resources = append(resources, responseList.Items...) - path, loadNextPage = services.LoadNextPage(responseList.PagedResults) - } - - return resources, nil -} - // Add creates a new lifecycle. func (s *LifecycleService) Add(lifecycle *Lifecycle) (*Lifecycle, error) { if IsNil(lifecycle) { @@ -60,18 +43,18 @@ func (s *LifecycleService) Add(lifecycle *Lifecycle) (*Lifecycle, error) { // Get returns a collection of lifecycles 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 *LifecycleService) Get(lifecyclesQuery Query) (*Lifecycles, error) { +func (s *LifecycleService) Get(lifecyclesQuery Query) (*resources.Resources[Lifecycle], error) { path, err := s.GetURITemplate().Expand(lifecyclesQuery) if err != nil { - return &Lifecycles{}, err + return &resources.Resources[Lifecycle]{}, err } - response, err := services.ApiGet(s.GetClient(), new(Lifecycles), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Lifecycle]), path) if err != nil { - return &Lifecycles{}, err + return &resources.Resources[Lifecycle]{}, err } - return response.(*Lifecycles), nil + return response.(*resources.Resources[Lifecycle]), nil } // GetAll returns all lifecycles. If none can be found or an error occurs, it @@ -119,7 +102,7 @@ func (s *LifecycleService) GetByPartialName(partialName string) ([]*Lifecycle, e return []*Lifecycle{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[Lifecycle](s, path) } func (s *LifecycleService) GetProjects(lifecycle *Lifecycle) ([]*projects.Project, error) { diff --git a/pkg/machines/deployment_target.go b/pkg/machines/deployment_target.go index 1572670b..f9da8800 100644 --- a/pkg/machines/deployment_target.go +++ b/pkg/machines/deployment_target.go @@ -4,19 +4,11 @@ import ( "encoding/json" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core" - "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" "github.com/go-playground/validator/v10" ) // TODO: research the JSON marshalling to include SpaceID -// DeploymentTargets defines a collection of deployment targets with built-in -// support for paged results from the API. -type DeploymentTargets struct { - Items []*DeploymentTarget `json:"Items"` - resources.PagedResults -} - type DeploymentTarget struct { EnvironmentIDs []string `json:"EnvironmentIds,omitempty"` Roles []string `json:"Roles,omitempty"` diff --git a/pkg/machines/endpoint_resource.go b/pkg/machines/endpoint_resource.go index 1dcc5765..629ce12c 100644 --- a/pkg/machines/endpoint_resource.go +++ b/pkg/machines/endpoint_resource.go @@ -56,11 +56,6 @@ type EndpointResource struct { resources.Resource } -type EndpointResources struct { - Items []*EndpointResource `json:"Items"` - resources.PagedResults -} - // NewEndpoint creates and initializes an account resource with a name and type. func NewEndpointResource(communicationStyle string) *EndpointResource { return &EndpointResource{ diff --git a/pkg/machines/machine_policy.go b/pkg/machines/machine_policy.go index bb2aeb66..e47b7566 100644 --- a/pkg/machines/machine_policy.go +++ b/pkg/machines/machine_policy.go @@ -9,11 +9,6 @@ import ( "github.com/go-playground/validator/v10/non-standard/validators" ) -type MachinePolicies struct { - Items []*MachinePolicy `json:"Items"` - resources.PagedResults -} - type MachinePolicy struct { ConnectionConnectTimeout time.Duration `json:"ConnectionConnectTimeout" validate:"required,min=10s"` ConnectionRetryCountLimit int32 `json:"ConnectionRetryCountLimit" validate:"required,gte=2"` diff --git a/pkg/machines/machine_policy_service.go b/pkg/machines/machine_policy_service.go index 4a99eb27..27790f6d 100644 --- a/pkg/machines/machine_policy_service.go +++ b/pkg/machines/machine_policy_service.go @@ -3,6 +3,7 @@ package machines 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" ) @@ -22,24 +23,6 @@ func NewMachinePolicyService(sling *sling.Sling, uriTemplate string, templatePat } } -func (s *MachinePolicyService) getPagedResponse(path string) ([]*MachinePolicy, error) { - resources := []*MachinePolicy{} - loadNextPage := true - - for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(MachinePolicies), path) - if err != nil { - return resources, err - } - - responseList := resp.(*MachinePolicies) - resources = append(resources, responseList.Items...) - path, loadNextPage = services.LoadNextPage(responseList.PagedResults) - } - - return resources, nil -} - // Add creates a new machine policy. func (s *MachinePolicyService) Add(machinePolicy *MachinePolicy) (*MachinePolicy, error) { if IsNil(machinePolicy) { @@ -62,18 +45,18 @@ func (s *MachinePolicyService) Add(machinePolicy *MachinePolicy) (*MachinePolicy // Get returns a collection of machine policies 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 *MachinePolicyService) Get(machinePoliciesQuery MachinePoliciesQuery) (*MachinePolicies, error) { +func (s *MachinePolicyService) Get(machinePoliciesQuery MachinePoliciesQuery) (*resources.Resources[MachinePolicy], error) { path, err := s.GetURITemplate().Expand(machinePoliciesQuery) if err != nil { - return &MachinePolicies{}, err + return &resources.Resources[MachinePolicy]{}, err } - response, err := services.ApiGet(s.GetClient(), new(MachinePolicies), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[MachinePolicy]), path) if err != nil { - return &MachinePolicies{}, err + return &resources.Resources[MachinePolicy]{}, err } - return response.(*MachinePolicies), nil + return response.(*resources.Resources[MachinePolicy]), nil } // GetAll returns all machine policies. If none can be found or an error @@ -121,7 +104,7 @@ func (s *MachinePolicyService) GetByPartialName(partialName string) ([]*MachineP return []*MachinePolicy{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[MachinePolicy](s, path) } func (s *MachinePolicyService) GetTemplate() (*MachinePolicy, error) { diff --git a/pkg/machines/machine_service.go b/pkg/machines/machine_service.go index 0777a0fb..67931105 100644 --- a/pkg/machines/machine_service.go +++ b/pkg/machines/machine_service.go @@ -3,6 +3,7 @@ package machines 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" ) @@ -26,24 +27,6 @@ func NewMachineService(sling *sling.Sling, uriTemplate string, discoverMachinePa } } -func (s *MachineService) getPagedResponse(path string) ([]*DeploymentTarget, error) { - resources := []*DeploymentTarget{} - loadNextPage := true - - for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(DeploymentTargets), path) - if err != nil { - return resources, err - } - - responseList := resp.(*DeploymentTargets) - resources = append(resources, responseList.Items...) - path, loadNextPage = services.LoadNextPage(responseList.PagedResults) - } - - return resources, nil -} - // Add creates a new machine. func (s *MachineService) Add(deploymentTarget *DeploymentTarget) (*DeploymentTarget, error) { if IsNil(deploymentTarget) { @@ -66,18 +49,18 @@ func (s *MachineService) Add(deploymentTarget *DeploymentTarget) (*DeploymentTar // Get returns a collection of machines 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 *MachineService) Get(machinesQuery MachinesQuery) (*DeploymentTargets, error) { +func (s *MachineService) Get(machinesQuery MachinesQuery) (*resources.Resources[DeploymentTarget], error) { path, err := s.GetURITemplate().Expand(machinesQuery) if err != nil { - return &DeploymentTargets{}, err + return &resources.Resources[DeploymentTarget]{}, err } - response, err := services.ApiGet(s.GetClient(), new(DeploymentTargets), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[DeploymentTarget]), path) if err != nil { - return &DeploymentTargets{}, err + return &resources.Resources[DeploymentTarget]{}, err } - return response.(*DeploymentTargets), nil + return response.(*resources.Resources[DeploymentTarget]), nil } // GetByID returns the machine that matches the input ID. If one cannot be @@ -124,7 +107,7 @@ func (s *MachineService) GetByName(name string) ([]*DeploymentTarget, error) { return []*DeploymentTarget{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[DeploymentTarget](s, path) } // GetByPartialName performs a lookup and returns the machine with a matching @@ -139,7 +122,7 @@ func (s *MachineService) GetByPartialName(partialName string) ([]*DeploymentTarg return []*DeploymentTarget{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[DeploymentTarget](s, path) } // Update updates an existing machine in Octopus Deploy diff --git a/pkg/machines/worker.go b/pkg/machines/worker.go index 5e88bedf..69273d2a 100644 --- a/pkg/machines/worker.go +++ b/pkg/machines/worker.go @@ -14,13 +14,6 @@ type Worker struct { machine } -// Workers defines a collection of workers with built-in support for paged -// results. -type Workers struct { - Items []*Worker `json:"Items"` - resources.PagedResults -} - func NewWorker(name string, endpoint IEndpoint) *Worker { worker := &Worker{ WorkerPoolIDs: []string{}, diff --git a/pkg/machines/worker_service.go b/pkg/machines/worker_service.go index 6cb16c7c..592ab143 100644 --- a/pkg/machines/worker_service.go +++ b/pkg/machines/worker_service.go @@ -26,24 +26,6 @@ func NewWorkerService(sling *sling.Sling, uriTemplate string, discoverWorkerPath } } -func (s *WorkerService) getPagedResponse(path string) ([]*Worker, error) { - resources := []*Worker{} - loadNextPage := true - - for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(Workers), path) - if err != nil { - return resources, err - } - - responseList := resp.(*Workers) - resources = append(resources, responseList.Items...) - path, loadNextPage = services.LoadNextPage(responseList.PagedResults) - } - - return resources, nil -} - // Add creates a new worker. func (s *WorkerService) Add(worker *Worker) (*Worker, error) { if IsNil(worker) { @@ -119,7 +101,7 @@ func (s *WorkerService) GetByIDs(ids []string) ([]*Worker, error) { return []*Worker{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[Worker](s, path) } // GetByName returns the workers with a matching partial name. @@ -133,7 +115,7 @@ func (s *WorkerService) GetByName(name string) ([]*Worker, error) { return []*Worker{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[Worker](s, path) } // GetByPartialName performs a lookup and returns enironments with a matching @@ -148,7 +130,7 @@ func (s *WorkerService) GetByPartialName(partialName string) ([]*Worker, error) return []*Worker{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[Worker](s, path) } // Update modifies an worker based on the one provided as input. diff --git a/pkg/packages/package.go b/pkg/packages/package.go index 83a4b89f..b4443ca9 100644 --- a/pkg/packages/package.go +++ b/pkg/packages/package.go @@ -23,11 +23,6 @@ type Package struct { resources.Resource } -type Packages struct { - Items []*Package `json:"Items"` - resources.PagedResults -} - func NewPackage() *Package { return &Package{ Resource: *resources.NewResource(), diff --git a/pkg/packages/package_description.go b/pkg/packages/package_description.go index 68ba59dc..4530ca64 100644 --- a/pkg/packages/package_description.go +++ b/pkg/packages/package_description.go @@ -9,8 +9,3 @@ type PackageDescription struct { resources.Resource } - -type PackageDescriptions struct { - Items []*PackageDescription `json:"Items"` - resources.PagedResults -} diff --git a/pkg/packages/package_version.go b/pkg/packages/package_version.go index 735be28e..6405c136 100644 --- a/pkg/packages/package_version.go +++ b/pkg/packages/package_version.go @@ -18,11 +18,6 @@ type PackageVersion struct { resources.Resource } -type PackageVersions struct { - Items []*PackageVersion `json:"Items"` - resources.PagedResults -} - func NewPackageVersion() *PackageVersion { return &PackageVersion{ Resource: *resources.NewResource(), diff --git a/pkg/projectgroups/project_group.go b/pkg/projectgroups/project_group.go index e593cb12..843410aa 100644 --- a/pkg/projectgroups/project_group.go +++ b/pkg/projectgroups/project_group.go @@ -2,11 +2,6 @@ package projectgroups import "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" -type ProjectGroups struct { - Items []*ProjectGroup `json:"Items"` - resources.PagedResults -} - type ProjectGroup struct { Description string `json:"Description,omitempty"` EnvironmentIDs []string `json:"EnvironmentIds,omitempty"` diff --git a/pkg/projectgroups/project_group_service.go b/pkg/projectgroups/project_group_service.go index 41d352b3..d145437f 100644 --- a/pkg/projectgroups/project_group_service.go +++ b/pkg/projectgroups/project_group_service.go @@ -4,6 +4,7 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services" "github.com/dghubble/sling" ) @@ -22,24 +23,6 @@ func NewProjectGroupService(sling *sling.Sling, uriTemplate string) *ProjectGrou } } -func (s *ProjectGroupService) getPagedResponse(path string) ([]*ProjectGroup, error) { - resources := []*ProjectGroup{} - loadNextPage := true - - for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(ProjectGroups), path) - if err != nil { - return resources, err - } - - responseList := resp.(*ProjectGroups) - resources = append(resources, responseList.Items...) - path, loadNextPage = services.LoadNextPage(responseList.PagedResults) - } - - return resources, nil -} - // Add creates a new project group. func (s *ProjectGroupService) Add(projectGroup *ProjectGroup) (*ProjectGroup, error) { if IsNil(projectGroup) { @@ -62,18 +45,18 @@ func (s *ProjectGroupService) Add(projectGroup *ProjectGroup) (*ProjectGroup, er // Get returns a collection of project groups 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 *ProjectGroupService) Get(projectGroupsQuery ProjectGroupsQuery) (*ProjectGroups, error) { +func (s *ProjectGroupService) Get(projectGroupsQuery ProjectGroupsQuery) (*resources.Resources[ProjectGroup], error) { path, err := s.GetURITemplate().Expand(projectGroupsQuery) if err != nil { - return &ProjectGroups{}, err + return &resources.Resources[ProjectGroup]{}, err } - response, err := services.ApiGet(s.GetClient(), new(ProjectGroups), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[ProjectGroup]), path) if err != nil { - return &ProjectGroups{}, err + return &resources.Resources[ProjectGroup]{}, err } - return response.(*ProjectGroups), nil + return response.(*resources.Resources[ProjectGroup]), nil } // GetAll returns all project groups. If none can be found or an error occurs, @@ -121,7 +104,7 @@ func (s *ProjectGroupService) GetByPartialName(partialName string) ([]*ProjectGr return []*ProjectGroup{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[ProjectGroup](s, path) } func (s *ProjectGroupService) GetProjects(projectGroup *ProjectGroup) ([]*projects.Project, error) { @@ -136,12 +119,12 @@ func (s *ProjectGroupService) GetProjects(projectGroup *ProjectGroup) ([]*projec loadNextPage := true for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(projects.Projects), path) + resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[projects.Project]), path) if err != nil { return projectsToReturn, err } - projectList := resp.(*projects.Projects) + projectList := resp.(*resources.Resources[projects.Project]) projectsToReturn = append(projectsToReturn, projectList.Items...) path, loadNextPage = services.LoadNextPage(projectList.PagedResults) } diff --git a/pkg/projects/project.go b/pkg/projects/project.go index b22ece32..db7c6c16 100644 --- a/pkg/projects/project.go +++ b/pkg/projects/project.go @@ -41,13 +41,8 @@ type Project struct { resources.Resource } -type Projects struct { - Items []*Project `json:"Items"` - resources.PagedResults -} - -func NewProjects() *Projects { - return &Projects{ +func NewProjects() *resources.Resources[Project] { + return &resources.Resources[Project]{ Items: []*Project{}, PagedResults: resources.PagedResults{ ItemType: "Project", diff --git a/pkg/projects/project_service.go b/pkg/projects/project_service.go index 6c35858d..ab9f78dc 100644 --- a/pkg/projects/project_service.go +++ b/pkg/projects/project_service.go @@ -10,6 +10,7 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/channels" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/releases" + "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" @@ -91,7 +92,7 @@ func (s *ProjectService) ConvertToVcs(project *Project, versionControlSettings * // Get returns a collection of projects 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 *ProjectService) Get(projectsQuery ProjectsQuery) (*Projects, error) { +func (s *ProjectService) Get(projectsQuery ProjectsQuery) (*resources.Resources[Project], error) { v, _ := query.Values(projectsQuery) path := s.BasePath encodedQueryString := v.Encode() @@ -99,12 +100,12 @@ func (s *ProjectService) Get(projectsQuery ProjectsQuery) (*Projects, error) { path += "?" + encodedQueryString } - resp, err := services.ApiGet(s.GetClient(), new(Projects), path) + resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[Project]), path) if err != nil { - return &Projects{}, err + return &resources.Resources[Project]{}, err } - return resp.(*Projects), nil + return resp.(*resources.Resources[Project]), nil } // GetAll returns all projects. If none can be found or an error occurs, it @@ -175,13 +176,13 @@ func (s *ProjectService) GetChannels(project *Project) ([]*channels.Channel, err loadNextPage := true for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(channels.Channels), path) + resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[channels.Channel]), path) if err != nil { return projectChannels, err } - r := resp.(*channels.Channels) + r := resp.(*resources.Resources[channels.Channel]) projectChannels = append(projectChannels, r.Items...) path, loadNextPage = services.LoadNextPage(r.PagedResults) } @@ -228,12 +229,12 @@ func (s *ProjectService) GetReleases(project *Project) ([]*releases.Release, err loadNextPage := true for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(releases.Releases), path) + resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[releases.Release]), path) if err != nil { return nil, err } - r := resp.(*releases.Releases) + r := resp.(*resources.Resources[releases.Release]) p = append(p, r.Items...) path, loadNextPage = services.LoadNextPage(r.PagedResults) } diff --git a/pkg/releases/release.go b/pkg/releases/release.go index 6b5f12a1..ba33a0c8 100644 --- a/pkg/releases/release.go +++ b/pkg/releases/release.go @@ -7,12 +7,6 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" ) -// Releases defines a collection of Release instance with built-in support for paged results from the API. -type Releases struct { - Items []*Release `json:"Items"` - resources.PagedResults -} - type Release struct { Assembled time.Time `json:"Assembled,omitempty"` BuildInformation []*ReleasePackageVersionBuildInformation `json:"BuildInformation,omitempty"` diff --git a/pkg/releases/release_service.go b/pkg/releases/release_service.go index e31f9802..fc347f53 100644 --- a/pkg/releases/release_service.go +++ b/pkg/releases/release_service.go @@ -4,6 +4,7 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/channels" "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" @@ -44,7 +45,7 @@ func (s *ReleaseService) Add(release *Release) (*Release, error) { // Get returns a collection of releases 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 *ReleaseService) Get(releasesQuery ...ReleasesQuery) (*Releases, error) { +func (s *ReleaseService) Get(releasesQuery ...ReleasesQuery) (*resources.Resources[Release], error) { v, _ := query.Values(releasesQuery[0]) path := s.BasePath encodedQueryString := v.Encode() @@ -52,12 +53,12 @@ func (s *ReleaseService) Get(releasesQuery ...ReleasesQuery) (*Releases, error) path += "?" + encodedQueryString } - resp, err := services.ApiGet(s.GetClient(), new(Releases), path) + resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[Release]), path) if err != nil { - return &Releases{}, err + return &resources.Resources[Release]{}, err } - return resp.(*Releases), nil + return resp.(*resources.Resources[Release]), nil } func (s *ReleaseService) CreateV1(createReleaseV1 *CreateReleaseV1) (*CreateReleaseResponseV1, error) { @@ -92,33 +93,33 @@ func (s *ReleaseService) GetByID(id string) (*Release, error) { return resp.(*Release), nil } -func (s *ReleaseService) GetReleases(channel *channels.Channel, releaseQuery ...*ReleaseQuery) (*Releases, error) { +func (s *ReleaseService) GetReleases(channel *channels.Channel, releaseQuery ...*ReleaseQuery) (*resources.Resources[Release], error) { if channel == nil { return nil, internal.CreateInvalidParameterError("GetReleases", "channel") } uriTemplate, err := uritemplates.Parse(channel.GetLinks()[constants.LinkReleases]) if err != nil { - return &Releases{}, err + return &resources.Resources[Release]{}, err } values := make(map[string]interface{}) path, err := uriTemplate.Expand(values) if err != nil { - return &Releases{}, err + return &resources.Resources[Release]{}, err } if releaseQuery != nil { path, err = uriTemplate.Expand(releaseQuery[0]) if err != nil { - return &Releases{}, err + return &resources.Resources[Release]{}, err } } - resp, err := services.ApiGet(s.GetClient(), new(Releases), path) + resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[Release]), path) if err != nil { - return &Releases{}, err + return &resources.Resources[Release]{}, err } - return resp.(*Releases), nil + return resp.(*resources.Resources[Release]), nil } diff --git a/pkg/resources/resource.go b/pkg/resources/resource.go index da6f721c..648fbd6f 100644 --- a/pkg/resources/resource.go +++ b/pkg/resources/resource.go @@ -19,8 +19,8 @@ type IResource interface { Validate() error } -type Resources struct { - Items []*Resource `json:"Items"` +type Resources[T any] struct { + Items []*T `json:"Items"` PagedResults } diff --git a/pkg/runbooks/runbook.go b/pkg/runbooks/runbook.go index 3868c4fa..fcda85a2 100644 --- a/pkg/runbooks/runbook.go +++ b/pkg/runbooks/runbook.go @@ -23,13 +23,6 @@ type Runbook struct { resources.Resource } -// Runbooks defines a collection of runbooks with built-in support for paged -// results. -type Runbooks struct { - Items []*Runbook `json:"Items"` - resources.PagedResults -} - // NewRunbook creates and initializes a runbook. func NewRunbook(name string, projectID string) *Runbook { return &Runbook{ diff --git a/pkg/runbooks/runbook_process.go b/pkg/runbooks/runbook_process.go index 104d1d18..57e67b12 100644 --- a/pkg/runbooks/runbook_process.go +++ b/pkg/runbooks/runbook_process.go @@ -5,11 +5,6 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" ) -type RunbookProcesses struct { - Items []*RunbookProcess `json:"Items"` - resources.PagedResults -} - type RunbookProcess struct { LastSnapshotID string `json:"LastSnapshotId,omitempty"` ProjectID string `json:"ProjectId,omitempty"` diff --git a/pkg/runbooks/runbook_snapshot.go b/pkg/runbooks/runbook_snapshot.go index be2b3867..6127a769 100644 --- a/pkg/runbooks/runbook_snapshot.go +++ b/pkg/runbooks/runbook_snapshot.go @@ -9,13 +9,6 @@ import ( "github.com/go-playground/validator/v10/non-standard/validators" ) -// RunbookSnapshots defines a collection of runbook snapshots with built-in -// support for paged results from the API. -type RunbookSnapshots struct { - Items []*RunbookSnapshot `json:"Items"` - resources.PagedResults -} - // RunbookSnapshot represents a runbook snapshot. type RunbookSnapshot struct { Assembled *time.Time `json:"Assembled,omitempty"` diff --git a/pkg/services/service.go b/pkg/services/service.go index 53ba15bf..2e0cffe5 100644 --- a/pkg/services/service.go +++ b/pkg/services/service.go @@ -58,6 +58,24 @@ func NewService(name string, sling *sling.Sling, uriTemplate string) Service { } } +func GetPagedResponse[T any](s IService, path string) ([]*T, error) { + resourcesToReturn := []*T{} + loadNextPage := true + + for loadNextPage { + resp, err := ApiGet(s.GetClient(), new(resources.Resources[T]), path) + if err != nil { + return resourcesToReturn, err + } + + responseList := resp.(*resources.Resources[T]) + resourcesToReturn = append(resourcesToReturn, responseList.Items...) + path, loadNextPage = LoadNextPage(responseList.PagedResults) + } + + return resourcesToReturn, nil +} + // Generic OctopusDeploy API Get Function. func ApiGet(sling *sling.Sling, inputStruct interface{}, path string) (interface{}, error) { if sling == nil { diff --git a/pkg/spaces/space.go b/pkg/spaces/space.go index ce88498a..bf784d48 100644 --- a/pkg/spaces/space.go +++ b/pkg/spaces/space.go @@ -5,11 +5,6 @@ import ( "github.com/go-playground/validator/v10" ) -type Spaces struct { - Items []*Space `json:"Items"` - resources.PagedResults -} - type Space struct { Description string `json:"Description,omitempty"` IsDefault bool `json:"IsDefault,omitempty"` diff --git a/pkg/spaces/space_service.go b/pkg/spaces/space_service.go index d5be218b..c081a5f2 100644 --- a/pkg/spaces/space_service.go +++ b/pkg/spaces/space_service.go @@ -4,6 +4,7 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services" "github.com/dghubble/sling" ) @@ -45,18 +46,18 @@ func (s *SpaceService) Add(space *Space) (*Space, error) { // Get returns a collection of spaces 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 *SpaceService) Get(spacesQuery SpacesQuery) (*Spaces, error) { +func (s *SpaceService) Get(spacesQuery SpacesQuery) (*resources.Resources[Space], error) { path, err := s.GetURITemplate().Expand(spacesQuery) if err != nil { - return &Spaces{}, err + return &resources.Resources[Space]{}, err } - response, err := services.ApiGet(s.GetClient(), new(Spaces), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Space]), path) if err != nil { - return &Spaces{}, err + return &resources.Resources[Space]{}, err } - return response.(*Spaces), nil + return response.(*resources.Resources[Space]), nil } // GetByID returns the space that matches the input ID. If one cannot be found, diff --git a/pkg/tagsets/tag_set.go b/pkg/tagsets/tag_set.go index 04739a10..6a591aee 100644 --- a/pkg/tagsets/tag_set.go +++ b/pkg/tagsets/tag_set.go @@ -2,11 +2,6 @@ package tagsets import "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" -type TagSets struct { - Items []*TagSet `json:"Items"` - resources.PagedResults -} - type TagSet struct { Description string `json:"Description"` Name string `json:"Name"` diff --git a/pkg/tagsets/tag_set_service.go b/pkg/tagsets/tag_set_service.go index 9a723aca..107f33c1 100644 --- a/pkg/tagsets/tag_set_service.go +++ b/pkg/tagsets/tag_set_service.go @@ -3,6 +3,7 @@ package tagsets 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" ) @@ -44,18 +45,18 @@ func (s *TagSetService) Add(tagSet *TagSet) (*TagSet, error) { // Get returns a collection of tag sets 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 *TagSetService) Get(tagSetsQuery TagSetsQuery) (*TagSets, error) { +func (s *TagSetService) Get(tagSetsQuery TagSetsQuery) (*resources.Resources[TagSet], error) { path, err := s.GetURITemplate().Expand(tagSetsQuery) if err != nil { - return &TagSets{}, err + return &resources.Resources[TagSet]{}, err } - response, err := services.ApiGet(s.GetClient(), new(TagSets), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[TagSet]), path) if err != nil { - return &TagSets{}, err + return &resources.Resources[TagSet]{}, err } - return response.(*TagSets), nil + return response.(*resources.Resources[TagSet]), nil } // GetByID returns the tag set that matches the input ID. If one cannot be diff --git a/pkg/tasks/task.go b/pkg/tasks/task.go index 67a3eb00..65833e34 100644 --- a/pkg/tasks/task.go +++ b/pkg/tasks/task.go @@ -31,11 +31,6 @@ type Task struct { resources.Resource } -type Tasks struct { - Items []*Task `json:"Items"` - resources.PagedResults -} - // NewTask creates and initializes a task. func NewTask() *Task { return &Task{ diff --git a/pkg/tasks/task_service.go b/pkg/tasks/task_service.go index 04fdbab4..4cbdc299 100644 --- a/pkg/tasks/task_service.go +++ b/pkg/tasks/task_service.go @@ -3,6 +3,7 @@ package tasks 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" ) @@ -42,16 +43,16 @@ func (s *TaskService) Add(task *Task) (*Task, error) { // Get returns a collection of tasks 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 *TaskService) Get(tasksQuery TasksQuery) (*Tasks, error) { +func (s *TaskService) Get(tasksQuery TasksQuery) (*resources.Resources[Task], error) { path, err := s.GetURITemplate().Expand(tasksQuery) if err != nil { - return &Tasks{}, err + return &resources.Resources[Task]{}, err } - response, err := services.ApiGet(s.GetClient(), new(Tasks), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Task]), path) if err != nil { - return &Tasks{}, err + return &resources.Resources[Task]{}, err } - return response.(*Tasks), nil + return response.(*resources.Resources[Task]), nil } diff --git a/pkg/teams/team.go b/pkg/teams/team.go index 93db16c2..4273fa24 100644 --- a/pkg/teams/team.go +++ b/pkg/teams/team.go @@ -6,12 +6,6 @@ import ( "github.com/go-playground/validator/v10" ) -// Teams defines a collection of teams with built-in support for paged results. -type Teams struct { - Items []*Team `json:"Items"` - resources.PagedResults -} - type Team struct { CanBeDeleted bool `json:"CanBeDeleted,omitempty"` CanBeRenamed bool `json:"CanBeRenamed,omitempty"` diff --git a/pkg/teams/team_service.go b/pkg/teams/team_service.go index 6897b7b1..1b4edc7d 100644 --- a/pkg/teams/team_service.go +++ b/pkg/teams/team_service.go @@ -4,6 +4,7 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/userroles" "github.com/OctopusDeploy/go-octopusdeploy/v2/uritemplates" @@ -22,24 +23,6 @@ func NewTeamService(sling *sling.Sling, uriTemplate string) *TeamService { } } -func (s *TeamService) getPagedResponse(path string) ([]*Team, error) { - resources := []*Team{} - loadNextPage := true - - for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(Teams), path) - if err != nil { - return resources, err - } - - responseList := resp.(*Teams) - resources = append(resources, responseList.Items...) - path, loadNextPage = services.LoadNextPage(responseList.PagedResults) - } - - return resources, nil -} - // Add creates a new team. func (s *TeamService) Add(team *Team) (*Team, error) { if IsNil(team) { @@ -78,18 +61,18 @@ func (s *TeamService) Delete(team *Team) error { // Get returns a collection of teams 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 *TeamService) Get(teamsQuery TeamsQuery) (*Teams, error) { +func (s *TeamService) Get(teamsQuery TeamsQuery) (*resources.Resources[Team], error) { path, err := s.GetURITemplate().Expand(teamsQuery) if err != nil { - return &Teams{}, err + return &resources.Resources[Team]{}, err } - response, err := services.ApiGet(s.GetClient(), new(Teams), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Team]), path) if err != nil { - return &Teams{}, err + return &resources.Resources[Team]{}, err } - return response.(*Teams), nil + return response.(*resources.Resources[Team]), nil } // GetAll returns all teams. If none can be found or an error occurs, it @@ -137,7 +120,7 @@ func (s *TeamService) GetByPartialName(partialName string) ([]*Team, error) { return []*Team{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[Team](s, path) } // Update modifies a team based on the one provided as input. @@ -155,14 +138,14 @@ func (s *TeamService) Update(team *Team) (*Team, error) { return resp.(*Team), nil } -func (s *TeamService) GetScopedUserRoles(team Team, query core.SkipTakeQuery) (*userroles.ScopedUserRoles, error) { +func (s *TeamService) GetScopedUserRoles(team Team, query core.SkipTakeQuery) (*resources.Resources[userroles.ScopedUserRole], error) { template, _ := uritemplates.Parse(team.Links["ScopedUserRoles"]) path, _ := template.Expand(query) - resp, err := services.ApiGet(s.GetClient(), new(userroles.ScopedUserRoles), path) + resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[userroles.ScopedUserRole]), path) if err != nil { return nil, err } - return resp.(*userroles.ScopedUserRoles), nil + return resp.(*resources.Resources[userroles.ScopedUserRole]), nil } diff --git a/pkg/tenants/tenant.go b/pkg/tenants/tenant.go index 3047e255..24c1b7ab 100644 --- a/pkg/tenants/tenant.go +++ b/pkg/tenants/tenant.go @@ -5,11 +5,6 @@ import ( "github.com/go-playground/validator/v10" ) -type Tenants struct { - Items []*Tenant `json:"Items"` - resources.PagedResults -} - type Tenant struct { ClonedFromTenantID string `json:"ClonedFromTenantId"` Description string `json:"Description"` diff --git a/pkg/tenants/tenant_service.go b/pkg/tenants/tenant_service.go index 7cb8dfce..34b838f6 100644 --- a/pkg/tenants/tenant_service.go +++ b/pkg/tenants/tenant_service.go @@ -3,6 +3,7 @@ package tenants 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/pkg/variables" "github.com/OctopusDeploy/go-octopusdeploy/v2/uritemplates" @@ -44,24 +45,6 @@ func (s *TenantService) getByProjectIDPath(id string) (string, error) { return s.GetURITemplate().Expand(values) } -func (s *TenantService) getPagedResponse(path string) ([]*Tenant, error) { - resources := []*Tenant{} - loadNextPage := true - - for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(Tenants), path) - if err != nil { - return resources, err - } - - responseList := resp.(*Tenants) - resources = append(resources, responseList.Items...) - path, loadNextPage = services.LoadNextPage(responseList.PagedResults) - } - - return resources, nil -} - // Add creates a new Tenant. func (s *TenantService) Add(tenant *Tenant) (*Tenant, error) { if IsNil(tenant) { @@ -93,18 +76,18 @@ func (s *TenantService) CreateVariables(tenant *Tenant, tenantVariable *variable // Get returns a collection of tenants 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 *TenantService) Get(tenantsQuery TenantsQuery) (*Tenants, error) { +func (s *TenantService) Get(tenantsQuery TenantsQuery) (*resources.Resources[Tenant], error) { path, err := s.GetURITemplate().Expand(tenantsQuery) if err != nil { - return &Tenants{}, err + return &resources.Resources[Tenant]{}, err } - response, err := services.ApiGet(s.GetClient(), new(Tenants), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Tenant]), path) if err != nil { - return &Tenants{}, err + return &resources.Resources[Tenant]{}, err } - return response.(*Tenants), nil + return response.(*resources.Resources[Tenant]), nil } // GetAll returns all tenants. If none can be found or an error occurs, it @@ -151,7 +134,7 @@ func (s *TenantService) GetByIDs(ids []string) ([]*Tenant, error) { return []*Tenant{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[Tenant](s, path) } func (s *TenantService) GetMissingVariables(missibleVariablesQuery variables.MissingVariablesQuery) (*[]variables.TenantsMissingVariables, error) { @@ -181,7 +164,7 @@ func (s *TenantService) GetByProjectID(id string) ([]*Tenant, error) { return []*Tenant{}, nil } - return s.getPagedResponse(path) + return services.GetPagedResponse[Tenant](s, path) } // GetByPartialName performs a lookup and returns all tenants with a matching @@ -196,7 +179,7 @@ func (s *TenantService) GetByPartialName(partialName string) ([]*Tenant, error) return []*Tenant{}, nil } - return s.getPagedResponse(path) + return services.GetPagedResponse[Tenant](s, path) } func (s *TenantService) GetVariables(tenant *Tenant) (*variables.TenantVariables, error) { diff --git a/pkg/triggers/project_trigger.go b/pkg/triggers/project_trigger.go index cec9a028..3bc500a2 100644 --- a/pkg/triggers/project_trigger.go +++ b/pkg/triggers/project_trigger.go @@ -9,11 +9,6 @@ import ( "github.com/go-playground/validator/v10" ) -type ProjectTriggers struct { - Items []*ProjectTrigger `json:"Items"` - resources.PagedResults -} - type ProjectTrigger struct { Action actions.ITriggerAction `json:"Action"` Description string `json:"Description,omitempty"` diff --git a/pkg/triggers/project_trigger_service.go b/pkg/triggers/project_trigger_service.go index 415f8649..8a577714 100644 --- a/pkg/triggers/project_trigger_service.go +++ b/pkg/triggers/project_trigger_service.go @@ -19,24 +19,6 @@ func NewProjectTriggerService(sling *sling.Sling, uriTemplate string) *ProjectTr } } -func (s *ProjectTriggerService) getPagedResponse(path string) ([]*ProjectTrigger, error) { - resources := []*ProjectTrigger{} - loadNextPage := true - - for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(ProjectTriggers), path) - if err != nil { - return resources, err - } - - responseList := resp.(*ProjectTriggers) - resources = append(resources, responseList.Items...) - path, loadNextPage = services.LoadNextPage(responseList.PagedResults) - } - - return resources, nil -} - // GetByID returns the project trigger that matches the input ID. If one cannot // be found, it returns nil and an error. func (s *ProjectTriggerService) GetByID(id string) (*ProjectTrigger, error) { @@ -79,7 +61,7 @@ func (s *ProjectTriggerService) GetAll() ([]*ProjectTrigger, error) { return []*ProjectTrigger{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[ProjectTrigger](s, path) } // Add creates a new project trigger. diff --git a/pkg/userroles/scoped_user_role.go b/pkg/userroles/scoped_user_role.go index 438e673c..e7ad4254 100644 --- a/pkg/userroles/scoped_user_role.go +++ b/pkg/userroles/scoped_user_role.go @@ -17,11 +17,6 @@ type ScopedUserRole struct { resources.Resource } -type ScopedUserRoles struct { - Items []*ScopedUserRole `json:"Items"` - resources.PagedResults -} - func NewScopedUserRole(userRoleId string) *ScopedUserRole { return &ScopedUserRole{ UserRoleID: userRoleId, diff --git a/pkg/userroles/scoped_user_role_service.go b/pkg/userroles/scoped_user_role_service.go index fd39d2d4..4f16cce9 100644 --- a/pkg/userroles/scoped_user_role_service.go +++ b/pkg/userroles/scoped_user_role_service.go @@ -3,6 +3,7 @@ package userroles 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" ) @@ -42,14 +43,14 @@ func (s *ScopedUserRoleService) Add(scopedUserRole *ScopedUserRole) (*ScopedUser // Currently no known query params, not even take and skip // Query params could exist, but are undocumented in the swagger -func (s *ScopedUserRoleService) Get() (*ScopedUserRoles, error) { +func (s *ScopedUserRoleService) Get() (*resources.Resources[ScopedUserRole], error) { path := s.BasePath - resp, err := services.ApiGet(s.GetClient(), new(ScopedUserRoles), path) + resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[ScopedUserRole]), path) if err != nil { - return &ScopedUserRoles{}, err + return &resources.Resources[ScopedUserRole]{}, err } - return resp.(*ScopedUserRoles), nil + return resp.(*resources.Resources[ScopedUserRole]), nil } func (s *ScopedUserRoleService) GetByID(id string) (*ScopedUserRole, error) { diff --git a/pkg/userroles/user_role.go b/pkg/userroles/user_role.go index bf8c7d7a..9dfa04e6 100644 --- a/pkg/userroles/user_role.go +++ b/pkg/userroles/user_role.go @@ -2,13 +2,6 @@ package userroles import "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" -// UserRoles defines a collection of user roles with built-in support for paged -// results. -type UserRoles struct { - Items []*UserRole `json:"Items"` - resources.PagedResults -} - // UserRole represents a user role in Octopus. type UserRole struct { CanBeDeleted bool `json:"CanBeDeleted,omitempty"` diff --git a/pkg/userroles/user_role_service.go b/pkg/userroles/user_role_service.go index 748e514b..e17dc4a4 100644 --- a/pkg/userroles/user_role_service.go +++ b/pkg/userroles/user_role_service.go @@ -3,6 +3,7 @@ package userroles 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" ) @@ -41,18 +42,18 @@ func (s *UserRoleService) Add(userRole *UserRole) (*UserRole, error) { // Get returns a collection of user roles 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 *UserRoleService) Get(userRolesQuery UserRolesQuery) (*UserRoles, error) { +func (s *UserRoleService) Get(userRolesQuery UserRolesQuery) (*resources.Resources[UserRole], error) { path, err := s.GetURITemplate().Expand(userRolesQuery) if err != nil { - return &UserRoles{}, err + return &resources.Resources[UserRole]{}, err } - response, err := services.ApiGet(s.GetClient(), new(UserRoles), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[UserRole]), path) if err != nil { - return &UserRoles{}, err + return &resources.Resources[UserRole]{}, err } - return response.(*UserRoles), nil + return response.(*resources.Resources[UserRole]), nil } // GetAll returns all user roles. If none can be found or an error occurs, it diff --git a/pkg/users/api_key.go b/pkg/users/api_key.go index e9673a4c..6741da34 100644 --- a/pkg/users/api_key.go +++ b/pkg/users/api_key.go @@ -6,13 +6,6 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" ) -// APIKeys defines a collection of API keys with built-in support for paged -// results. -type APIKeys struct { - Items []*APIKey `json:"Items"` - resources.PagedResults -} - // APIKey represents an API key. type APIKey struct { APIKey string `json:"ApiKey,omitempty"` diff --git a/pkg/users/api_key_service.go b/pkg/users/api_key_service.go index 865b1306..cd280e57 100644 --- a/pkg/users/api_key_service.go +++ b/pkg/users/api_key_service.go @@ -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" ) @@ -39,12 +40,12 @@ func (s *ApiKeyService) GetByUserID(userID string) ([]*APIKey, error) { loadNextPage := true for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(APIKeys), path) + resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[APIKey]), path) if err != nil { return nil, err } - r := resp.(*APIKeys) + r := resp.(*resources.Resources[APIKey]) p = append(p, r.Items...) path, loadNextPage = services.LoadNextPage(r.PagedResults) } diff --git a/pkg/users/user.go b/pkg/users/user.go index 0b3545ea..4e991eb3 100644 --- a/pkg/users/user.go +++ b/pkg/users/user.go @@ -2,12 +2,6 @@ package users import "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" -// Users defines a collection of users with built-in support for paged results. -type Users struct { - Items []*User `json:"Items"` - resources.PagedResults -} - // User represents a user in Octopus. type User struct { CanPasswordBeEdited bool `json:"CanPasswordBeEdited,omitempty"` diff --git a/pkg/users/user_service.go b/pkg/users/user_service.go index 7fb7e74c..3101f50f 100644 --- a/pkg/users/user_service.go +++ b/pkg/users/user_service.go @@ -4,6 +4,7 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/permissions" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/spaces" "github.com/dghubble/sling" @@ -102,7 +103,7 @@ func (s *UserService) GetAPIKeyByID(user *User, apiKeyID string) (*APIKey, error return response.(*APIKey), nil } -func (s *UserService) GetAPIKeys(user *User, apiQuery ...APIQuery) (*APIKeys, error) { +func (s *UserService) GetAPIKeys(user *User, apiQuery ...APIQuery) (*resources.Resources[APIKey], error) { if user == nil { return nil, internal.CreateInvalidParameterError(constants.OperationGetAPIKeys, constants.ParameterUser) } @@ -126,12 +127,12 @@ func (s *UserService) GetAPIKeys(user *User, apiQuery ...APIQuery) (*APIKeys, er path += "?" + encodedQueryString } - response, err := services.ApiGet(s.GetClient(), new(APIKeys), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[APIKey]), path) if err != nil { return nil, err } - return response.(*APIKeys), nil + return response.(*resources.Resources[APIKey]), nil } func (s *UserService) GetAuthentication() (*UserAuthentication, error) { @@ -162,18 +163,18 @@ func (s *UserService) GetAuthenticationByUser(user *User) (*UserAuthentication, // Get returns a collection of users 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 *UserService) Get(usersQuery UsersQuery) (*Users, error) { +func (s *UserService) Get(usersQuery UsersQuery) (*resources.Resources[User], error) { path, err := s.GetURITemplate().Expand(usersQuery) if err != nil { - return &Users{}, err + return &resources.Resources[User]{}, err } - response, err := services.ApiGet(s.GetClient(), new(Users), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[User]), path) if err != nil { - return &Users{}, err + return &resources.Resources[User]{}, err } - return response.(*Users), nil + return response.(*resources.Resources[User]), nil } // GetByID returns the user that matches the input ID. If one cannot be found, diff --git a/pkg/variables/library_variable_set.go b/pkg/variables/library_variable_set.go index f94e7580..f418c593 100644 --- a/pkg/variables/library_variable_set.go +++ b/pkg/variables/library_variable_set.go @@ -6,11 +6,6 @@ import ( "github.com/go-playground/validator/v10" ) -type LibraryVariableSets struct { - Items []*LibraryVariableSet `json:"Items"` - resources.PagedResults -} - type LibraryVariableSet struct { ContentType string `json:"ContentType" validate:"required,oneof=ScriptModule Variables"` Description string `json:"Description,omitempty"` diff --git a/pkg/variables/library_variable_set_service.go b/pkg/variables/library_variable_set_service.go index e3043345..8458ea99 100644 --- a/pkg/variables/library_variable_set_service.go +++ b/pkg/variables/library_variable_set_service.go @@ -3,6 +3,7 @@ package variables 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" ) @@ -19,24 +20,6 @@ func NewLibraryVariableSetService(sling *sling.Sling, uriTemplate string) *Libra } } -func (s *LibraryVariableSetService) getPagedResponse(path string) ([]*LibraryVariableSet, error) { - resources := []*LibraryVariableSet{} - loadNextPage := true - - for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(LibraryVariableSets), path) - if err != nil { - return resources, err - } - - responseList := resp.(*LibraryVariableSets) - resources = append(resources, responseList.Items...) - path, loadNextPage = services.LoadNextPage(responseList.PagedResults) - } - - return resources, nil -} - // Add creates a new library variable set. func (s *LibraryVariableSetService) Add(libraryVariableSet *LibraryVariableSet) (*LibraryVariableSet, error) { if IsNil(libraryVariableSet) { @@ -59,18 +42,18 @@ func (s *LibraryVariableSetService) Add(libraryVariableSet *LibraryVariableSet) // Get returns a collection of library variable sets 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 *LibraryVariableSetService) Get(libraryVariablesQuery LibraryVariablesQuery) (*LibraryVariableSets, error) { +func (s *LibraryVariableSetService) Get(libraryVariablesQuery LibraryVariablesQuery) (*resources.Resources[LibraryVariableSet], error) { path, err := s.GetURITemplate().Expand(libraryVariablesQuery) if err != nil { - return &LibraryVariableSets{}, err + return &resources.Resources[LibraryVariableSet]{}, err } - response, err := services.ApiGet(s.GetClient(), new(LibraryVariableSets), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[LibraryVariableSet]), path) if err != nil { - return &LibraryVariableSets{}, err + return &resources.Resources[LibraryVariableSet]{}, err } - return response.(*LibraryVariableSets), nil + return response.(*resources.Resources[LibraryVariableSet]), nil } // GetAll returns all library variable sets. If none can be found or an error @@ -117,7 +100,7 @@ func (s *LibraryVariableSetService) GetByPartialName(partialName string) ([]*Lib return []*LibraryVariableSet{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[LibraryVariableSet](s, path) } // Update modifies a library variable set based on the one provided as input. diff --git a/pkg/variables/script_module.go b/pkg/variables/script_module.go index a2314a09..57b60ef4 100644 --- a/pkg/variables/script_module.go +++ b/pkg/variables/script_module.go @@ -18,11 +18,6 @@ type ScriptModule struct { resources.Resource } -type ScriptModules struct { - Items []*ScriptModule `json:"Items"` - resources.PagedResults -} - // NewScriptModule creates and initializes a script module. func NewScriptModule(name string) *ScriptModule { return &ScriptModule{ diff --git a/pkg/variables/script_module_service.go b/pkg/variables/script_module_service.go index f4e6a8b4..b2c0c7bf 100644 --- a/pkg/variables/script_module_service.go +++ b/pkg/variables/script_module_service.go @@ -7,6 +7,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" ) @@ -23,24 +24,6 @@ func NewScriptModuleService(sling *sling.Sling, uriTemplate string) *ScriptModul } } -func (s *ScriptModuleService) getPagedResponse(path string) ([]*ScriptModule, error) { - resources := []*ScriptModule{} - loadNextPage := true - - for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(ScriptModules), path) - if err != nil { - return resources, err - } - - responseList := resp.(*ScriptModules) - resources = append(resources, responseList.Items...) - path, loadNextPage = services.LoadNextPage(responseList.PagedResults) - } - - return resources, nil -} - // Add creates a new script module. func (s *ScriptModuleService) Add(scriptModule *ScriptModule) (*ScriptModule, error) { if IsNil(scriptModule) { @@ -89,37 +72,37 @@ func (s *ScriptModuleService) Add(scriptModule *ScriptModule) (*ScriptModule, er // Get returns a collection of script modules 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 *ScriptModuleService) Get(libraryVariablesQuery LibraryVariablesQuery) (*ScriptModules, error) { +func (s *ScriptModuleService) Get(libraryVariablesQuery LibraryVariablesQuery) (*resources.Resources[ScriptModule], error) { path, err := s.GetURITemplate().Expand(libraryVariablesQuery) if err != nil { - return &ScriptModules{}, err + return &resources.Resources[ScriptModule]{}, err } - response, err := services.ApiGet(s.GetClient(), new(ScriptModules), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[ScriptModule]), path) if err != nil { - return &ScriptModules{}, err + return &resources.Resources[ScriptModule]{}, err } - return response.(*ScriptModules), nil + return response.(*resources.Resources[ScriptModule]), nil } // GetAll returns all script modules. If none can be found or an error // occurs, it returns an empty collection. -func (s *ScriptModuleService) GetAll() (*ScriptModules, error) { +func (s *ScriptModuleService) GetAll() (*resources.Resources[ScriptModule], error) { path, err := s.GetURITemplate().Expand(&LibraryVariablesQuery{ ContentType: "ScriptModule", Take: math.MaxInt32, }) if err != nil { - return &ScriptModules{}, err + return &resources.Resources[ScriptModule]{}, err } - response, err := services.ApiGet(s.GetClient(), new(ScriptModules), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[ScriptModule]), path) if err != nil { - return &ScriptModules{}, err + return &resources.Resources[ScriptModule]{}, err } - return response.(*ScriptModules), nil + return response.(*resources.Resources[ScriptModule]), nil } // GetByID returns the script module that matches the input ID. If one @@ -173,7 +156,7 @@ func (s *ScriptModuleService) GetByPartialName(partialName string) ([]*ScriptMod return []*ScriptModule{}, err } - return s.getPagedResponse(path) + return services.GetPagedResponse[ScriptModule](s, path) } // Update modifies a script module based on the one provided as input. diff --git a/pkg/workerpools/dynamic_worker_pool.go b/pkg/workerpools/dynamic_worker_pool.go index f9e7950f..2a09b01c 100644 --- a/pkg/workerpools/dynamic_worker_pool.go +++ b/pkg/workerpools/dynamic_worker_pool.go @@ -1,7 +1,6 @@ package workerpools import ( - "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" "github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10/non-standard/validators" ) @@ -18,11 +17,6 @@ type DynamicWorkerPool struct { workerPool } -type DynamicWorkerPools struct { - Items []*DynamicWorkerPool `json:"Items"` - resources.PagedResults -} - // NewDynamicWorkerPool creates and initializes a dynamic worker pool. func NewDynamicWorkerPool(name string, workerType WorkerType) *DynamicWorkerPool { return &DynamicWorkerPool{ diff --git a/pkg/workerpools/static_worker_pool.go b/pkg/workerpools/static_worker_pool.go index ae1e6916..c6ddc07b 100644 --- a/pkg/workerpools/static_worker_pool.go +++ b/pkg/workerpools/static_worker_pool.go @@ -1,7 +1,6 @@ package workerpools import ( - "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" "github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10/non-standard/validators" ) @@ -10,11 +9,6 @@ type StaticWorkerPool struct { workerPool } -type StaticWorkerPools struct { - Items []*StaticWorkerPool `json:"Items"` - resources.PagedResults -} - // NewStaticWorkerPool creates and initializes a static worker pool. func NewStaticWorkerPool(name string) *StaticWorkerPool { return &StaticWorkerPool{ diff --git a/pkg/workerpools/worker_pool_resource.go b/pkg/workerpools/worker_pool_resource.go index de7832a8..c6744a77 100644 --- a/pkg/workerpools/worker_pool_resource.go +++ b/pkg/workerpools/worker_pool_resource.go @@ -19,11 +19,6 @@ type WorkerPoolResource struct { resources.Resource } -type WorkerPoolResources struct { - Items []*WorkerPoolResource `json:"Items"` - resources.PagedResults -} - // NewWorkerPoolResource creates and initializes a worker pool resource. func NewWorkerPoolResource(name string, workerPoolType WorkerPoolType) *WorkerPoolResource { return &WorkerPoolResource{ diff --git a/pkg/workerpools/worker_pool_service.go b/pkg/workerpools/worker_pool_service.go index d623381d..bfa4ea67 100644 --- a/pkg/workerpools/worker_pool_service.go +++ b/pkg/workerpools/worker_pool_service.go @@ -3,6 +3,7 @@ package workerpools 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" @@ -59,12 +60,12 @@ func (s *WorkerPoolService) Get(workerPoolsQuery WorkerPoolsQuery) (*WorkerPools path += "?" + encodedQueryString } - response, err := services.ApiGet(s.GetClient(), new(WorkerPoolResources), path) + response, err := services.ApiGet(s.GetClient(), new(resources.Resources[WorkerPoolResource]), path) if err != nil { return &WorkerPools{}, err } - return ToWorkerPools(response.(*WorkerPoolResources)), nil + return ToWorkerPools(response.(*resources.Resources[WorkerPoolResource])), nil } // GetAll returns all worker pools. If none can be found or an error occurs, it diff --git a/pkg/workerpools/worker_pool_utilities.go b/pkg/workerpools/worker_pool_utilities.go index 30863cb8..b9c48c6b 100644 --- a/pkg/workerpools/worker_pool_utilities.go +++ b/pkg/workerpools/worker_pool_utilities.go @@ -2,6 +2,7 @@ package workerpools import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" ) func ToWorkerPool(workerPoolResource *WorkerPoolResource) (IWorkerPool, error) { @@ -37,7 +38,7 @@ func ToWorkerPool(workerPoolResource *WorkerPoolResource) (IWorkerPool, error) { return workerPool, nil } -func ToWorkerPools(workerPoolResources *WorkerPoolResources) *WorkerPools { +func ToWorkerPools(workerPoolResources *resources.Resources[WorkerPoolResource]) *WorkerPools { return &WorkerPools{ Items: ToWorkerPoolArray(workerPoolResources.Items), PagedResults: workerPoolResources.PagedResults, diff --git a/test/e2e/environment_service_test.go b/test/e2e/environment_service_test.go index 30654329..efb7876d 100644 --- a/test/e2e/environment_service_test.go +++ b/test/e2e/environment_service_test.go @@ -100,6 +100,13 @@ func TestEnvironmentServiceAddGetDelete(t *testing.T) { environment, err := client.Environments.GetByID(createdEnvironment.GetID()) require.NoError(t, err) require.NotNil(t, environment) + + environments, err := client.Environments.Get(environments.EnvironmentsQuery{IDs: []string{createdEnvironment.GetID()}}) + require.NoError(t, err) + require.NotNil(t, environments) + + environmentToCompare := environments.Items[0] + IsEqualEnvironments(t, environment, environmentToCompare) } func TestEnvironmentServiceDeleteAll(t *testing.T) { diff --git a/test/e2e/feed_service_test.go b/test/e2e/feed_service_test.go index fe6b005b..6dc4b37f 100644 --- a/test/e2e/feed_service_test.go +++ b/test/e2e/feed_service_test.go @@ -293,3 +293,26 @@ func TestFeedServiceSearchPackages(t *testing.T) { require.NotNil(t, packageDescriptions) require.NoError(t, err) } + +func TestFeedServiceSearchPackageVersions(t *testing.T) { + client := getOctopusClient() + require.NotNil(t, client) + + feed := CreateTestNuGetFeed(t, client) + require.NotNil(t, feed) + + packageDescriptions, err := client.Feeds.SearchPackages(feed, feeds.SearchPackagesQuery{ + Term: "OctopusTools", + }) + require.NotNil(t, packageDescriptions) + require.NoError(t, err) + + for _, v := range packageDescriptions.Items { + packageVersions, err := client.Feeds.SearchPackageVersions(v, feeds.SearchPackageVersionsQuery{ + FeedID: feed.GetID(), + PackageID: v.ID, + }) + require.NotNil(t, packageVersions) + require.NoError(t, err) + } +}