From 6bd1cc88239a337df4376524506a9554def94208 Mon Sep 17 00:00:00 2001 From: domenicsim1 <87625140+domenicsim1@users.noreply.github.com> Date: Thu, 28 Jul 2022 09:13:04 +1000 Subject: [PATCH] feat: NextPage func for Resources Type (#126) feat: GetAllPages func for Resources Type --- pkg/accounts/account_service.go | 11 +-- pkg/accounts/account_utilities.go | 2 +- pkg/accounts/accounts.go | 37 ++++++++-- .../community_action_template_service.go | 11 +-- .../action_template_service.go | 17 ++--- pkg/artifacts/artifact_service.go | 11 +-- pkg/authentication/authentication_service.go | 3 +- pkg/certificates/certificate_service.go | 15 +++-- pkg/channels/channel_service.go | 15 +++-- pkg/client/client_test.go | 11 +-- pkg/client/octopusdeploy.go | 5 +- pkg/client/root_service.go | 3 +- pkg/configuration/configuration_service.go | 3 +- pkg/deployments/deployment_process_service.go | 7 +- pkg/deployments/deployment_service.go | 19 +++--- pkg/environments/environment_service.go | 15 +++-- pkg/events/event_service.go | 19 +++--- pkg/feeds/feed_service.go | 35 +++++----- pkg/feeds/feed_utilities.go | 2 +- pkg/interruptions/interruption_service.go | 7 +- pkg/lifecycles/lifecycle_service.go | 17 ++--- pkg/machines/machine_policy_service.go | 17 ++--- pkg/machines/machine_service.go | 15 +++-- pkg/machines/worker_service.go | 11 +-- pkg/packages/package_service.go | 5 +- pkg/projectgroups/project_group_service.go | 19 +++--- pkg/projects/project.go | 4 +- pkg/projects/project_service.go | 25 +++---- pkg/releases/release_service.go | 25 +++---- pkg/resources/resource.go | 5 -- pkg/resources/resources.go | 48 +++++++++++++ pkg/runbooks/runbook_process_service.go | 3 +- pkg/runbooks/runbook_service.go | 7 +- pkg/runbooks/runbook_snapshot_service.go | 3 +- pkg/serverstatus/server_status_service.go | 3 +- pkg/services/api/api.go | 61 +++++++++++++++++ pkg/services/service.go | 67 +++---------------- pkg/spaces/space_service.go | 15 +++-- pkg/tagsets/tag_set_service.go | 15 +++-- pkg/tasks/task_service.go | 11 +-- pkg/teams/team_service.go | 21 +++--- pkg/tenants/tenant_service.go | 19 +++--- pkg/triggers/project_trigger_service.go | 3 +- pkg/userroles/scoped_user_role_service.go | 11 +-- pkg/userroles/user_role_service.go | 15 +++-- pkg/users/api_key_service.go | 7 +- pkg/users/user_service.go | 37 +++++----- pkg/variables/library_variable_set_service.go | 15 +++-- pkg/variables/script_module_service.go | 29 ++++---- pkg/variables/tenant_variable_service.go | 3 +- pkg/variables/variable_service.go | 3 +- pkg/workerpools/worker_pool_service.go | 9 +-- pkg/workerpools/worker_pool_utilities.go | 2 +- 53 files changed, 462 insertions(+), 336 deletions(-) create mode 100644 pkg/resources/resources.go create mode 100644 pkg/services/api/api.go diff --git a/pkg/accounts/account_service.go b/pkg/accounts/account_service.go index 24cd3814..a9b2aec4 100644 --- a/pkg/accounts/account_service.go +++ b/pkg/accounts/account_service.go @@ -5,6 +5,7 @@ import ( "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/services/api" "github.com/dghubble/sling" ) @@ -53,12 +54,12 @@ func (s *AccountService) Get(accountsQuery ...AccountsQuery) (*Accounts, error) } } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[AccountResource]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*AccountResource]), path) if err != nil { return &Accounts{}, err } - return ToAccounts(response.(*resources.Resources[AccountResource])), nil + return ToAccounts(response.(*resources.Resources[*AccountResource])), nil } // GetAll returns all accounts. If none are found or an error occurs, it @@ -70,7 +71,7 @@ func (s *AccountService) GetAll() ([]IAccount, error) { return ToAccountArray(items), err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return ToAccountArray(items), err } @@ -86,7 +87,7 @@ func (s *AccountService) GetByID(id string) (IAccount, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(AccountResource), path) + resp, err := api.ApiGet(s.GetClient(), new(AccountResource), path) if err != nil { return nil, err } @@ -97,7 +98,7 @@ func (s *AccountService) GetByID(id string) (IAccount, error) { // GetUsages lists the projects and deployments which are using an account. func (s *AccountService) GetUsages(account IAccount) (*AccountUsage, error) { path := account.GetLinks()[constants.LinkUsages] - resp, err := services.ApiGet(s.GetClient(), new(AccountUsage), path) + resp, err := api.ApiGet(s.GetClient(), new(AccountUsage), path) if err != nil { return nil, err } diff --git a/pkg/accounts/account_utilities.go b/pkg/accounts/account_utilities.go index a7596888..fed00fc9 100644 --- a/pkg/accounts/account_utilities.go +++ b/pkg/accounts/account_utilities.go @@ -87,7 +87,7 @@ func ToAccount(accountResource *AccountResource) (IAccount, error) { return account, nil } -func ToAccounts(accountResources *resources.Resources[AccountResource]) *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 3395b74b..e58e49f8 100644 --- a/pkg/accounts/accounts.go +++ b/pkg/accounts/accounts.go @@ -4,14 +4,13 @@ import ( "encoding/json" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" + "github.com/dghubble/sling" ) // Accounts defines a collection of accounts with built-in support for paged // results. -type Accounts struct { - Items []IAccount `json:"Items"` - resources.PagedResults -} +type Accounts resources.Resources[IAccount] // MarshalJSON returns an Accounts struct as its JSON encoding. func (a *Accounts) MarshalJSON() ([]byte, error) { @@ -127,3 +126,33 @@ func (a *Accounts) UnmarshalJSON(b []byte) error { return nil } + +// GetNextPage retrives the next page from the links collection. If no next page +// exists it will return nill +func (r *Accounts) GetNextPage(client *sling.Sling) (*Accounts, error) { + if r.Links.PageNext == "" { + return nil, nil + } + response, err := api.ApiGet(client, new(resources.Resources[*AccountResource]), r.Links.PageNext) + if err != nil { + return nil, err + } + return ToAccounts(response.(*resources.Resources[*AccountResource])), nil +} + +// GetAllPages will retrive all remaining next pages in the link collection +// and return the result as list of concatenated Items; Including the items +// from the base Resource. +func (r *Accounts) GetAllPages(client *sling.Sling) ([]IAccount, error) { + items := make([]IAccount, 0) + res := r + var err error + for res != nil { + items = append(items, res.Items...) + res, err = res.GetNextPage(client) + if err != nil { + return nil, err + } + } + return items, nil +} diff --git a/pkg/actions/community_action_template_service.go b/pkg/actions/community_action_template_service.go index 5f10ffda..c301e143 100644 --- a/pkg/actions/community_action_template_service.go +++ b/pkg/actions/community_action_template_service.go @@ -5,6 +5,7 @@ import ( "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/services/api" "github.com/dghubble/sling" "github.com/google/go-querystring/query" ) @@ -42,7 +43,7 @@ func (s *CommunityActionTemplateService) getInstallationPath(resource CommunityA // 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) (*resources.Resources[CommunityActionTemplate], error) { +func (s *CommunityActionTemplateService) Get(communityActionTemplatesQuery CommunityActionTemplatesQuery) (*resources.Resources[*CommunityActionTemplate], error) { v, _ := query.Values(communityActionTemplatesQuery) path := s.BasePath encodedQueryString := v.Encode() @@ -50,12 +51,12 @@ func (s *CommunityActionTemplateService) Get(communityActionTemplatesQuery Commu path += "?" + encodedQueryString } - resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[CommunityActionTemplate]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*CommunityActionTemplate]), path) if err != nil { - return &resources.Resources[CommunityActionTemplate]{}, err + return &resources.Resources[*CommunityActionTemplate]{}, err } - return resp.(*resources.Resources[CommunityActionTemplate]), nil + return resp.(*resources.Resources[*CommunityActionTemplate]), nil } // GetAll returns all community action templates. If none can be found or an @@ -81,7 +82,7 @@ func (s *CommunityActionTemplateService) GetByID(id string) (*CommunityActionTem return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(CommunityActionTemplate), path) + resp, err := api.ApiGet(s.GetClient(), new(CommunityActionTemplate), path) if err != nil { return nil, err } diff --git a/pkg/actiontemplates/action_template_service.go b/pkg/actiontemplates/action_template_service.go index 4744d62a..eddad856 100644 --- a/pkg/actiontemplates/action_template_service.go +++ b/pkg/actiontemplates/action_template_service.go @@ -7,6 +7,7 @@ import ( "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/services/api" "github.com/OctopusDeploy/go-octopusdeploy/v2/uritemplates" "github.com/dghubble/sling" "github.com/google/go-querystring/query" @@ -63,7 +64,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) (*resources.Resources[ActionTemplate], error) { +func (s *ActionTemplateService) Get(actionTemplatesQuery Query) (*resources.Resources[*ActionTemplate], error) { v, _ := query.Values(actionTemplatesQuery) path := s.BasePath encodedQueryString := v.Encode() @@ -71,12 +72,12 @@ func (s *ActionTemplateService) Get(actionTemplatesQuery Query) (*resources.Reso path += "?" + encodedQueryString } - resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[ActionTemplate]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*ActionTemplate]), path) if err != nil { - return &resources.Resources[ActionTemplate]{}, err + return &resources.Resources[*ActionTemplate]{}, err } - return resp.(*resources.Resources[ActionTemplate]), nil + return resp.(*resources.Resources[*ActionTemplate]), nil } // GetAll returns all action templates. If none can be found or an error @@ -88,7 +89,7 @@ func (s *ActionTemplateService) GetAll() ([]*ActionTemplate, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -101,7 +102,7 @@ func (s *ActionTemplateService) GetCategories() ([]ActionTemplateCategory, error path := s.categoriesPath - _, err := services.ApiGet(s.GetClient(), items, path) + _, err := api.ApiGet(s.GetClient(), items, path) return *items, err } @@ -118,7 +119,7 @@ func (s *ActionTemplateService) GetByID(id string) (*ActionTemplate, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(ActionTemplate), path) + resp, err := api.ApiGet(s.GetClient(), new(ActionTemplate), path) if err != nil { return nil, err } @@ -148,7 +149,7 @@ func (s *ActionTemplateService) Search(searchQuery string) ([]ActionTemplateSear path = strings.Split(path, "?")[0] } - _, err = services.ApiGet(s.GetClient(), &searchResults, path) + _, err = api.ApiGet(s.GetClient(), &searchResults, path) return searchResults, err } diff --git a/pkg/artifacts/artifact_service.go b/pkg/artifacts/artifact_service.go index 8d2c6f0f..9163c324 100644 --- a/pkg/artifacts/artifact_service.go +++ b/pkg/artifacts/artifact_service.go @@ -5,6 +5,7 @@ import ( "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/services/api" "github.com/dghubble/sling" "github.com/google/go-querystring/query" ) @@ -46,7 +47,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) (*resources.Resources[Artifact], error) { +func (s *ArtifactService) Get(artifactsQuery Query) (*resources.Resources[*Artifact], error) { v, _ := query.Values(artifactsQuery) path := s.BasePath encodedQueryString := v.Encode() @@ -54,12 +55,12 @@ func (s *ArtifactService) Get(artifactsQuery Query) (*resources.Resources[Artifa path += "?" + encodedQueryString } - resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[Artifact]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Artifact]), path) if err != nil { - return &resources.Resources[Artifact]{}, err + return &resources.Resources[*Artifact]{}, err } - return resp.(*resources.Resources[Artifact]), nil + return resp.(*resources.Resources[*Artifact]), nil } // GetAll returns all artifacts. If none can be found or an error occurs, it @@ -85,7 +86,7 @@ func (s *ArtifactService) GetByID(id string) (*Artifact, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(Artifact), path) + resp, err := api.ApiGet(s.GetClient(), new(Artifact), path) if err != nil { return nil, err } diff --git a/pkg/authentication/authentication_service.go b/pkg/authentication/authentication_service.go index 8351a49d..d857ae15 100644 --- a/pkg/authentication/authentication_service.go +++ b/pkg/authentication/authentication_service.go @@ -3,6 +3,7 @@ package authentication import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/dghubble/sling" ) @@ -27,7 +28,7 @@ func (s *AuthenticationService) Get() (*Authentication, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(Authentication), path) + resp, err := api.ApiGet(s.GetClient(), new(Authentication), path) if err != nil { return nil, err } diff --git a/pkg/certificates/certificate_service.go b/pkg/certificates/certificate_service.go index 82a8d59f..a2562036 100644 --- a/pkg/certificates/certificate_service.go +++ b/pkg/certificates/certificate_service.go @@ -7,6 +7,7 @@ import ( "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/services/api" "github.com/dghubble/sling" ) @@ -62,18 +63,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) (*resources.Resources[CertificateResource], error) { +func (s *CertificateService) Get(certificatesQuery CertificatesQuery) (*resources.Resources[*CertificateResource], error) { path, err := s.GetURITemplate().Expand(certificatesQuery) if err != nil { - return &resources.Resources[CertificateResource]{}, err + return &resources.Resources[*CertificateResource]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[CertificateResource]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*CertificateResource]), path) if err != nil { - return &resources.Resources[CertificateResource]{}, err + return &resources.Resources[*CertificateResource]{}, err } - return response.(*resources.Resources[CertificateResource]), nil + return response.(*resources.Resources[*CertificateResource]), nil } // GetAll returns all certificates. If none are found or an error occurs, it @@ -85,7 +86,7 @@ func (s *CertificateService) GetAll() ([]*CertificateResource, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -101,7 +102,7 @@ func (s *CertificateService) GetByID(id string) (*CertificateResource, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(CertificateResource), path) + resp, err := api.ApiGet(s.GetClient(), new(CertificateResource), path) if err != nil { return nil, err } diff --git a/pkg/channels/channel_service.go b/pkg/channels/channel_service.go index 99da7e51..bc81b93a 100644 --- a/pkg/channels/channel_service.go +++ b/pkg/channels/channel_service.go @@ -5,6 +5,7 @@ import ( "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/services/api" "github.com/dghubble/sling" ) @@ -49,18 +50,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) (*resources.Resources[Channel], error) { +func (s *ChannelService) Get(channelsQuery Query) (*resources.Resources[*Channel], error) { path, err := s.GetURITemplate().Expand(channelsQuery) if err != nil { - return &resources.Resources[Channel]{}, err + return &resources.Resources[*Channel]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Channel]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Channel]), path) if err != nil { - return &resources.Resources[Channel]{}, err + return &resources.Resources[*Channel]{}, err } - return response.(*resources.Resources[Channel]), nil + return response.(*resources.Resources[*Channel]), nil } // GetAll returns all channels. If none can be found or an error occurs, it @@ -72,7 +73,7 @@ func (s *ChannelService) GetAll() ([]*Channel, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -84,7 +85,7 @@ func (s *ChannelService) GetByID(id string) (*Channel, error) { } path := s.BasePath + "/" + id - resp, err := services.ApiGet(s.GetClient(), new(Channel), path) + resp, err := api.ApiGet(s.GetClient(), new(Channel), path) if err != nil { return nil, err } diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 375fdea6..83d9798e 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/dghubble/sling" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -57,12 +58,12 @@ func TestNewClient(t *testing.T) { } func TestGetUserAgentString(t *testing.T) { - userAgentString := services.GetUserAgentString() + userAgentString := api.GetUserAgentString() assert.NotNil(t, userAgentString) } func TestGetWithEmptyParameters(t *testing.T) { - resource, err := services.ApiGet(nil, nil, "") + resource, err := api.ApiGet(nil, nil, "") assert.Error(t, err) assert.Nil(t, resource) @@ -70,7 +71,7 @@ func TestGetWithEmptyParameters(t *testing.T) { func TestGetWithEmptySling(t *testing.T) { input := &inputTestValueStruct{test: "fake-value"} - resource, err := services.ApiGet(nil, input, "fake-path") + resource, err := api.ApiGet(nil, input, "fake-path") assert.Error(t, err) assert.Nil(t, resource) @@ -78,12 +79,12 @@ func TestGetWithEmptySling(t *testing.T) { func TestGetWithEmptyPath(t *testing.T) { input := &inputTestValueStruct{test: "fake-value"} - resource, err := services.ApiGet(sling.New(), input, "") + resource, err := api.ApiGet(sling.New(), input, "") assert.Error(t, err) assert.Nil(t, resource) - resource, err = services.ApiGet(sling.New(), input, " ") + resource, err = api.ApiGet(sling.New(), input, " ") assert.Error(t, err) assert.Nil(t, resource) diff --git a/pkg/client/octopusdeploy.go b/pkg/client/octopusdeploy.go index 072c2c76..0a802117 100644 --- a/pkg/client/octopusdeploy.go +++ b/pkg/client/octopusdeploy.go @@ -48,6 +48,7 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/scheduler" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/serverstatus" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/spaces" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/subscriptions" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/tagsets" @@ -180,7 +181,7 @@ func NewClient(httpClient *http.Client, apiURL *url.URL, apiKey string, spaceID // fetch root resource and process paths base := sling.New().Client(httpClient).Base(baseURLWithAPI).Set(constants.ClientAPIKeyHTTPHeader, apiKey) - base.Set("User-Agent", services.GetUserAgentString()) + base.Set("User-Agent", api.GetUserAgentString()) rootService := NewRootService(base, baseURLWithAPI) root, err := rootService.Get() @@ -194,7 +195,7 @@ func NewClient(httpClient *http.Client, apiURL *url.URL, apiKey string, spaceID if !internal.IsEmpty(spaceID) { baseURLWithAPI = fmt.Sprintf("%s/%s", baseURLWithAPI, spaceID) base = sling.New().Client(httpClient).Base(baseURLWithAPI).Set(constants.ClientAPIKeyHTTPHeader, apiKey) - base.Set("User-Agent", services.GetUserAgentString()) + base.Set("User-Agent", api.GetUserAgentString()) rootService = NewRootService(base, baseURLWithAPI) sroot, err = rootService.Get() diff --git a/pkg/client/root_service.go b/pkg/client/root_service.go index 5503bfb4..b53057f4 100644 --- a/pkg/client/root_service.go +++ b/pkg/client/root_service.go @@ -3,6 +3,7 @@ package client import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/dghubble/sling" ) @@ -22,7 +23,7 @@ func (s *RootService) Get() (*RootResource, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(RootResource), path) + resp, err := api.ApiGet(s.GetClient(), new(RootResource), path) if err != nil { return nil, err } diff --git a/pkg/configuration/configuration_service.go b/pkg/configuration/configuration_service.go index 353c9c43..620504a4 100644 --- a/pkg/configuration/configuration_service.go +++ b/pkg/configuration/configuration_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/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/dghubble/sling" ) @@ -31,7 +32,7 @@ func (s *ConfigurationService) GetByID(id string) (*ConfigurationSection, error) return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(ConfigurationSection), path) + resp, err := api.ApiGet(s.GetClient(), new(ConfigurationSection), path) if err != nil { return nil, err } diff --git a/pkg/deployments/deployment_process_service.go b/pkg/deployments/deployment_process_service.go index 26af5da0..6fa67bf7 100644 --- a/pkg/deployments/deployment_process_service.go +++ b/pkg/deployments/deployment_process_service.go @@ -5,6 +5,7 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/OctopusDeploy/go-octopusdeploy/v2/uritemplates" "github.com/dghubble/sling" ) @@ -39,7 +40,7 @@ func (s *DeploymentProcessService) Get(project *projects.Project, gitRef string) template, _ := uritemplates.Parse(project.Links["DeploymentProcess"]) path, _ := template.Expand(map[string]interface{}{"gitRef": gitRef}) - resp, err := services.ApiGet(s.GetClient(), new(DeploymentProcess), path) + resp, err := api.ApiGet(s.GetClient(), new(DeploymentProcess), path) if err != nil { return nil, err } @@ -69,7 +70,7 @@ func (s *DeploymentProcessService) GetTemplate(deploymentProcess *DeploymentProc path, _ := template.Expand(values) - resp, err := services.ApiGet(s.GetClient(), new(DeploymentProcessTemplate), path) + resp, err := api.ApiGet(s.GetClient(), new(DeploymentProcessTemplate), path) if err != nil { return nil, err } @@ -100,7 +101,7 @@ func (s *DeploymentProcessService) GetByID(id string) (*DeploymentProcess, error return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(DeploymentProcess), path) + resp, err := api.ApiGet(s.GetClient(), new(DeploymentProcess), path) if err != nil { return nil, err } diff --git a/pkg/deployments/deployment_service.go b/pkg/deployments/deployment_service.go index 77f1dc39..ef368605 100644 --- a/pkg/deployments/deployment_service.go +++ b/pkg/deployments/deployment_service.go @@ -6,6 +6,7 @@ import ( "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/pkg/services/api" "github.com/OctopusDeploy/go-octopusdeploy/v2/uritemplates" "github.com/dghubble/sling" ) @@ -57,7 +58,7 @@ func (s *DeploymentService) GetByID(id string) (*Deployment, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(Deployment), path) + resp, err := api.ApiGet(s.GetClient(), new(Deployment), path) if err != nil { return nil, err } @@ -108,35 +109,35 @@ func (s *DeploymentService) Update(resource Deployment) (*Deployment, error) { return resp.(*Deployment), nil } -func (s *DeploymentService) GetDeployments(release *releases.Release, deploymentQuery ...*DeploymentQuery) (*resources.Resources[Deployment], 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 &resources.Resources[Deployment]{}, err + return &resources.Resources[*Deployment]{}, err } values := make(map[string]interface{}) path, err := uriTemplate.Expand(values) if err != nil { - return &resources.Resources[Deployment]{}, err + return &resources.Resources[*Deployment]{}, err } if deploymentQuery != nil { path, err = uriTemplate.Expand(deploymentQuery[0]) if err != nil { - return &resources.Resources[Deployment]{}, err + return &resources.Resources[*Deployment]{}, err } } - resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[Deployment]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Deployment]), path) if err != nil { - return &resources.Resources[Deployment]{}, err + return &resources.Resources[*Deployment]{}, err } - return resp.(*resources.Resources[Deployment]), nil + return resp.(*resources.Resources[*Deployment]), nil } func (s *DeploymentService) GetProgression(release *releases.Release) (*releases.Progression, error) { @@ -145,7 +146,7 @@ func (s *DeploymentService) GetProgression(release *releases.Release) (*releases } path := release.GetLinks()[constants.LinkProgression] - resp, err := services.ApiGet(s.GetClient(), new(releases.Progression), path) + resp, err := api.ApiGet(s.GetClient(), new(releases.Progression), path) if err != nil { return nil, err } diff --git a/pkg/environments/environment_service.go b/pkg/environments/environment_service.go index 7857a8a9..145c5191 100644 --- a/pkg/environments/environment_service.go +++ b/pkg/environments/environment_service.go @@ -5,6 +5,7 @@ import ( "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/services/api" "github.com/dghubble/sling" ) @@ -49,18 +50,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) (*resources.Resources[Environment], error) { +func (s *EnvironmentService) Get(environmentsQuery EnvironmentsQuery) (*resources.Resources[*Environment], error) { path, err := s.GetURITemplate().Expand(environmentsQuery) if err != nil { - return &resources.Resources[Environment]{}, err + return &resources.Resources[*Environment]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Environment]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Environment]), path) if err != nil { - return &resources.Resources[Environment]{}, err + return &resources.Resources[*Environment]{}, err } - return response.(*resources.Resources[Environment]), nil + return response.(*resources.Resources[*Environment]), nil } // GetAll returns all environments. If none can be found or an error occurs, it @@ -72,7 +73,7 @@ func (s *EnvironmentService) GetAll() ([]*Environment, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -88,7 +89,7 @@ func (s *EnvironmentService) GetByID(id string) (*Environment, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(Environment), path) + resp, err := api.ApiGet(s.GetClient(), new(Environment), path) if err != nil { return nil, err } diff --git a/pkg/events/event_service.go b/pkg/events/event_service.go index ee041371..17fb168a 100644 --- a/pkg/events/event_service.go +++ b/pkg/events/event_service.go @@ -6,6 +6,7 @@ import ( "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/services/api" "github.com/OctopusDeploy/go-octopusdeploy/v2/uritemplates" "github.com/dghubble/sling" ) @@ -39,22 +40,22 @@ 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) (*resources.Resources[Event], error) { +func (s EventService) Get(query EventsQuery) (*resources.Resources[*Event], error) { path, err := s.GetURITemplate().Expand(query) if err != nil { - return &resources.Resources[Event]{}, err + return &resources.Resources[*Event]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Event]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Event]), path) if err != nil { - return &resources.Resources[Event]{}, err + return &resources.Resources[*Event]{}, err } - return response.(*resources.Resources[Event]), nil + return response.(*resources.Resources[*Event]), nil } func (s EventService) GetAgents() (*[]EventAgent, error) { - resp, err := services.ApiGet(s.GetClient(), new([]EventAgent), s.agentsPath.String()) + resp, err := api.ApiGet(s.GetClient(), new([]EventAgent), s.agentsPath.String()) if err != nil { return nil, err } @@ -68,7 +69,7 @@ func (s EventService) GetCategories(query EventCategoriesQuery) (*[]EventCategor return &[]EventCategory{}, err } - resp, err := services.ApiGet(s.GetClient(), new([]EventCategory), path) + resp, err := api.ApiGet(s.GetClient(), new([]EventCategory), path) if err != nil { return nil, err } @@ -77,7 +78,7 @@ func (s EventService) GetCategories(query EventCategoriesQuery) (*[]EventCategor } func (s EventService) GetDocumentTypes() (*[]DocumentType, error) { - resp, err := services.ApiGet(s.GetClient(), new([]DocumentType), s.documentTypesPath.String()) + resp, err := api.ApiGet(s.GetClient(), new([]DocumentType), s.documentTypesPath.String()) if err != nil { return nil, err } @@ -91,7 +92,7 @@ func (s EventService) GetGroups(query EventGroupsQuery) (*[]EventGroup, error) { return &[]EventGroup{}, err } - resp, err := services.ApiGet(s.GetClient(), new([]EventGroup), path) + resp, err := api.ApiGet(s.GetClient(), new([]EventGroup), path) if err != nil { return nil, err } diff --git a/pkg/feeds/feed_service.go b/pkg/feeds/feed_service.go index f5aa8e74..c02ca24d 100644 --- a/pkg/feeds/feed_service.go +++ b/pkg/feeds/feed_service.go @@ -6,6 +6,7 @@ import ( "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/pkg/services/api" "github.com/OctopusDeploy/go-octopusdeploy/v2/uritemplates" "github.com/dghubble/sling" "github.com/google/go-querystring/query" @@ -59,12 +60,12 @@ func (s *FeedService) Get(feedsQuery FeedsQuery) (*Feeds, error) { path += "?" + encodedQueryString } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[FeedResource]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*FeedResource]), path) if err != nil { return &Feeds{}, err } - return ToFeeds(response.(*resources.Resources[FeedResource])), nil + return ToFeeds(response.(*resources.Resources[*FeedResource])), nil } // GetAll returns all feeds. If none can be found or an error occurs, it @@ -76,7 +77,7 @@ func (s *FeedService) GetAll() ([]IFeed, error) { return ToFeedArray(items), err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return ToFeedArray(items), err } @@ -88,7 +89,7 @@ func (s *FeedService) GetByID(id string) (IFeed, error) { } path := s.BasePath + "/" + id - resp, err := services.ApiGet(s.GetClient(), new(FeedResource), path) + resp, err := api.ApiGet(s.GetClient(), new(FeedResource), path) if err != nil { return nil, err } @@ -99,7 +100,7 @@ func (s *FeedService) GetByID(id string) (IFeed, error) { // GetBuiltInFeedStatistics returns statistics for the built-in feeds. func (s *FeedService) GetBuiltInFeedStatistics() (*BuiltInFeedStatistics, error) { path := s.builtInFeedStats - resp, err := services.ApiGet(s.GetClient(), new(BuiltInFeedStatistics), path) + resp, err := api.ApiGet(s.GetClient(), new(BuiltInFeedStatistics), path) if err != nil { return nil, err } @@ -107,50 +108,50 @@ func (s *FeedService) GetBuiltInFeedStatistics() (*BuiltInFeedStatistics, error) return resp.(*BuiltInFeedStatistics), nil } -func (s *FeedService) SearchPackageVersions(packageDescription *packages.PackageDescription, searchPackageVersionsQuery SearchPackageVersionsQuery) (*resources.Resources[packages.PackageVersion], 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 &resources.Resources[packages.PackageVersion]{}, err + return &resources.Resources[*packages.PackageVersion]{}, err } path, err := uriTemplate.Expand(searchPackageVersionsQuery) if err != nil { - return &resources.Resources[packages.PackageVersion]{}, err + return &resources.Resources[*packages.PackageVersion]{}, err } - resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[packages.PackageVersion]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*packages.PackageVersion]), path) if err != nil { - return &resources.Resources[packages.PackageVersion]{}, err + return &resources.Resources[*packages.PackageVersion]{}, err } - return resp.(*resources.Resources[packages.PackageVersion]), nil + return resp.(*resources.Resources[*packages.PackageVersion]), nil } -func (s *FeedService) SearchPackages(feed IFeed, searchPackagesQuery SearchPackagesQuery) (*resources.Resources[packages.PackageDescription], 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 &resources.Resources[packages.PackageDescription]{}, err + return &resources.Resources[*packages.PackageDescription]{}, err } path, err := uriTemplate.Expand(searchPackagesQuery) if err != nil { - return &resources.Resources[packages.PackageDescription]{}, err + return &resources.Resources[*packages.PackageDescription]{}, err } - resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[packages.PackageDescription]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*packages.PackageDescription]), path) if err != nil { - return &resources.Resources[packages.PackageDescription]{}, err + return &resources.Resources[*packages.PackageDescription]{}, err } - return resp.(*resources.Resources[packages.PackageDescription]), 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 9d9cfc7a..dc4b069a 100644 --- a/pkg/feeds/feed_utilities.go +++ b/pkg/feeds/feed_utilities.go @@ -97,7 +97,7 @@ func ToFeed(feedResource *FeedResource) (IFeed, error) { return feed, nil } -func ToFeeds(feedResources *resources.Resources[FeedResource]) *Feeds { +func ToFeeds(feedResources *resources.Resources[*FeedResource]) *Feeds { return &Feeds{ Items: ToFeedArray(feedResources.Items), PagedResults: feedResources.PagedResults, diff --git a/pkg/interruptions/interruption_service.go b/pkg/interruptions/interruption_service.go index 61174f53..34667217 100644 --- a/pkg/interruptions/interruption_service.go +++ b/pkg/interruptions/interruption_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/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/users" "github.com/dghubble/sling" ) @@ -30,7 +31,7 @@ func (s *InterruptionService) GetByID(id string) (*Interruption, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(Interruption), path) + resp, err := api.ApiGet(s.GetClient(), new(Interruption), path) if err != nil { return nil, err } @@ -61,7 +62,7 @@ func (s *InterruptionService) GetAll() ([]*Interruption, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -81,7 +82,7 @@ func (s *InterruptionService) Submit(resource *Interruption, r *InterruptionSubm func (s InterruptionService) GetResponsibility(resource *Interruption) (*users.User, error) { path := resource.Links[constants.LinkResponsible] - resp, err := services.ApiGet(s.GetClient(), new(users.User), path) + resp, err := api.ApiGet(s.GetClient(), new(users.User), path) if err != nil { return nil, err } diff --git a/pkg/lifecycles/lifecycle_service.go b/pkg/lifecycles/lifecycle_service.go index 99834642..ce8b0735 100644 --- a/pkg/lifecycles/lifecycle_service.go +++ b/pkg/lifecycles/lifecycle_service.go @@ -6,6 +6,7 @@ import ( "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/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/dghubble/sling" ) @@ -43,18 +44,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) (*resources.Resources[Lifecycle], error) { +func (s *LifecycleService) Get(lifecyclesQuery Query) (*resources.Resources[*Lifecycle], error) { path, err := s.GetURITemplate().Expand(lifecyclesQuery) if err != nil { - return &resources.Resources[Lifecycle]{}, err + return &resources.Resources[*Lifecycle]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Lifecycle]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Lifecycle]), path) if err != nil { - return &resources.Resources[Lifecycle]{}, err + return &resources.Resources[*Lifecycle]{}, err } - return response.(*resources.Resources[Lifecycle]), nil + return response.(*resources.Resources[*Lifecycle]), nil } // GetAll returns all lifecycles. If none can be found or an error occurs, it @@ -66,7 +67,7 @@ func (s *LifecycleService) GetAll() ([]*Lifecycle, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -82,7 +83,7 @@ func (s *LifecycleService) GetByID(id string) (*Lifecycle, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(Lifecycle), path) + resp, err := api.ApiGet(s.GetClient(), new(Lifecycle), path) if err != nil { return nil, err } @@ -113,7 +114,7 @@ func (s *LifecycleService) GetProjects(lifecycle *Lifecycle) ([]*projects.Projec } path := lifecycle.Links["Projects"] - resp, err := services.ApiGet(s.GetClient(), new([]*projects.Project), path) + resp, err := api.ApiGet(s.GetClient(), new([]*projects.Project), path) if err != nil { return items, err } diff --git a/pkg/machines/machine_policy_service.go b/pkg/machines/machine_policy_service.go index 27790f6d..f535959a 100644 --- a/pkg/machines/machine_policy_service.go +++ b/pkg/machines/machine_policy_service.go @@ -5,6 +5,7 @@ import ( "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/services/api" "github.com/dghubble/sling" ) @@ -45,18 +46,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) (*resources.Resources[MachinePolicy], error) { +func (s *MachinePolicyService) Get(machinePoliciesQuery MachinePoliciesQuery) (*resources.Resources[*MachinePolicy], error) { path, err := s.GetURITemplate().Expand(machinePoliciesQuery) if err != nil { - return &resources.Resources[MachinePolicy]{}, err + return &resources.Resources[*MachinePolicy]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[MachinePolicy]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*MachinePolicy]), path) if err != nil { - return &resources.Resources[MachinePolicy]{}, err + return &resources.Resources[*MachinePolicy]{}, err } - return response.(*resources.Resources[MachinePolicy]), nil + return response.(*resources.Resources[*MachinePolicy]), nil } // GetAll returns all machine policies. If none can be found or an error @@ -68,7 +69,7 @@ func (s *MachinePolicyService) GetAll() ([]*MachinePolicy, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -84,7 +85,7 @@ func (s *MachinePolicyService) GetByID(id string) (*MachinePolicy, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(MachinePolicy), path) + resp, err := api.ApiGet(s.GetClient(), new(MachinePolicy), path) if err != nil { return nil, err } @@ -108,7 +109,7 @@ func (s *MachinePolicyService) GetByPartialName(partialName string) ([]*MachineP } func (s *MachinePolicyService) GetTemplate() (*MachinePolicy, error) { - resp, err := services.ApiGet(s.GetClient(), new(MachinePolicy), s.templatePath) + resp, err := api.ApiGet(s.GetClient(), new(MachinePolicy), s.templatePath) if err != nil { return nil, err } diff --git a/pkg/machines/machine_service.go b/pkg/machines/machine_service.go index 67931105..bd50d4ac 100644 --- a/pkg/machines/machine_service.go +++ b/pkg/machines/machine_service.go @@ -5,6 +5,7 @@ import ( "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/services/api" "github.com/dghubble/sling" ) @@ -49,18 +50,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) (*resources.Resources[DeploymentTarget], error) { +func (s *MachineService) Get(machinesQuery MachinesQuery) (*resources.Resources[*DeploymentTarget], error) { path, err := s.GetURITemplate().Expand(machinesQuery) if err != nil { - return &resources.Resources[DeploymentTarget]{}, err + return &resources.Resources[*DeploymentTarget]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[DeploymentTarget]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*DeploymentTarget]), path) if err != nil { - return &resources.Resources[DeploymentTarget]{}, err + return &resources.Resources[*DeploymentTarget]{}, err } - return response.(*resources.Resources[DeploymentTarget]), nil + return response.(*resources.Resources[*DeploymentTarget]), nil } // GetByID returns the machine that matches the input ID. If one cannot be @@ -75,7 +76,7 @@ func (s *MachineService) GetByID(id string) (*DeploymentTarget, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(DeploymentTarget), path) + resp, err := api.ApiGet(s.GetClient(), new(DeploymentTarget), path) if err != nil { return nil, err } @@ -92,7 +93,7 @@ func (s *MachineService) GetAll() ([]*DeploymentTarget, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } diff --git a/pkg/machines/worker_service.go b/pkg/machines/worker_service.go index 592ab143..a588c39f 100644 --- a/pkg/machines/worker_service.go +++ b/pkg/machines/worker_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/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/dghubble/sling" ) @@ -48,7 +49,7 @@ func (s *WorkerService) Add(worker *Worker) (*Worker, error) { // TODO: validation implementation // func (s workerService) DiscoverWorker() ([]string, error) { -// resp, err := services.ApiGet(s.GetClient(), new([]string), s.discoverWorkerPath) +// resp, err := api.ApiGet(s.GetClient(), new([]string), s.discoverWorkerPath) // if err != nil { // return nil, err // } @@ -66,7 +67,7 @@ func (s *WorkerService) GetAll() ([]*Worker, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -82,7 +83,7 @@ func (s *WorkerService) GetByID(id string) (*Worker, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(Worker), path) + resp, err := api.ApiGet(s.GetClient(), new(Worker), path) if err != nil { return nil, err } @@ -153,7 +154,7 @@ func (s *WorkerService) Update(worker *Worker) (*Worker, error) { } func (s *WorkerService) GetWorkerOperatingSystems() ([]string, error) { - resp, err := services.ApiGet(s.GetClient(), new([]string), s.operatingSystemsPath) + resp, err := api.ApiGet(s.GetClient(), new([]string), s.operatingSystemsPath) if err != nil { return nil, err } @@ -163,7 +164,7 @@ func (s *WorkerService) GetWorkerOperatingSystems() ([]string, error) { } func (s *WorkerService) GetWorkerShells() ([]string, error) { - resp, err := services.ApiGet(s.GetClient(), new([]string), s.shellsPath) + resp, err := api.ApiGet(s.GetClient(), new([]string), s.shellsPath) if err != nil { return nil, err } diff --git a/pkg/packages/package_service.go b/pkg/packages/package_service.go index dbbf4b66..a1f07412 100644 --- a/pkg/packages/package_service.go +++ b/pkg/packages/package_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/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/dghubble/sling" ) @@ -39,7 +40,7 @@ func (s *PackageService) GetAll() ([]*Package, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -55,7 +56,7 @@ func (s *PackageService) GetByID(id string) (*Package, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(Package), path) + resp, err := api.ApiGet(s.GetClient(), new(Package), path) if err != nil { return nil, err } diff --git a/pkg/projectgroups/project_group_service.go b/pkg/projectgroups/project_group_service.go index d145437f..a7f0bcac 100644 --- a/pkg/projectgroups/project_group_service.go +++ b/pkg/projectgroups/project_group_service.go @@ -6,6 +6,7 @@ import ( "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/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/dghubble/sling" ) @@ -45,18 +46,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) (*resources.Resources[ProjectGroup], error) { +func (s *ProjectGroupService) Get(projectGroupsQuery ProjectGroupsQuery) (*resources.Resources[*ProjectGroup], error) { path, err := s.GetURITemplate().Expand(projectGroupsQuery) if err != nil { - return &resources.Resources[ProjectGroup]{}, err + return &resources.Resources[*ProjectGroup]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[ProjectGroup]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*ProjectGroup]), path) if err != nil { - return &resources.Resources[ProjectGroup]{}, err + return &resources.Resources[*ProjectGroup]{}, err } - return response.(*resources.Resources[ProjectGroup]), nil + return response.(*resources.Resources[*ProjectGroup]), nil } // GetAll returns all project groups. If none can be found or an error occurs, @@ -68,7 +69,7 @@ func (s *ProjectGroupService) GetAll() ([]*ProjectGroup, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -84,7 +85,7 @@ func (s *ProjectGroupService) GetByID(id string) (*ProjectGroup, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(ProjectGroup), path) + resp, err := api.ApiGet(s.GetClient(), new(ProjectGroup), path) if err != nil { return nil, err } @@ -119,12 +120,12 @@ func (s *ProjectGroupService) GetProjects(projectGroup *ProjectGroup) ([]*projec loadNextPage := true for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[projects.Project]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*projects.Project]), path) if err != nil { return projectsToReturn, err } - projectList := resp.(*resources.Resources[projects.Project]) + 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 db7c6c16..1a9e4c9a 100644 --- a/pkg/projects/project.go +++ b/pkg/projects/project.go @@ -41,8 +41,8 @@ type Project struct { resources.Resource } -func NewProjects() *resources.Resources[Project] { - return &resources.Resources[Project]{ +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 ab9f78dc..6787f3c7 100644 --- a/pkg/projects/project_service.go +++ b/pkg/projects/project_service.go @@ -12,6 +12,7 @@ import ( "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/pkg/services/api" "github.com/OctopusDeploy/go-octopusdeploy/v2/uritemplates" "github.com/dghubble/sling" "github.com/google/go-querystring/query" @@ -92,7 +93,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) (*resources.Resources[Project], error) { +func (s *ProjectService) Get(projectsQuery ProjectsQuery) (*resources.Resources[*Project], error) { v, _ := query.Values(projectsQuery) path := s.BasePath encodedQueryString := v.Encode() @@ -100,12 +101,12 @@ func (s *ProjectService) Get(projectsQuery ProjectsQuery) (*resources.Resources[ path += "?" + encodedQueryString } - resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[Project]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Project]), path) if err != nil { - return &resources.Resources[Project]{}, err + return &resources.Resources[*Project]{}, err } - return resp.(*resources.Resources[Project]), nil + return resp.(*resources.Resources[*Project]), nil } // GetAll returns all projects. If none can be found or an error occurs, it @@ -117,7 +118,7 @@ func (s *ProjectService) GetAll() ([]*Project, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -133,7 +134,7 @@ func (s *ProjectService) GetByID(id string) (*Project, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(Project), path) + resp, err := api.ApiGet(s.GetClient(), new(Project), path) if err != nil { return nil, err } @@ -147,7 +148,7 @@ func (s *ProjectService) GetProject(channel *channels.Channel) (*Project, error) } path := channel.GetLinks()[constants.LinkProjects] - resp, err := services.ApiGet(s.GetClient(), new(Project), path) + resp, err := api.ApiGet(s.GetClient(), new(Project), path) if err != nil { return nil, err } @@ -176,13 +177,13 @@ func (s *ProjectService) GetChannels(project *Project) ([]*channels.Channel, err loadNextPage := true for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[channels.Channel]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*channels.Channel]), path) if err != nil { return projectChannels, err } - r := resp.(*resources.Resources[channels.Channel]) + r := resp.(*resources.Resources[*channels.Channel]) projectChannels = append(projectChannels, r.Items...) path, loadNextPage = services.LoadNextPage(r.PagedResults) } @@ -200,7 +201,7 @@ func (s *ProjectService) GetSummary(project *Project) (*ProjectSummary, error) { } path := project.Links[constants.LinkSummary] - resp, err := services.ApiGet(s.GetClient(), new(ProjectSummary), path) + resp, err := api.ApiGet(s.GetClient(), new(ProjectSummary), path) if err != nil { return nil, err } @@ -229,12 +230,12 @@ func (s *ProjectService) GetReleases(project *Project) ([]*releases.Release, err loadNextPage := true for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[releases.Release]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*releases.Release]), path) if err != nil { return nil, err } - r := resp.(*resources.Resources[releases.Release]) + r := resp.(*resources.Resources[*releases.Release]) p = append(p, r.Items...) path, loadNextPage = services.LoadNextPage(r.PagedResults) } diff --git a/pkg/releases/release_service.go b/pkg/releases/release_service.go index fc347f53..fd9ed756 100644 --- a/pkg/releases/release_service.go +++ b/pkg/releases/release_service.go @@ -6,6 +6,7 @@ import ( "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/services/api" "github.com/OctopusDeploy/go-octopusdeploy/v2/uritemplates" "github.com/dghubble/sling" "github.com/google/go-querystring/query" @@ -45,7 +46,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) (*resources.Resources[Release], error) { +func (s *ReleaseService) Get(releasesQuery ...ReleasesQuery) (*resources.Resources[*Release], error) { v, _ := query.Values(releasesQuery[0]) path := s.BasePath encodedQueryString := v.Encode() @@ -53,12 +54,12 @@ func (s *ReleaseService) Get(releasesQuery ...ReleasesQuery) (*resources.Resourc path += "?" + encodedQueryString } - resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[Release]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Release]), path) if err != nil { - return &resources.Resources[Release]{}, err + return &resources.Resources[*Release]{}, err } - return resp.(*resources.Resources[Release]), nil + return resp.(*resources.Resources[*Release]), nil } func (s *ReleaseService) CreateV1(createReleaseV1 *CreateReleaseV1) (*CreateReleaseResponseV1, error) { @@ -85,7 +86,7 @@ func (s *ReleaseService) GetByID(id string) (*Release, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(Release), path) + resp, err := api.ApiGet(s.GetClient(), new(Release), path) if err != nil { return nil, err } @@ -93,33 +94,33 @@ func (s *ReleaseService) GetByID(id string) (*Release, error) { return resp.(*Release), nil } -func (s *ReleaseService) GetReleases(channel *channels.Channel, releaseQuery ...*ReleaseQuery) (*resources.Resources[Release], 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 &resources.Resources[Release]{}, err + return &resources.Resources[*Release]{}, err } values := make(map[string]interface{}) path, err := uriTemplate.Expand(values) if err != nil { - return &resources.Resources[Release]{}, err + return &resources.Resources[*Release]{}, err } if releaseQuery != nil { path, err = uriTemplate.Expand(releaseQuery[0]) if err != nil { - return &resources.Resources[Release]{}, err + return &resources.Resources[*Release]{}, err } } - resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[Release]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Release]), path) if err != nil { - return &resources.Resources[Release]{}, err + return &resources.Resources[*Release]{}, err } - return resp.(*resources.Resources[Release]), nil + return resp.(*resources.Resources[*Release]), nil } diff --git a/pkg/resources/resource.go b/pkg/resources/resource.go index 648fbd6f..42f336d1 100644 --- a/pkg/resources/resource.go +++ b/pkg/resources/resource.go @@ -19,11 +19,6 @@ type IResource interface { Validate() error } -type Resources[T any] struct { - Items []*T `json:"Items"` - PagedResults -} - type Resource struct { ID string `json:"Id,omitempty"` ModifiedBy string `json:"LastModifiedBy,omitempty"` diff --git a/pkg/resources/resources.go b/pkg/resources/resources.go new file mode 100644 index 00000000..6757530b --- /dev/null +++ b/pkg/resources/resources.go @@ -0,0 +1,48 @@ +package resources + +import ( + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" + "github.com/dghubble/sling" +) + +type IResources[T any] interface { + GetNextPage(client *sling.Sling) (*Resources[T], error) + GetAllPages(client *sling.Sling) ([]T, error) +} + +type Resources[T any] struct { + Items []T `json:"Items"` + PagedResults +} + +// GetNextPage retrives the next page from the links collection. If no next page +// exists it will return nill +func (r *Resources[T]) GetNextPage(client *sling.Sling) (*Resources[T], error) { + if r.Links.PageNext == "" { + return nil, nil + } + response, err := api.ApiGet(client, new(Resources[T]), r.Links.PageNext) + if err != nil { + return nil, err + } + return response.(*Resources[T]), nil +} + +// GetAllPages will retrive all remaining next pages in the link collection +// and return the result as list of concatenated Items; Including the items +// from the base Resource. +func (r *Resources[T]) GetAllPages(client *sling.Sling) ([]T, error) { + items := make([]T, 0) + res := r + var err error + for res != nil { + items = append(items, res.Items...) + res, err = res.GetNextPage(client) + if err != nil { + return nil, err + } + } + return items, nil +} + +var _ IResources[any] = &Resources[any]{} diff --git a/pkg/runbooks/runbook_process_service.go b/pkg/runbooks/runbook_process_service.go index 5bc57a80..4357b2dd 100644 --- a/pkg/runbooks/runbook_process_service.go +++ b/pkg/runbooks/runbook_process_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/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/dghubble/sling" ) @@ -29,7 +30,7 @@ func (s *RunbookProcessService) GetByID(id string) (*RunbookProcess, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(RunbookProcess), path) + resp, err := api.ApiGet(s.GetClient(), new(RunbookProcess), path) if err != nil { return nil, err } diff --git a/pkg/runbooks/runbook_service.go b/pkg/runbooks/runbook_service.go index 8c633ac3..4f54fa0b 100644 --- a/pkg/runbooks/runbook_service.go +++ b/pkg/runbooks/runbook_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/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/dghubble/sling" ) @@ -51,7 +52,7 @@ func (s *RunbookService) GetAll() ([]*Runbook, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -67,7 +68,7 @@ func (s *RunbookService) GetByID(id string) (*Runbook, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(Runbook), path) + resp, err := api.ApiGet(s.GetClient(), new(Runbook), path) if err != nil { return nil, internal.CreateResourceNotFoundError("runbook", "ID", id) } @@ -76,7 +77,7 @@ func (s *RunbookService) GetByID(id string) (*Runbook, error) { } func (s *RunbookService) GetRunbookSnapshotTemplate(runbook *Runbook) (*RunbookSnapshotTemplate, error) { - resp, err := services.ApiGet(s.GetClient(), new(RunbookSnapshotTemplate), runbook.Links["RunbookSnapshotTemplate"]) + resp, err := api.ApiGet(s.GetClient(), new(RunbookSnapshotTemplate), runbook.Links["RunbookSnapshotTemplate"]) if err != nil { return nil, err } diff --git a/pkg/runbooks/runbook_snapshot_service.go b/pkg/runbooks/runbook_snapshot_service.go index a62de34b..7dacfd57 100644 --- a/pkg/runbooks/runbook_snapshot_service.go +++ b/pkg/runbooks/runbook_snapshot_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/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/dghubble/sling" ) @@ -54,7 +55,7 @@ func (s *RunbookSnapshotService) GetByID(id string) (*RunbookSnapshot, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(RunbookSnapshot), path) + resp, err := api.ApiGet(s.GetClient(), new(RunbookSnapshot), path) if err != nil { return nil, err } diff --git a/pkg/serverstatus/server_status_service.go b/pkg/serverstatus/server_status_service.go index 718ed6bd..acf5df7e 100644 --- a/pkg/serverstatus/server_status_service.go +++ b/pkg/serverstatus/server_status_service.go @@ -3,6 +3,7 @@ package serverstatus import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/dghubble/sling" ) @@ -30,7 +31,7 @@ func (s *ServerStatusService) Get() (*ServerStatus, error) { return nil, err } - response, err := services.ApiGet(s.GetClient(), new(ServerStatus), path) + response, err := api.ApiGet(s.GetClient(), new(ServerStatus), path) if err != nil { return nil, err } diff --git a/pkg/services/api/api.go b/pkg/services/api/api.go new file mode 100644 index 00000000..cc64982b --- /dev/null +++ b/pkg/services/api/api.go @@ -0,0 +1,61 @@ +package api + +import ( + "fmt" + "net/http" + "runtime" + "runtime/debug" + + "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/dghubble/sling" +) + +var version = "development" +var UserAgentString = GetUserAgentString() + +// Generic OctopusDeploy API Get Function. +func ApiGet(sling *sling.Sling, inputStruct interface{}, path string) (interface{}, error) { + if sling == nil { + return nil, internal.CreateInvalidParameterError(constants.OperationAPIGet, "sling") + } + + client := sling.New() + if client == nil { + return nil, internal.CreateClientInitializationError(constants.OperationAPIGet) + } + + client = client.Get(path) + if client == nil { + return nil, internal.CreateClientInitializationError(constants.OperationAPIGet) + } + + client.Set("User-Agent", UserAgentString) + + octopusDeployError := new(core.APIError) + resp, err := client.Receive(inputStruct, &octopusDeployError) + // if err != nil { + // return nil, err + // } + + apiErrorCheck := core.APIErrorChecker(path, resp, http.StatusOK, err, octopusDeployError) + if apiErrorCheck != nil { + return nil, apiErrorCheck + } + + return inputStruct, nil +} + +func GetUserAgentString() string { + if info, ok := debug.ReadBuildInfo(); ok { + for _, dep := range info.Deps { + if dep.Path == "github.com/OctopusDeploy/go-octopusdeploy/v2" { + if dep.Version != "" { + version = dep.Version + } + } + } + } + return fmt.Sprintf("%s/%s (%s; %s) go/%s", "go-octopusdeploy", version, runtime.GOOS, runtime.GOARCH, runtime.Version()) +} diff --git a/pkg/services/service.go b/pkg/services/service.go index 2e0cffe5..601bb13e 100644 --- a/pkg/services/service.go +++ b/pkg/services/service.go @@ -2,23 +2,18 @@ package services import ( "errors" - "fmt" "net/http" - "runtime" - "runtime/debug" "strings" "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/api" "github.com/OctopusDeploy/go-octopusdeploy/v2/uritemplates" "github.com/dghubble/sling" ) -var version = "development" -var UserAgentString = GetUserAgentString() - // IService defines the contract for all services that communicate with the // Octopus API. type IService interface { @@ -63,12 +58,12 @@ func GetPagedResponse[T any](s IService, path string) ([]*T, error) { loadNextPage := true for loadNextPage { - resp, err := ApiGet(s.GetClient(), new(resources.Resources[T]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*T]), path) if err != nil { return resourcesToReturn, err } - responseList := resp.(*resources.Resources[T]) + responseList := resp.(*resources.Resources[*T]) resourcesToReturn = append(resourcesToReturn, responseList.Items...) path, loadNextPage = LoadNextPage(responseList.PagedResults) } @@ -76,38 +71,6 @@ func GetPagedResponse[T any](s IService, path string) ([]*T, error) { return resourcesToReturn, nil } -// Generic OctopusDeploy API Get Function. -func ApiGet(sling *sling.Sling, inputStruct interface{}, path string) (interface{}, error) { - if sling == nil { - return nil, internal.CreateInvalidParameterError(constants.OperationAPIGet, "sling") - } - - client := sling.New() - if client == nil { - return nil, internal.CreateClientInitializationError(constants.OperationAPIGet) - } - - client = client.Get(path) - if client == nil { - return nil, internal.CreateClientInitializationError(constants.OperationAPIGet) - } - - client.Set("User-Agent", UserAgentString) - - octopusDeployError := new(core.APIError) - resp, err := client.Receive(inputStruct, &octopusDeployError) - // if err != nil { - // return nil, err - // } - - apiErrorCheck := core.APIErrorChecker(path, resp, http.StatusOK, err, octopusDeployError) - if apiErrorCheck != nil { - return nil, apiErrorCheck - } - - return inputStruct, nil -} - func (s *Service) GetBasePath() string { return s.BasePath } @@ -124,20 +87,6 @@ func (s *Service) GetPath() string { return s.Path } -// Returns the User-Agent String "go-octopusdeploy/version (os; arch) go/version" -func GetUserAgentString() string { - if info, ok := debug.ReadBuildInfo(); ok { - for _, dep := range info.Deps { - if dep.Path == "github.com/OctopusDeploy/go-octopusdeploy/v2" { - if dep.Version != "" { - version = dep.Version - } - } - } - } - return fmt.Sprintf("%s/%s (%s; %s) go/%s", "go-octopusdeploy", version, runtime.GOOS, runtime.GOARCH, runtime.Version()) -} - func (s *Service) GetURITemplate() *uritemplates.UriTemplate { return s.URITemplate } @@ -321,7 +270,7 @@ func ApiAdd(sling *sling.Sling, inputStruct interface{}, resource interface{}, p return nil, internal.CreateClientInitializationError(constants.OperationAPIAdd) } - client.Set("User-Agent", UserAgentString) + client.Set("User-Agent", api.UserAgentString) request := client.BodyJSON(inputStruct) if request == nil { @@ -358,7 +307,7 @@ func ApiAddWithResponseStatus(sling *sling.Sling, inputStruct interface{}, resou return nil, internal.CreateClientInitializationError(constants.OperationApiAddWithResponseStatus) } - client.Set("User-Agent", UserAgentString) + client.Set("User-Agent", api.UserAgentString) request := client.BodyJSON(inputStruct) if request == nil { @@ -395,7 +344,7 @@ func ApiPost(sling *sling.Sling, inputStruct interface{}, resource interface{}, return nil, internal.CreateClientInitializationError(constants.OperationAPIPost) } - client.Set("User-Agent", UserAgentString) + client.Set("User-Agent", api.UserAgentString) request := client.BodyJSON(inputStruct) if request == nil { @@ -432,7 +381,7 @@ func ApiUpdate(sling *sling.Sling, inputStruct interface{}, resource interface{} return nil, internal.CreateClientInitializationError(constants.OperationAPIUpdate) } - client.Set("User-Agent", UserAgentString) + client.Set("User-Agent", api.UserAgentString) request := client.BodyJSON(inputStruct) if request == nil { @@ -469,7 +418,7 @@ func ApiDelete(sling *sling.Sling, path string) error { return internal.CreateClientInitializationError(constants.OperationAPIDelete) } - client.Set("User-Agent", UserAgentString) + client.Set("User-Agent", api.UserAgentString) octopusDeployError := new(core.APIError) resp, err := client.Receive(nil, &octopusDeployError) diff --git a/pkg/spaces/space_service.go b/pkg/spaces/space_service.go index c081a5f2..6a910b8f 100644 --- a/pkg/spaces/space_service.go +++ b/pkg/spaces/space_service.go @@ -6,6 +6,7 @@ import ( "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/services/api" "github.com/dghubble/sling" ) @@ -46,18 +47,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) (*resources.Resources[Space], error) { +func (s *SpaceService) Get(spacesQuery SpacesQuery) (*resources.Resources[*Space], error) { path, err := s.GetURITemplate().Expand(spacesQuery) if err != nil { - return &resources.Resources[Space]{}, err + return &resources.Resources[*Space]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Space]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Space]), path) if err != nil { - return &resources.Resources[Space]{}, err + return &resources.Resources[*Space]{}, err } - return response.(*resources.Resources[Space]), nil + return response.(*resources.Resources[*Space]), nil } // GetByID returns the space that matches the input ID. If one cannot be found, @@ -72,7 +73,7 @@ func (s *SpaceService) GetByID(id string) (*Space, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(Space), path) + resp, err := api.ApiGet(s.GetClient(), new(Space), path) if err != nil { return nil, err } @@ -126,7 +127,7 @@ func (s *SpaceService) GetAll() ([]*Space, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } diff --git a/pkg/tagsets/tag_set_service.go b/pkg/tagsets/tag_set_service.go index 107f33c1..6f765db5 100644 --- a/pkg/tagsets/tag_set_service.go +++ b/pkg/tagsets/tag_set_service.go @@ -5,6 +5,7 @@ import ( "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/services/api" "github.com/dghubble/sling" ) @@ -45,18 +46,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) (*resources.Resources[TagSet], error) { +func (s *TagSetService) Get(tagSetsQuery TagSetsQuery) (*resources.Resources[*TagSet], error) { path, err := s.GetURITemplate().Expand(tagSetsQuery) if err != nil { - return &resources.Resources[TagSet]{}, err + return &resources.Resources[*TagSet]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[TagSet]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*TagSet]), path) if err != nil { - return &resources.Resources[TagSet]{}, err + return &resources.Resources[*TagSet]{}, err } - return response.(*resources.Resources[TagSet]), nil + return response.(*resources.Resources[*TagSet]), nil } // GetByID returns the tag set that matches the input ID. If one cannot be @@ -71,7 +72,7 @@ func (s *TagSetService) GetByID(id string) (*TagSet, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(TagSet), path) + resp, err := api.ApiGet(s.GetClient(), new(TagSet), path) if err != nil { return nil, err } @@ -88,7 +89,7 @@ func (s *TagSetService) GetAll() ([]*TagSet, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } diff --git a/pkg/tasks/task_service.go b/pkg/tasks/task_service.go index 4cbdc299..74fd42e3 100644 --- a/pkg/tasks/task_service.go +++ b/pkg/tasks/task_service.go @@ -5,6 +5,7 @@ import ( "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/services/api" "github.com/dghubble/sling" ) @@ -43,16 +44,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) (*resources.Resources[Task], error) { +func (s *TaskService) Get(tasksQuery TasksQuery) (*resources.Resources[*Task], error) { path, err := s.GetURITemplate().Expand(tasksQuery) if err != nil { - return &resources.Resources[Task]{}, err + return &resources.Resources[*Task]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Task]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Task]), path) if err != nil { - return &resources.Resources[Task]{}, err + return &resources.Resources[*Task]{}, err } - return response.(*resources.Resources[Task]), nil + return response.(*resources.Resources[*Task]), nil } diff --git a/pkg/teams/team_service.go b/pkg/teams/team_service.go index 1b4edc7d..4105a62e 100644 --- a/pkg/teams/team_service.go +++ b/pkg/teams/team_service.go @@ -6,6 +6,7 @@ import ( "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/services/api" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/userroles" "github.com/OctopusDeploy/go-octopusdeploy/v2/uritemplates" "github.com/dghubble/sling" @@ -61,18 +62,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) (*resources.Resources[Team], error) { +func (s *TeamService) Get(teamsQuery TeamsQuery) (*resources.Resources[*Team], error) { path, err := s.GetURITemplate().Expand(teamsQuery) if err != nil { - return &resources.Resources[Team]{}, err + return &resources.Resources[*Team]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Team]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Team]), path) if err != nil { - return &resources.Resources[Team]{}, err + return &resources.Resources[*Team]{}, err } - return response.(*resources.Resources[Team]), nil + return response.(*resources.Resources[*Team]), nil } // GetAll returns all teams. If none can be found or an error occurs, it @@ -84,7 +85,7 @@ func (s *TeamService) GetAll() ([]*Team, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -100,7 +101,7 @@ func (s *TeamService) GetByID(id string) (*Team, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(Team), path) + resp, err := api.ApiGet(s.GetClient(), new(Team), path) if err != nil { return nil, err } @@ -138,14 +139,14 @@ func (s *TeamService) Update(team *Team) (*Team, error) { return resp.(*Team), nil } -func (s *TeamService) GetScopedUserRoles(team Team, query core.SkipTakeQuery) (*resources.Resources[userroles.ScopedUserRole], 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(resources.Resources[userroles.ScopedUserRole]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*userroles.ScopedUserRole]), path) if err != nil { return nil, err } - return resp.(*resources.Resources[userroles.ScopedUserRole]), nil + return resp.(*resources.Resources[*userroles.ScopedUserRole]), nil } diff --git a/pkg/tenants/tenant_service.go b/pkg/tenants/tenant_service.go index 34b838f6..f175304e 100644 --- a/pkg/tenants/tenant_service.go +++ b/pkg/tenants/tenant_service.go @@ -5,6 +5,7 @@ import ( "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/services/api" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/variables" "github.com/OctopusDeploy/go-octopusdeploy/v2/uritemplates" "github.com/dghubble/sling" @@ -76,18 +77,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) (*resources.Resources[Tenant], error) { +func (s *TenantService) Get(tenantsQuery TenantsQuery) (*resources.Resources[*Tenant], error) { path, err := s.GetURITemplate().Expand(tenantsQuery) if err != nil { - return &resources.Resources[Tenant]{}, err + return &resources.Resources[*Tenant]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[Tenant]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*Tenant]), path) if err != nil { - return &resources.Resources[Tenant]{}, err + return &resources.Resources[*Tenant]{}, err } - return response.(*resources.Resources[Tenant]), nil + return response.(*resources.Resources[*Tenant]), nil } // GetAll returns all tenants. If none can be found or an error occurs, it @@ -99,7 +100,7 @@ func (s *TenantService) GetAll() ([]*Tenant, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -115,7 +116,7 @@ func (s *TenantService) GetByID(id string) (*Tenant, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(Tenant), path) + resp, err := api.ApiGet(s.GetClient(), new(Tenant), path) if err != nil { return nil, err } @@ -148,7 +149,7 @@ func (s *TenantService) GetMissingVariables(missibleVariablesQuery variables.Mis return &[]variables.TenantsMissingVariables{}, err } - response, err := services.ApiGet(s.GetClient(), new([]variables.TenantsMissingVariables), path) + response, err := api.ApiGet(s.GetClient(), new([]variables.TenantsMissingVariables), path) if err != nil { return &[]variables.TenantsMissingVariables{}, err } @@ -183,7 +184,7 @@ func (s *TenantService) GetByPartialName(partialName string) ([]*Tenant, error) } func (s *TenantService) GetVariables(tenant *Tenant) (*variables.TenantVariables, error) { - resp, err := services.ApiGet(s.GetClient(), new(variables.TenantVariables), tenant.Links["Variables"]) + resp, err := api.ApiGet(s.GetClient(), new(variables.TenantVariables), tenant.Links["Variables"]) if err != nil { return nil, err } diff --git a/pkg/triggers/project_trigger_service.go b/pkg/triggers/project_trigger_service.go index 8a577714..e15ba61b 100644 --- a/pkg/triggers/project_trigger_service.go +++ b/pkg/triggers/project_trigger_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/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/dghubble/sling" ) @@ -31,7 +32,7 @@ func (s *ProjectTriggerService) GetByID(id string) (*ProjectTrigger, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(ProjectTrigger), path) + resp, err := api.ApiGet(s.GetClient(), new(ProjectTrigger), path) if err != nil { return nil, err } diff --git a/pkg/userroles/scoped_user_role_service.go b/pkg/userroles/scoped_user_role_service.go index 4f16cce9..580566e4 100644 --- a/pkg/userroles/scoped_user_role_service.go +++ b/pkg/userroles/scoped_user_role_service.go @@ -5,6 +5,7 @@ import ( "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/services/api" "github.com/dghubble/sling" ) @@ -43,14 +44,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() (*resources.Resources[ScopedUserRole], error) { +func (s *ScopedUserRoleService) Get() (*resources.Resources[*ScopedUserRole], error) { path := s.BasePath - resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[ScopedUserRole]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*ScopedUserRole]), path) if err != nil { - return &resources.Resources[ScopedUserRole]{}, err + return &resources.Resources[*ScopedUserRole]{}, err } - return resp.(*resources.Resources[ScopedUserRole]), nil + return resp.(*resources.Resources[*ScopedUserRole]), nil } func (s *ScopedUserRoleService) GetByID(id string) (*ScopedUserRole, error) { @@ -63,7 +64,7 @@ func (s *ScopedUserRoleService) GetByID(id string) (*ScopedUserRole, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(ScopedUserRole), path) + resp, err := api.ApiGet(s.GetClient(), new(ScopedUserRole), path) if err != nil { return nil, err } diff --git a/pkg/userroles/user_role_service.go b/pkg/userroles/user_role_service.go index e17dc4a4..5b9eccf3 100644 --- a/pkg/userroles/user_role_service.go +++ b/pkg/userroles/user_role_service.go @@ -5,6 +5,7 @@ import ( "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/services/api" "github.com/dghubble/sling" ) @@ -42,18 +43,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) (*resources.Resources[UserRole], error) { +func (s *UserRoleService) Get(userRolesQuery UserRolesQuery) (*resources.Resources[*UserRole], error) { path, err := s.GetURITemplate().Expand(userRolesQuery) if err != nil { - return &resources.Resources[UserRole]{}, err + return &resources.Resources[*UserRole]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[UserRole]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*UserRole]), path) if err != nil { - return &resources.Resources[UserRole]{}, err + return &resources.Resources[*UserRole]{}, err } - return response.(*resources.Resources[UserRole]), nil + return response.(*resources.Resources[*UserRole]), nil } // GetAll returns all user roles. If none can be found or an error occurs, it @@ -65,7 +66,7 @@ func (s *UserRoleService) GetAll() ([]*UserRole, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -81,7 +82,7 @@ func (s *UserRoleService) GetByID(id string) (*UserRole, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(UserRole), path) + resp, err := api.ApiGet(s.GetClient(), new(UserRole), path) if err != nil { return nil, internal.CreateResourceNotFoundError("userRole", "ID", id) } diff --git a/pkg/users/api_key_service.go b/pkg/users/api_key_service.go index cd280e57..10637bd9 100644 --- a/pkg/users/api_key_service.go +++ b/pkg/users/api_key_service.go @@ -7,6 +7,7 @@ import ( "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/services/api" "github.com/dghubble/sling" ) @@ -40,12 +41,12 @@ func (s *ApiKeyService) GetByUserID(userID string) ([]*APIKey, error) { loadNextPage := true for loadNextPage { - resp, err := services.ApiGet(s.GetClient(), new(resources.Resources[APIKey]), path) + resp, err := api.ApiGet(s.GetClient(), new(resources.Resources[*APIKey]), path) if err != nil { return nil, err } - r := resp.(*resources.Resources[APIKey]) + r := resp.(*resources.Resources[*APIKey]) p = append(p, r.Items...) path, loadNextPage = services.LoadNextPage(r.PagedResults) } @@ -70,7 +71,7 @@ func (s *ApiKeyService) GetByID(userID string, apiKeyID string) (*APIKey, error) path := internal.TrimTemplate(s.GetPath()) path = fmt.Sprintf(path+"%s/apikeys/%s", userID, apiKeyID) - resp, err := services.ApiGet(s.GetClient(), new(APIKey), path) + resp, err := api.ApiGet(s.GetClient(), new(APIKey), path) if err != nil { return nil, err } diff --git a/pkg/users/user_service.go b/pkg/users/user_service.go index 3101f50f..85c90e6b 100644 --- a/pkg/users/user_service.go +++ b/pkg/users/user_service.go @@ -6,6 +6,7 @@ import ( "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/services/api" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/spaces" "github.com/dghubble/sling" "github.com/google/go-querystring/query" @@ -82,7 +83,7 @@ func (s *UserService) GetAll() ([]*User, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -95,7 +96,7 @@ func (s *UserService) GetAPIKeyByID(user *User, apiKeyID string) (*APIKey, error path := internal.TrimTemplate(user.Links[constants.LinkAPIKeys]) + "/" + apiKeyID - response, err := services.ApiGet(s.GetClient(), new(APIKey), path) + response, err := api.ApiGet(s.GetClient(), new(APIKey), path) if err != nil { return nil, err } @@ -103,7 +104,7 @@ func (s *UserService) GetAPIKeyByID(user *User, apiKeyID string) (*APIKey, error return response.(*APIKey), nil } -func (s *UserService) GetAPIKeys(user *User, apiQuery ...APIQuery) (*resources.Resources[APIKey], error) { +func (s *UserService) GetAPIKeys(user *User, apiQuery ...APIQuery) (*resources.Resources[*APIKey], error) { if user == nil { return nil, internal.CreateInvalidParameterError(constants.OperationGetAPIKeys, constants.ParameterUser) } @@ -127,17 +128,17 @@ func (s *UserService) GetAPIKeys(user *User, apiQuery ...APIQuery) (*resources.R path += "?" + encodedQueryString } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[APIKey]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*APIKey]), path) if err != nil { return nil, err } - return response.(*resources.Resources[APIKey]), nil + return response.(*resources.Resources[*APIKey]), nil } func (s *UserService) GetAuthentication() (*UserAuthentication, error) { path := internal.TrimTemplate(s.userAuthenticationPath) - resp, err := services.ApiGet(s.GetClient(), new(UserAuthentication), path) + resp, err := api.ApiGet(s.GetClient(), new(UserAuthentication), path) if err != nil { return nil, err } @@ -152,7 +153,7 @@ func (s *UserService) GetAuthenticationByUser(user *User) (*UserAuthentication, path := internal.TrimTemplate(s.userAuthenticationPath) + "/" + user.GetID() - resp, err := services.ApiGet(s.GetClient(), new(UserAuthentication), path) + resp, err := api.ApiGet(s.GetClient(), new(UserAuthentication), path) if err != nil { return nil, err } @@ -163,18 +164,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) (*resources.Resources[User], error) { +func (s *UserService) Get(usersQuery UsersQuery) (*resources.Resources[*User], error) { path, err := s.GetURITemplate().Expand(usersQuery) if err != nil { - return &resources.Resources[User]{}, err + return &resources.Resources[*User]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[User]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*User]), path) if err != nil { - return &resources.Resources[User]{}, err + return &resources.Resources[*User]{}, err } - return response.(*resources.Resources[User]), nil + return response.(*resources.Resources[*User]), nil } // GetByID returns the user that matches the input ID. If one cannot be found, @@ -189,7 +190,7 @@ func (s *UserService) GetByID(id string) (*User, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(User), path) + resp, err := api.ApiGet(s.GetClient(), new(User), path) if err != nil { return nil, internal.CreateResourceNotFoundError("user", "ID", id) } @@ -206,7 +207,7 @@ func (s *UserService) GetMe() (*User, error) { path := internal.TrimTemplate(s.GetPath()) path = path + "/me" - resp, err := services.ApiGet(s.GetClient(), new(User), path) + resp, err := api.ApiGet(s.GetClient(), new(User), path) if err != nil { return nil, err } @@ -226,7 +227,7 @@ func (s *UserService) GetPermissions(user *User, userQuery ...UserQuery) (*permi path += "?" + encodedQueryString } - response, err := services.ApiGet(s.GetClient(), new(permissions.UserPermissionSet), path) + response, err := api.ApiGet(s.GetClient(), new(permissions.UserPermissionSet), path) return response.(*permissions.UserPermissionSet), err } @@ -242,7 +243,7 @@ func (s *UserService) GetPermissionsConfiguration(user *User, userQuery ...UserQ path += "?" + encodedQueryString } - response, err := services.ApiGet(s.GetClient(), new(permissions.UserPermissionSet), path) + response, err := api.ApiGet(s.GetClient(), new(permissions.UserPermissionSet), path) return response.(*permissions.UserPermissionSet), err } @@ -255,7 +256,7 @@ func (s *UserService) GetSpaces(user *User) ([]*spaces.Space, error) { path := internal.TrimTemplate(user.Links[constants.LinkSpaces]) items := []*spaces.Space{} - _, err := services.ApiGet(s.GetClient(), &items, path) + _, err := api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -271,7 +272,7 @@ func (s *UserService) GetTeams(user *User, userQuery ...UserQuery) (*[]permissio path += "?" + encodedQueryString } - response, err := services.ApiGet(s.GetClient(), new([]permissions.ProjectedTeamReferenceDataItem), path) + response, err := api.ApiGet(s.GetClient(), new([]permissions.ProjectedTeamReferenceDataItem), path) if err != nil { return nil, err } diff --git a/pkg/variables/library_variable_set_service.go b/pkg/variables/library_variable_set_service.go index 8458ea99..6e8078c3 100644 --- a/pkg/variables/library_variable_set_service.go +++ b/pkg/variables/library_variable_set_service.go @@ -5,6 +5,7 @@ import ( "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/services/api" "github.com/dghubble/sling" ) @@ -42,18 +43,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) (*resources.Resources[LibraryVariableSet], error) { +func (s *LibraryVariableSetService) Get(libraryVariablesQuery LibraryVariablesQuery) (*resources.Resources[*LibraryVariableSet], error) { path, err := s.GetURITemplate().Expand(libraryVariablesQuery) if err != nil { - return &resources.Resources[LibraryVariableSet]{}, err + return &resources.Resources[*LibraryVariableSet]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[LibraryVariableSet]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*LibraryVariableSet]), path) if err != nil { - return &resources.Resources[LibraryVariableSet]{}, err + return &resources.Resources[*LibraryVariableSet]{}, err } - return response.(*resources.Resources[LibraryVariableSet]), nil + return response.(*resources.Resources[*LibraryVariableSet]), nil } // GetAll returns all library variable sets. If none can be found or an error @@ -65,7 +66,7 @@ func (s *LibraryVariableSetService) GetAll() ([]*LibraryVariableSet, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } @@ -81,7 +82,7 @@ func (s *LibraryVariableSetService) GetByID(id string) (*LibraryVariableSet, err return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(LibraryVariableSet), path) + resp, err := api.ApiGet(s.GetClient(), new(LibraryVariableSet), path) if err != nil { return nil, err } diff --git a/pkg/variables/script_module_service.go b/pkg/variables/script_module_service.go index b2c0c7bf..6c3e7984 100644 --- a/pkg/variables/script_module_service.go +++ b/pkg/variables/script_module_service.go @@ -9,6 +9,7 @@ import ( "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/services/api" "github.com/dghubble/sling" ) @@ -44,7 +45,7 @@ func (s *ScriptModuleService) Add(scriptModule *ScriptModule) (*ScriptModule, er // update associated variable set; add script body and syntax variablesPath := scriptModuleResponse.Links[constants.LinkVariables] - variablesResponse, err := services.ApiGet(s.GetClient(), new(VariableSet), variablesPath) + variablesResponse, err := api.ApiGet(s.GetClient(), new(VariableSet), variablesPath) if err != nil { return nil, err } @@ -72,37 +73,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) (*resources.Resources[ScriptModule], error) { +func (s *ScriptModuleService) Get(libraryVariablesQuery LibraryVariablesQuery) (*resources.Resources[*ScriptModule], error) { path, err := s.GetURITemplate().Expand(libraryVariablesQuery) if err != nil { - return &resources.Resources[ScriptModule]{}, err + return &resources.Resources[*ScriptModule]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[ScriptModule]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*ScriptModule]), path) if err != nil { - return &resources.Resources[ScriptModule]{}, err + return &resources.Resources[*ScriptModule]{}, err } - return response.(*resources.Resources[ScriptModule]), 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() (*resources.Resources[ScriptModule], error) { +func (s *ScriptModuleService) GetAll() (*resources.Resources[*ScriptModule], error) { path, err := s.GetURITemplate().Expand(&LibraryVariablesQuery{ ContentType: "ScriptModule", Take: math.MaxInt32, }) if err != nil { - return &resources.Resources[ScriptModule]{}, err + return &resources.Resources[*ScriptModule]{}, err } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[ScriptModule]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*ScriptModule]), path) if err != nil { - return &resources.Resources[ScriptModule]{}, err + return &resources.Resources[*ScriptModule]{}, err } - return response.(*resources.Resources[ScriptModule]), nil + return response.(*resources.Resources[*ScriptModule]), nil } // GetByID returns the script module that matches the input ID. If one @@ -117,7 +118,7 @@ func (s *ScriptModuleService) GetByID(id string) (*ScriptModule, error) { return nil, err } - response, err := services.ApiGet(s.GetClient(), new(ScriptModule), path) + response, err := api.ApiGet(s.GetClient(), new(ScriptModule), path) if err != nil { return nil, err } @@ -126,7 +127,7 @@ func (s *ScriptModuleService) GetByID(id string) (*ScriptModule, error) { // get associated variable set variablesPath := scriptModuleResponse.Links[constants.LinkVariables] - variablesResponse, err := services.ApiGet(s.GetClient(), new(VariableSet), variablesPath) + variablesResponse, err := api.ApiGet(s.GetClient(), new(VariableSet), variablesPath) if err != nil { return nil, err } @@ -180,7 +181,7 @@ func (s *ScriptModuleService) Update(scriptModule *ScriptModule) (*ScriptModule, // update associated variable set variablesPath := scriptModuleResponse.Links[constants.LinkVariables] - variablesResponse, err := services.ApiGet(s.GetClient(), new(VariableSet), variablesPath) + variablesResponse, err := api.ApiGet(s.GetClient(), new(VariableSet), variablesPath) if err != nil { return nil, err } diff --git a/pkg/variables/tenant_variable_service.go b/pkg/variables/tenant_variable_service.go index 9f780daf..7259f913 100644 --- a/pkg/variables/tenant_variable_service.go +++ b/pkg/variables/tenant_variable_service.go @@ -3,6 +3,7 @@ package variables import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/dghubble/sling" ) @@ -23,6 +24,6 @@ func (s *TenantVariableService) GetAll() ([]TenantVariables, error) { return items, err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return items, err } diff --git a/pkg/variables/variable_service.go b/pkg/variables/variable_service.go index 9f3bf663..5878a259 100644 --- a/pkg/variables/variable_service.go +++ b/pkg/variables/variable_service.go @@ -7,6 +7,7 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api" "github.com/dghubble/sling" ) @@ -46,7 +47,7 @@ func (s *VariableService) GetAll(ownerID string) (VariableSet, error) { path := internal.TrimTemplate(s.GetPath()) path = fmt.Sprintf(path+"/variableset-%s", ownerID) - resp, err := services.ApiGet(s.GetClient(), new(VariableSet), path) + resp, err := api.ApiGet(s.GetClient(), new(VariableSet), path) if err != nil { return VariableSet{}, err } diff --git a/pkg/workerpools/worker_pool_service.go b/pkg/workerpools/worker_pool_service.go index bfa4ea67..576b3245 100644 --- a/pkg/workerpools/worker_pool_service.go +++ b/pkg/workerpools/worker_pool_service.go @@ -5,6 +5,7 @@ import ( "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/services/api" "github.com/dghubble/sling" "github.com/google/go-querystring/query" ) @@ -60,12 +61,12 @@ func (s *WorkerPoolService) Get(workerPoolsQuery WorkerPoolsQuery) (*WorkerPools path += "?" + encodedQueryString } - response, err := services.ApiGet(s.GetClient(), new(resources.Resources[WorkerPoolResource]), path) + response, err := api.ApiGet(s.GetClient(), new(resources.Resources[*WorkerPoolResource]), path) if err != nil { return &WorkerPools{}, err } - return ToWorkerPools(response.(*resources.Resources[WorkerPoolResource])), nil + return ToWorkerPools(response.(*resources.Resources[*WorkerPoolResource])), nil } // GetAll returns all worker pools. If none can be found or an error occurs, it @@ -77,7 +78,7 @@ func (s *WorkerPoolService) GetAll() ([]IWorkerPool, error) { return ToWorkerPoolArray(items), err } - _, err = services.ApiGet(s.GetClient(), &items, path) + _, err = api.ApiGet(s.GetClient(), &items, path) return ToWorkerPoolArray(items), err } @@ -93,7 +94,7 @@ func (s *WorkerPoolService) GetByID(id string) (IWorkerPool, error) { return nil, err } - resp, err := services.ApiGet(s.GetClient(), new(WorkerPoolResource), path) + resp, err := api.ApiGet(s.GetClient(), new(WorkerPoolResource), path) if err != nil { return nil, err } diff --git a/pkg/workerpools/worker_pool_utilities.go b/pkg/workerpools/worker_pool_utilities.go index b9c48c6b..436a6d14 100644 --- a/pkg/workerpools/worker_pool_utilities.go +++ b/pkg/workerpools/worker_pool_utilities.go @@ -38,7 +38,7 @@ func ToWorkerPool(workerPoolResource *WorkerPoolResource) (IWorkerPool, error) { return workerPool, nil } -func ToWorkerPools(workerPoolResources *resources.Resources[WorkerPoolResource]) *WorkerPools { +func ToWorkerPools(workerPoolResources *resources.Resources[*WorkerPoolResource]) *WorkerPools { return &WorkerPools{ Items: ToWorkerPoolArray(workerPoolResources.Items), PagedResults: workerPoolResources.PagedResults,