diff --git a/pkg/releases/release_service.go b/pkg/releases/release_service.go index b3048b02..1873f36d 100644 --- a/pkg/releases/release_service.go +++ b/pkg/releases/release_service.go @@ -166,6 +166,30 @@ func GetReleaseDeploymentTemplate(client newclient.Client, spaceID string, relea return res, nil } +// SnapshotVariables refreshes (re-snapshots) the variable snapshot for an existing release. +// It POSTs to /api/{spaceId}/releases/{releaseId}/snapshot-variables and returns the updated release. +func SnapshotVariables(client newclient.Client, spaceID string, releaseID string) (*Release, error) { + if client == nil { + return nil, internal.CreateInvalidParameterError("SnapshotVariables", "client") + } + if spaceID == "" { + return nil, internal.CreateInvalidParameterError("SnapshotVariables", "spaceID") + } + if releaseID == "" { + return nil, internal.CreateInvalidParameterError("SnapshotVariables", "releaseID") + } + + expandedUri, err := client.URITemplateCache().Expand(uritemplates.ReleaseSnapshotVariables, map[string]any{ + "spaceId": spaceID, + "releaseId": releaseID, + }) + if err != nil { + return nil, err + } + + return newclient.Post[Release](client.HttpSession(), expandedUri, nil) +} + // ----- Experimental --------------------------------------------------------- // GetReleasesInProjectChannel is EXPERIMENTAL diff --git a/pkg/releases/release_service_test.go b/pkg/releases/release_service_test.go new file mode 100644 index 00000000..d7a34bd4 --- /dev/null +++ b/pkg/releases/release_service_test.go @@ -0,0 +1,22 @@ +package releases + +import ( + "testing" + + "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/newclient" + "github.com/stretchr/testify/assert" +) + +func TestReleaseSnapshotVariablesValidation(t *testing.T) { + _, err := SnapshotVariables(nil, "Spaces-1", "Releases-1") + assert.Equal(t, internal.CreateInvalidParameterError("SnapshotVariables", "client"), err) + + client := newclient.NewClient(&newclient.HttpSession{}) + + _, err = SnapshotVariables(client, "", "Releases-1") + assert.Equal(t, internal.CreateInvalidParameterError("SnapshotVariables", "spaceID"), err) + + _, err = SnapshotVariables(client, "Spaces-1", "") + assert.Equal(t, internal.CreateInvalidParameterError("SnapshotVariables", "releaseID"), err) +} diff --git a/pkg/runbooks/runbook_service.go b/pkg/runbooks/runbook_service.go index e60a32f7..79cd4249 100644 --- a/pkg/runbooks/runbook_service.go +++ b/pkg/runbooks/runbook_service.go @@ -240,6 +240,30 @@ func GetSnapshot(client newclient.Client, spaceID string, projectID string, snap return newclient.Get[RunbookSnapshot](client.HttpSession(), expandedUri) } +// SnapshotVariables refreshes (re-snapshots) the variable snapshot for an existing runbook snapshot. +// It POSTs to /api/{spaceId}/runbookSnapshots/{snapshotId}/snapshot-variables and returns the updated snapshot. +func SnapshotVariables(client newclient.Client, spaceID string, snapshotID string) (*RunbookSnapshot, error) { + if client == nil { + return nil, internal.CreateRequiredParameterIsEmptyOrNilError("client") + } + if spaceID == "" { + return nil, internal.CreateRequiredParameterIsEmptyOrNilError("spaceID") + } + if snapshotID == "" { + return nil, internal.CreateRequiredParameterIsEmptyOrNilError("snapshotID") + } + + expandedUri, err := client.URITemplateCache().Expand(uritemplates.RunbookSnapshotVariables, map[string]any{ + "spaceId": spaceID, + "snapshotId": snapshotID, + }) + if err != nil { + return nil, err + } + + return newclient.Post[RunbookSnapshot](client.HttpSession(), expandedUri, nil) +} + // ListEnvironments returns the list of valid environments for a given runbook func ListEnvironments(client newclient.Client, spaceID string, projectID string, runbookID string) ([]*environments.Environment, error) { if spaceID == "" { diff --git a/pkg/runbooks/runbook_service_test.go b/pkg/runbooks/runbook_service_test.go index fc751dca..f537b607 100644 --- a/pkg/runbooks/runbook_service_test.go +++ b/pkg/runbooks/runbook_service_test.go @@ -5,6 +5,7 @@ import ( "github.com/OctopusDeploy/go-octopusdeploy/v2/internal" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants" + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/newclient" "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services" "github.com/dghubble/sling" "github.com/stretchr/testify/assert" @@ -54,3 +55,16 @@ func TestRunbookServiceNew(t *testing.T) { }) } } + +func TestRunbookSnapshotVariablesValidation(t *testing.T) { + _, err := SnapshotVariables(nil, "Spaces-1", "RunbookSnapshots-1") + assert.Equal(t, internal.CreateRequiredParameterIsEmptyOrNilError("client"), err) + + client := newclient.NewClient(&newclient.HttpSession{}) + + _, err = SnapshotVariables(client, "", "RunbookSnapshots-1") + assert.Equal(t, internal.CreateRequiredParameterIsEmptyOrNilError("spaceID"), err) + + _, err = SnapshotVariables(client, "Spaces-1", "") + assert.Equal(t, internal.CreateRequiredParameterIsEmptyOrNilError("snapshotID"), err) +} diff --git a/uritemplates/links.go b/uritemplates/links.go index 2afd1955..16d47309 100644 --- a/uritemplates/links.go +++ b/uritemplates/links.go @@ -33,6 +33,7 @@ const ( ReleasesByProject = "/api/{spaceId}/projects/{projectId}/releases{/version}{?skip,take,searchByVersion}" // GET ReleasesByProjectAndChannel = "/api/{spaceId}/projects/{projectId}/channels/{channelId}/releases{?skip,take,searchByVersion}" // GET ReleaseDeploymentTemplate = "/api/{spaceId}/releases/{releaseId}/deployments/template" // GET + ReleaseSnapshotVariables = "/api/{spaceId}/releases/{releaseId}/snapshot-variables" // POST Runbooks = "/api/{spaceId}/runbooks{/id}{?skip,take,ids,partialName,clone,projectIds}" // GET RunbooksByProject = "/api/{spaceId}/projects/{projectId}/runbooks{?skip,take,partialName}" // GET @@ -43,6 +44,7 @@ const ( RunbookSnapshotsByProject = "/api/{spaceId}/projects/{projectId}/runbookSnapshots{/name}{?skip,take,searchByName}" // GET RunbookSnapshotRunPreview = "/api/{spaceId}/runbookSnapshots/{snapshotId}/runbookRuns/preview/{environmentId}{?includeDisabledSteps}" // GET RunbookRunTenantPreview = "/api/{spaceId}/projects/{projectId}/runbooks/{runbookId}/runbookRuns/previews" // POST + RunbookSnapshotVariables = "/api/{spaceId}/runbookSnapshots/{snapshotId}/snapshot-variables" // POST GitRunbookById = "/api/{spaceId}/projects/{projectId}/{gitRef}/runbooks/{id}" // GET, DELETE GitRunbooksByProject = "/api/{spaceId}/projects/{projectId}/{gitRef}/runbooks{?skip,take,partialName}" // GET